net: wireless: sd8797: Marvell sd8797 Wi-Fi driver
[linux-2.6.git] / drivers / net / korina.c
index dced5e7..763844c 100644 (file)
@@ -133,7 +133,9 @@ struct korina_private {
        int dma_halt_cnt;
        int dma_run_cnt;
        struct napi_struct napi;
+       struct timer_list media_check_timer;
        struct mii_if_info mii_if;
+       struct work_struct restart_task;
        struct net_device *dev;
        int phy_addr;
 };
@@ -199,7 +201,7 @@ static int korina_send_packet(struct sk_buff *skb, struct net_device *dev)
        struct korina_private *lp = netdev_priv(dev);
        unsigned long flags;
        u32 length;
-       u32 chain_index;
+       u32 chain_prev, chain_next;
        struct dma_desc *td;
 
        spin_lock_irqsave(&lp->lock, flags);
@@ -231,8 +233,8 @@ static int korina_send_packet(struct sk_buff *skb, struct net_device *dev)
        /* Setup the transmit descriptor. */
        dma_cache_inv((u32) td, sizeof(*td));
        td->ca = CPHYSADDR(skb->data);
-       chain_index = (lp->tx_chain_tail - 1) &
-                       KORINA_TDS_MASK;
+       chain_prev = (lp->tx_chain_tail - 1) & KORINA_TDS_MASK;
+       chain_next = (lp->tx_chain_tail + 1) & KORINA_TDS_MASK;
 
        if (readl(&(lp->tx_dma_regs->dmandptr)) == 0) {
                if (lp->tx_chain_status == desc_empty) {
@@ -240,7 +242,7 @@ static int korina_send_packet(struct sk_buff *skb, struct net_device *dev)
                        td->control = DMA_COUNT(length) |
                                        DMA_DESC_COF | DMA_DESC_IOF;
                        /* Move tail */
-                       lp->tx_chain_tail = chain_index;
+                       lp->tx_chain_tail = chain_next;
                        /* Write to NDPTR */
                        writel(CPHYSADDR(&lp->td_ring[lp->tx_chain_head]),
                                        &lp->tx_dma_regs->dmandptr);
@@ -251,12 +253,12 @@ static int korina_send_packet(struct sk_buff *skb, struct net_device *dev)
                        td->control = DMA_COUNT(length) |
                                        DMA_DESC_COF | DMA_DESC_IOF;
                        /* Link to prev */
-                       lp->td_ring[chain_index].control &=
+                       lp->td_ring[chain_prev].control &=
                                        ~DMA_DESC_COF;
                        /* Link to prev */
-                       lp->td_ring[chain_index].link =  CPHYSADDR(td);
+                       lp->td_ring[chain_prev].link =  CPHYSADDR(td);
                        /* Move tail */
-                       lp->tx_chain_tail = chain_index;
+                       lp->tx_chain_tail = chain_next;
                        /* Write to NDPTR */
                        writel(CPHYSADDR(&lp->td_ring[lp->tx_chain_head]),
                                        &(lp->tx_dma_regs->dmandptr));
@@ -270,17 +272,16 @@ static int korina_send_packet(struct sk_buff *skb, struct net_device *dev)
                        td->control = DMA_COUNT(length) |
                                        DMA_DESC_COF | DMA_DESC_IOF;
                        /* Move tail */
-                       lp->tx_chain_tail = chain_index;
+                       lp->tx_chain_tail = chain_next;
                        lp->tx_chain_status = desc_filled;
-                       netif_stop_queue(dev);
                } else {
                        /* Update tail */
                        td->control = DMA_COUNT(length) |
                                        DMA_DESC_COF | DMA_DESC_IOF;
-                       lp->td_ring[chain_index].control &=
+                       lp->td_ring[chain_prev].control &=
                                        ~DMA_DESC_COF;
-                       lp->td_ring[chain_index].link =  CPHYSADDR(td);
-                       lp->tx_chain_tail = chain_index;
+                       lp->td_ring[chain_prev].link =  CPHYSADDR(td);
+                       lp->tx_chain_tail = chain_next;
                }
        }
        dma_cache_wback((u32) td, sizeof(*td));
@@ -335,10 +336,10 @@ static irqreturn_t korina_rx_dma_interrupt(int irq, void *dev_id)
                                DMA_STAT_HALT | DMA_STAT_ERR),
                                &lp->rx_dma_regs->dmasm);
 
-               netif_rx_schedule(&lp->napi);
+               napi_schedule(&lp->napi);
 
                if (dmas & DMA_STAT_ERR)
-                       printk(KERN_ERR DRV_NAME "%s: DMA error\n", dev->name);
+                       printk(KERN_ERR "%s: DMA error\n", dev->name);
 
                retval = IRQ_HANDLED;
        } else
@@ -375,7 +376,7 @@ static int korina_rx(struct net_device *dev, int limit)
                if (devcs & ETH_RX_LE)
                        dev->stats.rx_length_errors++;
                if (devcs & ETH_RX_OVR)
-                       dev->stats.rx_over_errors++;
+                       dev->stats.rx_fifo_errors++;
                if (devcs & ETH_RX_CV)
                        dev->stats.rx_frame_errors++;
                if (devcs & ETH_RX_CES)
@@ -400,7 +401,7 @@ static int korina_rx(struct net_device *dev, int limit)
                        dma_cache_inv((unsigned long)pkt_buf, pkt_len - 4);
 
                        /* Malloc up new buffer. */
-                       skb_new = netdev_alloc_skb(dev, KORINA_RBSIZE + 2);
+                       skb_new = netdev_alloc_skb_ip_align(dev, KORINA_RBSIZE);
 
                        if (!skb_new)
                                break;
@@ -466,7 +467,7 @@ static int korina_poll(struct napi_struct *napi, int budget)
 
        work_done = korina_rx(dev, budget);
        if (work_done < budget) {
-               netif_rx_complete(napi);
+               napi_complete(napi);
 
                writel(readl(&lp->rx_dma_regs->dmasm) &
                        ~(DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR),
@@ -482,7 +483,7 @@ static void korina_multicast_list(struct net_device *dev)
 {
        struct korina_private *lp = netdev_priv(dev);
        unsigned long flags;
-       struct dev_mc_list *dmi = dev->mc_list;
+       struct netdev_hw_addr *ha;
        u32 recognise = ETH_ARC_AB;     /* always accept broadcasts */
        int i;
 
@@ -490,27 +491,20 @@ static void korina_multicast_list(struct net_device *dev)
        if (dev->flags & IFF_PROMISC)
                recognise |= ETH_ARC_PRO;
 
-       else if ((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 4))
+       else if ((dev->flags & IFF_ALLMULTI) || (netdev_mc_count(dev) > 4))
                /* All multicast and broadcast */
                recognise |= ETH_ARC_AM;
 
        /* Build the hash table */
-       if (dev->mc_count > 4) {
+       if (netdev_mc_count(dev) > 4) {
                u16 hash_table[4];
                u32 crc;
 
                for (i = 0; i < 4; i++)
                        hash_table[i] = 0;
 
-               for (i = 0; i < dev->mc_count; i++) {
-                       char *addrs = dmi->dmi_addr;
-
-                       dmi = dmi->next;
-
-                       if (!(*addrs & 1))
-                               continue;
-
-                       crc = ether_crc_le(6, addrs);
+               netdev_for_each_mc_addr(ha, dev) {
+                       crc = ether_crc_le(6, ha->addr);
                        crc >>= 26;
                        hash_table[crc >> 4] |= 1 << (15 - (crc & 0xf));
                }
@@ -552,7 +546,7 @@ static void korina_tx(struct net_device *dev)
                        dev->stats.tx_dropped++;
 
                        /* Should never happen */
-                       printk(KERN_ERR DRV_NAME "%s: split tx ignored\n",
+                       printk(KERN_ERR "%s: split tx ignored\n",
                                                        dev->name);
                } else if (devcs & ETH_TX_TOK) {
                        dev->stats.tx_packets++;
@@ -638,7 +632,7 @@ korina_tx_dma_interrupt(int irq, void *dev_id)
                        dev->trans_start = jiffies;
                }
                if (dmas & DMA_STAT_ERR)
-                       printk(KERN_ERR DRV_NAME "%s: DMA error\n", dev->name);
+                       printk(KERN_ERR "%s: DMA error\n", dev->name);
 
                retval = IRQ_HANDLED;
        } else
@@ -662,6 +656,15 @@ static void korina_check_media(struct net_device *dev, unsigned int init_media)
                                                &lp->eth_regs->ethmac2);
 }
 
+static void korina_poll_media(unsigned long data)
+{
+       struct net_device *dev = (struct net_device *) data;
+       struct korina_private *lp = netdev_priv(dev);
+
+       korina_check_media(dev, 0);
+       mod_timer(&lp->media_check_timer, jiffies + HZ);
+}
+
 static void korina_set_carrier(struct mii_if_info *mii)
 {
        if (mii->force_media) {
@@ -731,16 +734,17 @@ static u32 netdev_get_link(struct net_device *dev)
        return mii_link_ok(&lp->mii_if);
 }
 
-static struct ethtool_ops netdev_ethtool_ops = {
+static const struct ethtool_ops netdev_ethtool_ops = {
        .get_drvinfo            = netdev_get_drvinfo,
        .get_settings           = netdev_get_settings,
        .set_settings           = netdev_set_settings,
        .get_link               = netdev_get_link,
 };
 
-static void korina_alloc_ring(struct net_device *dev)
+static int korina_alloc_ring(struct net_device *dev)
 {
        struct korina_private *lp = netdev_priv(dev);
+       struct sk_buff *skb;
        int i;
 
        /* Initialize the transmit descriptors */
@@ -756,12 +760,9 @@ static void korina_alloc_ring(struct net_device *dev)
 
        /* Initialize the receive descriptors */
        for (i = 0; i < KORINA_NUM_RDS; i++) {
-               struct sk_buff *skb = lp->rx_skb[i];
-
-               skb = dev_alloc_skb(KORINA_RBSIZE + 2);
+               skb = netdev_alloc_skb_ip_align(dev, KORINA_RBSIZE);
                if (!skb)
-                       break;
-               skb_reserve(skb, 2);
+                       return -ENOMEM;
                lp->rx_skb[i] = skb;
                lp->rd_ring[i].control = DMA_DESC_IOD |
                                DMA_COUNT(KORINA_RBSIZE);
@@ -770,14 +771,17 @@ static void korina_alloc_ring(struct net_device *dev)
                lp->rd_ring[i].link = CPHYSADDR(&lp->rd_ring[i+1]);
        }
 
-       /* loop back */
-       lp->rd_ring[i].link = CPHYSADDR(&lp->rd_ring[0]);
-       lp->rx_next_done  = 0;
+       /* loop back receive descriptors, so the last
+        * descriptor points to the first one */
+       lp->rd_ring[i - 1].link = CPHYSADDR(&lp->rd_ring[0]);
+       lp->rd_ring[i - 1].control |= DMA_DESC_COD;
 
-       lp->rd_ring[i].control |= DMA_DESC_COD;
+       lp->rx_next_done  = 0;
        lp->rx_chain_head = 0;
        lp->rx_chain_tail = 0;
        lp->rx_chain_status = desc_empty;
+
+       return 0;
 }
 
 static void korina_free_ring(struct net_device *dev)
@@ -820,7 +824,11 @@ static int korina_init(struct net_device *dev)
        writel(ETH_INT_FC_EN, &lp->eth_regs->ethintfc);
 
        /* Allocate rings */
-       korina_alloc_ring(dev);
+       if (korina_alloc_ring(dev)) {
+               printk(KERN_ERR "%s: descriptor allocation failed\n", dev->name);
+               korina_free_ring(dev);
+               return -ENOMEM;
+       }
 
        writel(0, &lp->rx_dma_regs->dmas);
        /* Start Rx DMA */
@@ -877,12 +885,12 @@ static int korina_init(struct net_device *dev)
 
 /*
  * Restart the RC32434 ethernet controller.
- * FIXME: check the return status where we call it
  */
-static int korina_restart(struct net_device *dev)
+static void korina_restart_task(struct work_struct *work)
 {
-       struct korina_private *lp = netdev_priv(dev);
-       int ret;
+       struct korina_private *lp = container_of(work,
+                       struct korina_private, restart_task);
+       struct net_device *dev = lp->dev;
 
        /*
         * Disable interrupts
@@ -903,11 +911,9 @@ static int korina_restart(struct net_device *dev)
 
        napi_disable(&lp->napi);
 
-       ret = korina_init(dev);
-       if (ret < 0) {
-               printk(KERN_ERR DRV_NAME "%s: cannot restart device\n",
-                                                               dev->name);
-               return ret;
+       if (korina_init(dev) < 0) {
+               printk(KERN_ERR "%s: cannot restart device\n", dev->name);
+               return;
        }
        korina_multicast_list(dev);
 
@@ -915,8 +921,6 @@ static int korina_restart(struct net_device *dev)
        enable_irq(lp->ovr_irq);
        enable_irq(lp->tx_irq);
        enable_irq(lp->rx_irq);
-
-       return ret;
 }
 
 static void korina_clear_and_restart(struct net_device *dev, u32 value)
@@ -925,7 +929,7 @@ static void korina_clear_and_restart(struct net_device *dev, u32 value)
 
        netif_stop_queue(dev);
        writel(value, &lp->eth_regs->ethintfc);
-       korina_restart(dev);
+       schedule_work(&lp->restart_task);
 }
 
 /* Ethernet Tx Underflow interrupt */
@@ -950,11 +954,8 @@ static irqreturn_t korina_und_interrupt(int irq, void *dev_id)
 static void korina_tx_timeout(struct net_device *dev)
 {
        struct korina_private *lp = netdev_priv(dev);
-       unsigned long flags;
 
-       spin_lock_irqsave(&lp->lock, flags);
-       korina_restart(dev);
-       spin_unlock_irqrestore(&lp->lock, flags);
+       schedule_work(&lp->restart_task);
 }
 
 /* Ethernet Rx Overflow interrupt */
@@ -993,45 +994,46 @@ static int korina_open(struct net_device *dev)
        /* Initialize */
        ret = korina_init(dev);
        if (ret < 0) {
-               printk(KERN_ERR DRV_NAME "%s: cannot open device\n", dev->name);
+               printk(KERN_ERR "%s: cannot open device\n", dev->name);
                goto out;
        }
 
        /* Install the interrupt handler
         * that handles the Done Finished
         * Ovr and Und Events */
-       ret = request_irq(lp->rx_irq, &korina_rx_dma_interrupt,
-               IRQF_SHARED | IRQF_DISABLED, "Korina ethernet Rx", dev);
+       ret = request_irq(lp->rx_irq, korina_rx_dma_interrupt,
+                       IRQF_DISABLED, "Korina ethernet Rx", dev);
        if (ret < 0) {
-               printk(KERN_ERR DRV_NAME "%s: unable to get Rx DMA IRQ %d\n",
+               printk(KERN_ERR "%s: unable to get Rx DMA IRQ %d\n",
                    dev->name, lp->rx_irq);
                goto err_release;
        }
-       ret = request_irq(lp->tx_irq, &korina_tx_dma_interrupt,
-               IRQF_SHARED | IRQF_DISABLED, "Korina ethernet Tx", dev);
+       ret = request_irq(lp->tx_irq, korina_tx_dma_interrupt,
+                       IRQF_DISABLED, "Korina ethernet Tx", dev);
        if (ret < 0) {
-               printk(KERN_ERR DRV_NAME "%s: unable to get Tx DMA IRQ %d\n",
+               printk(KERN_ERR "%s: unable to get Tx DMA IRQ %d\n",
                    dev->name, lp->tx_irq);
                goto err_free_rx_irq;
        }
 
        /* Install handler for overrun error. */
-       ret = request_irq(lp->ovr_irq, &korina_ovr_interrupt,
-                       IRQF_SHARED | IRQF_DISABLED, "Ethernet Overflow", dev);
+       ret = request_irq(lp->ovr_irq, korina_ovr_interrupt,
+                       IRQF_DISABLED, "Ethernet Overflow", dev);
        if (ret < 0) {
-               printk(KERN_ERR DRV_NAME"%s: unable to get OVR IRQ %d\n",
+               printk(KERN_ERR "%s: unable to get OVR IRQ %d\n",
                    dev->name, lp->ovr_irq);
                goto err_free_tx_irq;
        }
 
        /* Install handler for underflow error. */
-       ret = request_irq(lp->und_irq, &korina_und_interrupt,
-                       IRQF_SHARED | IRQF_DISABLED, "Ethernet Underflow", dev);
+       ret = request_irq(lp->und_irq, korina_und_interrupt,
+                       IRQF_DISABLED, "Ethernet Underflow", dev);
        if (ret < 0) {
-               printk(KERN_ERR DRV_NAME "%s: unable to get UND IRQ %d\n",
+               printk(KERN_ERR "%s: unable to get UND IRQ %d\n",
                    dev->name, lp->und_irq);
                goto err_free_ovr_irq;
        }
+       mod_timer(&lp->media_check_timer, jiffies + 1);
 out:
        return ret;
 
@@ -1051,6 +1053,8 @@ static int korina_close(struct net_device *dev)
        struct korina_private *lp = netdev_priv(dev);
        u32 tmp;
 
+       del_timer(&lp->media_check_timer);
+
        /* Disable interrupts */
        disable_irq(lp->rx_irq);
        disable_irq(lp->tx_irq);
@@ -1071,6 +1075,8 @@ static int korina_close(struct net_device *dev)
 
        napi_disable(&lp->napi);
 
+       cancel_work_sync(&lp->restart_task);
+
        free_irq(lp->rx_irq, dev);
        free_irq(lp->tx_irq, dev);
        free_irq(lp->ovr_irq, dev);
@@ -1079,6 +1085,21 @@ static int korina_close(struct net_device *dev)
        return 0;
 }
 
+static const struct net_device_ops korina_netdev_ops = {
+       .ndo_open               = korina_open,
+       .ndo_stop               = korina_close,
+       .ndo_start_xmit         = korina_send_packet,
+       .ndo_set_multicast_list = korina_multicast_list,
+       .ndo_tx_timeout         = korina_tx_timeout,
+       .ndo_do_ioctl           = korina_ioctl,
+       .ndo_change_mtu         = eth_change_mtu,
+       .ndo_validate_addr      = eth_validate_addr,
+       .ndo_set_mac_address    = eth_mac_addr,
+#ifdef CONFIG_NET_POLL_CONTROLLER
+       .ndo_poll_controller    = korina_poll_controller,
+#endif
+};
+
 static int korina_probe(struct platform_device *pdev)
 {
        struct korina_device *bif = platform_get_drvdata(pdev);
@@ -1105,32 +1126,32 @@ static int korina_probe(struct platform_device *pdev)
 
        r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_regs");
        dev->base_addr = r->start;
-       lp->eth_regs = ioremap_nocache(r->start, r->end - r->start);
+       lp->eth_regs = ioremap_nocache(r->start, resource_size(r));
        if (!lp->eth_regs) {
-               printk(KERN_ERR DRV_NAME "cannot remap registers\n");
+               printk(KERN_ERR DRV_NAME ": cannot remap registers\n");
                rc = -ENXIO;
                goto probe_err_out;
        }
 
        r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_dma_rx");
-       lp->rx_dma_regs = ioremap_nocache(r->start, r->end - r->start);
+       lp->rx_dma_regs = ioremap_nocache(r->start, resource_size(r));
        if (!lp->rx_dma_regs) {
-               printk(KERN_ERR DRV_NAME "cannot remap Rx DMA registers\n");
+               printk(KERN_ERR DRV_NAME ": cannot remap Rx DMA registers\n");
                rc = -ENXIO;
                goto probe_err_dma_rx;
        }
 
        r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_dma_tx");
-       lp->tx_dma_regs = ioremap_nocache(r->start, r->end - r->start);
+       lp->tx_dma_regs = ioremap_nocache(r->start, resource_size(r));
        if (!lp->tx_dma_regs) {
-               printk(KERN_ERR DRV_NAME "cannot remap Tx DMA registers\n");
+               printk(KERN_ERR DRV_NAME ": cannot remap Tx DMA registers\n");
                rc = -ENXIO;
                goto probe_err_dma_tx;
        }
 
        lp->td_ring = kmalloc(TD_RING_SIZE + RD_RING_SIZE, GFP_KERNEL);
        if (!lp->td_ring) {
-               printk(KERN_ERR DRV_NAME "cannot allocate descriptors\n");
+               printk(KERN_ERR DRV_NAME ": cannot allocate descriptors\n");
                rc = -ENXIO;
                goto probe_err_td_ring;
        }
@@ -1147,17 +1168,9 @@ static int korina_probe(struct platform_device *pdev)
        dev->irq = lp->rx_irq;
        lp->dev = dev;
 
-       dev->open = korina_open;
-       dev->stop = korina_close;
-       dev->hard_start_xmit = korina_send_packet;
-       dev->set_multicast_list = &korina_multicast_list;
+       dev->netdev_ops = &korina_netdev_ops;
        dev->ethtool_ops = &netdev_ethtool_ops;
-       dev->tx_timeout = korina_tx_timeout;
        dev->watchdog_timeo = TX_TIMEOUT;
-       dev->do_ioctl = &korina_ioctl;
-#ifdef CONFIG_NET_POLL_CONTROLLER
-       dev->poll_controller = korina_poll_controller;
-#endif
        netif_napi_add(dev, &lp->napi, korina_poll, 64);
 
        lp->phy_addr = (((lp->rx_irq == 0x2c? 1:0) << 8) | 0x05);
@@ -1171,9 +1184,15 @@ static int korina_probe(struct platform_device *pdev)
        rc = register_netdev(dev);
        if (rc < 0) {
                printk(KERN_ERR DRV_NAME
-                       ": cannot register net device %d\n", rc);
+                       ": cannot register net device: %d\n", rc);
                goto probe_err_register;
        }
+       setup_timer(&lp->media_check_timer, korina_poll_media, (unsigned long) dev);
+
+       INIT_WORK(&lp->restart_task, korina_restart_task);
+
+       printk(KERN_INFO "%s: " DRV_NAME "-" DRV_VERSION " " DRV_RELDATE "\n",
+                       dev->name);
 out:
        return rc;