ibmveth: Add suspend/resume support
[safe/jmp/linux-2.6] / drivers / net / sunqe.c
index e811331..a7542d2 100644 (file)
@@ -3,7 +3,7 @@
  *          controller out there can be most efficiently programmed
  *          if you make it look like a LANCE.
  *
- * Copyright (C) 1996, 1999, 2003, 2006 David S. Miller (davem@davemloft.net)
+ * Copyright (C) 1996, 1999, 2003, 2006, 2008 David S. Miller (davem@davemloft.net)
  */
 
 #include <linux/module.h>
 #include <linux/skbuff.h>
 #include <linux/ethtool.h>
 #include <linux/bitops.h>
+#include <linux/dma-mapping.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
 
 #include <asm/system.h>
 #include <asm/io.h>
 #include <asm/dma.h>
 #include <asm/byteorder.h>
 #include <asm/idprom.h>
-#include <asm/sbus.h>
 #include <asm/openprom.h>
 #include <asm/oplib.h>
 #include <asm/auxio.h>
@@ -40,8 +42,8 @@
 #include "sunqe.h"
 
 #define DRV_NAME       "sunqe"
-#define DRV_VERSION    "4.0"
-#define DRV_RELDATE    "June 23, 2006"
+#define DRV_VERSION    "4.1"
+#define DRV_RELDATE    "August 27, 2008"
 #define DRV_AUTHOR     "David S. Miller (davem@davemloft.net)"
 
 static char version[] =
@@ -225,7 +227,7 @@ static int qe_init(struct sunqe *qep, int from_irq)
        if (!(sbus_readb(mregs + MREGS_PHYCONFIG) & MREGS_PHYCONFIG_LTESTDIS)) {
                int tries = 50;
 
-               while (tries--) {
+               while (--tries) {
                        u8 tmp;
 
                        mdelay(5);
@@ -444,7 +446,6 @@ static void qe_rx(struct sunqe *qep)
                                                 len);
                                skb->protocol = eth_type_trans(skb, qep->dev);
                                netif_rx(skb);
-                               qep->dev->last_rx = jiffies;
                                dev->stats.rx_packets++;
                                dev->stats.rx_bytes += len;
                        }
@@ -511,7 +512,7 @@ static irqreturn_t qec_interrupt(int irq, void *dev_id)
 
 static int qe_open(struct net_device *dev)
 {
-       struct sunqe *qep = (struct sunqe *) dev->priv;
+       struct sunqe *qep = netdev_priv(dev);
 
        qep->mconfig = (MREGS_MCONFIG_TXENAB |
                        MREGS_MCONFIG_RXENAB |
@@ -521,7 +522,7 @@ static int qe_open(struct net_device *dev)
 
 static int qe_close(struct net_device *dev)
 {
-       struct sunqe *qep = (struct sunqe *) dev->priv;
+       struct sunqe *qep = netdev_priv(dev);
 
        qe_stop(qep);
        return 0;
@@ -547,7 +548,7 @@ static void qe_tx_reclaim(struct sunqe *qep)
 
 static void qe_tx_timeout(struct net_device *dev)
 {
-       struct sunqe *qep = (struct sunqe *) dev->priv;
+       struct sunqe *qep = netdev_priv(dev);
        int tx_full;
 
        spin_lock_irq(&qep->lock);
@@ -573,7 +574,7 @@ out:
 /* Get a packet queued to go onto the wire. */
 static int qe_start_xmit(struct sk_buff *skb, struct net_device *dev)
 {
-       struct sunqe *qep = (struct sunqe *) dev->priv;
+       struct sunqe *qep = netdev_priv(dev);
        struct sunqe_buffers *qbufs = qep->buffers;
        __u32 txbuf_dvma, qbufs_dvma = qep->buffers_dvma;
        unsigned char *txbuf;
@@ -601,7 +602,6 @@ static int qe_start_xmit(struct sk_buff *skb, struct net_device *dev)
        qep->tx_new = NEXT_TX(entry);
 
        /* Get it going. */
-       dev->trans_start = jiffies;
        sbus_writel(CREG_CTRL_TWAKEUP, qep->qcregs + CREG_CTRL);
 
        dev->stats.tx_packets++;
@@ -620,13 +620,13 @@ static int qe_start_xmit(struct sk_buff *skb, struct net_device *dev)
 
        dev_kfree_skb(skb);
 
-       return 0;
+       return NETDEV_TX_OK;
 }
 
 static void qe_set_multicast(struct net_device *dev)
 {
-       struct sunqe *qep = (struct sunqe *) dev->priv;
-       struct dev_mc_list *dmi = dev->mc_list;
+       struct sunqe *qep = netdev_priv(dev);
+       struct netdev_hw_addr *ha;
        u8 new_mconfig = qep->mconfig;
        char *addrs;
        int i;
@@ -635,7 +635,7 @@ static void qe_set_multicast(struct net_device *dev)
        /* Lock out others. */
        netif_stop_queue(dev);
 
-       if ((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 64)) {
+       if ((dev->flags & IFF_ALLMULTI) || (netdev_mc_count(dev) > 64)) {
                sbus_writeb(MREGS_IACONFIG_ACHNGE | MREGS_IACONFIG_LARESET,
                            qep->mregs + MREGS_IACONFIG);
                while ((sbus_readb(qep->mregs + MREGS_IACONFIG) & MREGS_IACONFIG_ACHNGE) != 0)
@@ -649,12 +649,9 @@ static void qe_set_multicast(struct net_device *dev)
                u16 hash_table[4];
                u8 *hbytes = (unsigned char *) &hash_table[0];
 
-               for (i = 0; i < 4; i++)
-                       hash_table[i] = 0;
-
-               for (i = 0; i < dev->mc_count; i++) {
-                       addrs = dmi->dmi_addr;
-                       dmi = dmi->next;
+               memset(hash_table, 0, sizeof(hash_table));
+               netdev_for_each_mc_addr(ha, dev) {
+                       addrs = ha->addr;
 
                        if (!(*addrs & 1))
                                continue;
@@ -690,17 +687,23 @@ static void qe_set_multicast(struct net_device *dev)
 /* Ethtool support... */
 static void qe_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
 {
-       struct sunqe *qep = dev->priv;
+       const struct linux_prom_registers *regs;
+       struct sunqe *qep = netdev_priv(dev);
+       struct of_device *op;
 
        strcpy(info->driver, "sunqe");
        strcpy(info->version, "3.0");
-       sprintf(info->bus_info, "SBUS:%d",
-               qep->qe_sdev->slot);
+
+       op = qep->op;
+       regs = of_get_property(op->node, "reg", NULL);
+       if (regs)
+               sprintf(info->bus_info, "SBUS:%d", regs->which_io);
+
 }
 
 static u32 qe_get_link(struct net_device *dev)
 {
-       struct sunqe *qep = dev->priv;
+       struct sunqe *qep = netdev_priv(dev);
        void __iomem *mregs = qep->mregs;
        u8 phyconfig;
 
@@ -717,11 +720,11 @@ static const struct ethtool_ops qe_ethtool_ops = {
 };
 
 /* This is only called once at boot time for each card probed. */
-static inline void qec_init_once(struct sunqec *qecp, struct sbus_dev *qsdev)
+static void qec_init_once(struct sunqec *qecp, struct of_device *op)
 {
        u8 bsizes = qecp->qec_bursts;
 
-       if (sbus_can_burst64(qsdev) && (bsizes & DMA_BURST64)) {
+       if (sbus_can_burst64() && (bsizes & DMA_BURST64)) {
                sbus_writel(GLOB_CTRL_B64, qecp->gregs + GLOB_CTRL);
        } else if (bsizes & DMA_BURST32) {
                sbus_writel(GLOB_CTRL_B32, qecp->gregs + GLOB_CTRL);
@@ -735,15 +738,15 @@ static inline void qec_init_once(struct sunqec *qecp, struct sbus_dev *qsdev)
        sbus_writel(GLOB_PSIZE_2048, qecp->gregs + GLOB_PSIZE);
 
        /* Set the local memsize register, divided up to one piece per QE channel. */
-       sbus_writel((qsdev->reg_addrs[1].reg_size >> 2),
+       sbus_writel((resource_size(&op->resource[1]) >> 2),
                    qecp->gregs + GLOB_MSIZE);
 
        /* Divide up the local QEC memory amongst the 4 QE receiver and
         * transmitter FIFOs.  Basically it is (total / 2 / num_channels).
         */
-       sbus_writel((qsdev->reg_addrs[1].reg_size >> 2) >> 1,
+       sbus_writel((resource_size(&op->resource[1]) >> 2) >> 1,
                    qecp->gregs + GLOB_TSIZE);
-       sbus_writel((qsdev->reg_addrs[1].reg_size >> 2) >> 1,
+       sbus_writel((resource_size(&op->resource[1]) >> 2) >> 1,
                    qecp->gregs + GLOB_RSIZE);
 }
 
@@ -767,24 +770,21 @@ static u8 __devinit qec_get_burst(struct device_node *dp)
        return bsizes;
 }
 
-static struct sunqec * __devinit get_qec(struct sbus_dev *child_sdev)
+static struct sunqec * __devinit get_qec(struct of_device *child)
 {
-       struct sbus_dev *qec_sdev = child_sdev->parent;
+       struct of_device *op = to_of_device(child->dev.parent);
        struct sunqec *qecp;
 
-       for (qecp = root_qec_dev; qecp; qecp = qecp->next_module) {
-               if (qecp->qec_sdev == qec_sdev)
-                       break;
-       }
+       qecp = dev_get_drvdata(&op->dev);
        if (!qecp) {
                qecp = kzalloc(sizeof(struct sunqec), GFP_KERNEL);
                if (qecp) {
                        u32 ctrl;
 
-                       qecp->qec_sdev = qec_sdev;
-                       qecp->gregs = sbus_ioremap(&qec_sdev->resource[0], 0,
-                                                  GLOB_REG_SIZE,
-                                                  "QEC Global Registers");
+                       qecp->op = op;
+                       qecp->gregs = of_ioremap(&op->resource[0], 0,
+                                                GLOB_REG_SIZE,
+                                                "QEC Global Registers");
                        if (!qecp->gregs)
                                goto fail;
 
@@ -799,16 +799,18 @@ static struct sunqec * __devinit get_qec(struct sbus_dev *child_sdev)
                        if (qec_global_reset(qecp->gregs))
                                goto fail;
 
-                       qecp->qec_bursts = qec_get_burst(qec_sdev->ofdev.node);
+                       qecp->qec_bursts = qec_get_burst(op->node);
 
-                       qec_init_once(qecp, qec_sdev);
+                       qec_init_once(qecp, op);
 
-                       if (request_irq(qec_sdev->irqs[0], &qec_interrupt,
+                       if (request_irq(op->irqs[0], qec_interrupt,
                                        IRQF_SHARED, "qec", (void *) qecp)) {
                                printk(KERN_ERR "qec: Can't register irq.\n");
                                goto fail;
                        }
 
+                       dev_set_drvdata(&op->dev, qecp);
+
                        qecp->next_module = root_qec_dev;
                        root_qec_dev = qecp;
                }
@@ -818,17 +820,28 @@ static struct sunqec * __devinit get_qec(struct sbus_dev *child_sdev)
 
 fail:
        if (qecp->gregs)
-               sbus_iounmap(qecp->gregs, GLOB_REG_SIZE);
+               of_iounmap(&op->resource[0], qecp->gregs, GLOB_REG_SIZE);
        kfree(qecp);
        return NULL;
 }
 
-static int __devinit qec_ether_init(struct sbus_dev *sdev)
+static const struct net_device_ops qec_ops = {
+       .ndo_open               = qe_open,
+       .ndo_stop               = qe_close,
+       .ndo_start_xmit         = qe_start_xmit,
+       .ndo_set_multicast_list = qe_set_multicast,
+       .ndo_tx_timeout         = qe_tx_timeout,
+       .ndo_change_mtu         = eth_change_mtu,
+       .ndo_set_mac_address    = eth_mac_addr,
+       .ndo_validate_addr      = eth_validate_addr,
+};
+
+static int __devinit qec_ether_init(struct of_device *op)
 {
        static unsigned version_printed;
        struct net_device *dev;
-       struct sunqe *qe;
        struct sunqec *qecp;
+       struct sunqe *qe;
        int i, res;
 
        if (version_printed++ == 0)
@@ -842,49 +855,42 @@ static int __devinit qec_ether_init(struct sbus_dev *sdev)
 
        qe = netdev_priv(dev);
 
-       i = of_getintprop_default(sdev->ofdev.node, "channel#", -1);
-       if (i == -1) {
-               struct sbus_dev *td = sdev->parent->child;
-               i = 0;
-               while (td != sdev) {
-                       td = td->next;
-                       i++;
-               }
-       }
+       res = -ENODEV;
+
+       i = of_getintprop_default(op->node, "channel#", -1);
+       if (i == -1)
+               goto fail;
        qe->channel = i;
        spin_lock_init(&qe->lock);
 
-       res = -ENODEV;
-       qecp = get_qec(sdev);
+       qecp = get_qec(op);
        if (!qecp)
                goto fail;
 
        qecp->qes[qe->channel] = qe;
        qe->dev = dev;
        qe->parent = qecp;
-       qe->qe_sdev = sdev;
+       qe->op = op;
 
        res = -ENOMEM;
-       qe->qcregs = sbus_ioremap(&qe->qe_sdev->resource[0], 0,
-                                 CREG_REG_SIZE, "QEC Channel Registers");
+       qe->qcregs = of_ioremap(&op->resource[0], 0,
+                               CREG_REG_SIZE, "QEC Channel Registers");
        if (!qe->qcregs) {
                printk(KERN_ERR "qe: Cannot map channel registers.\n");
                goto fail;
        }
 
-       qe->mregs = sbus_ioremap(&qe->qe_sdev->resource[1], 0,
-                                MREGS_REG_SIZE, "QE MACE Registers");
+       qe->mregs = of_ioremap(&op->resource[1], 0,
+                              MREGS_REG_SIZE, "QE MACE Registers");
        if (!qe->mregs) {
                printk(KERN_ERR "qe: Cannot map MACE registers.\n");
                goto fail;
        }
 
-       qe->qe_block = sbus_alloc_consistent(qe->qe_sdev,
-                                            PAGE_SIZE,
-                                            &qe->qblock_dvma);
-       qe->buffers = sbus_alloc_consistent(qe->qe_sdev,
-                                           sizeof(struct sunqe_buffers),
-                                           &qe->buffers_dvma);
+       qe->qe_block = dma_alloc_coherent(&op->dev, PAGE_SIZE,
+                                         &qe->qblock_dvma, GFP_ATOMIC);
+       qe->buffers = dma_alloc_coherent(&op->dev, sizeof(struct sunqe_buffers),
+                                        &qe->buffers_dvma, GFP_ATOMIC);
        if (qe->qe_block == NULL || qe->qblock_dvma == 0 ||
            qe->buffers == NULL || qe->buffers_dvma == 0)
                goto fail;
@@ -892,23 +898,19 @@ static int __devinit qec_ether_init(struct sbus_dev *sdev)
        /* Stop this QE. */
        qe_stop(qe);
 
-       SET_NETDEV_DEV(dev, &sdev->ofdev.dev);
+       SET_NETDEV_DEV(dev, &op->dev);
 
-       dev->open = qe_open;
-       dev->stop = qe_close;
-       dev->hard_start_xmit = qe_start_xmit;
-       dev->set_multicast_list = qe_set_multicast;
-       dev->tx_timeout = qe_tx_timeout;
        dev->watchdog_timeo = 5*HZ;
-       dev->irq = sdev->irqs[0];
+       dev->irq = op->irqs[0];
        dev->dma = 0;
        dev->ethtool_ops = &qe_ethtool_ops;
+       dev->netdev_ops = &qec_ops;
 
        res = register_netdev(dev);
        if (res)
                goto fail;
 
-       dev_set_drvdata(&sdev->ofdev.dev, qe);
+       dev_set_drvdata(&op->dev, qe);
 
        printk(KERN_INFO "%s: qe channel[%d] ", dev->name, qe->channel);
        for (i = 0; i < 6; i++)
@@ -922,58 +924,50 @@ static int __devinit qec_ether_init(struct sbus_dev *sdev)
 
 fail:
        if (qe->qcregs)
-               sbus_iounmap(qe->qcregs, CREG_REG_SIZE);
+               of_iounmap(&op->resource[0], qe->qcregs, CREG_REG_SIZE);
        if (qe->mregs)
-               sbus_iounmap(qe->mregs, MREGS_REG_SIZE);
+               of_iounmap(&op->resource[1], qe->mregs, MREGS_REG_SIZE);
        if (qe->qe_block)
-               sbus_free_consistent(qe->qe_sdev,
-                                    PAGE_SIZE,
-                                    qe->qe_block,
-                                    qe->qblock_dvma);
+               dma_free_coherent(&op->dev, PAGE_SIZE,
+                                 qe->qe_block, qe->qblock_dvma);
        if (qe->buffers)
-               sbus_free_consistent(qe->qe_sdev,
-                                    sizeof(struct sunqe_buffers),
-                                    qe->buffers,
-                                    qe->buffers_dvma);
+               dma_free_coherent(&op->dev,
+                                 sizeof(struct sunqe_buffers),
+                                 qe->buffers,
+                                 qe->buffers_dvma);
 
        free_netdev(dev);
 
        return res;
 }
 
-static int __devinit qec_sbus_probe(struct of_device *dev, const struct of_device_id *match)
+static int __devinit qec_sbus_probe(struct of_device *op, const struct of_device_id *match)
 {
-       struct sbus_dev *sdev = to_sbus_device(&dev->dev);
-
-       return qec_ether_init(sdev);
+       return qec_ether_init(op);
 }
 
-static int __devexit qec_sbus_remove(struct of_device *dev)
+static int __devexit qec_sbus_remove(struct of_device *op)
 {
-       struct sunqe *qp = dev_get_drvdata(&dev->dev);
+       struct sunqe *qp = dev_get_drvdata(&op->dev);
        struct net_device *net_dev = qp->dev;
 
        unregister_netdev(net_dev);
 
-       sbus_iounmap(qp->qcregs, CREG_REG_SIZE);
-       sbus_iounmap(qp->mregs, MREGS_REG_SIZE);
-       sbus_free_consistent(qp->qe_sdev,
-                            PAGE_SIZE,
-                            qp->qe_block,
-                            qp->qblock_dvma);
-       sbus_free_consistent(qp->qe_sdev,
-                            sizeof(struct sunqe_buffers),
-                            qp->buffers,
-                            qp->buffers_dvma);
+       of_iounmap(&op->resource[0], qp->qcregs, CREG_REG_SIZE);
+       of_iounmap(&op->resource[1], qp->mregs, MREGS_REG_SIZE);
+       dma_free_coherent(&op->dev, PAGE_SIZE,
+                         qp->qe_block, qp->qblock_dvma);
+       dma_free_coherent(&op->dev, sizeof(struct sunqe_buffers),
+                         qp->buffers, qp->buffers_dvma);
 
        free_netdev(net_dev);
 
-       dev_set_drvdata(&dev->dev, NULL);
+       dev_set_drvdata(&op->dev, NULL);
 
        return 0;
 }
 
-static struct of_device_id qec_sbus_match[] = {
+static const struct of_device_id qec_sbus_match[] = {
        {
                .name = "qe",
        },
@@ -991,7 +985,7 @@ static struct of_platform_driver qec_sbus_driver = {
 
 static int __init qec_init(void)
 {
-       return of_register_driver(&qec_sbus_driver, &sbus_bus_type);
+       return of_register_driver(&qec_sbus_driver, &of_bus_type);
 }
 
 static void __exit qec_exit(void)
@@ -1000,11 +994,11 @@ static void __exit qec_exit(void)
 
        while (root_qec_dev) {
                struct sunqec *next = root_qec_dev->next_module;
+               struct of_device *op = root_qec_dev->op;
 
-               free_irq(root_qec_dev->qec_sdev->irqs[0],
-                        (void *) root_qec_dev);
-               sbus_iounmap(root_qec_dev->gregs, GLOB_REG_SIZE);
-
+               free_irq(op->irqs[0], (void *) root_qec_dev);
+               of_iounmap(&op->resource[0], root_qec_dev->gregs,
+                          GLOB_REG_SIZE);
                kfree(root_qec_dev);
 
                root_qec_dev = next;