Back to home page

OSCL-LXR

 
 

    


0001 /* ======================================================================
0002  *
0003  * A PCMCIA ethernet driver for the 3com 3c589 card.
0004  *
0005  * Copyright (C) 1999 David A. Hinds -- dahinds@users.sourceforge.net
0006  *
0007  * 3c589_cs.c 1.162 2001/10/13 00:08:50
0008  *
0009  * The network driver code is based on Donald Becker's 3c589 code:
0010  *
0011  * Written 1994 by Donald Becker.
0012  * Copyright 1993 United States Government as represented by the
0013  * Director, National Security Agency.  This software may be used and
0014  * distributed according to the terms of the GNU General Public License,
0015  * incorporated herein by reference.
0016  * Donald Becker may be reached at becker@scyld.com
0017  *
0018  * Updated for 2.5.x by Alan Cox <alan@lxorguk.ukuu.org.uk>
0019  *
0020  * ======================================================================
0021  */
0022 
0023 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0024 
0025 #define DRV_NAME    "3c589_cs"
0026 
0027 #include <linux/module.h>
0028 #include <linux/kernel.h>
0029 #include <linux/ptrace.h>
0030 #include <linux/slab.h>
0031 #include <linux/string.h>
0032 #include <linux/timer.h>
0033 #include <linux/interrupt.h>
0034 #include <linux/in.h>
0035 #include <linux/delay.h>
0036 #include <linux/ethtool.h>
0037 #include <linux/netdevice.h>
0038 #include <linux/etherdevice.h>
0039 #include <linux/skbuff.h>
0040 #include <linux/if_arp.h>
0041 #include <linux/ioport.h>
0042 #include <linux/bitops.h>
0043 #include <linux/jiffies.h>
0044 #include <linux/uaccess.h>
0045 #include <linux/io.h>
0046 
0047 #include <pcmcia/cistpl.h>
0048 #include <pcmcia/cisreg.h>
0049 #include <pcmcia/ciscode.h>
0050 #include <pcmcia/ds.h>
0051 
0052 
0053 /* To minimize the size of the driver source I only define operating
0054  * constants if they are used several times. You'll need the manual
0055  * if you want to understand driver details.
0056  */
0057 
0058 /* Offsets from base I/O address. */
0059 #define EL3_DATA    0x00
0060 #define EL3_TIMER   0x0a
0061 #define EL3_CMD     0x0e
0062 #define EL3_STATUS  0x0e
0063 
0064 #define EEPROM_READ 0x0080
0065 #define EEPROM_BUSY 0x8000
0066 
0067 #define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)
0068 
0069 /* The top five bits written to EL3_CMD are a command, the lower
0070  * 11 bits are the parameter, if applicable.
0071  */
0072 
0073 enum c509cmd {
0074     TotalReset  = 0<<11,
0075     SelectWindow    = 1<<11,
0076     StartCoax   = 2<<11,
0077     RxDisable   = 3<<11,
0078     RxEnable    = 4<<11,
0079     RxReset     = 5<<11,
0080     RxDiscard   = 8<<11,
0081     TxEnable    = 9<<11,
0082     TxDisable   = 10<<11,
0083     TxReset     = 11<<11,
0084     FakeIntr    = 12<<11,
0085     AckIntr     = 13<<11,
0086     SetIntrEnb  = 14<<11,
0087     SetStatusEnb    = 15<<11,
0088     SetRxFilter = 16<<11,
0089     SetRxThreshold  = 17<<11,
0090     SetTxThreshold  = 18<<11,
0091     SetTxStart  = 19<<11,
0092     StatsEnable = 21<<11,
0093     StatsDisable    = 22<<11,
0094     StopCoax    = 23<<11
0095 };
0096 
0097 enum c509status {
0098     IntLatch    = 0x0001,
0099     AdapterFailure  = 0x0002,
0100     TxComplete  = 0x0004,
0101     TxAvailable = 0x0008,
0102     RxComplete  = 0x0010,
0103     RxEarly     = 0x0020,
0104     IntReq      = 0x0040,
0105     StatsFull   = 0x0080,
0106     CmdBusy     = 0x1000
0107 };
0108 
0109 /* The SetRxFilter command accepts the following classes: */
0110 enum RxFilter {
0111     RxStation   = 1,
0112     RxMulticast = 2,
0113     RxBroadcast = 4,
0114     RxProm      = 8
0115 };
0116 
0117 /* Register window 1 offsets, the window used in normal operation. */
0118 #define TX_FIFO     0x00
0119 #define RX_FIFO     0x00
0120 #define RX_STATUS   0x08
0121 #define TX_STATUS   0x0B
0122 #define TX_FREE     0x0C    /* Remaining free bytes in Tx buffer. */
0123 
0124 #define WN0_IRQ     0x08    /* Window 0: Set IRQ line in bits 12-15. */
0125 #define WN4_MEDIA   0x0A    /* Window 4: Various transcvr/media bits. */
0126 #define MEDIA_TP    0x00C0  /* Enable link beat and jabber for 10baseT. */
0127 #define MEDIA_LED   0x0001  /* Enable link light on 3C589E cards. */
0128 
0129 /* Time in jiffies before concluding Tx hung */
0130 #define TX_TIMEOUT  ((400*HZ)/1000)
0131 
0132 struct el3_private {
0133     struct pcmcia_device    *p_dev;
0134     /* For transceiver monitoring */
0135     struct timer_list   media;
0136     u16         media_status;
0137     u16         fast_poll;
0138     unsigned long       last_irq;
0139     spinlock_t      lock;
0140 };
0141 
0142 static const char *if_names[] = { "auto", "10baseT", "10base2", "AUI" };
0143 
0144 /*====================================================================*/
0145 
0146 /* Module parameters */
0147 
0148 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
0149 MODULE_DESCRIPTION("3Com 3c589 series PCMCIA ethernet driver");
0150 MODULE_LICENSE("GPL");
0151 
0152 #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0)
0153 
0154 /* Special hook for setting if_port when module is loaded */
0155 INT_MODULE_PARM(if_port, 0);
0156 
0157 
0158 /*====================================================================*/
0159 
0160 static int tc589_config(struct pcmcia_device *link);
0161 static void tc589_release(struct pcmcia_device *link);
0162 
0163 static u16 read_eeprom(unsigned int ioaddr, int index);
0164 static void tc589_reset(struct net_device *dev);
0165 static void media_check(struct timer_list *t);
0166 static int el3_config(struct net_device *dev, struct ifmap *map);
0167 static int el3_open(struct net_device *dev);
0168 static netdev_tx_t el3_start_xmit(struct sk_buff *skb,
0169                     struct net_device *dev);
0170 static irqreturn_t el3_interrupt(int irq, void *dev_id);
0171 static void update_stats(struct net_device *dev);
0172 static struct net_device_stats *el3_get_stats(struct net_device *dev);
0173 static int el3_rx(struct net_device *dev);
0174 static int el3_close(struct net_device *dev);
0175 static void el3_tx_timeout(struct net_device *dev, unsigned int txqueue);
0176 static void set_rx_mode(struct net_device *dev);
0177 static void set_multicast_list(struct net_device *dev);
0178 static const struct ethtool_ops netdev_ethtool_ops;
0179 
0180 static void tc589_detach(struct pcmcia_device *p_dev);
0181 
0182 static const struct net_device_ops el3_netdev_ops = {
0183     .ndo_open       = el3_open,
0184     .ndo_stop       = el3_close,
0185     .ndo_start_xmit     = el3_start_xmit,
0186     .ndo_tx_timeout     = el3_tx_timeout,
0187     .ndo_set_config     = el3_config,
0188     .ndo_get_stats      = el3_get_stats,
0189     .ndo_set_rx_mode    = set_multicast_list,
0190     .ndo_set_mac_address    = eth_mac_addr,
0191     .ndo_validate_addr  = eth_validate_addr,
0192 };
0193 
0194 static int tc589_probe(struct pcmcia_device *link)
0195 {
0196     struct el3_private *lp;
0197     struct net_device *dev;
0198 
0199     dev_dbg(&link->dev, "3c589_attach()\n");
0200 
0201     /* Create new ethernet device */
0202     dev = alloc_etherdev(sizeof(struct el3_private));
0203     if (!dev)
0204         return -ENOMEM;
0205     lp = netdev_priv(dev);
0206     link->priv = dev;
0207     lp->p_dev = link;
0208 
0209     spin_lock_init(&lp->lock);
0210     link->resource[0]->end = 16;
0211     link->resource[0]->flags |= IO_DATA_PATH_WIDTH_16;
0212 
0213     link->config_flags |= CONF_ENABLE_IRQ;
0214     link->config_index = 1;
0215 
0216     dev->netdev_ops = &el3_netdev_ops;
0217     dev->watchdog_timeo = TX_TIMEOUT;
0218 
0219     dev->ethtool_ops = &netdev_ethtool_ops;
0220 
0221     return tc589_config(link);
0222 }
0223 
0224 static void tc589_detach(struct pcmcia_device *link)
0225 {
0226     struct net_device *dev = link->priv;
0227 
0228     dev_dbg(&link->dev, "3c589_detach\n");
0229 
0230     unregister_netdev(dev);
0231 
0232     tc589_release(link);
0233 
0234     free_netdev(dev);
0235 } /* tc589_detach */
0236 
0237 static int tc589_config(struct pcmcia_device *link)
0238 {
0239     struct net_device *dev = link->priv;
0240     int ret, i, j, multi = 0, fifo;
0241     __be16 addr[ETH_ALEN / 2];
0242     unsigned int ioaddr;
0243     static const char * const ram_split[] = {"5:3", "3:1", "1:1", "3:5"};
0244     u8 *buf;
0245     size_t len;
0246 
0247     dev_dbg(&link->dev, "3c589_config\n");
0248 
0249     /* Is this a 3c562? */
0250     if (link->manf_id != MANFID_3COM)
0251         dev_info(&link->dev, "hmmm, is this really a 3Com card??\n");
0252     multi = (link->card_id == PRODID_3COM_3C562);
0253 
0254     link->io_lines = 16;
0255 
0256     /* For the 3c562, the base address must be xx00-xx7f */
0257     for (i = j = 0; j < 0x400; j += 0x10) {
0258         if (multi && (j & 0x80))
0259             continue;
0260         link->resource[0]->start = j ^ 0x300;
0261         i = pcmcia_request_io(link);
0262         if (i == 0)
0263             break;
0264     }
0265     if (i != 0)
0266         goto failed;
0267 
0268     ret = pcmcia_request_irq(link, el3_interrupt);
0269     if (ret)
0270         goto failed;
0271 
0272     ret = pcmcia_enable_device(link);
0273     if (ret)
0274         goto failed;
0275 
0276     dev->irq = link->irq;
0277     dev->base_addr = link->resource[0]->start;
0278     ioaddr = dev->base_addr;
0279     EL3WINDOW(0);
0280 
0281     /* The 3c589 has an extra EEPROM for configuration info, including
0282      * the hardware address.  The 3c562 puts the address in the CIS.
0283      */
0284     len = pcmcia_get_tuple(link, 0x88, &buf);
0285     if (buf && len >= 6) {
0286         for (i = 0; i < 3; i++)
0287             addr[i] = htons(le16_to_cpu(buf[i*2]));
0288         kfree(buf);
0289     } else {
0290         kfree(buf); /* 0 < len < 6 */
0291         for (i = 0; i < 3; i++)
0292             addr[i] = htons(read_eeprom(ioaddr, i));
0293         if (addr[0] == htons(0x6060)) {
0294             dev_err(&link->dev, "IO port conflict at 0x%03lx-0x%03lx\n",
0295                     dev->base_addr, dev->base_addr+15);
0296             goto failed;
0297         }
0298     }
0299     eth_hw_addr_set(dev, (u8 *)addr);
0300 
0301     /* The address and resource configuration register aren't loaded from
0302      * the EEPROM and *must* be set to 0 and IRQ3 for the PCMCIA version.
0303      */
0304 
0305     outw(0x3f00, ioaddr + 8);
0306     fifo = inl(ioaddr);
0307 
0308     /* The if_port symbol can be set when the module is loaded */
0309     if ((if_port >= 0) && (if_port <= 3))
0310         dev->if_port = if_port;
0311     else
0312         dev_err(&link->dev, "invalid if_port requested\n");
0313 
0314     SET_NETDEV_DEV(dev, &link->dev);
0315 
0316     if (register_netdev(dev) != 0) {
0317         dev_err(&link->dev, "register_netdev() failed\n");
0318         goto failed;
0319     }
0320 
0321     netdev_info(dev, "3Com 3c%s, io %#3lx, irq %d, hw_addr %pM\n",
0322             (multi ? "562" : "589"), dev->base_addr, dev->irq,
0323             dev->dev_addr);
0324     netdev_info(dev, "  %dK FIFO split %s Rx:Tx, %s xcvr\n",
0325             (fifo & 7) ? 32 : 8, ram_split[(fifo >> 16) & 3],
0326             if_names[dev->if_port]);
0327     return 0;
0328 
0329 failed:
0330     tc589_release(link);
0331     return -ENODEV;
0332 } /* tc589_config */
0333 
0334 static void tc589_release(struct pcmcia_device *link)
0335 {
0336     pcmcia_disable_device(link);
0337 }
0338 
0339 static int tc589_suspend(struct pcmcia_device *link)
0340 {
0341     struct net_device *dev = link->priv;
0342 
0343     if (link->open)
0344         netif_device_detach(dev);
0345 
0346     return 0;
0347 }
0348 
0349 static int tc589_resume(struct pcmcia_device *link)
0350 {
0351     struct net_device *dev = link->priv;
0352 
0353     if (link->open) {
0354         tc589_reset(dev);
0355         netif_device_attach(dev);
0356     }
0357 
0358     return 0;
0359 }
0360 
0361 /*====================================================================*/
0362 
0363 /* Use this for commands that may take time to finish */
0364 
0365 static void tc589_wait_for_completion(struct net_device *dev, int cmd)
0366 {
0367     int i = 100;
0368     outw(cmd, dev->base_addr + EL3_CMD);
0369     while (--i > 0)
0370         if (!(inw(dev->base_addr + EL3_STATUS) & 0x1000))
0371             break;
0372     if (i == 0)
0373         netdev_warn(dev, "command 0x%04x did not complete!\n", cmd);
0374 }
0375 
0376 /* Read a word from the EEPROM using the regular EEPROM access register.
0377  * Assume that we are in register window zero.
0378  */
0379 
0380 static u16 read_eeprom(unsigned int ioaddr, int index)
0381 {
0382     int i;
0383     outw(EEPROM_READ + index, ioaddr + 10);
0384     /* Reading the eeprom takes 162 us */
0385     for (i = 1620; i >= 0; i--)
0386         if ((inw(ioaddr + 10) & EEPROM_BUSY) == 0)
0387             break;
0388     return inw(ioaddr + 12);
0389 }
0390 
0391 /* Set transceiver type, perhaps to something other than what the user
0392  * specified in dev->if_port.
0393  */
0394 
0395 static void tc589_set_xcvr(struct net_device *dev, int if_port)
0396 {
0397     struct el3_private *lp = netdev_priv(dev);
0398     unsigned int ioaddr = dev->base_addr;
0399 
0400     EL3WINDOW(0);
0401     switch (if_port) {
0402     case 0:
0403     case 1:
0404         outw(0, ioaddr + 6);
0405         break;
0406     case 2:
0407         outw(3<<14, ioaddr + 6);
0408         break;
0409     case 3:
0410         outw(1<<14, ioaddr + 6);
0411         break;
0412     }
0413     /* On PCMCIA, this just turns on the LED */
0414     outw((if_port == 2) ? StartCoax : StopCoax, ioaddr + EL3_CMD);
0415     /* 10baseT interface, enable link beat and jabber check. */
0416     EL3WINDOW(4);
0417     outw(MEDIA_LED | ((if_port < 2) ? MEDIA_TP : 0), ioaddr + WN4_MEDIA);
0418     EL3WINDOW(1);
0419     if (if_port == 2)
0420         lp->media_status = ((dev->if_port == 0) ? 0x8000 : 0x4000);
0421     else
0422         lp->media_status = ((dev->if_port == 0) ? 0x4010 : 0x8800);
0423 }
0424 
0425 static void dump_status(struct net_device *dev)
0426 {
0427     unsigned int ioaddr = dev->base_addr;
0428     EL3WINDOW(1);
0429     netdev_info(dev, "  irq status %04x, rx status %04x, tx status %02x  tx free %04x\n",
0430             inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS),
0431             inb(ioaddr+TX_STATUS), inw(ioaddr+TX_FREE));
0432     EL3WINDOW(4);
0433     netdev_info(dev, "  diagnostics: fifo %04x net %04x ethernet %04x media %04x\n",
0434             inw(ioaddr+0x04), inw(ioaddr+0x06), inw(ioaddr+0x08),
0435             inw(ioaddr+0x0a));
0436     EL3WINDOW(1);
0437 }
0438 
0439 /* Reset and restore all of the 3c589 registers. */
0440 static void tc589_reset(struct net_device *dev)
0441 {
0442     unsigned int ioaddr = dev->base_addr;
0443     int i;
0444 
0445     EL3WINDOW(0);
0446     outw(0x0001, ioaddr + 4);           /* Activate board. */
0447     outw(0x3f00, ioaddr + 8);           /* Set the IRQ line. */
0448 
0449     /* Set the station address in window 2. */
0450     EL3WINDOW(2);
0451     for (i = 0; i < 6; i++)
0452         outb(dev->dev_addr[i], ioaddr + i);
0453 
0454     tc589_set_xcvr(dev, dev->if_port);
0455 
0456     /* Switch to the stats window, and clear all stats by reading. */
0457     outw(StatsDisable, ioaddr + EL3_CMD);
0458     EL3WINDOW(6);
0459     for (i = 0; i < 9; i++)
0460         inb(ioaddr+i);
0461     inw(ioaddr + 10);
0462     inw(ioaddr + 12);
0463 
0464     /* Switch to register set 1 for normal use. */
0465     EL3WINDOW(1);
0466 
0467     set_rx_mode(dev);
0468     outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
0469     outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
0470     outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
0471     /* Allow status bits to be seen. */
0472     outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
0473     /* Ack all pending events, and set active indicator mask. */
0474     outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
0475      ioaddr + EL3_CMD);
0476     outw(SetIntrEnb | IntLatch | TxAvailable | RxComplete | StatsFull
0477      | AdapterFailure, ioaddr + EL3_CMD);
0478 }
0479 
0480 static void netdev_get_drvinfo(struct net_device *dev,
0481                    struct ethtool_drvinfo *info)
0482 {
0483     strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
0484     snprintf(info->bus_info, sizeof(info->bus_info),
0485         "PCMCIA 0x%lx", dev->base_addr);
0486 }
0487 
0488 static const struct ethtool_ops netdev_ethtool_ops = {
0489     .get_drvinfo        = netdev_get_drvinfo,
0490 };
0491 
0492 static int el3_config(struct net_device *dev, struct ifmap *map)
0493 {
0494     if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
0495         if (map->port <= 3) {
0496             dev->if_port = map->port;
0497             netdev_info(dev, "switched to %s port\n", if_names[dev->if_port]);
0498             tc589_set_xcvr(dev, dev->if_port);
0499         } else {
0500             return -EINVAL;
0501         }
0502     }
0503     return 0;
0504 }
0505 
0506 static int el3_open(struct net_device *dev)
0507 {
0508     struct el3_private *lp = netdev_priv(dev);
0509     struct pcmcia_device *link = lp->p_dev;
0510 
0511     if (!pcmcia_dev_present(link))
0512         return -ENODEV;
0513 
0514     link->open++;
0515     netif_start_queue(dev);
0516 
0517     tc589_reset(dev);
0518     timer_setup(&lp->media, media_check, 0);
0519     mod_timer(&lp->media, jiffies + HZ);
0520 
0521     dev_dbg(&link->dev, "%s: opened, status %4.4x.\n",
0522       dev->name, inw(dev->base_addr + EL3_STATUS));
0523 
0524     return 0;
0525 }
0526 
0527 static void el3_tx_timeout(struct net_device *dev, unsigned int txqueue)
0528 {
0529     unsigned int ioaddr = dev->base_addr;
0530 
0531     netdev_warn(dev, "Transmit timed out!\n");
0532     dump_status(dev);
0533     dev->stats.tx_errors++;
0534     netif_trans_update(dev); /* prevent tx timeout */
0535     /* Issue TX_RESET and TX_START commands. */
0536     tc589_wait_for_completion(dev, TxReset);
0537     outw(TxEnable, ioaddr + EL3_CMD);
0538     netif_wake_queue(dev);
0539 }
0540 
0541 static void pop_tx_status(struct net_device *dev)
0542 {
0543     unsigned int ioaddr = dev->base_addr;
0544     int i;
0545 
0546     /* Clear the Tx status stack. */
0547     for (i = 32; i > 0; i--) {
0548         u_char tx_status = inb(ioaddr + TX_STATUS);
0549         if (!(tx_status & 0x84))
0550             break;
0551         /* reset transmitter on jabber error or underrun */
0552         if (tx_status & 0x30)
0553             tc589_wait_for_completion(dev, TxReset);
0554         if (tx_status & 0x38) {
0555             netdev_dbg(dev, "transmit error: status 0x%02x\n", tx_status);
0556             outw(TxEnable, ioaddr + EL3_CMD);
0557             dev->stats.tx_aborted_errors++;
0558         }
0559         outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
0560     }
0561 }
0562 
0563 static netdev_tx_t el3_start_xmit(struct sk_buff *skb,
0564                     struct net_device *dev)
0565 {
0566     unsigned int ioaddr = dev->base_addr;
0567     struct el3_private *priv = netdev_priv(dev);
0568     unsigned long flags;
0569 
0570     netdev_dbg(dev, "el3_start_xmit(length = %ld) called, status %4.4x.\n",
0571            (long)skb->len, inw(ioaddr + EL3_STATUS));
0572 
0573     spin_lock_irqsave(&priv->lock, flags);
0574 
0575     dev->stats.tx_bytes += skb->len;
0576 
0577     /* Put out the doubleword header... */
0578     outw(skb->len, ioaddr + TX_FIFO);
0579     outw(0x00, ioaddr + TX_FIFO);
0580     /* ... and the packet rounded to a doubleword. */
0581     outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
0582 
0583     if (inw(ioaddr + TX_FREE) <= 1536) {
0584         netif_stop_queue(dev);
0585         /* Interrupt us when the FIFO has room for max-sized packet. */
0586         outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);
0587     }
0588 
0589     pop_tx_status(dev);
0590     spin_unlock_irqrestore(&priv->lock, flags);
0591     dev_kfree_skb(skb);
0592 
0593     return NETDEV_TX_OK;
0594 }
0595 
0596 /* The EL3 interrupt handler. */
0597 static irqreturn_t el3_interrupt(int irq, void *dev_id)
0598 {
0599     struct net_device *dev = (struct net_device *) dev_id;
0600     struct el3_private *lp = netdev_priv(dev);
0601     unsigned int ioaddr;
0602     __u16 status;
0603     int i = 0, handled = 1;
0604 
0605     if (!netif_device_present(dev))
0606         return IRQ_NONE;
0607 
0608     ioaddr = dev->base_addr;
0609 
0610     netdev_dbg(dev, "interrupt, status %4.4x.\n", inw(ioaddr + EL3_STATUS));
0611 
0612     spin_lock(&lp->lock);
0613     while ((status = inw(ioaddr + EL3_STATUS)) &
0614     (IntLatch | RxComplete | StatsFull)) {
0615         if ((status & 0xe000) != 0x2000) {
0616             netdev_dbg(dev, "interrupt from dead card\n");
0617             handled = 0;
0618             break;
0619         }
0620         if (status & RxComplete)
0621             el3_rx(dev);
0622         if (status & TxAvailable) {
0623             netdev_dbg(dev, "    TX room bit was handled.\n");
0624             /* There's room in the FIFO for a full-sized packet. */
0625             outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
0626             netif_wake_queue(dev);
0627         }
0628         if (status & TxComplete)
0629             pop_tx_status(dev);
0630         if (status & (AdapterFailure | RxEarly | StatsFull)) {
0631             /* Handle all uncommon interrupts. */
0632             if (status & StatsFull)     /* Empty statistics. */
0633                 update_stats(dev);
0634             if (status & RxEarly) {
0635                 /* Rx early is unused. */
0636                 el3_rx(dev);
0637                 outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
0638             }
0639             if (status & AdapterFailure) {
0640                 u16 fifo_diag;
0641                 EL3WINDOW(4);
0642                 fifo_diag = inw(ioaddr + 4);
0643                 EL3WINDOW(1);
0644                 netdev_warn(dev, "adapter failure, FIFO diagnostic register %04x.\n",
0645                 fifo_diag);
0646                 if (fifo_diag & 0x0400) {
0647                     /* Tx overrun */
0648                     tc589_wait_for_completion(dev, TxReset);
0649                     outw(TxEnable, ioaddr + EL3_CMD);
0650                 }
0651                 if (fifo_diag & 0x2000) {
0652                     /* Rx underrun */
0653                     tc589_wait_for_completion(dev, RxReset);
0654                     set_rx_mode(dev);
0655                     outw(RxEnable, ioaddr + EL3_CMD);
0656                 }
0657                 outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
0658             }
0659         }
0660         if (++i > 10) {
0661             netdev_err(dev, "infinite loop in interrupt, status %4.4x.\n",
0662                     status);
0663             /* Clear all interrupts */
0664             outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
0665             break;
0666         }
0667         /* Acknowledge the IRQ. */
0668         outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD);
0669     }
0670     lp->last_irq = jiffies;
0671     spin_unlock(&lp->lock);
0672     netdev_dbg(dev, "exiting interrupt, status %4.4x.\n",
0673             inw(ioaddr + EL3_STATUS));
0674     return IRQ_RETVAL(handled);
0675 }
0676 
0677 static void media_check(struct timer_list *t)
0678 {
0679     struct el3_private *lp = from_timer(lp, t, media);
0680     struct net_device *dev = lp->p_dev->priv;
0681     unsigned int ioaddr = dev->base_addr;
0682     u16 media, errs;
0683     unsigned long flags;
0684 
0685     if (!netif_device_present(dev))
0686         goto reschedule;
0687 
0688     /* Check for pending interrupt with expired latency timer: with
0689      * this, we can limp along even if the interrupt is blocked
0690      */
0691     if ((inw(ioaddr + EL3_STATUS) & IntLatch) &&
0692     (inb(ioaddr + EL3_TIMER) == 0xff)) {
0693         if (!lp->fast_poll)
0694             netdev_warn(dev, "interrupt(s) dropped!\n");
0695 
0696         local_irq_save(flags);
0697         el3_interrupt(dev->irq, dev);
0698         local_irq_restore(flags);
0699 
0700         lp->fast_poll = HZ;
0701     }
0702     if (lp->fast_poll) {
0703         lp->fast_poll--;
0704         lp->media.expires = jiffies + HZ/100;
0705         add_timer(&lp->media);
0706         return;
0707     }
0708 
0709     /* lp->lock guards the EL3 window. Window should always be 1 except
0710      * when the lock is held
0711      */
0712 
0713     spin_lock_irqsave(&lp->lock, flags);
0714     EL3WINDOW(4);
0715     media = inw(ioaddr+WN4_MEDIA) & 0xc810;
0716 
0717     /* Ignore collisions unless we've had no irq's recently */
0718     if (time_before(jiffies, lp->last_irq + HZ)) {
0719         media &= ~0x0010;
0720     } else {
0721         /* Try harder to detect carrier errors */
0722         EL3WINDOW(6);
0723         outw(StatsDisable, ioaddr + EL3_CMD);
0724         errs = inb(ioaddr + 0);
0725         outw(StatsEnable, ioaddr + EL3_CMD);
0726         dev->stats.tx_carrier_errors += errs;
0727         if (errs || (lp->media_status & 0x0010))
0728             media |= 0x0010;
0729     }
0730 
0731     if (media != lp->media_status) {
0732         if ((media & lp->media_status & 0x8000) &&
0733                 ((lp->media_status ^ media) & 0x0800))
0734         netdev_info(dev, "%s link beat\n",
0735                 (lp->media_status & 0x0800 ? "lost" : "found"));
0736         else if ((media & lp->media_status & 0x4000) &&
0737          ((lp->media_status ^ media) & 0x0010))
0738         netdev_info(dev, "coax cable %s\n",
0739                 (lp->media_status & 0x0010 ? "ok" : "problem"));
0740         if (dev->if_port == 0) {
0741             if (media & 0x8000) {
0742                 if (media & 0x0800)
0743                     netdev_info(dev, "flipped to 10baseT\n");
0744                 else
0745             tc589_set_xcvr(dev, 2);
0746             } else if (media & 0x4000) {
0747                 if (media & 0x0010)
0748                     tc589_set_xcvr(dev, 1);
0749                 else
0750                     netdev_info(dev, "flipped to 10base2\n");
0751             }
0752         }
0753         lp->media_status = media;
0754     }
0755 
0756     EL3WINDOW(1);
0757     spin_unlock_irqrestore(&lp->lock, flags);
0758 
0759 reschedule:
0760     lp->media.expires = jiffies + HZ;
0761     add_timer(&lp->media);
0762 }
0763 
0764 static struct net_device_stats *el3_get_stats(struct net_device *dev)
0765 {
0766     struct el3_private *lp = netdev_priv(dev);
0767     unsigned long flags;
0768     struct pcmcia_device *link = lp->p_dev;
0769 
0770     if (pcmcia_dev_present(link)) {
0771         spin_lock_irqsave(&lp->lock, flags);
0772         update_stats(dev);
0773         spin_unlock_irqrestore(&lp->lock, flags);
0774     }
0775     return &dev->stats;
0776 }
0777 
0778 /* Update statistics.  We change to register window 6, so this should be run
0779 * single-threaded if the device is active. This is expected to be a rare
0780 * operation, and it's simpler for the rest of the driver to assume that
0781 * window 1 is always valid rather than use a special window-state variable.
0782 *
0783 * Caller must hold the lock for this
0784 */
0785 
0786 static void update_stats(struct net_device *dev)
0787 {
0788     unsigned int ioaddr = dev->base_addr;
0789 
0790     netdev_dbg(dev, "updating the statistics.\n");
0791     /* Turn off statistics updates while reading. */
0792     outw(StatsDisable, ioaddr + EL3_CMD);
0793     /* Switch to the stats window, and read everything. */
0794     EL3WINDOW(6);
0795     dev->stats.tx_carrier_errors    += inb(ioaddr + 0);
0796     dev->stats.tx_heartbeat_errors  += inb(ioaddr + 1);
0797     /* Multiple collisions. */
0798     inb(ioaddr + 2);
0799     dev->stats.collisions       += inb(ioaddr + 3);
0800     dev->stats.tx_window_errors     += inb(ioaddr + 4);
0801     dev->stats.rx_fifo_errors       += inb(ioaddr + 5);
0802     dev->stats.tx_packets       += inb(ioaddr + 6);
0803     /* Rx packets   */
0804     inb(ioaddr + 7);
0805     /* Tx deferrals */
0806     inb(ioaddr + 8);
0807     /* Rx octets */
0808     inw(ioaddr + 10);
0809     /* Tx octets */
0810     inw(ioaddr + 12);
0811 
0812     /* Back to window 1, and turn statistics back on. */
0813     EL3WINDOW(1);
0814     outw(StatsEnable, ioaddr + EL3_CMD);
0815 }
0816 
0817 static int el3_rx(struct net_device *dev)
0818 {
0819     unsigned int ioaddr = dev->base_addr;
0820     int worklimit = 32;
0821     short rx_status;
0822 
0823     netdev_dbg(dev, "in rx_packet(), status %4.4x, rx_status %4.4x.\n",
0824            inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS));
0825     while (!((rx_status = inw(ioaddr + RX_STATUS)) & 0x8000) &&
0826             worklimit > 0) {
0827         worklimit--;
0828         if (rx_status & 0x4000) { /* Error, update stats. */
0829             short error = rx_status & 0x3800;
0830             dev->stats.rx_errors++;
0831             switch (error) {
0832             case 0x0000:
0833                 dev->stats.rx_over_errors++;
0834                 break;
0835             case 0x0800:
0836                 dev->stats.rx_length_errors++;
0837                 break;
0838             case 0x1000:
0839                 dev->stats.rx_frame_errors++;
0840                 break;
0841             case 0x1800:
0842                 dev->stats.rx_length_errors++;
0843                 break;
0844             case 0x2000:
0845                 dev->stats.rx_frame_errors++;
0846                 break;
0847             case 0x2800:
0848                 dev->stats.rx_crc_errors++;
0849                 break;
0850             }
0851         } else {
0852             short pkt_len = rx_status & 0x7ff;
0853             struct sk_buff *skb;
0854 
0855             skb = netdev_alloc_skb(dev, pkt_len + 5);
0856 
0857             netdev_dbg(dev, "    Receiving packet size %d status %4.4x.\n",
0858                pkt_len, rx_status);
0859             if (skb != NULL) {
0860                 skb_reserve(skb, 2);
0861                 insl(ioaddr+RX_FIFO, skb_put(skb, pkt_len),
0862             (pkt_len+3)>>2);
0863                 skb->protocol = eth_type_trans(skb, dev);
0864                 netif_rx(skb);
0865                 dev->stats.rx_packets++;
0866                 dev->stats.rx_bytes += pkt_len;
0867             } else {
0868                 netdev_dbg(dev, "couldn't allocate a sk_buff of size %d.\n",
0869                pkt_len);
0870                 dev->stats.rx_dropped++;
0871             }
0872         }
0873         /* Pop the top of the Rx FIFO */
0874         tc589_wait_for_completion(dev, RxDiscard);
0875     }
0876     if (worklimit == 0)
0877         netdev_warn(dev, "too much work in el3_rx!\n");
0878     return 0;
0879 }
0880 
0881 static void set_rx_mode(struct net_device *dev)
0882 {
0883     unsigned int ioaddr = dev->base_addr;
0884     u16 opts = SetRxFilter | RxStation | RxBroadcast;
0885 
0886     if (dev->flags & IFF_PROMISC)
0887         opts |= RxMulticast | RxProm;
0888     else if (!netdev_mc_empty(dev) || (dev->flags & IFF_ALLMULTI))
0889         opts |= RxMulticast;
0890     outw(opts, ioaddr + EL3_CMD);
0891 }
0892 
0893 static void set_multicast_list(struct net_device *dev)
0894 {
0895     struct el3_private *priv = netdev_priv(dev);
0896     unsigned long flags;
0897 
0898     spin_lock_irqsave(&priv->lock, flags);
0899     set_rx_mode(dev);
0900     spin_unlock_irqrestore(&priv->lock, flags);
0901 }
0902 
0903 static int el3_close(struct net_device *dev)
0904 {
0905     struct el3_private *lp = netdev_priv(dev);
0906     struct pcmcia_device *link = lp->p_dev;
0907     unsigned int ioaddr = dev->base_addr;
0908 
0909     dev_dbg(&link->dev, "%s: shutting down ethercard.\n", dev->name);
0910 
0911     if (pcmcia_dev_present(link)) {
0912         /* Turn off statistics ASAP.  We update dev->stats below. */
0913         outw(StatsDisable, ioaddr + EL3_CMD);
0914 
0915         /* Disable the receiver and transmitter. */
0916         outw(RxDisable, ioaddr + EL3_CMD);
0917         outw(TxDisable, ioaddr + EL3_CMD);
0918 
0919         if (dev->if_port == 2)
0920             /* Turn off thinnet power.  Green! */
0921             outw(StopCoax, ioaddr + EL3_CMD);
0922         else if (dev->if_port == 1) {
0923             /* Disable link beat and jabber */
0924             EL3WINDOW(4);
0925             outw(0, ioaddr + WN4_MEDIA);
0926         }
0927 
0928         /* Switching back to window 0 disables the IRQ. */
0929         EL3WINDOW(0);
0930         /* But we explicitly zero the IRQ line select anyway. */
0931         outw(0x0f00, ioaddr + WN0_IRQ);
0932 
0933         /* Check if the card still exists */
0934         if ((inw(ioaddr+EL3_STATUS) & 0xe000) == 0x2000)
0935             update_stats(dev);
0936     }
0937 
0938     link->open--;
0939     netif_stop_queue(dev);
0940     del_timer_sync(&lp->media);
0941 
0942     return 0;
0943 }
0944 
0945 static const struct pcmcia_device_id tc589_ids[] = {
0946     PCMCIA_MFC_DEVICE_MANF_CARD(0, 0x0101, 0x0562),
0947     PCMCIA_MFC_DEVICE_PROD_ID1(0, "Motorola MARQUIS", 0xf03e4e77),
0948     PCMCIA_DEVICE_MANF_CARD(0x0101, 0x0589),
0949     PCMCIA_DEVICE_PROD_ID12("Farallon", "ENet", 0x58d93fc4, 0x992c2202),
0950     PCMCIA_MFC_DEVICE_CIS_MANF_CARD(0, 0x0101, 0x0035, "cis/3CXEM556.cis"),
0951     PCMCIA_MFC_DEVICE_CIS_MANF_CARD(0, 0x0101, 0x003d, "cis/3CXEM556.cis"),
0952     PCMCIA_DEVICE_NULL,
0953 };
0954 MODULE_DEVICE_TABLE(pcmcia, tc589_ids);
0955 
0956 static struct pcmcia_driver tc589_driver = {
0957     .owner      = THIS_MODULE,
0958     .name       = "3c589_cs",
0959     .probe      = tc589_probe,
0960     .remove     = tc589_detach,
0961     .id_table   = tc589_ids,
0962     .suspend    = tc589_suspend,
0963     .resume     = tc589_resume,
0964 };
0965 module_pcmcia_driver(tc589_driver);