fix the case when NETDEVICES_MULTIQUEUE is not enabled (#2506)
[openwrt/openwrt.git] / target / linux / ar7 / files / drivers / net / cpmac.c
index d41b18b4a6053d910b5fb779f0b93148101a796b..1d0ba11d258f14be37fb7f383819a12a2573c950 100644 (file)
 #include <linux/dma-mapping.h>
 #include <asm/gpio.h>
 
-MODULE_AUTHOR("Eugene Konev");
+MODULE_AUTHOR("Eugene Konev <ejka@imfi.kspu.ru>");
 MODULE_DESCRIPTION("TI AR7 ethernet driver (CPMAC)");
 MODULE_LICENSE("GPL");
 
-static int rx_ring_size = 64;
-static int disable_napi;
 static int debug_level = 8;
 static int dumb_switch;
 
-module_param(rx_ring_size, int, 0644);
-module_param(disable_napi, int, 0644);
 /* Next 2 are only used in cpmac_probe, so it's pointless to change them */
 module_param(debug_level, int, 0444);
 module_param(dumb_switch, int, 0444);
 
-MODULE_PARM_DESC(rx_ring_size, "Size of rx ring (in skbs)");
-MODULE_PARM_DESC(disable_napi, "Disable NAPI polling");
 MODULE_PARM_DESC(debug_level, "Number of NETIF_MSG bits to enable");
 MODULE_PARM_DESC(dumb_switch, "Assume switch is not connected to MDIO bus");
 
+#define CPMAC_VERSION "0.5.0"
+/* stolen from net/ieee80211.h */
+#ifndef MAC_FMT
+#define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x"
+#define MAC_ARG(x) ((u8*)(x))[0], ((u8*)(x))[1], ((u8*)(x))[2], \
+                  ((u8*)(x))[3], ((u8*)(x))[4], ((u8*)(x))[5]
+#endif
 /* frame size + 802.1q tag */
 #define CPMAC_SKB_SIZE         (ETH_FRAME_LEN + 4)
-#define CPMAC_TX_RING_SIZE     8
+#define CPMAC_QUEUES   8
 
 /* Ethernet registers */
 #define CPMAC_TX_CONTROL               0x0004
@@ -119,7 +120,7 @@ MODULE_PARM_DESC(dumb_switch, "Assume switch is not connected to MDIO bus");
 #define CPMAC_TX_ACK(channel)          (0x0640 + (channel) * 4)
 #define CPMAC_RX_ACK(channel)          (0x0660 + (channel) * 4)
 #define CPMAC_REG_END                  0x0680
-/* 
+/*
  * Rx/Tx statistics
  * TODO: use some of them to fill stats in cpmac_stats()
  */
@@ -195,26 +196,26 @@ struct cpmac_desc {
 };
 
 struct cpmac_priv {
-       struct net_device_stats stats;
        spinlock_t lock;
+       spinlock_t rx_lock;
        struct cpmac_desc *rx_head;
-       int tx_head, tx_tail;
+       int ring_size;
        struct cpmac_desc *desc_ring;
        dma_addr_t dma_ring;
        void __iomem *regs;
        struct mii_bus *mii_bus;
        struct phy_device *phy;
        char phy_name[BUS_ID_SIZE];
-       struct plat_cpmac_data *config;
        int oldlink, oldspeed, oldduplex;
        u32 msg_enable;
        struct net_device *dev;
-       struct work_struct alloc_work;
+       struct work_struct reset_work;
+       struct platform_device *pdev;
 };
 
 static irqreturn_t cpmac_irq(int, void *);
-static void cpmac_reset(struct net_device *dev);
-static void cpmac_hw_init(struct net_device *dev);
+static void cpmac_hw_start(struct net_device *dev);
+static void cpmac_hw_stop(struct net_device *dev);
 static int cpmac_stop(struct net_device *dev);
 static int cpmac_open(struct net_device *dev);
 
@@ -313,18 +314,6 @@ static int cpmac_config(struct net_device *dev, struct ifmap *map)
        return 0;
 }
 
-static int cpmac_set_mac_address(struct net_device *dev, void *addr)
-{
-       struct sockaddr *sa = addr;
-
-       if (dev->flags & IFF_UP)
-               return -EBUSY;
-
-       memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
-
-       return 0;
-}
-
 static void cpmac_set_multicast_list(struct net_device *dev)
 {
        struct dev_mc_list *iter;
@@ -344,7 +333,7 @@ static void cpmac_set_multicast_list(struct net_device *dev)
                        cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, 0xffffffff);
                        cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, 0xffffffff);
                } else {
-                       /* 
+                       /*
                         * cpmac uses some strange mac address hashing
                         * (not crc32)
                         */
@@ -377,7 +366,6 @@ static struct sk_buff *cpmac_rx_one(struct net_device *dev,
                                    struct cpmac_priv *priv,
                                    struct cpmac_desc *desc)
 {
-       unsigned long flags;
        struct sk_buff *skb, *result = NULL;
 
        if (unlikely(netif_msg_hw(priv)))
@@ -391,14 +379,13 @@ static struct sk_buff *cpmac_rx_one(struct net_device *dev,
        }
 
        skb = netdev_alloc_skb(dev, CPMAC_SKB_SIZE);
-       spin_lock_irqsave(&priv->lock, flags);
        if (likely(skb)) {
                skb_reserve(skb, 2);
                skb_put(desc->skb, desc->datalen);
                desc->skb->protocol = eth_type_trans(desc->skb, dev);
                desc->skb->ip_summed = CHECKSUM_NONE;
-               priv->stats.rx_packets++;
-               priv->stats.rx_bytes += desc->datalen;
+               dev->stats.rx_packets++;
+               dev->stats.rx_bytes += desc->datalen;
                result = desc->skb;
                dma_unmap_single(&dev->dev, desc->data_mapping, CPMAC_SKB_SIZE,
                                 DMA_FROM_DEVICE);
@@ -413,11 +400,10 @@ static struct sk_buff *cpmac_rx_one(struct net_device *dev,
                }
        } else {
                if (netif_msg_rx_err(priv) && net_ratelimit())
-                       printk(KERN_WARNING 
+                       printk(KERN_WARNING
                               "%s: low on skbs, dropping packet\n", dev->name);
-               priv->stats.rx_dropped++;
+               dev->stats.rx_dropped++;
        }
-       spin_unlock_irqrestore(&priv->lock, flags);
 
        desc->buflen = CPMAC_SKB_SIZE;
        desc->dataflags = CPMAC_OWN;
@@ -425,32 +411,6 @@ static struct sk_buff *cpmac_rx_one(struct net_device *dev,
        return result;
 }
 
-static void cpmac_rx(struct net_device *dev)
-{
-       struct sk_buff *skb;
-       struct cpmac_desc *desc;
-       struct cpmac_priv *priv = netdev_priv(dev);
-
-       spin_lock(&priv->lock);
-       if (unlikely(!priv->rx_head)) {
-               spin_unlock(&priv->lock);
-               return;
-       }
-
-       desc = priv->rx_head;
-
-       while ((desc->dataflags & CPMAC_OWN) == 0) {
-               skb = cpmac_rx_one(dev, priv, desc);
-               if (likely(skb))
-                       netif_rx(skb);
-               desc = desc->next;
-       }
-
-       priv->rx_head = desc;
-       cpmac_write(priv->regs, CPMAC_RX_PTR(0), (u32)desc->mapping);
-       spin_unlock(&priv->lock);
-}
-
 static int cpmac_poll(struct net_device *dev, int *budget)
 {
        struct sk_buff *skb;
@@ -458,6 +418,7 @@ static int cpmac_poll(struct net_device *dev, int *budget)
        int received = 0, quota = min(dev->quota, *budget);
        struct cpmac_priv *priv = netdev_priv(dev);
 
+       spin_lock(&priv->rx_lock);
        if (unlikely(!priv->rx_head)) {
                if (netif_msg_rx_err(priv) && net_ratelimit())
                        printk(KERN_WARNING "%s: rx: polling, but no queue\n",
@@ -467,7 +428,6 @@ static int cpmac_poll(struct net_device *dev, int *budget)
        }
 
        desc = priv->rx_head;
-
        while ((received < quota) && ((desc->dataflags & CPMAC_OWN) == 0)) {
                skb = cpmac_rx_one(dev, priv, desc);
                if (likely(skb)) {
@@ -478,6 +438,7 @@ static int cpmac_poll(struct net_device *dev, int *budget)
        }
 
        priv->rx_head = desc;
+       spin_unlock(&priv->rx_lock);
        *budget -= received;
        dev->quota -= received;
        if (unlikely(netif_msg_rx_status(priv)))
@@ -495,41 +456,43 @@ static int cpmac_poll(struct net_device *dev, int *budget)
 
 static int cpmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
 {
-       unsigned long flags;
-       int channel, len;
+       int queue, len;
        struct cpmac_desc *desc;
        struct cpmac_priv *priv = netdev_priv(dev);
 
        if (unlikely(skb_padto(skb, ETH_ZLEN))) {
                if (netif_msg_tx_err(priv) && net_ratelimit())
-                       printk(KERN_WARNING "%s: tx: padding failed, dropping\n",
-                              dev->name);
-               spin_lock_irqsave(&priv->lock, flags);
-               priv->stats.tx_dropped++;
-               spin_unlock_irqrestore(&priv->lock, flags);
+                       printk(KERN_WARNING
+                              "%s: tx: padding failed, dropping\n", dev->name);
+               spin_lock(&priv->lock);
+               dev->stats.tx_dropped++;
+               spin_unlock(&priv->lock);
                return -ENOMEM;
        }
 
        len = max(skb->len, ETH_ZLEN);
-       spin_lock_irqsave(&priv->lock, flags);
-       channel = priv->tx_tail++;
-       priv->tx_tail %= CPMAC_TX_RING_SIZE;
-       if (priv->tx_tail == priv->tx_head)
-               netif_stop_queue(dev);
+       queue = skb->queue_mapping;
+#ifdef CONFIG_NETDEVICES_MULTIQUEUE
+       netif_stop_subqueue(dev, queue);
+#else
+       netif_stop_queue(dev);
+#endif
 
-       desc = &priv->desc_ring[channel];
-       if (desc->dataflags & CPMAC_OWN) {
+       desc = &priv->desc_ring[queue];
+       if (unlikely(desc->dataflags & CPMAC_OWN)) {
                if (netif_msg_tx_err(priv) && net_ratelimit())
                        printk(KERN_WARNING "%s: tx dma ring full, dropping\n",
                               dev->name);
-               priv->stats.tx_dropped++;
-               spin_unlock_irqrestore(&priv->lock, flags);
+               spin_lock(&priv->lock);
+               dev->stats.tx_dropped++;
+               spin_unlock(&priv->lock);
                dev_kfree_skb_any(skb);
                return -ENOMEM;
        }
 
+       spin_lock(&priv->lock);
        dev->trans_start = jiffies;
-       spin_unlock_irqrestore(&priv->lock, flags);
+       spin_unlock(&priv->lock);
        desc->dataflags = CPMAC_SOP | CPMAC_EOP | CPMAC_OWN;
        desc->skb = skb;
        desc->data_mapping = dma_map_single(&dev->dev, skb->data, len,
@@ -544,22 +507,23 @@ static int cpmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
                cpmac_dump_desc(dev, desc);
        if (unlikely(netif_msg_pktdata(priv)))
                cpmac_dump_skb(dev, skb);
-       cpmac_write(priv->regs, CPMAC_TX_PTR(channel), (u32)desc->mapping);
+       cpmac_write(priv->regs, CPMAC_TX_PTR(queue), (u32)desc->mapping);
 
        return 0;
 }
 
-static void cpmac_end_xmit(struct net_device *dev, int channel)
+static void cpmac_end_xmit(struct net_device *dev, int queue)
 {
        struct cpmac_desc *desc;
        struct cpmac_priv *priv = netdev_priv(dev);
 
-       spin_lock(&priv->lock);
-       desc = &priv->desc_ring[channel];
-       cpmac_write(priv->regs, CPMAC_TX_ACK(channel), (u32)desc->mapping);
+       desc = &priv->desc_ring[queue];
+       cpmac_write(priv->regs, CPMAC_TX_ACK(queue), (u32)desc->mapping);
        if (likely(desc->skb)) {
-               priv->stats.tx_packets++;
-               priv->stats.tx_bytes += desc->skb->len;
+               spin_lock(&priv->lock);
+               dev->stats.tx_packets++;
+               dev->stats.tx_bytes += desc->skb->len;
+               spin_unlock(&priv->lock);
                dma_unmap_single(&dev->dev, desc->data_mapping, desc->skb->len,
                                 DMA_TO_DEVICE);
 
@@ -568,21 +532,35 @@ static void cpmac_end_xmit(struct net_device *dev, int channel)
                               desc->skb, desc->skb->len);
 
                dev_kfree_skb_irq(desc->skb);
+               desc->skb = NULL;
+#ifdef CONFIG_NETDEVICES_MULTIQUEUE
+               if (netif_subqueue_stopped(dev, queue))
+                       netif_wake_subqueue(dev, queue);
+#else
                if (netif_queue_stopped(dev))
                        netif_wake_queue(dev);
-       } else
+#endif
+       } else {
                if (netif_msg_tx_err(priv) && net_ratelimit())
                        printk(KERN_WARNING
                               "%s: end_xmit: spurious interrupt\n", dev->name);
-       spin_unlock(&priv->lock);
+#ifdef CONFIG_NETDEVICES_MULTIQUEUE
+               if (netif_subqueue_stopped(dev, queue))
+                       netif_wake_subqueue(dev, queue);
+#else
+               if (netif_queue_stopped(dev))
+                       netif_wake_queue(dev);
+#endif
+       }
 }
 
-static void cpmac_reset(struct net_device *dev)
+static void cpmac_hw_stop(struct net_device *dev)
 {
        int i;
        struct cpmac_priv *priv = netdev_priv(dev);
+       struct plat_cpmac_data *pdata = priv->pdev->dev.platform_data;
 
-       ar7_device_reset(priv->config->reset_bit);
+       ar7_device_reset(pdata->reset_bit);
        cpmac_write(priv->regs, CPMAC_RX_CONTROL,
                    cpmac_read(priv->regs, CPMAC_RX_CONTROL) & ~1);
        cpmac_write(priv->regs, CPMAC_TX_CONTROL,
@@ -591,23 +569,64 @@ static void cpmac_reset(struct net_device *dev)
                cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
                cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
        }
+       cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
+       cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
+       cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
+       cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
        cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
                    cpmac_read(priv->regs, CPMAC_MAC_CONTROL) & ~MAC_MII);
 }
 
-static inline void cpmac_free_rx_ring(struct net_device *dev)
+static void cpmac_hw_start(struct net_device *dev)
 {
-       struct cpmac_desc *desc;
        int i;
        struct cpmac_priv *priv = netdev_priv(dev);
+       struct plat_cpmac_data *pdata = priv->pdev->dev.platform_data;
+
+       ar7_device_reset(pdata->reset_bit);
+       for (i = 0; i < 8; i++) {
+               cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
+               cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
+       }
+       cpmac_write(priv->regs, CPMAC_RX_PTR(0), priv->rx_head->mapping);
+
+       cpmac_write(priv->regs, CPMAC_MBP, MBP_RXSHORT | MBP_RXBCAST |
+                   MBP_RXMCAST);
+       cpmac_write(priv->regs, CPMAC_BUFFER_OFFSET, 0);
+       for (i = 0; i < 8; i++)
+               cpmac_write(priv->regs, CPMAC_MAC_ADDR_LO(i), dev->dev_addr[5]);
+       cpmac_write(priv->regs, CPMAC_MAC_ADDR_MID, dev->dev_addr[4]);
+       cpmac_write(priv->regs, CPMAC_MAC_ADDR_HI, dev->dev_addr[0] |
+                   (dev->dev_addr[1] << 8) | (dev->dev_addr[2] << 16) |
+                   (dev->dev_addr[3] << 24));
+       cpmac_write(priv->regs, CPMAC_MAX_LENGTH, CPMAC_SKB_SIZE);
+       cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
+       cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
+       cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
+       cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
+       cpmac_write(priv->regs, CPMAC_UNICAST_ENABLE, 1);
+       cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
+       cpmac_write(priv->regs, CPMAC_TX_INT_ENABLE, 0xff);
+       cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
 
+       cpmac_write(priv->regs, CPMAC_RX_CONTROL,
+                   cpmac_read(priv->regs, CPMAC_RX_CONTROL) | 1);
+       cpmac_write(priv->regs, CPMAC_TX_CONTROL,
+                   cpmac_read(priv->regs, CPMAC_TX_CONTROL) | 1);
+       cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
+                   cpmac_read(priv->regs, CPMAC_MAC_CONTROL) | MAC_MII |
+                   MAC_FDX);
+}
+
+static void cpmac_clear_rx(struct net_device *dev)
+{
+       struct cpmac_priv *priv = netdev_priv(dev);
+       struct cpmac_desc *desc;
+       int i;
        if (unlikely(!priv->rx_head))
                return;
-
        desc = priv->rx_head;
-
-       for (i = 0; i < rx_ring_size; i++) {
-               desc->buflen = CPMAC_SKB_SIZE;
+       for (i = 0; i < priv->ring_size; i++) {
                if ((desc->dataflags & CPMAC_OWN) == 0) {
                        if (netif_msg_rx_err(priv) && net_ratelimit())
                                printk(KERN_WARNING "%s: packet dropped\n",
@@ -615,16 +634,44 @@ static inline void cpmac_free_rx_ring(struct net_device *dev)
                        if (unlikely(netif_msg_hw(priv)))
                                cpmac_dump_desc(dev, desc);
                        desc->dataflags = CPMAC_OWN;
-                       priv->stats.rx_dropped++;
+                       dev->stats.rx_dropped++;
                }
                desc = desc->next;
        }
 }
 
+static void cpmac_clear_tx(struct net_device *dev)
+{
+       struct cpmac_priv *priv = netdev_priv(dev);
+       int i;
+       if (unlikely(!priv->desc_ring))
+               return;
+       for (i = 0; i < CPMAC_QUEUES; i++)
+               if (priv->desc_ring[i].skb) {
+                       dev_kfree_skb_any(priv->desc_ring[i].skb);
+                       if (netif_subqueue_stopped(dev, i))
+                           netif_wake_subqueue(dev, i);
+               }
+}
+
+static void cpmac_hw_error(struct work_struct *work)
+{
+       struct cpmac_priv *priv =
+               container_of(work, struct cpmac_priv, reset_work);
+
+       spin_lock(&priv->rx_lock);
+       cpmac_clear_rx(priv->dev);
+       spin_unlock(&priv->rx_lock);
+       cpmac_clear_tx(priv->dev);
+       cpmac_hw_start(priv->dev);
+       netif_start_queue(priv->dev);
+}
+
 static irqreturn_t cpmac_irq(int irq, void *dev_id)
 {
        struct net_device *dev = dev_id;
-       struct cpmac_priv *priv;
+       struct cpmac_priv *priv;
+       int queue;
        u32 status;
 
        if (!dev)
@@ -642,12 +689,9 @@ static irqreturn_t cpmac_irq(int irq, void *dev_id)
                cpmac_end_xmit(dev, (status & 7));
 
        if (status & MAC_INT_RX) {
-               if (disable_napi)
-                       cpmac_rx(dev);
-               else {
-                       cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 1);
-                       netif_rx_schedule(dev);
-               }
+               queue = (status >> 8) & 7;
+               netif_rx_schedule(dev);
+               cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 1 << queue);
        }
 
        cpmac_write(priv->regs, CPMAC_MAC_EOI_VECTOR, 0);
@@ -656,14 +700,11 @@ static irqreturn_t cpmac_irq(int irq, void *dev_id)
                if (netif_msg_drv(priv) && net_ratelimit())
                        printk(KERN_ERR "%s: hw error, resetting...\n",
                               dev->name);
+               netif_stop_queue(dev);
+               cpmac_hw_stop(dev);
+               schedule_work(&priv->reset_work);
                if (unlikely(netif_msg_hw(priv)))
                        cpmac_dump_regs(dev);
-               spin_lock(&priv->lock);
-               phy_stop(priv->phy);
-               cpmac_reset(dev);
-               cpmac_free_rx_ring(dev);
-               cpmac_hw_init(dev);
-               spin_unlock(&priv->lock);
        }
 
        return IRQ_HANDLED;
@@ -672,16 +713,29 @@ static irqreturn_t cpmac_irq(int irq, void *dev_id)
 static void cpmac_tx_timeout(struct net_device *dev)
 {
        struct cpmac_priv *priv = netdev_priv(dev);
-       struct cpmac_desc *desc;
+       int i;
 
-       priv->stats.tx_errors++;
-       desc = &priv->desc_ring[priv->tx_head++];
-       priv->tx_head %= 8;
+       spin_lock(&priv->lock);
+       dev->stats.tx_errors++;
+       spin_unlock(&priv->lock);
        if (netif_msg_tx_err(priv) && net_ratelimit())
                printk(KERN_WARNING "%s: transmit timeout\n", dev->name);
-       if (desc->skb)
-               dev_kfree_skb_any(desc->skb);
+       /* 
+        * FIXME: waking up random queue is not the best thing to
+        * do... on the other hand why we got here at all?
+        */
+#ifdef CONFIG_NETDEVICES_MULTIQUEUE
+       for (i = 0; i < CPMAC_QUEUES; i++)
+               if (priv->desc_ring[i].skb) {
+                       dev_kfree_skb_any(priv->desc_ring[i].skb);
+                       netif_wake_subqueue(dev, i);
+                       break;
+               }
+#else
+       if (priv->desc_ring[0].skb)
+               dev_kfree_skb_any(priv->desc_ring[0].skb);
        netif_wake_queue(dev);
+#endif
 }
 
 static int cpmac_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
@@ -695,7 +749,7 @@ static int cpmac_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
            (cmd == SIOCSMIIREG))
                return phy_mii_ioctl(priv->phy, if_mii(ifr), cmd);
 
-       return -EINVAL;
+       return -EOPNOTSUPP;
 }
 
 static int cpmac_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
@@ -721,11 +775,36 @@ static int cpmac_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
        return -EINVAL;
 }
 
+static void cpmac_get_ringparam(struct net_device *dev, struct ethtool_ringparam* ring)
+{
+       struct cpmac_priv *priv = netdev_priv(dev);
+
+       ring->rx_max_pending = 1024;
+       ring->rx_mini_max_pending = 1;
+       ring->rx_jumbo_max_pending = 1;
+       ring->tx_max_pending = 1;
+
+       ring->rx_pending = priv->ring_size;
+       ring->rx_mini_pending = 1;
+       ring->rx_jumbo_pending = 1;
+       ring->tx_pending = 1;
+}
+
+static int cpmac_set_ringparam(struct net_device *dev, struct ethtool_ringparam* ring)
+{
+       struct cpmac_priv *priv = netdev_priv(dev);
+
+       if (dev->flags && IFF_UP)
+               return -EBUSY;
+       priv->ring_size = ring->rx_pending;
+       return 0;
+}
+
 static void cpmac_get_drvinfo(struct net_device *dev,
                              struct ethtool_drvinfo *info)
 {
        strcpy(info->driver, "cpmac");
-       strcpy(info->version, "0.0.3");
+       strcpy(info->version, CPMAC_VERSION);
        info->fw_version[0] = '\0';
        sprintf(info->bus_info, "%s", "cpmac");
        info->regdump_len = 0;
@@ -736,42 +815,18 @@ static const struct ethtool_ops cpmac_ethtool_ops = {
        .set_settings = cpmac_set_settings,
        .get_drvinfo = cpmac_get_drvinfo,
        .get_link = ethtool_op_get_link,
+       .get_ringparam = cpmac_get_ringparam,
+       .set_ringparam = cpmac_set_ringparam,
 };
 
-static struct net_device_stats *cpmac_stats(struct net_device *dev)
-{
-       struct cpmac_priv *priv = netdev_priv(dev);
-
-       if (netif_device_present(dev))
-               return &priv->stats;
-
-       return NULL;
-}
-
-static int cpmac_change_mtu(struct net_device *dev, int mtu)
-{
-       unsigned long flags;
-       struct cpmac_priv *priv = netdev_priv(dev);
-       spinlock_t *lock = &priv->lock;
-
-       if ((mtu < 68) || (mtu > 1500))
-               return -EINVAL;
-
-       spin_lock_irqsave(lock, flags);
-       dev->mtu = mtu;
-       spin_unlock_irqrestore(lock, flags);
-
-       return 0;
-}
-
 static void cpmac_adjust_link(struct net_device *dev)
 {
        struct cpmac_priv *priv = netdev_priv(dev);
-       unsigned long flags;
        int new_state = 0;
 
-       spin_lock_irqsave(&priv->lock, flags);
+       spin_lock(&priv->lock);
        if (priv->phy->link) {
+               netif_start_queue(dev);
                if (priv->phy->duplex != priv->oldduplex) {
                        new_state = 1;
                        priv->oldduplex = priv->phy->duplex;
@@ -788,6 +843,7 @@ static void cpmac_adjust_link(struct net_device *dev)
                        netif_schedule(dev);
                }
        } else if (priv->oldlink) {
+               netif_stop_queue(dev);
                new_state = 1;
                priv->oldlink = 0;
                priv->oldspeed = 0;
@@ -797,55 +853,14 @@ static void cpmac_adjust_link(struct net_device *dev)
        if (new_state && netif_msg_link(priv) && net_ratelimit())
                phy_print_status(priv->phy);
 
-       spin_unlock_irqrestore(&priv->lock, flags);
-}
-
-static void cpmac_hw_init(struct net_device *dev)
-{
-       int i;
-       struct cpmac_priv *priv = netdev_priv(dev);
-
-       for (i = 0; i < 8; i++) {
-               cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
-               cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
-       }
-       cpmac_write(priv->regs, CPMAC_RX_PTR(0), priv->rx_head->mapping);
-
-       cpmac_write(priv->regs, CPMAC_MBP, MBP_RXSHORT | MBP_RXBCAST |
-                   MBP_RXMCAST);
-       cpmac_write(priv->regs, CPMAC_UNICAST_ENABLE, 1);
-       cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xfe);
-       cpmac_write(priv->regs, CPMAC_BUFFER_OFFSET, 0);
-       for (i = 0; i < 8; i++)
-               cpmac_write(priv->regs, CPMAC_MAC_ADDR_LO(i), dev->dev_addr[5]);
-       cpmac_write(priv->regs, CPMAC_MAC_ADDR_MID, dev->dev_addr[4]);
-       cpmac_write(priv->regs, CPMAC_MAC_ADDR_HI, dev->dev_addr[0] |
-                   (dev->dev_addr[1] << 8) | (dev->dev_addr[2] << 16) |
-                   (dev->dev_addr[3] << 24));
-       cpmac_write(priv->regs, CPMAC_MAX_LENGTH, CPMAC_SKB_SIZE);
-       cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
-       cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
-       cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
-       cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
-       cpmac_write(priv->regs, CPMAC_TX_INT_ENABLE, 0xff);
-       cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
-
-       cpmac_write(priv->regs, CPMAC_RX_CONTROL,
-                   cpmac_read(priv->regs, CPMAC_RX_CONTROL) | 1);
-       cpmac_write(priv->regs, CPMAC_TX_CONTROL,
-                   cpmac_read(priv->regs, CPMAC_TX_CONTROL) | 1);
-       cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
-                   cpmac_read(priv->regs, CPMAC_MAC_CONTROL) | MAC_MII |
-                   MAC_FDX);
-
-       priv->phy->state = PHY_CHANGELINK;
-       phy_start(priv->phy);
+       spin_unlock(&priv->lock);
 }
 
 static int cpmac_open(struct net_device *dev)
 {
        int i, size, res;
        struct cpmac_priv *priv = netdev_priv(dev);
+       struct resource *mem;
        struct cpmac_desc *desc;
        struct sk_buff *skb;
 
@@ -858,8 +873,8 @@ static int cpmac_open(struct net_device *dev)
                return PTR_ERR(priv->phy);
        }
 
-       if (!request_mem_region(dev->mem_start, dev->mem_end -
-                               dev->mem_start, dev->name)) {
+       mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
+       if (!request_mem_region(mem->start, mem->end - mem->start, dev->name)) {
                if (netif_msg_drv(priv))
                        printk(KERN_ERR "%s: failed to request registers\n",
                               dev->name);
@@ -867,8 +882,7 @@ static int cpmac_open(struct net_device *dev)
                goto fail_reserve;
        }
 
-       priv->regs = ioremap(dev->mem_start, dev->mem_end -
-                            dev->mem_start);
+       priv->regs = ioremap(mem->start, mem->end - mem->start);
        if (!priv->regs) {
                if (netif_msg_drv(priv))
                        printk(KERN_ERR "%s: failed to remap registers\n",
@@ -877,8 +891,7 @@ static int cpmac_open(struct net_device *dev)
                goto fail_remap;
        }
 
-       priv->rx_head = NULL;
-       size = rx_ring_size + CPMAC_TX_RING_SIZE;
+       size = priv->ring_size + CPMAC_QUEUES;
        priv->desc_ring = dma_alloc_coherent(&dev->dev,
                                             sizeof(struct cpmac_desc) * size,
                                             &priv->dma_ring,
@@ -888,11 +901,11 @@ static int cpmac_open(struct net_device *dev)
                goto fail_alloc;
        }
 
-       priv->rx_head = &priv->desc_ring[CPMAC_TX_RING_SIZE];
        for (i = 0; i < size; i++)
                priv->desc_ring[i].mapping = priv->dma_ring + sizeof(*desc) * i;
 
-       for (i = 0, desc = &priv->rx_head[i]; i < rx_ring_size; i++, desc++) {
+       priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
+       for (i = 0, desc = priv->rx_head; i < priv->ring_size; i++, desc++) {
                skb = netdev_alloc_skb(dev, CPMAC_SKB_SIZE);
                if (unlikely(!skb)) {
                        res = -ENOMEM;
@@ -906,7 +919,7 @@ static int cpmac_open(struct net_device *dev)
                desc->hw_data = (u32)desc->data_mapping;
                desc->buflen = CPMAC_SKB_SIZE;
                desc->dataflags = CPMAC_OWN;
-               desc->next = &priv->rx_head[(i + 1) % rx_ring_size];
+               desc->next = &priv->rx_head[(i + 1) % priv->ring_size];
                desc->hw_next = (u32)desc->next->mapping;
        }
 
@@ -918,21 +931,23 @@ static int cpmac_open(struct net_device *dev)
                goto fail_irq;
        }
 
-       cpmac_reset(dev);
-       cpmac_hw_init(dev);
+       INIT_WORK(&priv->reset_work, cpmac_hw_error);
+       cpmac_hw_start(dev);
+
+       priv->phy->state = PHY_CHANGELINK;
+       phy_start(priv->phy);
 
-       netif_start_queue(dev);
        return 0;
 
 fail_irq:
 fail_desc:
-       for (i = 0; i < rx_ring_size; i++) {
+       for (i = 0; i < priv->ring_size; i++) {
                if (priv->rx_head[i].skb) {
-                       kfree_skb(priv->rx_head[i].skb);
                        dma_unmap_single(&dev->dev,
                                         priv->rx_head[i].data_mapping,
                                         CPMAC_SKB_SIZE,
                                         DMA_FROM_DEVICE);
+                       kfree_skb(priv->rx_head[i].skb);
                }
        }
 fail_alloc:
@@ -940,8 +955,7 @@ fail_alloc:
        iounmap(priv->regs);
 
 fail_remap:
-       release_mem_region(dev->mem_start, dev->mem_end -
-                          dev->mem_start);
+       release_mem_region(mem->start, mem->end - mem->start);
 
 fail_reserve:
        phy_disconnect(priv->phy);
@@ -953,14 +967,16 @@ static int cpmac_stop(struct net_device *dev)
 {
        int i;
        struct cpmac_priv *priv = netdev_priv(dev);
+       struct resource *mem;
 
        netif_stop_queue(dev);
 
+       cancel_work_sync(&priv->reset_work);
        phy_stop(priv->phy);
        phy_disconnect(priv->phy);
        priv->phy = NULL;
 
-       cpmac_reset(dev);
+       cpmac_hw_stop(dev);
 
        for (i = 0; i < 8; i++)
                cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
@@ -968,21 +984,22 @@ static int cpmac_stop(struct net_device *dev)
        cpmac_write(priv->regs, CPMAC_MBP, 0);
 
        free_irq(dev->irq, dev);
-       release_mem_region(dev->mem_start, dev->mem_end -
-                          dev->mem_start);
-       priv->rx_head = &priv->desc_ring[CPMAC_TX_RING_SIZE];
-       for (i = 0; i < rx_ring_size; i++) {
+       iounmap(priv->regs);
+       mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
+       release_mem_region(mem->start, mem->end - mem->start);
+       priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
+       for (i = 0; i < priv->ring_size; i++) {
                if (priv->rx_head[i].skb) {
-                       kfree_skb(priv->rx_head[i].skb);
                        dma_unmap_single(&dev->dev,
                                         priv->rx_head[i].data_mapping,
                                         CPMAC_SKB_SIZE,
                                         DMA_FROM_DEVICE);
+                       kfree_skb(priv->rx_head[i].skb);
                }
        }
 
        dma_free_coherent(&dev->dev, sizeof(struct cpmac_desc) *
-                         (CPMAC_TX_RING_SIZE + rx_ring_size),
+                         (CPMAC_QUEUES + priv->ring_size),
                          priv->desc_ring, priv->dma_ring);
        return 0;
 }
@@ -991,8 +1008,8 @@ static int external_switch;
 
 static int __devinit cpmac_probe(struct platform_device *pdev)
 {
-       int i, rc, phy_id;
-       struct resource *res;
+       int rc, phy_id;
+       struct resource *mem;
        struct cpmac_priv *priv;
        struct net_device *dev;
        struct plat_cpmac_data *pdata;
@@ -1016,25 +1033,23 @@ static int __devinit cpmac_probe(struct platform_device *pdev)
                }
        }
 
-       dev = alloc_etherdev(sizeof(struct cpmac_priv));
+       dev = alloc_etherdev_mq(sizeof(*priv), CPMAC_QUEUES);
 
        if (!dev) {
                printk(KERN_ERR "cpmac: Unable to allocate net_device\n");
                return -ENOMEM;
        }
 
-       SET_MODULE_OWNER(dev);
        platform_set_drvdata(pdev, dev);
        priv = netdev_priv(dev);
 
-       res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
-       if (!res) {
+       priv->pdev = pdev;
+       mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
+       if (!mem) {
                rc = -ENODEV;
                goto fail;
        }
 
-       dev->mem_start = res->start;
-       dev->mem_end = res->end;
        dev->irq = platform_get_irq_byname(pdev, "irq");
 
        dev->open               = cpmac_open;
@@ -1042,27 +1057,22 @@ static int __devinit cpmac_probe(struct platform_device *pdev)
        dev->set_config         = cpmac_config;
        dev->hard_start_xmit    = cpmac_start_xmit;
        dev->do_ioctl           = cpmac_ioctl;
-       dev->get_stats          = cpmac_stats;
-       dev->change_mtu         = cpmac_change_mtu;
-       dev->set_mac_address    = cpmac_set_mac_address;
        dev->set_multicast_list = cpmac_set_multicast_list;
        dev->tx_timeout         = cpmac_tx_timeout;
        dev->ethtool_ops        = &cpmac_ethtool_ops;
-       if (!disable_napi) {
-               dev->poll = cpmac_poll;
-               dev->weight = min(rx_ring_size, 64);
-       }
+       dev->poll = cpmac_poll;
+       dev->weight = 64;
+       dev->features |= NETIF_F_MULTI_QUEUE;
 
        spin_lock_init(&priv->lock);
-       priv->msg_enable = netif_msg_init(debug_level, 0xff);
-       priv->config = pdata;
+       spin_lock_init(&priv->rx_lock);
        priv->dev = dev;
-       memcpy(dev->dev_addr, priv->config->dev_addr, sizeof(dev->dev_addr));
+       priv->ring_size = 64;
+       priv->msg_enable = netif_msg_init(debug_level, 0xff);
+       memcpy(dev->dev_addr, pdata->dev_addr, sizeof(dev->dev_addr));
        if (phy_id == 31) {
                snprintf(priv->phy_name, BUS_ID_SIZE, PHY_ID_FMT,
                         cpmac_mii.id, phy_id);
-/*             cpmac_write(cpmac_mii.priv, CPMAC_MDIO_PHYSEL(0), PHYSEL_LINKSEL
-               | PHYSEL_LINKINT | phy_id);*/
        } else
                snprintf(priv->phy_name, BUS_ID_SIZE, "fixed@%d:%d", 100, 1);
 
@@ -1074,11 +1084,9 @@ static int __devinit cpmac_probe(struct platform_device *pdev)
 
        if (netif_msg_probe(priv)) {
                printk(KERN_INFO
-                      "cpmac: device %s (regs: %p, irq: %d, phy: %s, mac: ",
-                      dev->name, (u32 *)dev->mem_start, dev->irq,
-                      priv->phy_name);
-               for (i = 0; i < 6; i++)
-                       printk("%02x%s", dev->dev_addr[i], i < 5 ? ":" : ")\n");
+                      "cpmac: device %s (regs: %p, irq: %d, phy: %s, mac: "
+                      MAC_FMT ")\n", dev->name, (void *)mem->start, dev->irq,
+                      priv->phy_name, MAC_ARG(dev->dev_addr));
        }
        return 0;
 
@@ -1098,7 +1106,7 @@ static int __devexit cpmac_remove(struct platform_device *pdev)
 static struct platform_driver cpmac_driver = {
        .driver.name = "cpmac",
        .probe = cpmac_probe,
-       .remove = cpmac_remove,
+       .remove = __devexit_p(cpmac_remove),
 };
 
 int __devinit cpmac_init(void)