add a fix for memleaks caused by failed memory allocations
[openwrt.git] / target / linux / ar7 / files / drivers / net / cpmac.c
index 1134a03..4b6bfe1 100644 (file)
 #include <linux/platform_device.h>
 #include <linux/dma-mapping.h>
 #include <asm/gpio.h>
+#include <asm/atomic.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
@@ -190,31 +192,33 @@ 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;
 };
 
 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;
+       atomic_t reset_pending;
 };
 
 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);
 
@@ -243,6 +247,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;
@@ -313,18 +327,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;
@@ -377,7 +379,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 +392,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);
@@ -415,9 +415,8 @@ static struct sk_buff *cpmac_rx_one(struct net_device *dev,
                if (netif_msg_rx_err(priv) && net_ratelimit())
                        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,50 +424,43 @@ 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;
-       struct cpmac_desc *desc;
-       int received = 0, quota = min(dev->quota, *budget);
+       struct cpmac_desc *desc, *restart;
+       int received = 0, processed = 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",
                               dev->name);
+               spin_unlock(&priv->rx_lock);
                netif_rx_complete(dev);
                return 0;
        }
 
        desc = priv->rx_head;
-
+       restart = NULL;
        while ((received < quota) && ((desc->dataflags & CPMAC_OWN) == 0)) {
+               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",
+                                              dev->name, restart, desc);
+                               goto fatal_error;
+                       }
+
+                       restart = desc->next;
+               }
+                       
                skb = cpmac_rx_one(dev, priv, desc);
                if (likely(skb)) {
                        netif_receive_skb(skb);
@@ -476,60 +468,116 @@ 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) {
+               dev->stats.rx_errors++;
+               dev->stats.rx_fifo_errors++;
+               if (netif_msg_rx_err(priv) && net_ratelimit())
+                       printk(KERN_WARNING "%s: rx dma ring overrun\n", 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",
+                                      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) {
+       
+       if (processed == 0) {
+               /* we ran out of packets to read, revert to interrupt-driven mode */
                netif_rx_complete(dev);
-               cpmac_write(priv->regs, CPMAC_RX_PTR(0), (u32)desc->mapping);
                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", dev->name);
+               cpmac_dump_all_desc(dev);
+               printk(KERN_DEBUG "%s: RX_PTR(0)=0x%08x RX_ACK(0)=0x%08x\n",
+                      dev->name,
+                      cpmac_read(priv->regs, CPMAC_RX_PTR(0)),
+                      cpmac_read(priv->regs, CPMAC_RX_ACK(0)));
+       }
+
+       spin_unlock(&priv->rx_lock);
+       netif_rx_complete(dev);
+       netif_stop_queue(dev);
+
+       atomic_inc(&priv->reset_pending);
+       cpmac_hw_stop(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)
 {
-       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);
-               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);
-       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",
+                       printk(KERN_WARNING "%s: tx dma ring full\n",
                               dev->name);
-               priv->stats.tx_dropped++;
-               spin_unlock_irqrestore(&priv->lock, flags);
-               dev_kfree_skb_any(skb);
-               return -ENOMEM;
+               return NETDEV_TX_BUSY;
        }
 
+       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 +592,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;
+       return NETDEV_TX_OK;
 }
 
-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 +617,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 +654,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 +719,87 @@ 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->hw_next = desc->next->mapping;
                desc = desc->next;
        }
+
+       priv->rx_head->prev->hw_next = 0;
+}
+
+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++) {
+               priv->desc_ring[i].dataflags = 0;
+               if (priv->desc_ring[i].skb) {
+                       dev_kfree_skb_any(priv->desc_ring[i].skb);
+                       priv->desc_ring[i].skb = NULL;
+               }
+       }
+}
+
+static void cpmac_hw_error(struct work_struct *work)
+{
+       int i;
+       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);
+       barrier();
+       atomic_dec(&priv->reset_pending);
+       
+       for (i = 0; i < CPMAC_QUEUES; i++) {
+               netif_wake_subqueue(priv->dev, i);
+       }
+       netif_wake_queue(priv->dev);
+}
+
+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_stop_queue(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);
+       }
 }
 
 static irqreturn_t cpmac_irq(int irq, void *dev_id)
 {
        struct net_device *dev = dev_id;
        struct cpmac_priv *priv;
+       int queue;
        u32 status;
 
        if (!dev)
@@ -642,46 +817,40 @@ 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);
 
-       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);
-               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);
-       }
+       if (unlikely(status & (MAC_INT_HOST | MAC_INT_STATUS)))
+               cpmac_check_status(dev);
 
        return IRQ_HANDLED;
 }
 
 static void cpmac_tx_timeout(struct net_device *dev)
 {
+       int i;
        struct cpmac_priv *priv = netdev_priv(dev);
-       struct cpmac_desc *desc;
 
-       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);
-       netif_wake_queue(dev);
+
+       atomic_inc(&priv->reset_pending);
+       barrier();
+       cpmac_clear_tx(dev);
+       barrier();
+       atomic_dec(&priv->reset_pending);
+
+       netif_wake_queue(priv->dev);
+       for (i = 0; i < CPMAC_QUEUES; i++) {
+               netif_wake_subqueue(dev, i);
+       }
 }
 
 static int cpmac_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
@@ -695,7 +864,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 +890,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 (netif_running(dev))
+               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 +930,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 +958,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 +968,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 +988,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 +997,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 +1006,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 +1016,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,10 +1034,13 @@ 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->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))
@@ -918,21 +1049,24 @@ static int cpmac_open(struct net_device *dev)
                goto fail_irq;
        }
 
-       cpmac_reset(dev);
-       cpmac_hw_init(dev);
+       atomic_set(&priv->reset_pending, 0);
+       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 +1074,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 +1086,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 +1103,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 +1127,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 +1152,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 +1176,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->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;
+       dev->weight = max(4, priv->ring_size/4);
+       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 +1203,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 +1225,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)
This page took 0.071879 seconds and 4 git commands to generate.