linux-bk/drivers/net/pcmcia/3c589_cs.c
<<
>>
Prefs
   1/*======================================================================
   2
   3    A PCMCIA ethernet driver for the 3com 3c589 card.
   4    
   5    Copyright (C) 1999 David A. Hinds -- dahinds@users.sourceforge.net
   6
   7    3c589_cs.c 1.162 2001/10/13 00:08:50
   8
   9    The network driver code is based on Donald Becker's 3c589 code:
  10    
  11    Written 1994 by Donald Becker.
  12    Copyright 1993 United States Government as represented by the
  13    Director, National Security Agency.  This software may be used and
  14    distributed according to the terms of the GNU General Public License,
  15    incorporated herein by reference.
  16    Donald Becker may be reached at becker@scyld.com
  17    
  18    Updated for 2.5.x by Alan Cox <alan@redhat.com>
  19
  20======================================================================*/
  21
  22#define DRV_NAME        "3c589_cs"
  23#define DRV_VERSION     "1.162-ac"
  24
  25#include <linux/module.h>
  26#include <linux/init.h>
  27#include <linux/kernel.h>
  28#include <linux/ptrace.h>
  29#include <linux/slab.h>
  30#include <linux/string.h>
  31#include <linux/timer.h>
  32#include <linux/interrupt.h>
  33#include <linux/in.h>
  34#include <linux/delay.h>
  35#include <linux/ethtool.h>
  36#include <linux/netdevice.h>
  37#include <linux/etherdevice.h>
  38#include <linux/skbuff.h>
  39#include <linux/if_arp.h>
  40#include <linux/ioport.h>
  41
  42#include <pcmcia/version.h>
  43#include <pcmcia/cs_types.h>
  44#include <pcmcia/cs.h>
  45#include <pcmcia/cistpl.h>
  46#include <pcmcia/cisreg.h>
  47#include <pcmcia/ciscode.h>
  48#include <pcmcia/ds.h>
  49
  50#include <asm/uaccess.h>
  51#include <asm/io.h>
  52#include <asm/system.h>
  53#include <asm/bitops.h>
  54
  55/* To minimize the size of the driver source I only define operating
  56   constants if they are used several times.  You'll need the manual
  57   if you want to understand driver details. */
  58/* Offsets from base I/O address. */
  59#define EL3_DATA        0x00
  60#define EL3_TIMER       0x0a
  61#define EL3_CMD         0x0e
  62#define EL3_STATUS      0x0e
  63
  64#define EEPROM_READ     0x0080
  65#define EEPROM_BUSY     0x8000
  66
  67#define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)
  68
  69/* The top five bits written to EL3_CMD are a command, the lower
  70   11 bits are the parameter, if applicable. */
  71enum c509cmd {
  72    TotalReset = 0<<11, SelectWindow = 1<<11, StartCoax = 2<<11,
  73    RxDisable = 3<<11, RxEnable = 4<<11, RxReset = 5<<11, RxDiscard = 8<<11,
  74    TxEnable = 9<<11, TxDisable = 10<<11, TxReset = 11<<11,
  75    FakeIntr = 12<<11, AckIntr = 13<<11, SetIntrEnb = 14<<11,
  76    SetStatusEnb = 15<<11, SetRxFilter = 16<<11, SetRxThreshold = 17<<11,
  77    SetTxThreshold = 18<<11, SetTxStart = 19<<11, StatsEnable = 21<<11,
  78    StatsDisable = 22<<11, StopCoax = 23<<11,
  79};
  80
  81enum c509status {
  82    IntLatch = 0x0001, AdapterFailure = 0x0002, TxComplete = 0x0004,
  83    TxAvailable = 0x0008, RxComplete = 0x0010, RxEarly = 0x0020,
  84    IntReq = 0x0040, StatsFull = 0x0080, CmdBusy = 0x1000
  85};
  86
  87/* The SetRxFilter command accepts the following classes: */
  88enum RxFilter {
  89    RxStation = 1, RxMulticast = 2, RxBroadcast = 4, RxProm = 8
  90};
  91
  92/* Register window 1 offsets, the window used in normal operation. */
  93#define TX_FIFO         0x00
  94#define RX_FIFO         0x00
  95#define RX_STATUS       0x08
  96#define TX_STATUS       0x0B
  97#define TX_FREE         0x0C    /* Remaining free bytes in Tx buffer. */
  98
  99#define WN0_IRQ         0x08    /* Window 0: Set IRQ line in bits 12-15. */
 100#define WN4_MEDIA       0x0A    /* Window 4: Various transcvr/media bits. */
 101#define MEDIA_TP        0x00C0  /* Enable link beat and jabber for 10baseT. */
 102#define MEDIA_LED       0x0001  /* Enable link light on 3C589E cards. */
 103
 104/* Time in jiffies before concluding Tx hung */
 105#define TX_TIMEOUT      ((400*HZ)/1000)
 106
 107struct el3_private {
 108    dev_link_t          link;
 109    dev_node_t          node;
 110    struct net_device_stats stats;
 111    /* For transceiver monitoring */
 112    struct timer_list   media;
 113    u16                 media_status;
 114    u16                 fast_poll;
 115    unsigned long       last_irq;
 116    spinlock_t          lock;
 117};
 118
 119static char *if_names[] = { "auto", "10baseT", "10base2", "AUI" };
 120
 121/*====================================================================*/
 122
 123/* Module parameters */
 124
 125MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
 126MODULE_DESCRIPTION("3Com 3c589 series PCMCIA ethernet driver");
 127MODULE_LICENSE("GPL");
 128
 129#define INT_MODULE_PARM(n, v) static int n = v; MODULE_PARM(n, "i")
 130
 131/* Special hook for setting if_port when module is loaded */
 132INT_MODULE_PARM(if_port, 0);
 133
 134/* Bit map of interrupts to choose from */
 135INT_MODULE_PARM(irq_mask, 0xdeb8);
 136static int irq_list[4] = { -1 };
 137MODULE_PARM(irq_list, "1-4i");
 138
 139#ifdef PCMCIA_DEBUG
 140INT_MODULE_PARM(pc_debug, PCMCIA_DEBUG);
 141#define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
 142static char *version =
 143DRV_NAME ".c " DRV_VERSION " 2001/10/13 00:08:50 (David Hinds)";
 144#else
 145#define DEBUG(n, args...)
 146#endif
 147
 148/*====================================================================*/
 149
 150static void tc589_config(dev_link_t *link);
 151static void tc589_release(dev_link_t *link);
 152static int tc589_event(event_t event, int priority,
 153                       event_callback_args_t *args);
 154
 155static u16 read_eeprom(ioaddr_t ioaddr, int index);
 156static void tc589_reset(struct net_device *dev);
 157static void media_check(unsigned long arg);
 158static int el3_config(struct net_device *dev, struct ifmap *map);
 159static int el3_open(struct net_device *dev);
 160static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev);
 161static irqreturn_t el3_interrupt(int irq, void *dev_id, struct pt_regs *regs);
 162static void update_stats(struct net_device *dev);
 163static struct net_device_stats *el3_get_stats(struct net_device *dev);
 164static int el3_rx(struct net_device *dev);
 165static int el3_close(struct net_device *dev);
 166static void el3_tx_timeout(struct net_device *dev);
 167static void set_multicast_list(struct net_device *dev);
 168static struct ethtool_ops netdev_ethtool_ops;
 169
 170static dev_info_t dev_info = "3c589_cs";
 171
 172static dev_link_t *tc589_attach(void);
 173static void tc589_detach(dev_link_t *);
 174
 175static dev_link_t *dev_list;
 176
 177/*======================================================================
 178
 179    tc589_attach() creates an "instance" of the driver, allocating
 180    local data structures for one device.  The device is registered
 181    with Card Services.
 182
 183======================================================================*/
 184
 185static dev_link_t *tc589_attach(void)
 186{
 187    struct el3_private *lp;
 188    client_reg_t client_reg;
 189    dev_link_t *link;
 190    struct net_device *dev;
 191    int i, ret;
 192
 193    DEBUG(0, "3c589_attach()\n");
 194    
 195    /* Create new ethernet device */
 196    dev = alloc_etherdev(sizeof(struct el3_private));
 197    if (!dev)
 198         return NULL;
 199    lp = dev->priv;
 200    link = &lp->link;
 201    link->priv = dev;
 202
 203    spin_lock_init(&lp->lock);
 204    link->io.NumPorts1 = 16;
 205    link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
 206    link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
 207    link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
 208    if (irq_list[0] == -1)
 209        link->irq.IRQInfo2 = irq_mask;
 210    else
 211        for (i = 0; i < 4; i++)
 212            link->irq.IRQInfo2 |= 1 << irq_list[i];
 213    link->irq.Handler = &el3_interrupt;
 214    link->irq.Instance = dev;
 215    link->conf.Attributes = CONF_ENABLE_IRQ;
 216    link->conf.Vcc = 50;
 217    link->conf.IntType = INT_MEMORY_AND_IO;
 218    link->conf.ConfigIndex = 1;
 219    link->conf.Present = PRESENT_OPTION;
 220    
 221    /* The EL3-specific entries in the device structure. */
 222    SET_MODULE_OWNER(dev);
 223    dev->hard_start_xmit = &el3_start_xmit;
 224    dev->set_config = &el3_config;
 225    dev->get_stats = &el3_get_stats;
 226    dev->set_multicast_list = &set_multicast_list;
 227    dev->open = &el3_open;
 228    dev->stop = &el3_close;
 229#ifdef HAVE_TX_TIMEOUT
 230    dev->tx_timeout = el3_tx_timeout;
 231    dev->watchdog_timeo = TX_TIMEOUT;
 232#endif
 233    SET_ETHTOOL_OPS(dev, &netdev_ethtool_ops);
 234
 235    /* Register with Card Services */
 236    link->next = dev_list;
 237    dev_list = link;
 238    client_reg.dev_info = &dev_info;
 239    client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
 240    client_reg.EventMask =
 241        CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
 242        CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
 243        CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
 244    client_reg.event_handler = &tc589_event;
 245    client_reg.Version = 0x0210;
 246    client_reg.event_callback_args.client_data = link;
 247    ret = CardServices(RegisterClient, &link->handle, &client_reg);
 248    if (ret != 0) {
 249        cs_error(link->handle, RegisterClient, ret);
 250        tc589_detach(link);
 251        return NULL;
 252    }
 253    
 254    return link;
 255} /* tc589_attach */
 256
 257/*======================================================================
 258
 259    This deletes a driver "instance".  The device is de-registered
 260    with Card Services.  If it has been released, all local data
 261    structures are freed.  Otherwise, the structures will be freed
 262    when the device is released.
 263
 264======================================================================*/
 265
 266static void tc589_detach(dev_link_t *link)
 267{
 268    struct net_device *dev = link->priv;
 269    dev_link_t **linkp;
 270    
 271    DEBUG(0, "3c589_detach(0x%p)\n", link);
 272    
 273    /* Locate device structure */
 274    for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
 275        if (*linkp == link) break;
 276    if (*linkp == NULL)
 277        return;
 278
 279    if (link->state & DEV_CONFIG) {
 280        tc589_release(link);
 281        if (link->state & DEV_STALE_CONFIG)
 282            return;
 283    }
 284    
 285    if (link->handle)
 286        CardServices(DeregisterClient, link->handle);
 287    
 288    /* Unlink device structure, free bits */
 289    *linkp = link->next;
 290    if (link->dev) {
 291        unregister_netdev(dev);
 292        free_netdev(dev);
 293    } else
 294        kfree(dev);
 295    
 296} /* tc589_detach */
 297
 298/*======================================================================
 299
 300    tc589_config() is scheduled to run after a CARD_INSERTION event
 301    is received, to configure the PCMCIA socket, and to make the
 302    ethernet device available to the system.
 303    
 304======================================================================*/
 305
 306#define CS_CHECK(fn, args...) \
 307while ((last_ret=CardServices(last_fn=(fn), args))!=0) goto cs_failed
 308
 309static void tc589_config(dev_link_t *link)
 310{
 311    client_handle_t handle = link->handle;
 312    struct net_device *dev = link->priv;
 313    struct el3_private *lp = dev->priv;
 314    tuple_t tuple;
 315    cisparse_t parse;
 316    u16 buf[32], *phys_addr;
 317    int last_fn, last_ret, i, j, multi = 0;
 318    ioaddr_t ioaddr;
 319    char *ram_split[] = {"5:3", "3:1", "1:1", "3:5"};
 320    
 321    DEBUG(0, "3c589_config(0x%p)\n", link);
 322
 323    phys_addr = (u16 *)dev->dev_addr;
 324    tuple.Attributes = 0;
 325    tuple.DesiredTuple = CISTPL_CONFIG;
 326    CS_CHECK(GetFirstTuple, handle, &tuple);
 327    tuple.TupleData = (cisdata_t *)buf;
 328    tuple.TupleDataMax = sizeof(buf);
 329    tuple.TupleOffset = 0;
 330    CS_CHECK(GetTupleData, handle, &tuple);
 331    CS_CHECK(ParseTuple, handle, &tuple, &parse);
 332    link->conf.ConfigBase = parse.config.base;
 333    link->conf.Present = parse.config.rmask[0];
 334    
 335    /* Is this a 3c562? */
 336    tuple.DesiredTuple = CISTPL_MANFID;
 337    tuple.Attributes = TUPLE_RETURN_COMMON;
 338    if ((CardServices(GetFirstTuple, handle, &tuple) == CS_SUCCESS) &&
 339        (CardServices(GetTupleData, handle, &tuple) == CS_SUCCESS)) {
 340        if (le16_to_cpu(buf[0]) != MANFID_3COM)
 341            printk(KERN_INFO "3c589_cs: hmmm, is this really a "
 342                   "3Com card??\n");
 343        multi = (le16_to_cpu(buf[1]) == PRODID_3COM_3C562);
 344    }
 345    
 346    /* Configure card */
 347    link->state |= DEV_CONFIG;
 348
 349    /* For the 3c562, the base address must be xx00-xx7f */
 350    link->io.IOAddrLines = 16;
 351    for (i = j = 0; j < 0x400; j += 0x10) {
 352        if (multi && (j & 0x80)) continue;
 353        link->io.BasePort1 = j ^ 0x300;
 354        i = CardServices(RequestIO, link->handle, &link->io);
 355        if (i == CS_SUCCESS) break;
 356    }
 357    if (i != CS_SUCCESS) {
 358        cs_error(link->handle, RequestIO, i);
 359        goto failed;
 360    }
 361    CS_CHECK(RequestIRQ, link->handle, &link->irq);
 362    CS_CHECK(RequestConfiguration, link->handle, &link->conf);
 363        
 364    dev->irq = link->irq.AssignedIRQ;
 365    dev->base_addr = link->io.BasePort1;
 366    if (register_netdev(dev) != 0) {
 367        printk(KERN_ERR "3c589_cs: register_netdev() failed\n");
 368        goto failed;
 369    }
 370    
 371    ioaddr = dev->base_addr;
 372    EL3WINDOW(0);
 373
 374    /* The 3c589 has an extra EEPROM for configuration info, including
 375       the hardware address.  The 3c562 puts the address in the CIS. */
 376    tuple.DesiredTuple = 0x88;
 377    if (CardServices(GetFirstTuple, handle, &tuple) == CS_SUCCESS) {
 378        CardServices(GetTupleData, handle, &tuple);
 379        for (i = 0; i < 3; i++)
 380            phys_addr[i] = htons(buf[i]);
 381    } else {
 382        for (i = 0; i < 3; i++)
 383            phys_addr[i] = htons(read_eeprom(ioaddr, i));
 384        if (phys_addr[0] == 0x6060) {
 385            printk(KERN_ERR "3c589_cs: IO port conflict at 0x%03lx"
 386                   "-0x%03lx\n", dev->base_addr, dev->base_addr+15);
 387            goto failed;
 388        }
 389    }
 390
 391    strcpy(lp->node.dev_name, dev->name);
 392    link->dev = &lp->node;
 393    link->state &= ~DEV_CONFIG_PENDING;
 394    
 395    /* The address and resource configuration register aren't loaded from
 396       the EEPROM and *must* be set to 0 and IRQ3 for the PCMCIA version. */
 397    outw(0x3f00, ioaddr + 8);
 398
 399    /* The if_port symbol can be set when the module is loaded */
 400    if ((if_port >= 0) && (if_port <= 3))
 401        dev->if_port = if_port;
 402    else
 403        printk(KERN_ERR "3c589_cs: invalid if_port requested\n");
 404    
 405    printk(KERN_INFO "%s: 3Com 3c%s, io %#3lx, irq %d, hw_addr ",
 406           dev->name, (multi ? "562" : "589"), dev->base_addr,
 407           dev->irq);
 408    for (i = 0; i < 6; i++)
 409        printk("%02X%s", dev->dev_addr[i], ((i<5) ? ":" : "\n"));
 410    i = inl(ioaddr);
 411    printk(KERN_INFO "  %dK FIFO split %s Rx:Tx, %s xcvr\n",
 412           (i & 7) ? 32 : 8, ram_split[(i >> 16) & 3],
 413           if_names[dev->if_port]);
 414    return;
 415
 416cs_failed:
 417    cs_error(link->handle, last_fn, last_ret);
 418failed:
 419    tc589_release(link);
 420    return;
 421    
 422} /* tc589_config */
 423
 424/*======================================================================
 425
 426    After a card is removed, tc589_release() will unregister the net
 427    device, and release the PCMCIA configuration.  If the device is
 428    still open, this will be postponed until it is closed.
 429    
 430======================================================================*/
 431
 432static void tc589_release(dev_link_t *link)
 433{
 434    DEBUG(0, "3c589_release(0x%p)\n", link);
 435    
 436    if (link->open) {
 437        DEBUG(1, "3c589_cs: release postponed, '%s' still open\n",
 438              link->dev->dev_name);
 439        link->state |= DEV_STALE_CONFIG;
 440        return;
 441    }
 442    
 443    CardServices(ReleaseConfiguration, link->handle);
 444    CardServices(ReleaseIO, link->handle, &link->io);
 445    CardServices(ReleaseIRQ, link->handle, &link->irq);
 446    
 447    link->state &= ~DEV_CONFIG;
 448
 449    if (link->state & DEV_STALE_CONFIG)
 450            tc589_detach(link);
 451}
 452
 453/*======================================================================
 454
 455    The card status event handler.  Mostly, this schedules other
 456    stuff to run after an event is received.  A CARD_REMOVAL event
 457    also sets some flags to discourage the net drivers from trying
 458    to talk to the card any more.
 459    
 460======================================================================*/
 461
 462static int tc589_event(event_t event, int priority,
 463                       event_callback_args_t *args)
 464{
 465    dev_link_t *link = args->client_data;
 466    struct net_device *dev = link->priv;
 467    
 468    DEBUG(1, "3c589_event(0x%06x)\n", event);
 469    
 470    switch (event) {
 471    case CS_EVENT_CARD_REMOVAL:
 472        link->state &= ~DEV_PRESENT;
 473        if (link->state & DEV_CONFIG) {
 474            netif_device_detach(dev);
 475            tc589_release(link);
 476        }
 477        break;
 478    case CS_EVENT_CARD_INSERTION:
 479        link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
 480        tc589_config(link);
 481        break;
 482    case CS_EVENT_PM_SUSPEND:
 483        link->state |= DEV_SUSPEND;
 484        /* Fall through... */
 485    case CS_EVENT_RESET_PHYSICAL:
 486        if (link->state & DEV_CONFIG) {
 487            if (link->open)
 488                netif_device_detach(dev);
 489            CardServices(ReleaseConfiguration, link->handle);
 490        }
 491        break;
 492    case CS_EVENT_PM_RESUME:
 493        link->state &= ~DEV_SUSPEND;
 494        /* Fall through... */
 495    case CS_EVENT_CARD_RESET:
 496        if (link->state & DEV_CONFIG) {
 497            CardServices(RequestConfiguration, link->handle, &link->conf);
 498            if (link->open) {
 499                tc589_reset(dev);
 500                netif_device_attach(dev);
 501            }
 502        }
 503        break;
 504    }
 505    return 0;
 506} /* tc589_event */
 507
 508/*====================================================================*/
 509
 510/*
 511  Use this for commands that may take time to finish
 512*/
 513static void tc589_wait_for_completion(struct net_device *dev, int cmd)
 514{
 515    int i = 100;
 516    outw(cmd, dev->base_addr + EL3_CMD);
 517    while (--i > 0)
 518        if (!(inw(dev->base_addr + EL3_STATUS) & 0x1000)) break;
 519    if (i == 0)
 520        printk(KERN_WARNING "%s: command 0x%04x did not complete!\n",
 521               dev->name, cmd);
 522}
 523
 524/*
 525  Read a word from the EEPROM using the regular EEPROM access register.
 526  Assume that we are in register window zero.
 527*/
 528static u16 read_eeprom(ioaddr_t ioaddr, int index)
 529{
 530    int i;
 531    outw(EEPROM_READ + index, ioaddr + 10);
 532    /* Reading the eeprom takes 162 us */
 533    for (i = 1620; i >= 0; i--)
 534        if ((inw(ioaddr + 10) & EEPROM_BUSY) == 0)
 535            break;
 536    return inw(ioaddr + 12);
 537}
 538
 539/*
 540  Set transceiver type, perhaps to something other than what the user
 541  specified in dev->if_port.
 542*/
 543static void tc589_set_xcvr(struct net_device *dev, int if_port)
 544{
 545    struct el3_private *lp = (struct el3_private *)dev->priv;
 546    ioaddr_t ioaddr = dev->base_addr;
 547    
 548    EL3WINDOW(0);
 549    switch (if_port) {
 550    case 0: case 1: outw(0, ioaddr + 6); break;
 551    case 2: outw(3<<14, ioaddr + 6); break;
 552    case 3: outw(1<<14, ioaddr + 6); break;
 553    }
 554    /* On PCMCIA, this just turns on the LED */
 555    outw((if_port == 2) ? StartCoax : StopCoax, ioaddr + EL3_CMD);
 556    /* 10baseT interface, enable link beat and jabber check. */
 557    EL3WINDOW(4);
 558    outw(MEDIA_LED | ((if_port < 2) ? MEDIA_TP : 0), ioaddr + WN4_MEDIA);
 559    EL3WINDOW(1);
 560    if (if_port == 2)
 561        lp->media_status = ((dev->if_port == 0) ? 0x8000 : 0x4000);
 562    else
 563        lp->media_status = ((dev->if_port == 0) ? 0x4010 : 0x8800);
 564}
 565
 566static void dump_status(struct net_device *dev)
 567{
 568    ioaddr_t ioaddr = dev->base_addr;
 569    EL3WINDOW(1);
 570    printk(KERN_INFO "  irq status %04x, rx status %04x, tx status "
 571           "%02x  tx free %04x\n", inw(ioaddr+EL3_STATUS),
 572           inw(ioaddr+RX_STATUS), inb(ioaddr+TX_STATUS),
 573           inw(ioaddr+TX_FREE));
 574    EL3WINDOW(4);
 575    printk(KERN_INFO "  diagnostics: fifo %04x net %04x ethernet %04x"
 576           " media %04x\n", inw(ioaddr+0x04), inw(ioaddr+0x06),
 577           inw(ioaddr+0x08), inw(ioaddr+0x0a));
 578    EL3WINDOW(1);
 579}
 580
 581/* Reset and restore all of the 3c589 registers. */
 582static void tc589_reset(struct net_device *dev)
 583{
 584    ioaddr_t ioaddr = dev->base_addr;
 585    int i;
 586    
 587    EL3WINDOW(0);
 588    outw(0x0001, ioaddr + 4);                   /* Activate board. */ 
 589    outw(0x3f00, ioaddr + 8);                   /* Set the IRQ line. */
 590    
 591    /* Set the station address in window 2. */
 592    EL3WINDOW(2);
 593    for (i = 0; i < 6; i++)
 594        outb(dev->dev_addr[i], ioaddr + i);
 595
 596    tc589_set_xcvr(dev, dev->if_port);
 597    
 598    /* Switch to the stats window, and clear all stats by reading. */
 599    outw(StatsDisable, ioaddr + EL3_CMD);
 600    EL3WINDOW(6);
 601    for (i = 0; i < 9; i++)
 602        inb(ioaddr+i);
 603    inw(ioaddr + 10);
 604    inw(ioaddr + 12);
 605    
 606    /* Switch to register set 1 for normal use. */
 607    EL3WINDOW(1);
 608
 609    /* Accept b-cast and phys addr only. */
 610    outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
 611    outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
 612    outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
 613    outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
 614    /* Allow status bits to be seen. */
 615    outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
 616    /* Ack all pending events, and set active indicator mask. */
 617    outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
 618         ioaddr + EL3_CMD);
 619    outw(SetIntrEnb | IntLatch | TxAvailable | RxComplete | StatsFull
 620         | AdapterFailure, ioaddr + EL3_CMD);
 621}
 622
 623static void netdev_get_drvinfo(struct net_device *dev,
 624                               struct ethtool_drvinfo *info)
 625{
 626        strcpy(info->driver, DRV_NAME);
 627        strcpy(info->version, DRV_VERSION);
 628        sprintf(info->bus_info, "PCMCIA 0x%lx", dev->base_addr);
 629}
 630
 631#ifdef PCMCIA_DEBUG
 632static u32 netdev_get_msglevel(struct net_device *dev)
 633{
 634        return pc_debug;
 635}
 636
 637static void netdev_set_msglevel(struct net_device *dev, u32 level)
 638{
 639        pc_debug = level;
 640}
 641#endif /* PCMCIA_DEBUG */
 642
 643static struct ethtool_ops netdev_ethtool_ops = {
 644        .get_drvinfo            = netdev_get_drvinfo,
 645#ifdef PCMCIA_DEBUG
 646        .get_msglevel           = netdev_get_msglevel,
 647        .set_msglevel           = netdev_set_msglevel,
 648#endif /* PCMCIA_DEBUG */
 649};
 650
 651static int el3_config(struct net_device *dev, struct ifmap *map)
 652{
 653    if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
 654        if (map->port <= 3) {
 655            dev->if_port = map->port;
 656            printk(KERN_INFO "%s: switched to %s port\n",
 657                   dev->name, if_names[dev->if_port]);
 658            tc589_set_xcvr(dev, dev->if_port);
 659        } else
 660            return -EINVAL;
 661    }
 662    return 0;
 663}
 664
 665static int el3_open(struct net_device *dev)
 666{
 667    struct el3_private *lp = (struct el3_private *)dev->priv;
 668    dev_link_t *link = &lp->link;
 669    
 670    if (!DEV_OK(link))
 671        return -ENODEV;
 672
 673    link->open++;
 674    netif_start_queue(dev);
 675    
 676    tc589_reset(dev);
 677    init_timer(&lp->media);
 678    lp->media.function = &media_check;
 679    lp->media.data = (unsigned long) dev;
 680    lp->media.expires = jiffies + HZ;
 681    add_timer(&lp->media);
 682
 683    DEBUG(1, "%s: opened, status %4.4x.\n",
 684          dev->name, inw(dev->base_addr + EL3_STATUS));
 685    
 686    return 0;
 687}
 688
 689static void el3_tx_timeout(struct net_device *dev)
 690{
 691    struct el3_private *lp = (struct el3_private *)dev->priv;
 692    ioaddr_t ioaddr = dev->base_addr;
 693    
 694    printk(KERN_WARNING "%s: Transmit timed out!\n", dev->name);
 695    dump_status(dev);
 696    lp->stats.tx_errors++;
 697    dev->trans_start = jiffies;
 698    /* Issue TX_RESET and TX_START commands. */
 699    tc589_wait_for_completion(dev, TxReset);
 700    outw(TxEnable, ioaddr + EL3_CMD);
 701    netif_wake_queue(dev);
 702}
 703
 704static void pop_tx_status(struct net_device *dev)
 705{
 706    struct el3_private *lp = (struct el3_private *)dev->priv;
 707    ioaddr_t ioaddr = dev->base_addr;
 708    int i;
 709    
 710    /* Clear the Tx status stack. */
 711    for (i = 32; i > 0; i--) {
 712        u_char tx_status = inb(ioaddr + TX_STATUS);
 713        if (!(tx_status & 0x84)) break;
 714        /* reset transmitter on jabber error or underrun */
 715        if (tx_status & 0x30)
 716            tc589_wait_for_completion(dev, TxReset);
 717        if (tx_status & 0x38) {
 718            DEBUG(1, "%s: transmit error: status 0x%02x\n",
 719                  dev->name, tx_status);
 720            outw(TxEnable, ioaddr + EL3_CMD);
 721            lp->stats.tx_aborted_errors++;
 722        }
 723        outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
 724    }
 725}
 726
 727static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev)
 728{
 729    ioaddr_t ioaddr = dev->base_addr;
 730
 731    DEBUG(3, "%s: el3_start_xmit(length = %ld) called, "
 732          "status %4.4x.\n", dev->name, (long)skb->len,
 733          inw(ioaddr + EL3_STATUS));
 734
 735    ((struct el3_private *)dev->priv)->stats.tx_bytes += skb->len;
 736
 737    /* Put out the doubleword header... */
 738    outw(skb->len, ioaddr + TX_FIFO);
 739    outw(0x00, ioaddr + TX_FIFO);
 740    /* ... and the packet rounded to a doubleword. */
 741    outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
 742
 743    dev->trans_start = jiffies;
 744    if (inw(ioaddr + TX_FREE) <= 1536) {
 745        netif_stop_queue(dev);
 746        /* Interrupt us when the FIFO has room for max-sized packet. */
 747        outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);
 748    }
 749
 750    dev_kfree_skb(skb);
 751    pop_tx_status(dev);
 752    
 753    return 0;
 754}
 755
 756/* The EL3 interrupt handler. */
 757static irqreturn_t el3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
 758{
 759    struct net_device *dev = (struct net_device *) dev_id;
 760    struct el3_private *lp = dev->priv;
 761    ioaddr_t ioaddr, status;
 762    int i = 0, handled = 1;
 763    
 764    if (!netif_device_present(dev))
 765        return IRQ_NONE;
 766
 767    ioaddr = dev->base_addr;
 768
 769    DEBUG(3, "%s: interrupt, status %4.4x.\n",
 770          dev->name, inw(ioaddr + EL3_STATUS));
 771
 772    spin_lock(&lp->lock);    
 773    while ((status = inw(ioaddr + EL3_STATUS)) &
 774        (IntLatch | RxComplete | StatsFull)) {
 775        if ((status & 0xe000) != 0x2000) {
 776            DEBUG(1, "%s: interrupt from dead card\n", dev->name);
 777            handled = 0;
 778            break;
 779        }
 780        
 781        if (status & RxComplete)
 782            el3_rx(dev);
 783        
 784        if (status & TxAvailable) {
 785            DEBUG(3, "    TX room bit was handled.\n");
 786            /* There's room in the FIFO for a full-sized packet. */
 787            outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
 788            netif_wake_queue(dev);
 789        }
 790        
 791        if (status & TxComplete)
 792            pop_tx_status(dev);
 793
 794        if (status & (AdapterFailure | RxEarly | StatsFull)) {
 795            /* Handle all uncommon interrupts. */
 796            if (status & StatsFull)             /* Empty statistics. */
 797                update_stats(dev);
 798            if (status & RxEarly) {             /* Rx early is unused. */
 799                el3_rx(dev);
 800                outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
 801            }
 802            if (status & AdapterFailure) {
 803                u16 fifo_diag;
 804                EL3WINDOW(4);
 805                fifo_diag = inw(ioaddr + 4);
 806                EL3WINDOW(1);
 807                printk(KERN_WARNING "%s: adapter failure, FIFO diagnostic"
 808                       " register %04x.\n", dev->name, fifo_diag);
 809                if (fifo_diag & 0x0400) {
 810                    /* Tx overrun */
 811                    tc589_wait_for_completion(dev, TxReset);
 812                    outw(TxEnable, ioaddr + EL3_CMD);
 813                }
 814                if (fifo_diag & 0x2000) {
 815                    /* Rx underrun */
 816                    tc589_wait_for_completion(dev, RxReset);
 817                    set_multicast_list(dev);
 818                    outw(RxEnable, ioaddr + EL3_CMD);
 819                }
 820                outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
 821            }
 822        }
 823        
 824        if (++i > 10) {
 825            printk(KERN_ERR "%s: infinite loop in interrupt, "
 826                   "status %4.4x.\n", dev->name, status);
 827            /* Clear all interrupts */
 828            outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
 829            break;
 830        }
 831        /* Acknowledge the IRQ. */
 832        outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD);
 833    }
 834
 835    lp->last_irq = jiffies;
 836    spin_unlock(&lp->lock);    
 837    DEBUG(3, "%s: exiting interrupt, status %4.4x.\n",
 838          dev->name, inw(ioaddr + EL3_STATUS));
 839    return IRQ_RETVAL(handled);
 840}
 841
 842static void media_check(unsigned long arg)
 843{
 844    struct net_device *dev = (struct net_device *)(arg);
 845    struct el3_private *lp = dev->priv;
 846    ioaddr_t ioaddr = dev->base_addr;
 847    u16 media, errs;
 848    unsigned long flags;
 849
 850    if (!netif_device_present(dev)) goto reschedule;
 851
 852    EL3WINDOW(1);
 853    /* Check for pending interrupt with expired latency timer: with
 854       this, we can limp along even if the interrupt is blocked */
 855    if ((inw(ioaddr + EL3_STATUS) & IntLatch) &&
 856        (inb(ioaddr + EL3_TIMER) == 0xff)) {
 857        if (!lp->fast_poll)
 858            printk(KERN_WARNING "%s: interrupt(s) dropped!\n", dev->name);
 859        el3_interrupt(dev->irq, lp, NULL);
 860        lp->fast_poll = HZ;
 861    }
 862    if (lp->fast_poll) {
 863        lp->fast_poll--;
 864        lp->media.expires = jiffies + HZ/100;
 865        add_timer(&lp->media);
 866        return;
 867    }
 868
 869    /* lp->lock guards the EL3 window. Window should always be 1 except
 870       when the lock is held */
 871    spin_lock_irqsave(&lp->lock, flags);    
 872    EL3WINDOW(4);
 873    media = inw(ioaddr+WN4_MEDIA) & 0xc810;
 874
 875    /* Ignore collisions unless we've had no irq's recently */
 876    if (jiffies - lp->last_irq < HZ) {
 877        media &= ~0x0010;
 878    } else {
 879        /* Try harder to detect carrier errors */
 880        EL3WINDOW(6);
 881        outw(StatsDisable, ioaddr + EL3_CMD);
 882        errs = inb(ioaddr + 0);
 883        outw(StatsEnable, ioaddr + EL3_CMD);
 884        lp->stats.tx_carrier_errors += errs;
 885        if (errs || (lp->media_status & 0x0010)) media |= 0x0010;
 886    }
 887
 888    if (media != lp->media_status) {
 889        if ((media & lp->media_status & 0x8000) &&
 890            ((lp->media_status ^ media) & 0x0800))
 891            printk(KERN_INFO "%s: %s link beat\n", dev->name,
 892                   (lp->media_status & 0x0800 ? "lost" : "found"));
 893        else if ((media & lp->media_status & 0x4000) &&
 894                 ((lp->media_status ^ media) & 0x0010))
 895            printk(KERN_INFO "%s: coax cable %s\n", dev->name,
 896                   (lp->media_status & 0x0010 ? "ok" : "problem"));
 897        if (dev->if_port == 0) {
 898            if (media & 0x8000) {
 899                if (media & 0x0800)
 900                    printk(KERN_INFO "%s: flipped to 10baseT\n",
 901                           dev->name);
 902                else
 903                    tc589_set_xcvr(dev, 2);
 904            } else if (media & 0x4000) {
 905                if (media & 0x0010)
 906                    tc589_set_xcvr(dev, 1);
 907                else
 908                    printk(KERN_INFO "%s: flipped to 10base2\n",
 909                           dev->name);
 910            }
 911        }
 912        lp->media_status = media;
 913    }
 914    
 915    EL3WINDOW(1);
 916    spin_unlock_irqrestore(&lp->lock, flags);    
 917
 918reschedule:
 919    lp->media.expires = jiffies + HZ;
 920    add_timer(&lp->media);
 921}
 922
 923static struct net_device_stats *el3_get_stats(struct net_device *dev)
 924{
 925    struct el3_private *lp = (struct el3_private *)dev->priv;
 926    unsigned long flags;
 927    dev_link_t *link = &lp->link;
 928
 929    if (DEV_OK(link)) {
 930        spin_lock_irqsave(&lp->lock, flags);
 931        update_stats(dev);
 932        spin_unlock_irqrestore(&lp->lock, flags);
 933    }
 934    return &lp->stats;
 935}
 936
 937/*
 938  Update statistics.  We change to register window 6, so this should be run
 939  single-threaded if the device is active. This is expected to be a rare
 940  operation, and it's simpler for the rest of the driver to assume that
 941  window 1 is always valid rather than use a special window-state variable.
 942  
 943  Caller must hold the lock for this
 944*/
 945static void update_stats(struct net_device *dev)
 946{
 947    struct el3_private *lp = (struct el3_private *)dev->priv;
 948    ioaddr_t ioaddr = dev->base_addr;
 949
 950    DEBUG(2, "%s: updating the statistics.\n", dev->name);
 951    /* Turn off statistics updates while reading. */
 952    outw(StatsDisable, ioaddr + EL3_CMD);
 953    /* Switch to the stats window, and read everything. */
 954    EL3WINDOW(6);
 955    lp->stats.tx_carrier_errors         += inb(ioaddr + 0);
 956    lp->stats.tx_heartbeat_errors       += inb(ioaddr + 1);
 957    /* Multiple collisions. */          inb(ioaddr + 2);
 958    lp->stats.collisions                += inb(ioaddr + 3);
 959    lp->stats.tx_window_errors          += inb(ioaddr + 4);
 960    lp->stats.rx_fifo_errors            += inb(ioaddr + 5);
 961    lp->stats.tx_packets                += inb(ioaddr + 6);
 962    /* Rx packets   */                  inb(ioaddr + 7);
 963    /* Tx deferrals */                  inb(ioaddr + 8);
 964    /* Rx octets */                     inw(ioaddr + 10);
 965    /* Tx octets */                     inw(ioaddr + 12);
 966    
 967    /* Back to window 1, and turn statistics back on. */
 968    EL3WINDOW(1);
 969    outw(StatsEnable, ioaddr + EL3_CMD);
 970}
 971
 972static int el3_rx(struct net_device *dev)
 973{
 974    struct el3_private *lp = (struct el3_private *)dev->priv;
 975    ioaddr_t ioaddr = dev->base_addr;
 976    int worklimit = 32;
 977    short rx_status;
 978    
 979    DEBUG(3, "%s: in rx_packet(), status %4.4x, rx_status %4.4x.\n",
 980          dev->name, inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS));
 981    while (!((rx_status = inw(ioaddr + RX_STATUS)) & 0x8000) &&
 982           (--worklimit >= 0)) {
 983        if (rx_status & 0x4000) { /* Error, update stats. */
 984            short error = rx_status & 0x3800;
 985            lp->stats.rx_errors++;
 986            switch (error) {
 987            case 0x0000:        lp->stats.rx_over_errors++; break;
 988            case 0x0800:        lp->stats.rx_length_errors++; break;
 989            case 0x1000:        lp->stats.rx_frame_errors++; break;
 990            case 0x1800:        lp->stats.rx_length_errors++; break;
 991            case 0x2000:        lp->stats.rx_frame_errors++; break;
 992            case 0x2800:        lp->stats.rx_crc_errors++; break;
 993            }
 994        } else {
 995            short pkt_len = rx_status & 0x7ff;
 996            struct sk_buff *skb;
 997            
 998            skb = dev_alloc_skb(pkt_len+5);
 999            
1000            DEBUG(3, "    Receiving packet size %d status %4.4x.\n",
1001                  pkt_len, rx_status);
1002            if (skb != NULL) {
1003                skb->dev = dev;
1004                skb_reserve(skb, 2);
1005                insl(ioaddr+RX_FIFO, skb_put(skb, pkt_len),
1006                        (pkt_len+3)>>2);
1007                skb->protocol = eth_type_trans(skb, dev);
1008                netif_rx(skb);
1009                dev->last_rx = jiffies;
1010                lp->stats.rx_packets++;
1011                lp->stats.rx_bytes += pkt_len;
1012            } else {
1013                DEBUG(1, "%s: couldn't allocate a sk_buff of"
1014                      " size %d.\n", dev->name, pkt_len);
1015                lp->stats.rx_dropped++;
1016            }
1017        }
1018        /* Pop the top of the Rx FIFO */
1019        tc589_wait_for_completion(dev, RxDiscard);
1020    }
1021    if (worklimit == 0)
1022        printk(KERN_WARNING "%s: too much work in el3_rx!\n", dev->name);
1023    return 0;
1024}
1025
1026static void set_multicast_list(struct net_device *dev)
1027{
1028    struct el3_private *lp = dev->priv;
1029    dev_link_t *link = &lp->link;
1030    ioaddr_t ioaddr = dev->base_addr;
1031    u16 opts = SetRxFilter | RxStation | RxBroadcast;
1032
1033    if (!(DEV_OK(link))) return;
1034    if (dev->flags & IFF_PROMISC)
1035        opts |= RxMulticast | RxProm;
1036    else if (dev->mc_count || (dev->flags & IFF_ALLMULTI))
1037        opts |= RxMulticast;
1038    outw(opts, ioaddr + EL3_CMD);
1039}
1040
1041static int el3_close(struct net_device *dev)
1042{
1043    struct el3_private *lp = dev->priv;
1044    dev_link_t *link = &lp->link;
1045    ioaddr_t ioaddr = dev->base_addr;
1046    
1047    DEBUG(1, "%s: shutting down ethercard.\n", dev->name);
1048
1049    if (DEV_OK(link)) {
1050        /* Turn off statistics ASAP.  We update lp->stats below. */
1051        outw(StatsDisable, ioaddr + EL3_CMD);
1052        
1053        /* Disable the receiver and transmitter. */
1054        outw(RxDisable, ioaddr + EL3_CMD);
1055        outw(TxDisable, ioaddr + EL3_CMD);
1056        
1057        if (dev->if_port == 2)
1058            /* Turn off thinnet power.  Green! */
1059            outw(StopCoax, ioaddr + EL3_CMD);
1060        else if (dev->if_port == 1) {
1061            /* Disable link beat and jabber */
1062            EL3WINDOW(4);
1063            outw(0, ioaddr + WN4_MEDIA);
1064        }
1065        
1066        /* Switching back to window 0 disables the IRQ. */
1067        EL3WINDOW(0);
1068        /* But we explicitly zero the IRQ line select anyway. */
1069        outw(0x0f00, ioaddr + WN0_IRQ);
1070        
1071        /* Check if the card still exists */
1072        if ((inw(ioaddr+EL3_STATUS) & 0xe000) == 0x2000)
1073            update_stats(dev);
1074    }
1075
1076    link->open--;
1077    netif_stop_queue(dev);
1078    del_timer_sync(&lp->media);
1079    if (link->state & DEV_STALE_CONFIG)
1080             tc589_release(link);
1081    
1082    return 0;
1083}
1084
1085static struct pcmcia_driver tc589_driver = {
1086        .owner          = THIS_MODULE,
1087        .drv            = {
1088                .name   = "3c589_cs",
1089        },
1090        .attach         = tc589_attach,
1091        .detach         = tc589_detach,
1092};
1093
1094static int __init init_tc589(void)
1095{
1096        return pcmcia_register_driver(&tc589_driver);
1097}
1098
1099static void __exit exit_tc589(void)
1100{
1101        pcmcia_unregister_driver(&tc589_driver);
1102        while (dev_list != NULL)
1103                tc589_detach(dev_list);
1104}
1105
1106module_init(init_tc589);
1107module_exit(exit_tc589);
1108
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.