qlcnic: cleanup dma mask setting
[safe/jmp/linux-2.6] / drivers / net / cpmac.c
index ed53aaa..3c58db5 100644 (file)
 #include <linux/errno.h>
 #include <linux/types.h>
 #include <linux/delay.h>
-#include <linux/version.h>
 
 #include <linux/netdevice.h>
+#include <linux/if_vlan.h>
 #include <linux/etherdevice.h>
 #include <linux/ethtool.h>
 #include <linux/skbuff.h>
 #include <linux/mii.h>
 #include <linux/phy.h>
+#include <linux/phy_fixed.h>
 #include <linux/platform_device.h>
 #include <linux/dma-mapping.h>
+#include <linux/clk.h>
 #include <asm/gpio.h>
+#include <asm/atomic.h>
 
 MODULE_AUTHOR("Eugene Konev <ejka@imfi.kspu.ru>");
 MODULE_DESCRIPTION("TI AR7 ethernet driver (CPMAC)");
 MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:cpmac");
 
 static int debug_level = 8;
 static int dumb_switch;
@@ -52,15 +56,9 @@ module_param(dumb_switch, int, 0444);
 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_VERSION "0.5.2"
+/* frame size + 802.1q tag + FCS size */
+#define CPMAC_SKB_SIZE         (ETH_FRAME_LEN + ETH_FCS_LEN + VLAN_HLEN)
 #define CPMAC_QUEUES   8
 
 /* Ethernet registers */
@@ -191,6 +189,7 @@ struct cpmac_desc {
 #define CPMAC_EOQ                      0x1000
        struct sk_buff *skb;
        struct cpmac_desc *next;
+       struct cpmac_desc *prev;
        dma_addr_t mapping;
        dma_addr_t data_mapping;
 };
@@ -205,12 +204,14 @@ struct cpmac_priv {
        void __iomem *regs;
        struct mii_bus *mii_bus;
        struct phy_device *phy;
-       char phy_name[BUS_ID_SIZE];
+       char phy_name[MII_BUS_ID_SIZE + 3];
        int oldlink, oldspeed, oldduplex;
        u32 msg_enable;
        struct net_device *dev;
        struct work_struct reset_work;
        struct platform_device *pdev;
+       struct napi_struct napi;
+       atomic_t reset_pending;
 };
 
 static irqreturn_t cpmac_irq(int, void *);
@@ -244,6 +245,16 @@ static void cpmac_dump_desc(struct net_device *dev, struct cpmac_desc *desc)
        printk("\n");
 }
 
+static void cpmac_dump_all_desc(struct net_device *dev)
+{
+       struct cpmac_priv *priv = netdev_priv(dev);
+       struct cpmac_desc *dump = priv->rx_head;
+       do {
+               cpmac_dump_desc(dev, dump);
+               dump = dump->next;
+       } while (dump != priv->rx_head);
+}
+
 static void cpmac_dump_skb(struct net_device *dev, struct sk_buff *skb)
 {
        int i;
@@ -285,21 +296,22 @@ static int cpmac_mdio_write(struct mii_bus *bus, int phy_id,
 
 static int cpmac_mdio_reset(struct mii_bus *bus)
 {
+       struct clk *cpmac_clk;
+
+       cpmac_clk = clk_get(&bus->dev, "cpmac");
+       if (IS_ERR(cpmac_clk)) {
+               printk(KERN_ERR "unable to get cpmac clock\n");
+               return -1;
+       }
        ar7_device_reset(AR7_RESET_BIT_MDIO);
        cpmac_write(bus->priv, CPMAC_MDIO_CONTROL, MDIOC_ENABLE |
-                   MDIOC_CLKDIV(ar7_cpmac_freq() / 2200000 - 1));
+                   MDIOC_CLKDIV(clk_get_rate(cpmac_clk) / 2200000 - 1));
        return 0;
 }
 
 static int mii_irqs[PHY_MAX_ADDR] = { PHY_POLL, };
 
-static struct mii_bus cpmac_mii = {
-       .name = "cpmac-mii",
-       .read = cpmac_mdio_read,
-       .write = cpmac_mdio_write,
-       .reset = cpmac_mdio_reset,
-       .irq = mii_irqs,
-};
+static struct mii_bus *cpmac_mii;
 
 static int cpmac_config(struct net_device *dev, struct ifmap *map)
 {
@@ -316,8 +328,7 @@ static int cpmac_config(struct net_device *dev, struct ifmap *map)
 
 static void cpmac_set_multicast_list(struct net_device *dev)
 {
-       struct dev_mc_list *iter;
-       int i;
+       struct netdev_hw_addr *ha;
        u8 tmp;
        u32 mbp, bit, hash[2] = { 0, };
        struct cpmac_priv *priv = netdev_priv(dev);
@@ -337,20 +348,19 @@ static void cpmac_set_multicast_list(struct net_device *dev)
                         * cpmac uses some strange mac address hashing
                         * (not crc32)
                         */
-                       for (i = 0, iter = dev->mc_list; i < dev->mc_count;
-                            i++, iter = iter->next) {
+                       netdev_for_each_mc_addr(ha, dev) {
                                bit = 0;
-                               tmp = iter->dmi_addr[0];
+                               tmp = ha->addr[0];
                                bit  ^= (tmp >> 2) ^ (tmp << 4);
-                               tmp = iter->dmi_addr[1];
+                               tmp = ha->addr[1];
                                bit  ^= (tmp >> 4) ^ (tmp << 2);
-                               tmp = iter->dmi_addr[2];
+                               tmp = ha->addr[2];
                                bit  ^= (tmp >> 6) ^ tmp;
-                               tmp = iter->dmi_addr[3];
+                               tmp = ha->addr[3];
                                bit  ^= (tmp >> 2) ^ (tmp << 4);
-                               tmp = iter->dmi_addr[4];
+                               tmp = ha->addr[4];
                                bit  ^= (tmp >> 4) ^ (tmp << 2);
-                               tmp = iter->dmi_addr[5];
+                               tmp = ha->addr[5];
                                bit  ^= (tmp >> 6) ^ tmp;
                                bit &= 0x3f;
                                hash[bit / 32] |= 1 << (bit % 32);
@@ -362,47 +372,47 @@ static void cpmac_set_multicast_list(struct net_device *dev)
        }
 }
 
-static struct sk_buff *cpmac_rx_one(struct net_device *dev,
-                                   struct cpmac_priv *priv,
+static struct sk_buff *cpmac_rx_one(struct cpmac_priv *priv,
                                    struct cpmac_desc *desc)
 {
        struct sk_buff *skb, *result = NULL;
 
        if (unlikely(netif_msg_hw(priv)))
-               cpmac_dump_desc(dev, desc);
+               cpmac_dump_desc(priv->dev, desc);
        cpmac_write(priv->regs, CPMAC_RX_ACK(0), (u32)desc->mapping);
        if (unlikely(!desc->datalen)) {
                if (netif_msg_rx_err(priv) && net_ratelimit())
                        printk(KERN_WARNING "%s: rx: spurious interrupt\n",
-                              dev->name);
+                              priv->dev->name);
                return NULL;
        }
 
-       skb = netdev_alloc_skb(dev, CPMAC_SKB_SIZE);
+       skb = netdev_alloc_skb_ip_align(priv->dev, CPMAC_SKB_SIZE);
        if (likely(skb)) {
-               skb_reserve(skb, 2);
                skb_put(desc->skb, desc->datalen);
-               desc->skb->protocol = eth_type_trans(desc->skb, dev);
+               desc->skb->protocol = eth_type_trans(desc->skb, priv->dev);
                desc->skb->ip_summed = CHECKSUM_NONE;
-               dev->stats.rx_packets++;
-               dev->stats.rx_bytes += desc->datalen;
+               priv->dev->stats.rx_packets++;
+               priv->dev->stats.rx_bytes += desc->datalen;
                result = desc->skb;
-               dma_unmap_single(&dev->dev, desc->data_mapping, CPMAC_SKB_SIZE,
-                                DMA_FROM_DEVICE);
+               dma_unmap_single(&priv->dev->dev, desc->data_mapping,
+                                CPMAC_SKB_SIZE, DMA_FROM_DEVICE);
                desc->skb = skb;
-               desc->data_mapping = dma_map_single(&dev->dev, skb->data,
+               desc->data_mapping = dma_map_single(&priv->dev->dev, skb->data,
                                                    CPMAC_SKB_SIZE,
                                                    DMA_FROM_DEVICE);
                desc->hw_data = (u32)desc->data_mapping;
                if (unlikely(netif_msg_pktdata(priv))) {
-                       printk(KERN_DEBUG "%s: received packet:\n", dev->name);
-                       cpmac_dump_skb(dev, result);
+                       printk(KERN_DEBUG "%s: received packet:\n",
+                              priv->dev->name);
+                       cpmac_dump_skb(priv->dev, result);
                }
        } else {
                if (netif_msg_rx_err(priv) && net_ratelimit())
                        printk(KERN_WARNING
-                              "%s: low on skbs, dropping packet\n", dev->name);
-               dev->stats.rx_dropped++;
+                              "%s: low on skbs, dropping packet\n",
+                              priv->dev->name);
+               priv->dev->stats.rx_dropped++;
        }
 
        desc->buflen = CPMAC_SKB_SIZE;
@@ -411,25 +421,46 @@ static struct sk_buff *cpmac_rx_one(struct net_device *dev,
        return result;
 }
 
-static int cpmac_poll(struct net_device *dev, int *budget)
+static int cpmac_poll(struct napi_struct *napi, int budget)
 {
        struct sk_buff *skb;
-       struct cpmac_desc *desc;
-       int received = 0, quota = min(dev->quota, *budget);
-       struct cpmac_priv *priv = netdev_priv(dev);
+       struct cpmac_desc *desc, *restart;
+       struct cpmac_priv *priv = container_of(napi, struct cpmac_priv, napi);
+       int received = 0, processed = 0;
 
        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",
-                              dev->name);
-               netif_rx_complete(dev);
+                              priv->dev->name);
+               spin_unlock(&priv->rx_lock);
+               napi_complete(napi);
                return 0;
        }
 
        desc = priv->rx_head;
-       while ((received < quota) && ((desc->dataflags & CPMAC_OWN) == 0)) {
-               skb = cpmac_rx_one(dev, priv, desc);
+       restart = NULL;
+       while (((desc->dataflags & CPMAC_OWN) == 0) && (received < budget)) {
+               processed++;
+
+               if ((desc->dataflags & CPMAC_EOQ) != 0) {
+                       /* The last update to eoq->hw_next didn't happen
+                       * soon enough, and the receiver stopped here.
+                       *Remember this descriptor so we can restart
+                       * the receiver after freeing some space.
+                       */
+                       if (unlikely(restart)) {
+                               if (netif_msg_rx_err(priv))
+                                       printk(KERN_ERR "%s: poll found a"
+                                               " duplicate EOQ: %p and %p\n",
+                                               priv->dev->name, restart, desc);
+                               goto fatal_error;
+                       }
+
+                       restart = desc->next;
+               }
+
+               skb = cpmac_rx_one(priv, desc);
                if (likely(skb)) {
                        netif_receive_skb(skb);
                        received++;
@@ -437,21 +468,90 @@ static int cpmac_poll(struct net_device *dev, int *budget)
                desc = desc->next;
        }
 
+       if (desc != priv->rx_head) {
+               /* We freed some buffers, but not the whole ring,
+                * add what we did free to the rx list */
+               desc->prev->hw_next = (u32)0;
+               priv->rx_head->prev->hw_next = priv->rx_head->mapping;
+       }
+
+       /* Optimization: If we did not actually process an EOQ (perhaps because
+        * of quota limits), check to see if the tail of the queue has EOQ set.
+       * We should immediately restart in that case so that the receiver can
+       * restart and run in parallel with more packet processing.
+       * This lets us handle slightly larger bursts before running
+       * out of ring space (assuming dev->weight < ring_size) */
+
+       if (!restart &&
+            (priv->rx_head->prev->dataflags & (CPMAC_OWN|CPMAC_EOQ))
+                   == CPMAC_EOQ &&
+            (priv->rx_head->dataflags & CPMAC_OWN) != 0) {
+               /* reset EOQ so the poll loop (above) doesn't try to
+               * restart this when it eventually gets to this descriptor.
+               */
+               priv->rx_head->prev->dataflags &= ~CPMAC_EOQ;
+               restart = priv->rx_head;
+       }
+
+       if (restart) {
+               priv->dev->stats.rx_errors++;
+               priv->dev->stats.rx_fifo_errors++;
+               if (netif_msg_rx_err(priv) && net_ratelimit())
+                       printk(KERN_WARNING "%s: rx dma ring overrun\n",
+                              priv->dev->name);
+
+               if (unlikely((restart->dataflags & CPMAC_OWN) == 0)) {
+                       if (netif_msg_drv(priv))
+                               printk(KERN_ERR "%s: cpmac_poll is trying to "
+                                       "restart rx from a descriptor that's "
+                                       "not free: %p\n",
+                                       priv->dev->name, restart);
+                               goto fatal_error;
+               }
+
+               cpmac_write(priv->regs, CPMAC_RX_PTR(0), restart->mapping);
+       }
+
        priv->rx_head = desc;
        spin_unlock(&priv->rx_lock);
-       *budget -= received;
-       dev->quota -= received;
        if (unlikely(netif_msg_rx_status(priv)))
-               printk(KERN_DEBUG "%s: poll processed %d packets\n", dev->name,
-                      received);
-       if (desc->dataflags & CPMAC_OWN) {
-               netif_rx_complete(dev);
-               cpmac_write(priv->regs, CPMAC_RX_PTR(0), (u32)desc->mapping);
+               printk(KERN_DEBUG "%s: poll processed %d packets\n",
+                      priv->dev->name, received);
+       if (processed == 0) {
+               /* we ran out of packets to read,
+                * revert to interrupt-driven mode */
+               napi_complete(napi);
                cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
                return 0;
        }
 
        return 1;
+
+fatal_error:
+       /* Something went horribly wrong.
+        * Reset hardware to try to recover rather than wedging. */
+
+       if (netif_msg_drv(priv)) {
+               printk(KERN_ERR "%s: cpmac_poll is confused. "
+                               "Resetting hardware\n", priv->dev->name);
+               cpmac_dump_all_desc(priv->dev);
+               printk(KERN_DEBUG "%s: RX_PTR(0)=0x%08x RX_ACK(0)=0x%08x\n",
+                       priv->dev->name,
+                       cpmac_read(priv->regs, CPMAC_RX_PTR(0)),
+                       cpmac_read(priv->regs, CPMAC_RX_ACK(0)));
+       }
+
+       spin_unlock(&priv->rx_lock);
+       napi_complete(napi);
+       netif_tx_stop_all_queues(priv->dev);
+       napi_disable(&priv->napi);
+
+       atomic_inc(&priv->reset_pending);
+       cpmac_hw_stop(priv->dev);
+       if (!schedule_work(&priv->reset_work))
+               atomic_dec(&priv->reset_pending);
+       return 0;
+
 }
 
 static int cpmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
@@ -460,38 +560,25 @@ static int cpmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
        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(&priv->lock);
-               dev->stats.tx_dropped++;
-               spin_unlock(&priv->lock);
-               return -ENOMEM;
-       }
+       if (unlikely(atomic_read(&priv->reset_pending)))
+               return NETDEV_TX_BUSY;
+
+       if (unlikely(skb_padto(skb, ETH_ZLEN)))
+               return NETDEV_TX_OK;
 
        len = max(skb->len, ETH_ZLEN);
-       queue = skb->queue_mapping;
-#ifdef CONFIG_NETDEVICES_MULTIQUEUE
+       queue = skb_get_queue_mapping(skb);
        netif_stop_subqueue(dev, queue);
-#else
-       netif_stop_queue(dev);
-#endif
 
        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",
+                       printk(KERN_WARNING "%s: tx dma ring full\n",
                               dev->name);
-               spin_lock(&priv->lock);
-               dev->stats.tx_dropped++;
-               spin_unlock(&priv->lock);
-               dev_kfree_skb_any(skb);
-               return -ENOMEM;
+               return NETDEV_TX_BUSY;
        }
 
        spin_lock(&priv->lock);
-       dev->trans_start = jiffies;
        spin_unlock(&priv->lock);
        desc->dataflags = CPMAC_SOP | CPMAC_EOP | CPMAC_OWN;
        desc->skb = skb;
@@ -509,7 +596,7 @@ static int cpmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
                cpmac_dump_skb(dev, skb);
        cpmac_write(priv->regs, CPMAC_TX_PTR(queue), (u32)desc->mapping);
 
-       return 0;
+       return NETDEV_TX_OK;
 }
 
 static void cpmac_end_xmit(struct net_device *dev, int queue)
@@ -533,24 +620,14 @@ static void cpmac_end_xmit(struct net_device *dev, int queue)
 
                dev_kfree_skb_irq(desc->skb);
                desc->skb = NULL;
-#ifdef CONFIG_NETDEVICES_MULTIQUEUE
-               if (netif_subqueue_stopped(dev, queue))
+               if (__netif_subqueue_stopped(dev, queue))
                        netif_wake_subqueue(dev, queue);
-#else
-               if (netif_queue_stopped(dev))
-                       netif_wake_queue(dev);
-#endif
        } else {
                if (netif_msg_tx_err(priv) && net_ratelimit())
                        printk(KERN_WARNING
                               "%s: end_xmit: spurious interrupt\n", dev->name);
-#ifdef CONFIG_NETDEVICES_MULTIQUEUE
-               if (netif_subqueue_stopped(dev, queue))
+               if (__netif_subqueue_stopped(dev, queue))
                        netif_wake_subqueue(dev, queue);
-#else
-               if (netif_queue_stopped(dev))
-                       netif_wake_queue(dev);
-#endif
        }
 }
 
@@ -636,8 +713,10 @@ static void cpmac_clear_rx(struct net_device *dev)
                        desc->dataflags = CPMAC_OWN;
                        dev->stats.rx_dropped++;
                }
+               desc->hw_next = desc->next->mapping;
                desc = desc->next;
        }
+       priv->rx_head->prev->hw_next = 0;
 }
 
 static void cpmac_clear_tx(struct net_device *dev)
@@ -646,12 +725,13 @@ static void cpmac_clear_tx(struct net_device *dev)
        int i;
        if (unlikely(!priv->desc_ring))
                return;
-       for (i = 0; i < CPMAC_QUEUES; i++)
+       for (i = 0; i < CPMAC_QUEUES; i++) {
+               priv->desc_ring[i].dataflags = 0;
                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);
+                       priv->desc_ring[i].skb = NULL;
                }
+       }
 }
 
 static void cpmac_hw_error(struct work_struct *work)
@@ -664,7 +744,46 @@ static void cpmac_hw_error(struct work_struct *work)
        spin_unlock(&priv->rx_lock);
        cpmac_clear_tx(priv->dev);
        cpmac_hw_start(priv->dev);
-       netif_start_queue(priv->dev);
+       barrier();
+       atomic_dec(&priv->reset_pending);
+
+       netif_tx_wake_all_queues(priv->dev);
+       cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
+}
+
+static void cpmac_check_status(struct net_device *dev)
+{
+       struct cpmac_priv *priv = netdev_priv(dev);
+
+       u32 macstatus = cpmac_read(priv->regs, CPMAC_MAC_STATUS);
+       int rx_channel = (macstatus >> 8) & 7;
+       int rx_code = (macstatus >> 12) & 15;
+       int tx_channel = (macstatus >> 16) & 7;
+       int tx_code = (macstatus >> 20) & 15;
+
+       if (rx_code || tx_code) {
+               if (netif_msg_drv(priv) && net_ratelimit()) {
+                       /* Can't find any documentation on what these
+                        *error codes actually are. So just log them and hope..
+                        */
+                       if (rx_code)
+                               printk(KERN_WARNING "%s: host error %d on rx "
+                                    "channel %d (macstatus %08x), resetting\n",
+                                    dev->name, rx_code, rx_channel, macstatus);
+                       if (tx_code)
+                               printk(KERN_WARNING "%s: host error %d on tx "
+                                    "channel %d (macstatus %08x), resetting\n",
+                                    dev->name, tx_code, tx_channel, macstatus);
+               }
+
+               netif_tx_stop_all_queues(dev);
+               cpmac_hw_stop(dev);
+               if (schedule_work(&priv->reset_work))
+                       atomic_inc(&priv->reset_pending);
+               if (unlikely(netif_msg_hw(priv)))
+                       cpmac_dump_regs(dev);
+       }
+       cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
 }
 
 static irqreturn_t cpmac_irq(int irq, void *dev_id)
@@ -674,9 +793,6 @@ static irqreturn_t cpmac_irq(int irq, void *dev_id)
        int queue;
        u32 status;
 
-       if (!dev)
-               return IRQ_NONE;
-
        priv = netdev_priv(dev);
 
        status = cpmac_read(priv->regs, CPMAC_MAC_INT_VECTOR);
@@ -690,22 +806,16 @@ static irqreturn_t cpmac_irq(int irq, void *dev_id)
 
        if (status & MAC_INT_RX) {
                queue = (status >> 8) & 7;
-               netif_rx_schedule(dev);
-               cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 1 << queue);
+               if (napi_schedule_prep(&priv->napi)) {
+                       cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 1 << queue);
+                       __napi_schedule(&priv->napi);
+               }
        }
 
        cpmac_write(priv->regs, CPMAC_MAC_EOI_VECTOR, 0);
 
-       if (unlikely(status & (MAC_INT_HOST | MAC_INT_STATUS))) {
-               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);
-       }
+       if (unlikely(status & (MAC_INT_HOST | MAC_INT_STATUS)))
+               cpmac_check_status(dev);
 
        return IRQ_HANDLED;
 }
@@ -713,29 +823,20 @@ 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);
-       int i;
 
        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);
-       /* 
-        * 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
+
+       atomic_inc(&priv->reset_pending);
+       barrier();
+       cpmac_clear_tx(dev);
+       barrier();
+       atomic_dec(&priv->reset_pending);
+
+       netif_tx_wake_all_queues(priv->dev);
 }
 
 static int cpmac_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
@@ -794,7 +895,7 @@ static int cpmac_set_ringparam(struct net_device *dev, struct ethtool_ringparam*
 {
        struct cpmac_priv *priv = netdev_priv(dev);
 
-       if (dev->flags && IFF_UP)
+       if (netif_running(dev))
                return -EBUSY;
        priv->ring_size = ring->rx_pending;
        return 0;
@@ -826,7 +927,7 @@ static void cpmac_adjust_link(struct net_device *dev)
 
        spin_lock(&priv->lock);
        if (priv->phy->link) {
-               netif_start_queue(dev);
+               netif_tx_start_all_queues(dev);
                if (priv->phy->duplex != priv->oldduplex) {
                        new_state = 1;
                        priv->oldduplex = priv->phy->duplex;
@@ -840,10 +941,8 @@ static void cpmac_adjust_link(struct net_device *dev)
                if (!priv->oldlink) {
                        new_state = 1;
                        priv->oldlink = 1;
-                       netif_schedule(dev);
                }
        } else if (priv->oldlink) {
-               netif_stop_queue(dev);
                new_state = 1;
                priv->oldlink = 0;
                priv->oldspeed = 0;
@@ -864,15 +963,6 @@ static int cpmac_open(struct net_device *dev)
        struct cpmac_desc *desc;
        struct sk_buff *skb;
 
-       priv->phy = phy_connect(dev, priv->phy_name, &cpmac_adjust_link,
-                               0, PHY_INTERFACE_MODE_MII);
-       if (IS_ERR(priv->phy)) {
-               if (netif_msg_drv(priv))
-                       printk(KERN_ERR "%s: Could not attach to PHY\n",
-                              dev->name);
-               return PTR_ERR(priv->phy);
-       }
-
        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))
@@ -906,12 +996,11 @@ static int cpmac_open(struct net_device *dev)
 
        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);
+               skb = netdev_alloc_skb_ip_align(dev, CPMAC_SKB_SIZE);
                if (unlikely(!skb)) {
                        res = -ENOMEM;
                        goto fail_desc;
                }
-               skb_reserve(skb, 2);
                desc->skb = skb;
                desc->data_mapping = dma_map_single(&dev->dev, skb->data,
                                                    CPMAC_SKB_SIZE,
@@ -920,9 +1009,12 @@ static int cpmac_open(struct net_device *dev)
                desc->buflen = CPMAC_SKB_SIZE;
                desc->dataflags = CPMAC_OWN;
                desc->next = &priv->rx_head[(i + 1) % priv->ring_size];
+               desc->next->prev = desc;
                desc->hw_next = (u32)desc->next->mapping;
        }
 
+       priv->rx_head->prev->hw_next = (u32)0;
+
        if ((res = request_irq(dev->irq, cpmac_irq, IRQF_SHARED,
                               dev->name, dev))) {
                if (netif_msg_drv(priv))
@@ -931,9 +1023,11 @@ static int cpmac_open(struct net_device *dev)
                goto fail_irq;
        }
 
+       atomic_set(&priv->reset_pending, 0);
        INIT_WORK(&priv->reset_work, cpmac_hw_error);
        cpmac_hw_start(dev);
 
+       napi_enable(&priv->napi);
        priv->phy->state = PHY_CHANGELINK;
        phy_start(priv->phy);
 
@@ -958,8 +1052,6 @@ fail_remap:
        release_mem_region(mem->start, mem->end - mem->start);
 
 fail_reserve:
-       phy_disconnect(priv->phy);
-
        return res;
 }
 
@@ -969,12 +1061,11 @@ static int cpmac_stop(struct net_device *dev)
        struct cpmac_priv *priv = netdev_priv(dev);
        struct resource *mem;
 
-       netif_stop_queue(dev);
+       netif_tx_stop_all_queues(dev);
 
        cancel_work_sync(&priv->reset_work);
+       napi_disable(&priv->napi);
        phy_stop(priv->phy);
-       phy_disconnect(priv->phy);
-       priv->phy = NULL;
 
        cpmac_hw_stop(dev);
 
@@ -1004,11 +1095,25 @@ static int cpmac_stop(struct net_device *dev)
        return 0;
 }
 
+static const struct net_device_ops cpmac_netdev_ops = {
+       .ndo_open               = cpmac_open,
+       .ndo_stop               = cpmac_stop,
+       .ndo_start_xmit         = cpmac_start_xmit,
+       .ndo_tx_timeout         = cpmac_tx_timeout,
+       .ndo_set_multicast_list = cpmac_set_multicast_list,
+       .ndo_do_ioctl           = cpmac_ioctl,
+       .ndo_set_config         = cpmac_config,
+       .ndo_change_mtu         = eth_change_mtu,
+       .ndo_validate_addr      = eth_validate_addr,
+       .ndo_set_mac_address    = eth_mac_addr,
+};
+
 static int external_switch;
 
 static int __devinit cpmac_probe(struct platform_device *pdev)
 {
        int rc, phy_id;
+       char mdio_bus_id[MII_BUS_ID_SIZE];
        struct resource *mem;
        struct cpmac_priv *priv;
        struct net_device *dev;
@@ -1016,21 +1121,24 @@ static int __devinit cpmac_probe(struct platform_device *pdev)
 
        pdata = pdev->dev.platform_data;
 
-       for (phy_id = 0; phy_id < PHY_MAX_ADDR; phy_id++) {
-               if (!(pdata->phy_mask & (1 << phy_id)))
-                       continue;
-               if (!cpmac_mii.phy_map[phy_id])
-                       continue;
-               break;
+       if (external_switch || dumb_switch) {
+               strncpy(mdio_bus_id, "0", MII_BUS_ID_SIZE); /* fixed phys bus */
+               phy_id = pdev->id;
+       } else {
+               for (phy_id = 0; phy_id < PHY_MAX_ADDR; phy_id++) {
+                       if (!(pdata->phy_mask & (1 << phy_id)))
+                               continue;
+                       if (!cpmac_mii->phy_map[phy_id])
+                               continue;
+                       strncpy(mdio_bus_id, cpmac_mii->id, MII_BUS_ID_SIZE);
+                       break;
+               }
        }
 
        if (phy_id == PHY_MAX_ADDR) {
-               if (external_switch || dumb_switch)
-                       phy_id = 0;
-               else {
-                       printk(KERN_ERR "cpmac: no PHY present\n");
-                       return -ENODEV;
-               }
+               dev_err(&pdev->dev, "no PHY present, falling back to switch on MDIO bus 0\n");
+               strncpy(mdio_bus_id, "0", MII_BUS_ID_SIZE); /* fixed phys bus */
+               phy_id = pdev->id;
        }
 
        dev = alloc_etherdev_mq(sizeof(*priv), CPMAC_QUEUES);
@@ -1052,29 +1160,29 @@ static int __devinit cpmac_probe(struct platform_device *pdev)
 
        dev->irq = platform_get_irq_byname(pdev, "irq");
 
-       dev->open               = cpmac_open;
-       dev->stop               = cpmac_stop;
-       dev->set_config         = cpmac_config;
-       dev->hard_start_xmit    = cpmac_start_xmit;
-       dev->do_ioctl           = cpmac_ioctl;
-       dev->set_multicast_list = cpmac_set_multicast_list;
-       dev->tx_timeout         = cpmac_tx_timeout;
-       dev->ethtool_ops        = &cpmac_ethtool_ops;
-       dev->poll = cpmac_poll;
-       dev->weight = 64;
-       dev->features |= NETIF_F_MULTI_QUEUE;
+       dev->netdev_ops = &cpmac_netdev_ops;
+       dev->ethtool_ops = &cpmac_ethtool_ops;
+
+       netif_napi_add(dev, &priv->napi, cpmac_poll, 64);
 
        spin_lock_init(&priv->lock);
        spin_lock_init(&priv->rx_lock);
        priv->dev = dev;
        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);
-       } else
-               snprintf(priv->phy_name, BUS_ID_SIZE, "fixed@%d:%d", 100, 1);
+       memcpy(dev->dev_addr, pdata->dev_addr, sizeof(pdata->dev_addr));
+
+       snprintf(priv->phy_name, MII_BUS_ID_SIZE, PHY_ID_FMT, mdio_bus_id, phy_id);
+
+       priv->phy = phy_connect(dev, priv->phy_name, &cpmac_adjust_link, 0,
+                                               PHY_INTERFACE_MODE_MII);
+
+       if (IS_ERR(priv->phy)) {
+               if (netif_msg_drv(priv))
+                       printk(KERN_ERR "%s: Could not attach to PHY\n",
+                              dev->name);
+               return PTR_ERR(priv->phy);
+       }
 
        if ((rc = register_netdev(dev))) {
                printk(KERN_ERR "cpmac: error %i registering device %s\n", rc,
@@ -1084,9 +1192,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: "
-                      MAC_FMT ")\n", dev->name, (void *)mem->start, dev->irq,
-                      priv->phy_name, MAC_ARG(dev->dev_addr));
+                      "cpmac: device %s (regs: %p, irq: %d, phy: %s, "
+                      "mac: %pM)\n", dev->name, (void *)mem->start, dev->irq,
+                      priv->phy_name, dev->dev_addr);
        }
        return 0;
 
@@ -1105,6 +1213,7 @@ static int __devexit cpmac_remove(struct platform_device *pdev)
 
 static struct platform_driver cpmac_driver = {
        .driver.name = "cpmac",
+       .driver.owner = THIS_MODULE,
        .probe = cpmac_probe,
        .remove = __devexit_p(cpmac_remove),
 };
@@ -1114,11 +1223,22 @@ int __devinit cpmac_init(void)
        u32 mask;
        int i, res;
 
-       cpmac_mii.priv = ioremap(AR7_REGS_MDIO, 256);
+       cpmac_mii = mdiobus_alloc();
+       if (cpmac_mii == NULL)
+               return -ENOMEM;
 
-       if (!cpmac_mii.priv) {
+       cpmac_mii->name = "cpmac-mii";
+       cpmac_mii->read = cpmac_mdio_read;
+       cpmac_mii->write = cpmac_mdio_write;
+       cpmac_mii->reset = cpmac_mdio_reset;
+       cpmac_mii->irq = mii_irqs;
+
+       cpmac_mii->priv = ioremap(AR7_REGS_MDIO, 256);
+
+       if (!cpmac_mii->priv) {
                printk(KERN_ERR "Can't ioremap mdio registers\n");
-               return -ENXIO;
+               res = -ENXIO;
+               goto fail_alloc;
        }
 
 #warning FIXME: unhardcode gpio&reset bits
@@ -1128,13 +1248,13 @@ int __devinit cpmac_init(void)
        ar7_device_reset(AR7_RESET_BIT_CPMAC_HI);
        ar7_device_reset(AR7_RESET_BIT_EPHY);
 
-       cpmac_mii.reset(&cpmac_mii);
+       cpmac_mii->reset(cpmac_mii);
 
-       for (i = 0; i < 300000; i++)
-               if ((mask = cpmac_read(cpmac_mii.priv, CPMAC_MDIO_ALIVE)))
+       for (i = 0; i < 300; i++)
+               if ((mask = cpmac_read(cpmac_mii->priv, CPMAC_MDIO_ALIVE)))
                        break;
                else
-                       cpu_relax();
+                       msleep(10);
 
        mask &= 0x7fffffff;
        if (mask & (mask - 1)) {
@@ -1142,9 +1262,10 @@ int __devinit cpmac_init(void)
                mask = 0;
        }
 
-       cpmac_mii.phy_mask = ~(mask | 0x80000000);
+       cpmac_mii->phy_mask = ~(mask | 0x80000000);
+       snprintf(cpmac_mii->id, MII_BUS_ID_SIZE, "1");
 
-       res = mdiobus_register(&cpmac_mii);
+       res = mdiobus_register(cpmac_mii);
        if (res)
                goto fail_mii;
 
@@ -1155,10 +1276,13 @@ int __devinit cpmac_init(void)
        return 0;
 
 fail_cpmac:
-       mdiobus_unregister(&cpmac_mii);
+       mdiobus_unregister(cpmac_mii);
 
 fail_mii:
-       iounmap(cpmac_mii.priv);
+       iounmap(cpmac_mii->priv);
+
+fail_alloc:
+       mdiobus_free(cpmac_mii);
 
        return res;
 }
@@ -1166,8 +1290,9 @@ fail_mii:
 void __devexit cpmac_exit(void)
 {
        platform_driver_unregister(&cpmac_driver);
-       mdiobus_unregister(&cpmac_mii);
-       iounmap(cpmac_mii.priv);
+       mdiobus_unregister(cpmac_mii);
+       iounmap(cpmac_mii->priv);
+       mdiobus_free(cpmac_mii);
 }
 
 module_init(cpmac_init);