linux-bk/drivers/net/3c509.c
<<
>>
Prefs
   1/* 3c509.c: A 3c509 EtherLink3 ethernet driver for linux. */
   2/*
   3        Written 1993-2000 by Donald Becker.
   4
   5        Copyright 1994-2000 by Donald Becker.
   6        Copyright 1993 United States Government as represented by the
   7        Director, National Security Agency.      This software may be used and
   8        distributed according to the terms of the GNU General Public License,
   9        incorporated herein by reference.
  10
  11        This driver is for the 3Com EtherLinkIII series.
  12
  13        The author may be reached as becker@scyld.com, or C/O
  14        Scyld Computing Corporation
  15        410 Severn Ave., Suite 210
  16        Annapolis MD 21403
  17
  18        Known limitations:
  19        Because of the way 3c509 ISA detection works it's difficult to predict
  20        a priori which of several ISA-mode cards will be detected first.
  21
  22        This driver does not use predictive interrupt mode, resulting in higher
  23        packet latency but lower overhead.  If interrupts are disabled for an
  24        unusually long time it could also result in missed packets, but in
  25        practice this rarely happens.
  26
  27
  28        FIXES:
  29                Alan Cox:       Removed the 'Unexpected interrupt' bug.
  30                Michael Meskes: Upgraded to Donald Becker's version 1.07.
  31                Alan Cox:       Increased the eeprom delay. Regardless of 
  32                                what the docs say some people definitely
  33                                get problems with lower (but in card spec)
  34                                delays
  35                v1.10 4/21/97 Fixed module code so that multiple cards may be detected,
  36                                other cleanups.  -djb
  37                Andrea Arcangeli:       Upgraded to Donald Becker's version 1.12.
  38                Rick Payne:     Fixed SMP race condition
  39                v1.13 9/8/97 Made 'max_interrupt_work' an insmod-settable variable -djb
  40                v1.14 10/15/97 Avoided waiting..discard message for fast machines -djb
  41                v1.15 1/31/98 Faster recovery for Tx errors. -djb
  42                v1.16 2/3/98 Different ID port handling to avoid sound cards. -djb
  43                v1.18 12Mar2001 Andrew Morton <andrewm@uow.edu.au>
  44                        - Avoid bogus detect of 3c590's (Andrzej Krzysztofowicz)
  45                        - Reviewed against 1.18 from scyld.com
  46                v1.18a 17Nov2001 Jeff Garzik <jgarzik@pobox.com>
  47                        - ethtool support
  48                v1.18b 1Mar2002 Zwane Mwaikambo <zwane@commfireservices.com>
  49                        - Power Management support
  50                v1.18c 1Mar2002 David Ruggiero <jdr@farfalle.com>
  51                        - Full duplex support
  52                v1.19  16Oct2002 Zwane Mwaikambo <zwane@linuxpower.ca>
  53                        - Additional ethtool features
  54                v1.19a 28Oct2002 Davud Ruggiero <jdr@farfalle.com>
  55                        - Increase *read_eeprom udelay to workaround oops with 2 cards.
  56                v1.19b 08Nov2002 Marc Zyngier <maz@wild-wind.fr.eu.org>
  57                    - Introduce driver model for EISA cards.
  58*/
  59/*
  60  FIXES for PC-9800:
  61  Shu Iwanaga: 3c569B(PC-9801 C-bus) support
  62*/
  63
  64#define DRV_NAME        "3c509"
  65#define DRV_VERSION     "1.19b"
  66#define DRV_RELDATE     "08Nov2002"
  67
  68/* A few values that may be tweaked. */
  69
  70/* Time in jiffies before concluding the transmitter is hung. */
  71#define TX_TIMEOUT  (400*HZ/1000)
  72/* Maximum events (Rx packets, etc.) to handle at each interrupt. */
  73static int max_interrupt_work = 10;
  74
  75#include <linux/config.h>
  76#include <linux/module.h>
  77#ifdef CONFIG_MCA
  78#include <linux/mca.h>
  79#endif
  80#include <linux/isapnp.h>
  81#include <linux/string.h>
  82#include <linux/interrupt.h>
  83#include <linux/errno.h>
  84#include <linux/in.h>
  85#include <linux/slab.h>
  86#include <linux/ioport.h>
  87#include <linux/init.h>
  88#include <linux/netdevice.h>
  89#include <linux/etherdevice.h>
  90#include <linux/pm.h>
  91#include <linux/skbuff.h>
  92#include <linux/delay.h>        /* for udelay() */
  93#include <linux/spinlock.h>
  94#include <linux/ethtool.h>
  95#include <linux/device.h>
  96#include <linux/eisa.h>
  97
  98#include <asm/uaccess.h>
  99#include <asm/bitops.h>
 100#include <asm/io.h>
 101#include <asm/irq.h>
 102
 103static char versionA[] __initdata = DRV_NAME ".c:" DRV_VERSION " " DRV_RELDATE " becker@scyld.com\n";
 104static char versionB[] __initdata = "http://www.scyld.com/network/3c509.html\n";
 105
 106#ifdef EL3_DEBUG
 107static int el3_debug = EL3_DEBUG;
 108#else
 109static int el3_debug = 2;
 110#endif
 111
 112/* Used to do a global count of all the cards in the system.  Must be
 113 * a global variable so that the mca/eisa probe routines can increment
 114 * it */
 115static int el3_cards = 0;
 116
 117/* To minimize the size of the driver source I only define operating
 118   constants if they are used several times.  You'll need the manual
 119   anyway if you want to understand driver details. */
 120/* Offsets from base I/O address. */
 121#define EL3_DATA 0x00
 122#define EL3_CMD 0x0e
 123#define EL3_STATUS 0x0e
 124#define  EEPROM_READ 0x80
 125
 126#define EL3_IO_EXTENT   16
 127
 128#define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)
 129
 130
 131/* The top five bits written to EL3_CMD are a command, the lower
 132   11 bits are the parameter, if applicable. */
 133enum c509cmd {
 134        TotalReset = 0<<11, SelectWindow = 1<<11, StartCoax = 2<<11,
 135        RxDisable = 3<<11, RxEnable = 4<<11, RxReset = 5<<11, RxDiscard = 8<<11,
 136        TxEnable = 9<<11, TxDisable = 10<<11, TxReset = 11<<11,
 137        FakeIntr = 12<<11, AckIntr = 13<<11, SetIntrEnb = 14<<11,
 138        SetStatusEnb = 15<<11, SetRxFilter = 16<<11, SetRxThreshold = 17<<11,
 139        SetTxThreshold = 18<<11, SetTxStart = 19<<11, StatsEnable = 21<<11,
 140        StatsDisable = 22<<11, StopCoax = 23<<11, PowerUp = 27<<11,
 141        PowerDown = 28<<11, PowerAuto = 29<<11};
 142
 143enum c509status {
 144        IntLatch = 0x0001, AdapterFailure = 0x0002, TxComplete = 0x0004,
 145        TxAvailable = 0x0008, RxComplete = 0x0010, RxEarly = 0x0020,
 146        IntReq = 0x0040, StatsFull = 0x0080, CmdBusy = 0x1000, };
 147
 148/* The SetRxFilter command accepts the following classes: */
 149enum RxFilter {
 150        RxStation = 1, RxMulticast = 2, RxBroadcast = 4, RxProm = 8 };
 151
 152/* Register window 1 offsets, the window used in normal operation. */
 153#define TX_FIFO         0x00
 154#define RX_FIFO         0x00
 155#define RX_STATUS       0x08
 156#define TX_STATUS       0x0B
 157#define TX_FREE         0x0C            /* Remaining free bytes in Tx buffer. */
 158
 159#define WN0_CONF_CTRL   0x04            /* Window 0: Configuration control register */
 160#define WN0_ADDR_CONF   0x06            /* Window 0: Address configuration register */
 161#define WN0_IRQ         0x08            /* Window 0: Set IRQ line in bits 12-15. */
 162#define WN4_MEDIA       0x0A            /* Window 4: Various transcvr/media bits. */
 163#define MEDIA_TP        0x00C0          /* Enable link beat and jabber for 10baseT. */
 164#define WN4_NETDIAG     0x06            /* Window 4: Net diagnostic */
 165#define FD_ENABLE       0x8000          /* Enable full-duplex ("external loopback") */  
 166
 167/*
 168 * Must be a power of two (we use a binary and in the
 169 * circular queue)
 170 */
 171#define SKB_QUEUE_SIZE  64
 172
 173struct el3_private {
 174        struct net_device_stats stats;
 175        struct net_device *next_dev;
 176        spinlock_t lock;
 177        /* skb send-queue */
 178        int head, size;
 179        struct sk_buff *queue[SKB_QUEUE_SIZE];
 180#ifdef CONFIG_PM
 181        struct pm_dev *pmdev;
 182#endif
 183        enum {
 184                EL3_MCA,
 185                EL3_PNP,
 186                EL3_EISA,
 187        } type;                                         /* type of device */
 188        struct device *dev;
 189};
 190static int id_port __initdata = 0x110;  /* Start with 0x110 to avoid new sound cards.*/
 191static struct net_device *el3_root_dev;
 192
 193static ushort id_read_eeprom(int index);
 194static ushort read_eeprom(int ioaddr, int index);
 195static int el3_open(struct net_device *dev);
 196static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev);
 197static irqreturn_t el3_interrupt(int irq, void *dev_id, struct pt_regs *regs);
 198static void update_stats(struct net_device *dev);
 199static struct net_device_stats *el3_get_stats(struct net_device *dev);
 200static int el3_rx(struct net_device *dev);
 201static int el3_close(struct net_device *dev);
 202static void set_multicast_list(struct net_device *dev);
 203static void el3_tx_timeout (struct net_device *dev);
 204static int netdev_ioctl (struct net_device *dev, struct ifreq *rq, int cmd);
 205static void el3_down(struct net_device *dev);
 206static void el3_up(struct net_device *dev);
 207#ifdef CONFIG_PM
 208static int el3_suspend(struct pm_dev *pdev);
 209static int el3_resume(struct pm_dev *pdev);
 210static int el3_pm_callback(struct pm_dev *pdev, pm_request_t rqst, void *data);
 211#endif
 212/* generic device remove for all device types */
 213#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
 214static int el3_device_remove (struct device *device);
 215#endif
 216
 217#ifdef CONFIG_EISA
 218struct eisa_device_id el3_eisa_ids[] = {
 219                { "TCM5092" },
 220                { "TCM5093" },
 221                { "" }
 222};
 223
 224static int el3_eisa_probe (struct device *device);
 225
 226struct eisa_driver el3_eisa_driver = {
 227                .id_table = el3_eisa_ids,
 228                .driver   = {
 229                                .name    = "3c509",
 230                                .probe   = el3_eisa_probe,
 231                                .remove  = __devexit_p (el3_device_remove)
 232                }
 233};
 234#endif
 235
 236#ifdef CONFIG_MCA
 237static int el3_mca_probe(struct device *dev);
 238
 239static short el3_mca_adapter_ids[] __initdata = {
 240                0x627c,
 241                0x627d,
 242                0x62db,
 243                0x62f6,
 244                0x62f7,
 245                0x0000
 246};
 247
 248static char *el3_mca_adapter_names[] __initdata = {
 249                "3Com 3c529 EtherLink III (10base2)",
 250                "3Com 3c529 EtherLink III (10baseT)",
 251                "3Com 3c529 EtherLink III (test mode)",
 252                "3Com 3c529 EtherLink III (TP or coax)",
 253                "3Com 3c529 EtherLink III (TP)",
 254                NULL
 255};
 256
 257static struct mca_driver el3_mca_driver = {
 258                .id_table = el3_mca_adapter_ids,
 259                .driver = {
 260                                .name = "3c529",
 261                                .bus = &mca_bus_type,
 262                                .probe = el3_mca_probe,
 263                                .remove = __devexit_p(el3_device_remove),
 264                },
 265};
 266#endif /* CONFIG_MCA */
 267
 268#if defined(__ISAPNP__) && !defined(CONFIG_X86_PC9800)
 269static struct isapnp_device_id el3_isapnp_adapters[] __initdata = {
 270        {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
 271                ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5090),
 272                (long) "3Com Etherlink III (TP)" },
 273        {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
 274                ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5091),
 275                (long) "3Com Etherlink III" },
 276        {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
 277                ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5094),
 278                (long) "3Com Etherlink III (combo)" },
 279        {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
 280                ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5095),
 281                (long) "3Com Etherlink III (TPO)" },
 282        {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
 283                ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5098),
 284                (long) "3Com Etherlink III (TPC)" },
 285        {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
 286                ISAPNP_VENDOR('P', 'N', 'P'), ISAPNP_FUNCTION(0x80f7),
 287                (long) "3Com Etherlink III compatible" },
 288        {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
 289                ISAPNP_VENDOR('P', 'N', 'P'), ISAPNP_FUNCTION(0x80f8),
 290                (long) "3Com Etherlink III compatible" },
 291        { }     /* terminate list */
 292};
 293
 294static u16 el3_isapnp_phys_addr[8][3];
 295static int nopnp;
 296#endif /* __ISAPNP__ */
 297
 298/* With the driver model introduction for EISA devices, both init
 299 * and cleanup have been split :
 300 * - EISA devices probe/remove starts in el3_eisa_probe/el3_device_remove
 301 * - MCA/ISA still use el3_probe
 302 *
 303 * Both call el3_common_init/el3_common_remove. */
 304
 305static int __init el3_common_init(struct net_device *dev)
 306{
 307        struct el3_private *lp = dev->priv;
 308        short i;
 309        int err;
 310
 311        spin_lock_init(&lp->lock);
 312
 313        if (dev->mem_start & 0x05) { /* xcvr codes 1/3/4/12 */
 314                dev->if_port = (dev->mem_start & 0x0f);
 315        } else { /* xcvr codes 0/8 */
 316                /* use eeprom value, but save user's full-duplex selection */
 317                dev->if_port |= (dev->mem_start & 0x08);
 318        }
 319
 320        /* The EL3-specific entries in the device structure. */
 321        dev->open = &el3_open;
 322        dev->hard_start_xmit = &el3_start_xmit;
 323        dev->stop = &el3_close;
 324        dev->get_stats = &el3_get_stats;
 325        dev->set_multicast_list = &set_multicast_list;
 326        dev->tx_timeout = el3_tx_timeout;
 327        dev->watchdog_timeo = TX_TIMEOUT;
 328        dev->do_ioctl = netdev_ioctl;
 329
 330        err = register_netdev(dev);
 331        if (err) {
 332                printk(KERN_ERR "Failed to register 3c5x9 at %#3.3lx, IRQ %d.\n",
 333                        dev->base_addr, dev->irq);
 334                release_region(dev->base_addr, EL3_IO_EXTENT);
 335                return err;
 336        }
 337
 338        {
 339                const char *if_names[] = {"10baseT", "AUI", "undefined", "BNC"};
 340                printk("%s: 3c5x9 found at %#3.3lx, %s port, address ",
 341                        dev->name, dev->base_addr, 
 342                        if_names[(dev->if_port & 0x03)]);
 343        }
 344
 345        /* Read in the station address. */
 346        for (i = 0; i < 6; i++)
 347                printk(" %2.2x", dev->dev_addr[i]);
 348        printk(", IRQ %d.\n", dev->irq);
 349
 350        if (el3_debug > 0)
 351                printk(KERN_INFO "%s" KERN_INFO "%s", versionA, versionB);
 352        return 0;
 353
 354}
 355
 356static void el3_common_remove (struct net_device *dev)
 357{
 358                struct el3_private *lp = dev->priv;
 359
 360                (void) lp;                              /* Keep gcc quiet... */
 361#ifdef CONFIG_PM
 362                if (lp->pmdev)
 363                        pm_unregister(lp->pmdev);
 364#endif
 365#if defined(__ISAPNP__) && !defined(CONFIG_X86_PC9800)
 366                if (lp->type == EL3_PNP)
 367                        pnp_device_detach(to_pnp_dev(lp->dev));
 368#endif
 369
 370                unregister_netdev (dev);
 371                release_region(dev->base_addr, EL3_IO_EXTENT);
 372                free_netdev (dev);
 373}
 374
 375static int __init el3_probe(int card_idx)
 376{
 377        struct net_device *dev;
 378        struct el3_private *lp;
 379        short lrs_state = 0xff, i;
 380        int ioaddr, irq, if_port;
 381        u16 phys_addr[3];
 382        static int current_tag;
 383        int err = -ENODEV;
 384#if defined(__ISAPNP__) && !defined(CONFIG_X86_PC9800)
 385        static int pnp_cards;
 386        struct pnp_dev *idev = NULL;
 387
 388        if (nopnp == 1)
 389                goto no_pnp;
 390
 391        for (i=0; el3_isapnp_adapters[i].vendor != 0; i++) {
 392                int j;
 393                while ((idev = pnp_find_dev(NULL,
 394                                            el3_isapnp_adapters[i].vendor,
 395                                            el3_isapnp_adapters[i].function,
 396                                            idev))) {
 397                        if (pnp_device_attach(idev) < 0)
 398                                continue;
 399                        if (pnp_activate_dev(idev) < 0) {
 400                              __again:
 401                                pnp_device_detach(idev);
 402                                continue;
 403                        }
 404                        if (!pnp_port_valid(idev, 0) || !pnp_irq_valid(idev, 0))
 405                                goto __again;
 406                        ioaddr = pnp_port_start(idev, 0);
 407                        if (!request_region(ioaddr, EL3_IO_EXTENT, "3c509 PnP")) {
 408                                pnp_device_detach(idev);
 409                                return -EBUSY;
 410                        }
 411                        irq = pnp_irq(idev, 0);
 412                        if (el3_debug > 3)
 413                                printk ("ISAPnP reports %s at i/o 0x%x, irq %d\n",
 414                                        (char*) el3_isapnp_adapters[i].driver_data, ioaddr, irq);
 415                        EL3WINDOW(0);
 416                        for (j = 0; j < 3; j++)
 417                                el3_isapnp_phys_addr[pnp_cards][j] =
 418                                        phys_addr[j] =
 419                                                htons(read_eeprom(ioaddr, j));
 420                        if_port = read_eeprom(ioaddr, 8) >> 14;
 421                        dev = alloc_etherdev(sizeof (struct el3_private));
 422                        if (!dev) {
 423                                        release_region(ioaddr, EL3_IO_EXTENT);
 424                                        pnp_device_detach(idev);
 425                                        return -ENOMEM;
 426                        }
 427
 428                        SET_MODULE_OWNER(dev);
 429                        pnp_cards++;
 430
 431                        netdev_boot_setup_check(dev);
 432                        goto found;
 433                }
 434        }
 435no_pnp:
 436#endif /* __ISAPNP__ */
 437
 438#ifdef CONFIG_X86_PC9800
 439        id_port = 0x71d0;
 440#else
 441        /* Select an open I/O location at 0x1*0 to do contention select. */
 442        for ( ; id_port < 0x200; id_port += 0x10) {
 443                if (!request_region(id_port, 1, "3c509"))
 444                        continue;
 445                outb(0x00, id_port);
 446                outb(0xff, id_port);
 447                if (inb(id_port) & 0x01){
 448                        release_region(id_port, 1);
 449                        break;
 450                } else
 451                        release_region(id_port, 1);
 452        }
 453        if (id_port >= 0x200) {
 454                /* Rare -- do we really need a warning? */
 455                printk(" WARNING: No I/O port available for 3c509 activation.\n");
 456                return -ENODEV;
 457        }
 458#endif /* CONFIG_X86_PC9800 */
 459        /* Next check for all ISA bus boards by sending the ID sequence to the
 460           ID_PORT.  We find cards past the first by setting the 'current_tag'
 461           on cards as they are found.  Cards with their tag set will not
 462           respond to subsequent ID sequences. */
 463
 464        outb(0x00, id_port);
 465        outb(0x00, id_port);
 466        for(i = 0; i < 255; i++) {
 467                outb(lrs_state, id_port);
 468                lrs_state <<= 1;
 469                lrs_state = lrs_state & 0x100 ? lrs_state ^ 0xcf : lrs_state;
 470        }
 471
 472        /* For the first probe, clear all board's tag registers. */
 473        if (current_tag == 0)
 474                outb(0xd0, id_port);
 475        else                            /* Otherwise kill off already-found boards. */
 476                outb(0xd8, id_port);
 477
 478        if (id_read_eeprom(7) != 0x6d50) {
 479                return -ENODEV;
 480        }
 481
 482        /* Read in EEPROM data, which does contention-select.
 483           Only the lowest address board will stay "on-line".
 484           3Com got the byte order backwards. */
 485        for (i = 0; i < 3; i++) {
 486                phys_addr[i] = htons(id_read_eeprom(i));
 487        }
 488
 489#if defined(__ISAPNP__) && !defined(CONFIG_X86_PC9800)
 490        if (nopnp == 0) {
 491                /* The ISA PnP 3c509 cards respond to the ID sequence.
 492                   This check is needed in order not to register them twice. */
 493                for (i = 0; i < pnp_cards; i++) {
 494                        if (phys_addr[0] == el3_isapnp_phys_addr[i][0] &&
 495                            phys_addr[1] == el3_isapnp_phys_addr[i][1] &&
 496                            phys_addr[2] == el3_isapnp_phys_addr[i][2])
 497                        {
 498                                if (el3_debug > 3)
 499                                        printk("3c509 with address %02x %02x %02x %02x %02x %02x was found by ISAPnP\n",
 500                                                phys_addr[0] & 0xff, phys_addr[0] >> 8,
 501                                                phys_addr[1] & 0xff, phys_addr[1] >> 8,
 502                                                phys_addr[2] & 0xff, phys_addr[2] >> 8);
 503                                /* Set the adaptor tag so that the next card can be found. */
 504                                outb(0xd0 + ++current_tag, id_port);
 505                                goto no_pnp;
 506                        }
 507                }
 508        }
 509#endif /* __ISAPNP__ */
 510
 511        {
 512                unsigned int iobase = id_read_eeprom(8);
 513                if_port = iobase >> 14;
 514#ifdef CONFIG_X86_PC9800
 515                ioaddr = 0x40d0 + ((iobase & 0x1f) << 8);
 516#else
 517                ioaddr = 0x200 + ((iobase & 0x1f) << 4);
 518#endif
 519        }
 520        irq = id_read_eeprom(9) >> 12;
 521#ifdef CONFIG_X86_PC9800
 522        if (irq == 7)
 523                irq = 6;
 524        else if (irq == 15)
 525                irq = 13;
 526#endif
 527
 528        dev = alloc_etherdev(sizeof (struct el3_private));
 529        if (!dev)
 530                return -ENOMEM;
 531
 532        SET_MODULE_OWNER(dev);
 533
 534        netdev_boot_setup_check(dev);
 535        
 536        /* Set passed-in IRQ or I/O Addr. */
 537        if (dev->irq > 1  &&  dev->irq < 16)
 538                        irq = dev->irq;
 539
 540        if (dev->base_addr) {
 541                if (dev->mem_end == 0x3c509     /* Magic key */
 542                    && dev->base_addr >= 0x200  &&  dev->base_addr <= 0x3e0)
 543                        ioaddr = dev->base_addr & 0x3f0;
 544                else if (dev->base_addr != ioaddr)
 545                        goto out;
 546        }
 547
 548        if (!request_region(ioaddr, EL3_IO_EXTENT, "3c509")) {
 549                err = -EBUSY;
 550                goto out;
 551        }
 552
 553        /* Set the adaptor tag so that the next card can be found. */
 554        outb(0xd0 + ++current_tag, id_port);
 555
 556        /* Activate the adaptor at the EEPROM location. */
 557#ifdef CONFIG_X86_PC9800
 558        outb((ioaddr >> 8) | 0xe0, id_port);
 559#else
 560        outb((ioaddr >> 4) | 0xe0, id_port);
 561#endif
 562
 563        EL3WINDOW(0);
 564        if (inw(ioaddr) != 0x6d50)
 565                goto out1;
 566
 567        /* Free the interrupt so that some other card can use it. */
 568        outw(0x0f00, ioaddr + WN0_IRQ);
 569
 570#if defined(__ISAPNP__) && !defined(CONFIG_X86_PC9800)
 571 found:                                                 /* PNP jumps here... */
 572#endif /* __ISAPNP__ */
 573
 574        memcpy(dev->dev_addr, phys_addr, sizeof(phys_addr));
 575        dev->base_addr = ioaddr;
 576        dev->irq = irq;
 577        dev->if_port = if_port;
 578        lp = dev->priv;
 579#if defined(__ISAPNP__) && !defined(CONFIG_X86_PC9800)
 580        lp->dev = &idev->dev;
 581#endif
 582        err = el3_common_init(dev);
 583
 584        if (err)
 585                goto out1;
 586
 587#ifdef CONFIG_PM
 588        /* register power management */
 589        lp->pmdev = pm_register(PM_ISA_DEV, card_idx, el3_pm_callback);
 590        if (lp->pmdev) {
 591                struct pm_dev *p;
 592                p = lp->pmdev;
 593                p->data = (struct net_device *)dev;
 594        }
 595#endif
 596
 597        el3_cards++;
 598#if !defined(__ISAPNP__) || defined(CONFIG_X86_PC9800)
 599        lp->next_dev = el3_root_dev;
 600        el3_root_dev = dev;
 601#endif
 602        return 0;
 603
 604out1:
 605#if defined(__ISAPNP__) && !defined(CONFIG_X86_PC9800)
 606        if (idev)
 607                pnp_device_detach(idev);
 608#endif
 609out:
 610        free_netdev(dev);
 611        return err;
 612}
 613
 614#ifdef CONFIG_MCA
 615static int __init el3_mca_probe(struct device *device) {
 616                /* Based on Erik Nygren's (nygren@mit.edu) 3c529 patch,
 617                 * heavily modified by Chris Beauregard
 618                 * (cpbeaure@csclub.uwaterloo.ca) to support standard MCA
 619                 * probing.
 620                 *
 621                 * redone for multi-card detection by ZP Gu (zpg@castle.net)
 622                 * now works as a module */
 623
 624                struct el3_private *lp;
 625                short i;
 626                int ioaddr, irq, if_port;
 627                u16 phys_addr[3];
 628                struct net_device *dev = NULL;
 629                u_char pos4, pos5;
 630                struct mca_device *mdev = to_mca_device(device);
 631                int slot = mdev->slot;
 632                int err;
 633
 634                pos4 = mca_device_read_stored_pos(mdev, 4);
 635                pos5 = mca_device_read_stored_pos(mdev, 5);
 636
 637                ioaddr = ((short)((pos4&0xfc)|0x02)) << 8;
 638                irq = pos5 & 0x0f;
 639
 640
 641                printk("3c529: found %s at slot %d\n",
 642                           el3_mca_adapter_names[mdev->index], slot + 1);
 643
 644                /* claim the slot */
 645                strncpy(mdev->name, el3_mca_adapter_names[mdev->index],
 646                                sizeof(mdev->name));
 647                mca_device_set_claim(mdev, 1);
 648
 649                if_port = pos4 & 0x03;
 650
 651                irq = mca_device_transform_irq(mdev, irq);
 652                ioaddr = mca_device_transform_ioport(mdev, ioaddr); 
 653                if (el3_debug > 2) {
 654                                printk("3c529: irq %d  ioaddr 0x%x  ifport %d\n", irq, ioaddr, if_port);
 655                }
 656                EL3WINDOW(0);
 657                for (i = 0; i < 3; i++) {
 658                                phys_addr[i] = htons(read_eeprom(ioaddr, i));
 659                }
 660
 661                dev = alloc_etherdev(sizeof (struct el3_private));
 662                if (dev == NULL) {
 663                                release_region(ioaddr, EL3_IO_EXTENT);
 664                                return -ENOMEM;
 665                }
 666
 667                SET_MODULE_OWNER(dev);
 668                netdev_boot_setup_check(dev);
 669
 670                memcpy(dev->dev_addr, phys_addr, sizeof(phys_addr));
 671                dev->base_addr = ioaddr;
 672                dev->irq = irq;
 673                dev->if_port = if_port;
 674                lp = dev->priv;
 675                lp->dev = device;
 676                lp->type = EL3_MCA;
 677                device->driver_data = dev;
 678                err = el3_common_init(dev);
 679
 680                if (err) {
 681                        return -ENOMEM;
 682                }
 683
 684                el3_cards++;
 685                return 0;
 686}
 687                
 688#endif /* CONFIG_MCA */
 689
 690#ifdef CONFIG_EISA
 691static int __init el3_eisa_probe (struct device *device)
 692{
 693        struct el3_private *lp;
 694        short i;
 695        int ioaddr, irq, if_port;
 696        u16 phys_addr[3];
 697        struct net_device *dev = NULL;
 698        struct eisa_device *edev;
 699        int err;
 700
 701        /* Yeepee, The driver framework is calling us ! */
 702        edev = to_eisa_device (device);
 703        ioaddr = edev->base_addr;
 704        
 705        if (!request_region(ioaddr, EL3_IO_EXTENT, "3c509"))
 706                return -EBUSY;
 707
 708        /* Change the register set to the configuration window 0. */
 709        outw(SelectWindow | 0, ioaddr + 0xC80 + EL3_CMD);
 710
 711        irq = inw(ioaddr + WN0_IRQ) >> 12;
 712        if_port = inw(ioaddr + 6)>>14;
 713        for (i = 0; i < 3; i++)
 714                        phys_addr[i] = htons(read_eeprom(ioaddr, i));
 715
 716        /* Restore the "Product ID" to the EEPROM read register. */
 717        read_eeprom(ioaddr, 3);
 718
 719        dev = alloc_etherdev(sizeof (struct el3_private));
 720        if (dev == NULL) {
 721                        release_region(ioaddr, EL3_IO_EXTENT);
 722                        return -ENOMEM;
 723        }
 724
 725        SET_MODULE_OWNER(dev);
 726
 727        netdev_boot_setup_check(dev);
 728
 729        memcpy(dev->dev_addr, phys_addr, sizeof(phys_addr));
 730        dev->base_addr = ioaddr;
 731        dev->irq = irq;
 732        dev->if_port = if_port;
 733        lp = dev->priv;
 734        lp->dev = device;
 735        lp->type = EL3_EISA;
 736        eisa_set_drvdata (edev, dev);
 737        err = el3_common_init(dev);
 738
 739        if (err) {
 740                return err;
 741        }
 742
 743        el3_cards++;
 744        return 0;
 745}
 746#endif
 747
 748#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
 749/* This remove works for all device types.
 750 *
 751 * The net dev must be stored in the driver_data field */
 752static int __devexit el3_device_remove (struct device *device)
 753{
 754                struct net_device *dev;
 755
 756                dev  = device->driver_data;
 757
 758                el3_common_remove (dev);
 759                return 0;
 760}
 761#endif
 762
 763/* Read a word from the EEPROM using the regular EEPROM access register.
 764   Assume that we are in register window zero.
 765 */
 766static ushort read_eeprom(int ioaddr, int index)
 767{
 768        outw(EEPROM_READ + index, ioaddr + 10);
 769        /* Pause for at least 162 us. for the read to take place. 
 770           Some chips seem to require much longer */
 771        mdelay(2);
 772        return inw(ioaddr + 12);
 773}
 774
 775/* Read a word from the EEPROM when in the ISA ID probe state. */
 776static ushort __init id_read_eeprom(int index)
 777{
 778        int bit, word = 0;
 779
 780        /* Issue read command, and pause for at least 162 us. for it to complete.
 781           Assume extra-fast 16Mhz bus. */
 782        outb(EEPROM_READ + index, id_port);
 783
 784        /* Pause for at least 162 us. for the read to take place. */
 785        /* Some chips seem to require much longer */
 786        mdelay(4);
 787        
 788        for (bit = 15; bit >= 0; bit--)
 789                word = (word << 1) + (inb(id_port) & 0x01);
 790
 791        if (el3_debug > 3)
 792                printk("  3c509 EEPROM word %d %#4.4x.\n", index, word);
 793
 794        return word;
 795}
 796
 797
 798static int
 799el3_open(struct net_device *dev)
 800{
 801        int ioaddr = dev->base_addr;
 802        int i;
 803
 804        outw(TxReset, ioaddr + EL3_CMD);
 805        outw(RxReset, ioaddr + EL3_CMD);
 806        outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD);
 807
 808        i = request_irq(dev->irq, &el3_interrupt, 0, dev->name, dev);
 809        if (i) return i;
 810
 811        EL3WINDOW(0);
 812        if (el3_debug > 3)
 813                printk("%s: Opening, IRQ %d      status@%x %4.4x.\n", dev->name,
 814                           dev->irq, ioaddr + EL3_STATUS, inw(ioaddr + EL3_STATUS));
 815
 816        el3_up(dev);
 817
 818        if (el3_debug > 3)
 819                printk("%s: Opened 3c509  IRQ %d  status %4.4x.\n",
 820                           dev->name, dev->irq, inw(ioaddr + EL3_STATUS));
 821
 822        return 0;
 823}
 824
 825static void
 826el3_tx_timeout (struct net_device *dev)
 827{
 828        struct el3_private *lp = (struct el3_private *)dev->priv;
 829        int ioaddr = dev->base_addr;
 830
 831        /* Transmitter timeout, serious problems. */
 832        printk("%s: transmit timed out, Tx_status %2.2x status %4.4x "
 833                   "Tx FIFO room %d.\n",
 834                   dev->name, inb(ioaddr + TX_STATUS), inw(ioaddr + EL3_STATUS),
 835                   inw(ioaddr + TX_FREE));
 836        lp->stats.tx_errors++;
 837        dev->trans_start = jiffies;
 838        /* Issue TX_RESET and TX_START commands. */
 839        outw(TxReset, ioaddr + EL3_CMD);
 840        outw(TxEnable, ioaddr + EL3_CMD);
 841        netif_wake_queue(dev);
 842}
 843
 844
 845static int
 846el3_start_xmit(struct sk_buff *skb, struct net_device *dev)
 847{
 848        struct el3_private *lp = (struct el3_private *)dev->priv;
 849        int ioaddr = dev->base_addr;
 850        unsigned long flags;
 851
 852        netif_stop_queue (dev);
 853
 854        lp->stats.tx_bytes += skb->len;
 855        
 856        if (el3_debug > 4) {
 857                printk("%s: el3_start_xmit(length = %u) called, status %4.4x.\n",
 858                           dev->name, skb->len, inw(ioaddr + EL3_STATUS));
 859        }
 860#if 0
 861#ifndef final_version
 862        {       /* Error-checking code, delete someday. */
 863                ushort status = inw(ioaddr + EL3_STATUS);
 864                if (status & 0x0001             /* IRQ line active, missed one. */
 865                        && inw(ioaddr + EL3_STATUS) & 1) {                      /* Make sure. */
 866                        printk("%s: Missed interrupt, status then %04x now %04x"
 867                                   "  Tx %2.2x Rx %4.4x.\n", dev->name, status,
 868                                   inw(ioaddr + EL3_STATUS), inb(ioaddr + TX_STATUS),
 869                                   inw(ioaddr + RX_STATUS));
 870                        /* Fake interrupt trigger by masking, acknowledge interrupts. */
 871                        outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD);
 872                        outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
 873                                 ioaddr + EL3_CMD);
 874                        outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
 875                }
 876        }
 877#endif
 878#endif
 879        /*
 880         *      We lock the driver against other processors. Note
 881         *      we don't need to lock versus the IRQ as we suspended
 882         *      that. This means that we lose the ability to take
 883         *      an RX during a TX upload. That sucks a bit with SMP
 884         *      on an original 3c509 (2K buffer)
 885         *
 886         *      Using disable_irq stops us crapping on other
 887         *      time sensitive devices.
 888         */
 889
 890        spin_lock_irqsave(&lp->lock, flags);
 891            
 892        /* Put out the doubleword header... */
 893        outw(skb->len, ioaddr + TX_FIFO);
 894        outw(0x00, ioaddr + TX_FIFO);
 895        /* ... and the packet rounded to a doubleword. */
 896#ifdef  __powerpc__
 897        outsl_ns(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
 898#else
 899        outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
 900#endif
 901
 902        dev->trans_start = jiffies;
 903        if (inw(ioaddr + TX_FREE) > 1536)
 904                netif_start_queue(dev);
 905        else
 906                /* Interrupt us when the FIFO has room for max-sized packet. */
 907                outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);
 908
 909        spin_unlock_irqrestore(&lp->lock, flags);
 910
 911        dev_kfree_skb (skb);
 912
 913        /* Clear the Tx status stack. */
 914        {
 915                short tx_status;
 916                int i = 4;
 917
 918                while (--i > 0  &&      (tx_status = inb(ioaddr + TX_STATUS)) > 0) {
 919                        if (tx_status & 0x38) lp->stats.tx_aborted_errors++;
 920                        if (tx_status & 0x30) outw(TxReset, ioaddr + EL3_CMD);
 921                        if (tx_status & 0x3C) outw(TxEnable, ioaddr + EL3_CMD);
 922                        outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
 923                }
 924        }
 925        return 0;
 926}
 927
 928/* The EL3 interrupt handler. */
 929static irqreturn_t
 930el3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
 931{
 932        struct net_device *dev = (struct net_device *)dev_id;
 933        struct el3_private *lp;
 934        int ioaddr, status;
 935        int i = max_interrupt_work;
 936
 937        if (dev == NULL) {
 938                printk ("el3_interrupt(): irq %d for unknown device.\n", irq);
 939                return IRQ_NONE;
 940        }
 941
 942        lp = (struct el3_private *)dev->priv;
 943        spin_lock(&lp->lock);
 944
 945        ioaddr = dev->base_addr;
 946
 947        if (el3_debug > 4) {
 948                status = inw(ioaddr + EL3_STATUS);
 949                printk("%s: interrupt, status %4.4x.\n", dev->name, status);
 950        }
 951
 952        while ((status = inw(ioaddr + EL3_STATUS)) &
 953                   (IntLatch | RxComplete | StatsFull)) {
 954
 955                if (status & RxComplete)
 956                        el3_rx(dev);
 957
 958                if (status & TxAvailable) {
 959                        if (el3_debug > 5)
 960                                printk("        TX room bit was handled.\n");
 961                        /* There's room in the FIFO for a full-sized packet. */
 962                        outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
 963                        netif_wake_queue (dev);
 964                }
 965                if (status & (AdapterFailure | RxEarly | StatsFull | TxComplete)) {
 966                        /* Handle all uncommon interrupts. */
 967                        if (status & StatsFull)                         /* Empty statistics. */
 968                                update_stats(dev);
 969                        if (status & RxEarly) {                         /* Rx early is unused. */
 970                                el3_rx(dev);
 971                                outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
 972                        }
 973                        if (status & TxComplete) {                      /* Really Tx error. */
 974                                struct el3_private *lp = (struct el3_private *)dev->priv;
 975                                short tx_status;
 976                                int i = 4;
 977
 978                                while (--i>0 && (tx_status = inb(ioaddr + TX_STATUS)) > 0) {
 979                                        if (tx_status & 0x38) lp->stats.tx_aborted_errors++;
 980                                        if (tx_status & 0x30) outw(TxReset, ioaddr + EL3_CMD);
 981                                        if (tx_status & 0x3C) outw(TxEnable, ioaddr + EL3_CMD);
 982                                        outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
 983                                }
 984                        }
 985                        if (status & AdapterFailure) {
 986                                /* Adapter failure requires Rx reset and reinit. */
 987                                outw(RxReset, ioaddr + EL3_CMD);
 988                                /* Set the Rx filter to the current state. */
 989                                outw(SetRxFilter | RxStation | RxBroadcast
 990                                         | (dev->flags & IFF_ALLMULTI ? RxMulticast : 0)
 991                                         | (dev->flags & IFF_PROMISC ? RxProm : 0),
 992                                         ioaddr + EL3_CMD);
 993                                outw(RxEnable, ioaddr + EL3_CMD); /* Re-enable the receiver. */
 994                                outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
 995                        }
 996                }
 997
 998                if (--i < 0) {
 999                        printk("%s: Infinite loop in interrupt, status %4.4x.\n",
1000                                   dev->name, status);
1001                        /* Clear all interrupts. */
1002                        outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
1003                        break;
1004                }
1005                /* Acknowledge the IRQ. */
1006                outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD); /* Ack IRQ */
1007        }
1008
1009        if (el3_debug > 4) {
1010                printk("%s: exiting interrupt, status %4.4x.\n", dev->name,
1011                           inw(ioaddr + EL3_STATUS));
1012        }
1013        spin_unlock(&lp->lock);
1014        return IRQ_HANDLED;
1015}
1016
1017
1018static struct net_device_stats *
1019el3_get_stats(struct net_device *dev)
1020{
1021        struct el3_private *lp = (struct el3_private *)dev->priv;
1022        unsigned long flags;
1023
1024        /*
1025         *      This is fast enough not to bother with disable IRQ
1026         *      stuff.
1027         */
1028         
1029        spin_lock_irqsave(&lp->lock, flags);
1030        update_stats(dev);
1031        spin_unlock_irqrestore(&lp->lock, flags);
1032        return &lp->stats;
1033}
1034
1035/*  Update statistics.  We change to register window 6, so this should be run
1036        single-threaded if the device is active. This is expected to be a rare
1037        operation, and it's simpler for the rest of the driver to assume that
1038        window 1 is always valid rather than use a special window-state variable.
1039        */
1040static void update_stats(struct net_device *dev)
1041{
1042        struct el3_private *lp = (struct el3_private *)dev->priv;
1043        int ioaddr = dev->base_addr;
1044
1045        if (el3_debug > 5)
1046                printk("   Updating the statistics.\n");
1047        /* Turn off statistics updates while reading. */
1048        outw(StatsDisable, ioaddr + EL3_CMD);
1049        /* Switch to the stats window, and read everything. */
1050        EL3WINDOW(6);
1051        lp->stats.tx_carrier_errors     += inb(ioaddr + 0);
1052        lp->stats.tx_heartbeat_errors   += inb(ioaddr + 1);
1053        /* Multiple collisions. */         inb(ioaddr + 2);
1054        lp->stats.collisions            += inb(ioaddr + 3);
1055        lp->stats.tx_window_errors      += inb(ioaddr + 4);
1056        lp->stats.rx_fifo_errors        += inb(ioaddr + 5);
1057        lp->stats.tx_packets            += inb(ioaddr + 6);
1058        /* Rx packets   */                 inb(ioaddr + 7);
1059        /* Tx deferrals */                 inb(ioaddr + 8);
1060        inw(ioaddr + 10);       /* Total Rx and Tx octets. */
1061        inw(ioaddr + 12);
1062
1063        /* Back to window 1, and turn statistics back on. */
1064        EL3WINDOW(1);
1065        outw(StatsEnable, ioaddr + EL3_CMD);
1066        return;
1067}
1068
1069static int
1070el3_rx(struct net_device *dev)
1071{
1072        struct el3_private *lp = (struct el3_private *)dev->priv;
1073        int ioaddr = dev->base_addr;
1074        short rx_status;
1075
1076        if (el3_debug > 5)
1077                printk("   In rx_packet(), status %4.4x, rx_status %4.4x.\n",
1078                           inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS));
1079        while ((rx_status = inw(ioaddr + RX_STATUS)) > 0) {
1080                if (rx_status & 0x4000) { /* Error, update stats. */
1081                        short error = rx_status & 0x3800;
1082
1083                        outw(RxDiscard, ioaddr + EL3_CMD);
1084                        lp->stats.rx_errors++;
1085                        switch (error) {
1086                        case 0x0000:            lp->stats.rx_over_errors++; break;
1087                        case 0x0800:            lp->stats.rx_length_errors++; break;
1088                        case 0x1000:            lp->stats.rx_frame_errors++; break;
1089                        case 0x1800:            lp->stats.rx_length_errors++; break;
1090                        case 0x2000:            lp->stats.rx_frame_errors++; break;
1091                        case 0x2800:            lp->stats.rx_crc_errors++; break;
1092                        }
1093                } else {
1094                        short pkt_len = rx_status & 0x7ff;
1095                        struct sk_buff *skb;
1096
1097                        skb = dev_alloc_skb(pkt_len+5);
1098                        lp->stats.rx_bytes += pkt_len;
1099                        if (el3_debug > 4)
1100                                printk("Receiving packet size %d status %4.4x.\n",
1101                                           pkt_len, rx_status);
1102                        if (skb != NULL) {
1103                                skb->dev = dev;
1104                                skb_reserve(skb, 2);     /* Align IP on 16 byte */
1105
1106                                /* 'skb->data' points to the start of sk_buff data area. */
1107#ifdef  __powerpc__
1108                                insl_ns(ioaddr+RX_FIFO, skb_put(skb,pkt_len),
1109                                                           (pkt_len + 3) >> 2);
1110#else
1111                                insl(ioaddr + RX_FIFO, skb_put(skb,pkt_len),
1112                                         (pkt_len + 3) >> 2);
1113#endif
1114
1115                                outw(RxDiscard, ioaddr + EL3_CMD); /* Pop top Rx packet. */
1116                                skb->protocol = eth_type_trans(skb,dev);
1117                                netif_rx(skb);
1118                                dev->last_rx = jiffies;
1119                                lp->stats.rx_packets++;
1120                                continue;
1121                        }
1122                        outw(RxDiscard, ioaddr + EL3_CMD);
1123                        lp->stats.rx_dropped++;
1124                        if (el3_debug)
1125                                printk("%s: Couldn't allocate a sk_buff of size %d.\n",
1126                                           dev->name, pkt_len);
1127                }
1128                inw(ioaddr + EL3_STATUS);                               /* Delay. */
1129                while (inw(ioaddr + EL3_STATUS) & 0x1000)
1130                        printk(KERN_DEBUG "     Waiting for 3c509 to discard packet, status %x.\n",
1131                                   inw(ioaddr + EL3_STATUS) );
1132        }
1133
1134        return 0;
1135}
1136
1137/*
1138 *     Set or clear the multicast filter for this adaptor.
1139 */
1140static void
1141set_multicast_list(struct net_device *dev)
1142{
1143        unsigned long flags;
1144        struct el3_private *lp = (struct el3_private *)dev->priv;
1145        int ioaddr = dev->base_addr;
1146
1147        if (el3_debug > 1) {
1148                static int old;
1149                if (old != dev->mc_count) {
1150                        old = dev->mc_count;
1151                        printk("%s: Setting Rx mode to %d addresses.\n", dev->name, dev->mc_count);
1152                }
1153        }
1154        spin_lock_irqsave(&lp->lock, flags);
1155        if (dev->flags&IFF_PROMISC) {
1156                outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast | RxProm,
1157                         ioaddr + EL3_CMD);
1158        }
1159        else if (dev->mc_count || (dev->flags&IFF_ALLMULTI)) {
1160                outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast, ioaddr + EL3_CMD);
1161        }
1162        else
1163                outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
1164        spin_unlock_irqrestore(&lp->lock, flags);
1165}
1166
1167static int
1168el3_close(struct net_device *dev)
1169{
1170        int ioaddr = dev->base_addr;
1171        struct el3_private *lp = (struct el3_private *)dev->priv;
1172        
1173        if (el3_debug > 2)
1174                printk("%s: Shutting down ethercard.\n", dev->name);
1175
1176        el3_down(dev);
1177
1178        free_irq(dev->irq, dev);
1179        /* Switching back to window 0 disables the IRQ. */
1180        EL3WINDOW(0);
1181        if (lp->type != EL3_EISA) {
1182            /* But we explicitly zero the IRQ line select anyway. Don't do
1183             * it on EISA cards, it prevents the module from getting an
1184             * IRQ after unload+reload... */
1185            outw(0x0f00, ioaddr + WN0_IRQ);
1186        }
1187
1188        return 0;
1189}
1190
1191static int 
1192el3_link_ok(struct net_device *dev)
1193{
1194        int ioaddr = dev->base_addr;
1195        u16 tmp;
1196
1197        EL3WINDOW(4);
1198        tmp = inw(ioaddr + WN4_MEDIA);
1199        EL3WINDOW(1);
1200        return tmp & (1<<11);
1201}
1202
1203static int
1204el3_netdev_get_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
1205{
1206        u16 tmp;
1207        int ioaddr = dev->base_addr;
1208        
1209        EL3WINDOW(0);
1210        /* obtain current transceiver via WN4_MEDIA? */ 
1211        tmp = inw(ioaddr + WN0_ADDR_CONF);
1212        ecmd->transceiver = XCVR_INTERNAL;
1213        switch (tmp >> 14) {
1214        case 0:
1215                ecmd->port = PORT_TP;
1216                break;
1217        case 1:
1218                ecmd->port = PORT_AUI;
1219                ecmd->transceiver = XCVR_EXTERNAL;
1220                break;
1221        case 3:
1222                ecmd->port = PORT_BNC;
1223        default:
1224                break;
1225        }
1226
1227        ecmd->duplex = DUPLEX_HALF;
1228        ecmd->supported = 0;
1229        tmp = inw(ioaddr + WN0_CONF_CTRL);
1230        if (tmp & (1<<13))
1231                ecmd->supported |= SUPPORTED_AUI;
1232        if (tmp & (1<<12))
1233                ecmd->supported |= SUPPORTED_BNC;
1234        if (tmp & (1<<9)) {
1235                ecmd->supported |= SUPPORTED_TP | SUPPORTED_10baseT_Half |
1236                                SUPPORTED_10baseT_Full; /* hmm... */
1237                EL3WINDOW(4);
1238                tmp = inw(ioaddr + WN4_NETDIAG);
1239                if (tmp & FD_ENABLE)
1240                        ecmd->duplex = DUPLEX_FULL;
1241        }
1242
1243        ecmd->speed = SPEED_10;
1244        EL3WINDOW(1);
1245        return 0;
1246}
1247
1248static int
1249el3_netdev_set_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
1250{
1251        u16 tmp;
1252        int ioaddr = dev->base_addr;
1253
1254        if (ecmd->speed != SPEED_10)
1255                return -EINVAL;
1256        if ((ecmd->duplex != DUPLEX_HALF) && (ecmd->duplex != DUPLEX_FULL))
1257                return -EINVAL;
1258        if ((ecmd->transceiver != XCVR_INTERNAL) && (ecmd->transceiver != XCVR_EXTERNAL))
1259                return -EINVAL;
1260
1261        /* change XCVR type */
1262        EL3WINDOW(0);
1263        tmp = inw(ioaddr + WN0_ADDR_CONF);
1264        switch (ecmd->port) {
1265        case PORT_TP:
1266                tmp &= ~(3<<14);
1267                dev->if_port = 0;
1268                break;
1269        case PORT_AUI:
1270                tmp |= (1<<14);
1271                dev->if_port = 1;
1272                break;
1273        case PORT_BNC:
1274                tmp |= (3<<14);
1275                dev->if_port = 3;
1276                break;
1277        default:
1278                return -EINVAL;
1279        }
1280
1281        outw(tmp, ioaddr + WN0_ADDR_CONF);
1282        if (dev->if_port == 3) {
1283                /* fire up the DC-DC convertor if BNC gets enabled */
1284                tmp = inw(ioaddr + WN0_ADDR_CONF);
1285                if (tmp & (3 << 14)) {
1286                        outw(StartCoax, ioaddr + EL3_CMD);
1287                        udelay(800);
1288                } else
1289                        return -EIO;
1290        }
1291
1292        EL3WINDOW(4);
1293        tmp = inw(ioaddr + WN4_NETDIAG);
1294        if (ecmd->duplex == DUPLEX_FULL)
1295                tmp |= FD_ENABLE;
1296        else
1297                tmp &= ~FD_ENABLE;
1298        outw(tmp, ioaddr + WN4_NETDIAG);
1299        EL3WINDOW(1);
1300
1301        return 0;
1302}
1303
1304/**
1305 * netdev_ethtool_ioctl: Handle network interface SIOCETHTOOL ioctls
1306 * @dev: network interface on which out-of-band action is to be performed
1307 * @useraddr: userspace address to which data is to be read and returned
1308 *
1309 * Process the various commands of the SIOCETHTOOL interface.
1310 */
1311
1312static int
1313netdev_ethtool_ioctl (struct net_device *dev, void *useraddr)
1314{
1315        u32 ethcmd;
1316        struct el3_private *lp = dev->priv;
1317
1318        /* dev_ioctl() in ../../net/core/dev.c has already checked
1319           capable(CAP_NET_ADMIN), so don't bother with that here.  */
1320
1321        if (get_user(ethcmd, (u32 *)useraddr))
1322                return -EFAULT;
1323
1324        switch (ethcmd) {
1325
1326        case ETHTOOL_GDRVINFO: {
1327                struct ethtool_drvinfo info = { ETHTOOL_GDRVINFO };
1328                strcpy (info.driver, DRV_NAME);
1329                strcpy (info.version, DRV_VERSION);
1330                if (copy_to_user (useraddr, &info, sizeof (info)))
1331                        return -EFAULT;
1332                return 0;
1333        }
1334
1335        /* get settings */
1336        case ETHTOOL_GSET: {
1337                int ret;
1338                struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1339                spin_lock_irq(&lp->lock);
1340                ret = el3_netdev_get_ecmd(dev, &ecmd);
1341                spin_unlock_irq(&lp->lock);
1342                if (copy_to_user(useraddr, &ecmd, sizeof(ecmd)))
1343                        return -EFAULT;
1344                return ret;
1345        }
1346
1347        /* set settings */
1348        case ETHTOOL_SSET: {
1349                int ret;
1350                struct ethtool_cmd ecmd;
1351                if (copy_from_user(&ecmd, useraddr, sizeof(ecmd)))
1352                        return -EFAULT;
1353                spin_lock_irq(&lp->lock);
1354                ret = el3_netdev_set_ecmd(dev, &ecmd);
1355                spin_unlock_irq(&lp->lock);
1356                return ret;
1357        }
1358
1359        /* get link status */
1360        case ETHTOOL_GLINK: {
1361                struct ethtool_value edata = { ETHTOOL_GLINK };
1362                spin_lock_irq(&lp->lock);
1363                edata.data = el3_link_ok(dev);
1364                spin_unlock_irq(&lp->lock);
1365                if (copy_to_user(useraddr, &edata, sizeof(edata)))
1366                        return -EFAULT;
1367                return 0;
1368        }
1369
1370        /* get message-level */
1371        case ETHTOOL_GMSGLVL: {
1372                struct ethtool_value edata = {ETHTOOL_GMSGLVL};
1373                edata.data = el3_debug;
1374                if (copy_to_user(useraddr, &edata, sizeof(edata)))
1375                        return -EFAULT;
1376                return 0;
1377        }
1378        /* set message-level */
1379        case ETHTOOL_SMSGLVL: {
1380                struct ethtool_value edata;
1381                if (copy_from_user(&edata, useraddr, sizeof(edata)))
1382                        return -EFAULT;
1383                el3_debug = edata.data;
1384                return 0;
1385        }
1386
1387        default:
1388                break;
1389        }
1390
1391        return -EOPNOTSUPP;
1392}
1393
1394/**
1395 * netdev_ioctl: Handle network interface ioctls
1396 * @dev: network interface on which out-of-band action is to be performed
1397 * @rq: user request data
1398 * @cmd: command issued by user
1399 *
1400 * Process the various out-of-band ioctls passed to this driver.
1401 */
1402
1403static int
1404netdev_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
1405{
1406        int rc = 0;
1407
1408        switch (cmd) {
1409        case SIOCETHTOOL:
1410                rc = netdev_ethtool_ioctl(dev, (void *) rq->ifr_data);
1411                break;
1412
1413        default:
1414                rc = -EOPNOTSUPP;
1415                break;
1416        }
1417
1418        return rc;
1419}
1420
1421static void
1422el3_down(struct net_device *dev)
1423{
1424        int ioaddr = dev->base_addr;
1425
1426        netif_stop_queue(dev);
1427
1428        /* Turn off statistics ASAP.  We update lp->stats below. */
1429        outw(StatsDisable, ioaddr + EL3_CMD);
1430
1431        /* Disable the receiver and transmitter. */
1432        outw(RxDisable, ioaddr + EL3_CMD);
1433        outw(TxDisable, ioaddr + EL3_CMD);
1434
1435        if (dev->if_port == 3)
1436                /* Turn off thinnet power.  Green! */
1437                outw(StopCoax, ioaddr + EL3_CMD);
1438        else if (dev->if_port == 0) {
1439                /* Disable link beat and jabber, if_port may change here next open(). */
1440                EL3WINDOW(4);
1441                outw(inw(ioaddr + WN4_MEDIA) & ~MEDIA_TP, ioaddr + WN4_MEDIA);
1442        }
1443
1444        outw(SetIntrEnb | 0x0000, ioaddr + EL3_CMD);
1445
1446        update_stats(dev);
1447}
1448
1449static void
1450el3_up(struct net_device *dev)
1451{
1452        int i, sw_info, net_diag;
1453        int ioaddr = dev->base_addr;
1454        
1455        /* Activating the board required and does no harm otherwise */
1456        outw(0x0001, ioaddr + 4);
1457
1458        /* Set the IRQ line. */
1459#ifdef CONFIG_X86_PC9800
1460        if (dev->irq == 6)
1461                dev->irq = 7;
1462        else if (dev->irq == 13)
1463                dev->irq = 15;
1464#endif
1465        outw((dev->irq << 12) | 0x0f00, ioaddr + WN0_IRQ);
1466
1467        /* Set the station address in window 2 each time opened. */
1468        EL3WINDOW(2);
1469
1470        for (i = 0; i < 6; i++)
1471                outb(dev->dev_addr[i], ioaddr + i);
1472
1473        if ((dev->if_port & 0x03) == 3) /* BNC interface */
1474                /* Start the thinnet transceiver. We should really wait 50ms...*/
1475                outw(StartCoax, ioaddr + EL3_CMD);
1476        else if ((dev->if_port & 0x03) == 0) { /* 10baseT interface */
1477                /* Combine secondary sw_info word (the adapter level) and primary
1478                        sw_info word (duplex setting plus other useless bits) */
1479                EL3WINDOW(0);
1480                sw_info = (read_eeprom(ioaddr, 0x14) & 0x400f) | 
1481                        (read_eeprom(ioaddr, 0x0d) & 0xBff0);
1482
1483                EL3WINDOW(4);
1484                net_diag = inw(ioaddr + WN4_NETDIAG);
1485                net_diag = (net_diag | FD_ENABLE); /* temporarily assume full-duplex will be set */
1486                printk("%s: ", dev->name);
1487                switch (dev->if_port & 0x0c) {
1488                        case 12:
1489                                /* force full-duplex mode if 3c5x9b */
1490                                if (sw_info & 0x000f) {
1491                                        printk("Forcing 3c5x9b full-duplex mode");
1492                                        break;
1493                                }
1494                        case 8:
1495                                /* set full-duplex mode based on eeprom config setting */
1496                                if ((sw_info & 0x000f) && (sw_info & 0x8000)) {
1497                                        printk("Setting 3c5x9b full-duplex mode (from EEPROM configuration bit)");
1498                                        break;
1499                                }
1500                        default:
1501                                /* xcvr=(0 || 4) OR user has an old 3c5x9 non "B" model */
1502                                printk("Setting 3c5x9/3c5x9B half-duplex mode");
1503                                net_diag = (net_diag & ~FD_ENABLE); /* disable full duplex */
1504                }
1505
1506                outw(net_diag, ioaddr + WN4_NETDIAG);
1507                printk(" if_port: %d, sw_info: %4.4x\n", dev->if_port, sw_info);
1508                if (el3_debug > 3)
1509                        printk("%s: 3c5x9 net diag word is now: %4.4x.\n", dev->name, net_diag);
1510                /* Enable link beat and jabber check. */
1511                outw(inw(ioaddr + WN4_MEDIA) | MEDIA_TP, ioaddr + WN4_MEDIA);
1512        }
1513
1514        /* Switch to the stats window, and clear all stats by reading. */
1515        outw(StatsDisable, ioaddr + EL3_CMD);
1516        EL3WINDOW(6);
1517        for (i = 0; i < 9; i++)
1518                inb(ioaddr + i);
1519        inw(ioaddr + 10);
1520        inw(ioaddr + 12);
1521
1522        /* Switch to register set 1 for normal use. */
1523        EL3WINDOW(1);
1524
1525        /* Accept b-case and phys addr only. */
1526        outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
1527        outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
1528
1529        outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
1530        outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
1531        /* Allow status bits to be seen. */
1532        outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
1533        /* Ack all pending events, and set active indicator mask. */
1534        outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
1535                 ioaddr + EL3_CMD);
1536        outw(SetIntrEnb | IntLatch|TxAvailable|TxComplete|RxComplete|StatsFull,
1537                 ioaddr + EL3_CMD);
1538
1539        netif_start_queue(dev);
1540}
1541
1542/* Power Management support functions */
1543#ifdef CONFIG_PM
1544
1545static int
1546el3_suspend(struct pm_dev *pdev)
1547{
1548        unsigned long flags;
1549        struct net_device *dev;
1550        struct el3_private *lp;
1551        int ioaddr;
1552        
1553        if (!pdev && !pdev->data)
1554                return -EINVAL;
1555
1556        dev = (struct net_device *)pdev->data;
1557        lp = (struct el3_private *)dev->priv;
1558        ioaddr = dev->base_addr;
1559
1560        spin_lock_irqsave(&lp->lock, flags);
1561
1562        if (netif_running(dev))
1563                netif_device_detach(dev);
1564
1565        el3_down(dev);
1566        outw(PowerDown, ioaddr + EL3_CMD);
1567
1568        spin_unlock_irqrestore(&lp->lock, flags);
1569        return 0;
1570}
1571
1572static int
1573el3_resume(struct pm_dev *pdev)
1574{
1575        unsigned long flags;
1576        struct net_device *dev;
1577        struct el3_private *lp;
1578        int ioaddr;
1579        
1580        if (!pdev && !pdev->data)
1581                return -EINVAL;
1582
1583        dev = (struct net_device *)pdev->data;
1584        lp = (struct el3_private *)dev->priv;
1585        ioaddr = dev->base_addr;
1586
1587        spin_lock_irqsave(&lp->lock, flags);
1588
1589        outw(PowerUp, ioaddr + EL3_CMD);
1590        el3_up(dev);
1591
1592        if (netif_running(dev))
1593                netif_device_attach(dev);
1594                
1595        spin_unlock_irqrestore(&lp->lock, flags);
1596        return 0;
1597}
1598
1599static int
1600el3_pm_callback(struct pm_dev *pdev, pm_request_t rqst, void *data)
1601{
1602        switch (rqst) {
1603                case PM_SUSPEND:
1604                        return el3_suspend(pdev);
1605
1606                case PM_RESUME:
1607                        return el3_resume(pdev);
1608        }
1609        return 0;
1610}
1611
1612#endif /* CONFIG_PM */
1613
1614/* Parameters that may be passed into the module. */
1615static int debug = -1;
1616static int irq[] = {-1, -1, -1, -1, -1, -1, -1, -1};
1617static int xcvr[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
1618
1619MODULE_PARM(debug,"i");
1620MODULE_PARM(irq,"1-8i");
1621MODULE_PARM(xcvr,"1-12i");
1622MODULE_PARM(max_interrupt_work, "i");
1623MODULE_PARM_DESC(debug, "debug level (0-6)");
1624MODULE_PARM_DESC(irq, "IRQ number(s) (assigned)");
1625MODULE_PARM_DESC(xcvr,"transceiver(s) (0=internal, 1=external)");
1626MODULE_PARM_DESC(max_interrupt_work, "maximum events handled per interrupt");
1627#if defined(__ISAPNP__) && !defined(CONFIG_X86_PC9800)
1628MODULE_PARM(nopnp, "i");
1629MODULE_PARM_DESC(nopnp, "disable ISA PnP support (0-1)");
1630MODULE_DEVICE_TABLE(isapnp, el3_isapnp_adapters);
1631#endif  /* __ISAPNP__ */
1632MODULE_DESCRIPTION("3Com Etherlink III (3c509, 3c509B) ISA/PnP ethernet driver");
1633MODULE_LICENSE("GPL");
1634
1635static int __init el3_init_module(void)
1636{
1637        el3_cards = 0;
1638
1639        if (debug >= 0)
1640                el3_debug = debug;
1641
1642        el3_root_dev = NULL;
1643        while (el3_probe(el3_cards) == 0) {
1644                if (irq[el3_cards] > 1)
1645                        el3_root_dev->irq = irq[el3_cards];
1646                if (xcvr[el3_cards] >= 0)
1647                        el3_root_dev->if_port = xcvr[el3_cards];
1648                el3_cards++;
1649        }
1650
1651#ifdef CONFIG_EISA
1652        if (eisa_driver_register (&el3_eisa_driver) < 0) {
1653                        eisa_driver_unregister (&el3_eisa_driver);
1654        }
1655#endif
1656#ifdef CONFIG_MCA
1657        mca_register_driver(&el3_mca_driver);
1658#endif
1659        return el3_cards ? 0 : -ENODEV;
1660}
1661
1662static void __exit el3_cleanup_module(void)
1663{
1664        struct net_device *next_dev;
1665
1666        while (el3_root_dev) {
1667                struct el3_private *lp = (struct el3_private *)el3_root_dev->priv;
1668
1669                next_dev = lp->next_dev;
1670                el3_common_remove (el3_root_dev);
1671                el3_root_dev = next_dev;
1672        }
1673
1674#ifdef CONFIG_EISA
1675        eisa_driver_unregister (&el3_eisa_driver);
1676#endif
1677#ifdef CONFIG_MCA
1678        mca_unregister_driver(&el3_mca_driver);
1679#endif
1680}
1681
1682module_init (el3_init_module);
1683module_exit (el3_cleanup_module);
1684
1685/*
1686 * Local variables:
1687 *  compile-command: "gcc -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -c 3c509.c"
1688 *  version-control: t
1689 *  kept-new-versions: 5
1690 *  tab-width: 4
1691 * End:
1692 */
1693
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.