enic: Bug fix: try harder to fill Rx ring on skb allocation failures
[safe/jmp/linux-2.6] / drivers / net / enic / enic_main.c
index e01067e..452a6b7 100644 (file)
@@ -362,7 +362,7 @@ static void enic_mtu_check(struct enic *enic)
 {
        u32 mtu = vnic_dev_mtu(enic->vdev);
 
-       if (mtu != enic->port_mtu) {
+       if (mtu && mtu != enic->port_mtu) {
                if (mtu < enic->netdev->mtu)
                        printk(KERN_WARNING PFX
                                "%s: interface MTU (%d) set higher "
@@ -870,19 +870,6 @@ static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
        dev_kfree_skb_any(buf->os_buf);
 }
 
-static inline struct sk_buff *enic_rq_alloc_skb(struct net_device *netdev,
-       unsigned int size)
-{
-       struct sk_buff *skb;
-
-       skb = netdev_alloc_skb(netdev, size + NET_IP_ALIGN);
-
-       if (skb)
-               skb_reserve(skb, NET_IP_ALIGN);
-
-       return skb;
-}
-
 static int enic_rq_alloc_buf(struct vnic_rq *rq)
 {
        struct enic *enic = vnic_dev_priv(rq->vdev);
@@ -892,7 +879,7 @@ static int enic_rq_alloc_buf(struct vnic_rq *rq)
        unsigned int os_buf_index = 0;
        dma_addr_t dma_addr;
 
-       skb = enic_rq_alloc_skb(netdev, len);
+       skb = netdev_alloc_skb_ip_align(netdev, len);
        if (!skb)
                return -ENOMEM;
 
@@ -1097,34 +1084,6 @@ static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
        return 0;
 }
 
-static void enic_rq_drop_buf(struct vnic_rq *rq,
-       struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
-       int skipped, void *opaque)
-{
-       struct enic *enic = vnic_dev_priv(rq->vdev);
-       struct sk_buff *skb = buf->os_buf;
-
-       if (skipped)
-               return;
-
-       pci_unmap_single(enic->pdev, buf->dma_addr,
-               buf->len, PCI_DMA_FROMDEVICE);
-
-       dev_kfree_skb_any(skb);
-}
-
-static int enic_rq_service_drop(struct vnic_dev *vdev, struct cq_desc *cq_desc,
-       u8 type, u16 q_number, u16 completed_index, void *opaque)
-{
-       struct enic *enic = vnic_dev_priv(vdev);
-
-       vnic_rq_service(&enic->rq[q_number], cq_desc,
-               completed_index, VNIC_RQ_RETURN_DESC,
-               enic_rq_drop_buf, opaque);
-
-       return 0;
-}
-
 static int enic_poll(struct napi_struct *napi, int budget)
 {
        struct enic *enic = container_of(napi, struct enic, napi);
@@ -1132,6 +1091,7 @@ static int enic_poll(struct napi_struct *napi, int budget)
        unsigned int rq_work_to_do = budget;
        unsigned int wq_work_to_do = -1; /* no limit */
        unsigned int  work_done, rq_work_done, wq_work_done;
+       int err;
 
        /* Service RQ (first) and WQ
         */
@@ -1155,16 +1115,19 @@ static int enic_poll(struct napi_struct *napi, int budget)
                        0 /* don't unmask intr */,
                        0 /* don't reset intr timer */);
 
-       if (rq_work_done > 0) {
+       err = vnic_rq_fill(&enic->rq[0], enic->rq_alloc_buf);
 
-               /* Replenish RQ
-                */
+       /* Buffer allocation failed. Stay in polling
+        * mode so we can try to fill the ring again.
+        */
 
-               vnic_rq_fill(&enic->rq[0], enic->rq_alloc_buf);
+       if (err)
+               rq_work_done = rq_work_to_do;
 
-       } else {
+       if (rq_work_done < rq_work_to_do) {
 
-               /* If no work done, flush all LROs and exit polling
+               /* Some work done, but not enough to stay in polling,
+                * flush all LROs and exit polling
                 */
 
                if (netdev->features & NETIF_F_LRO)
@@ -1183,6 +1146,7 @@ static int enic_poll_msix(struct napi_struct *napi, int budget)
        struct net_device *netdev = enic->netdev;
        unsigned int work_to_do = budget;
        unsigned int work_done;
+       int err;
 
        /* Service RQ
         */
@@ -1190,25 +1154,30 @@ static int enic_poll_msix(struct napi_struct *napi, int budget)
        work_done = vnic_cq_service(&enic->cq[ENIC_CQ_RQ],
                work_to_do, enic_rq_service, NULL);
 
-       if (work_done > 0) {
-
-               /* Replenish RQ
-                */
-
-               vnic_rq_fill(&enic->rq[0], enic->rq_alloc_buf);
-
-               /* Return intr event credits for this polling
-                * cycle.  An intr event is the completion of a
-                * RQ packet.
-                */
+       /* Return intr event credits for this polling
+        * cycle.  An intr event is the completion of a
+        * RQ packet.
+        */
 
+       if (work_done > 0)
                vnic_intr_return_credits(&enic->intr[ENIC_MSIX_RQ],
                        work_done,
                        0 /* don't unmask intr */,
                        0 /* don't reset intr timer */);
-       } else {
 
-               /* If no work done, flush all LROs and exit polling
+       err = vnic_rq_fill(&enic->rq[0], enic->rq_alloc_buf);
+
+       /* Buffer allocation failed. Stay in polling mode
+        * so we can try to fill the ring again.
+        */
+
+       if (err)
+               work_done = work_to_do;
+
+       if (work_done < work_to_do) {
+
+               /* Some work done, but not enough to stay in polling,
+                * flush all LROs and exit polling
                 */
 
                if (netdev->features & NETIF_F_LRO)
@@ -1317,6 +1286,24 @@ static int enic_request_intr(struct enic *enic)
        return err;
 }
 
+static void enic_synchronize_irqs(struct enic *enic)
+{
+       unsigned int i;
+
+       switch (vnic_dev_get_intr_mode(enic->vdev)) {
+       case VNIC_DEV_INTR_MODE_INTX:
+       case VNIC_DEV_INTR_MODE_MSI:
+               synchronize_irq(enic->pdev->irq);
+               break;
+       case VNIC_DEV_INTR_MODE_MSIX:
+               for (i = 0; i < enic->intr_count; i++)
+                       synchronize_irq(enic->msix_entry[i].vector);
+               break;
+       default:
+               break;
+       }
+}
+
 static int enic_notify_set(struct enic *enic)
 {
        int err;
@@ -1373,11 +1360,13 @@ static int enic_open(struct net_device *netdev)
        }
 
        for (i = 0; i < enic->rq_count; i++) {
-               err = vnic_rq_fill(&enic->rq[i], enic->rq_alloc_buf);
-               if (err) {
+               vnic_rq_fill(&enic->rq[i], enic->rq_alloc_buf);
+               /* Need at least one buffer on ring to get going */
+               if (vnic_rq_desc_used(&enic->rq[i]) == 0) {
                        printk(KERN_ERR PFX
                                "%s: Unable to alloc receive buffers.\n",
                                netdev->name);
+                       err = -ENOMEM;
                        goto err_out_notify_unset;
                }
        }
@@ -1422,16 +1411,19 @@ static int enic_stop(struct net_device *netdev)
        unsigned int i;
        int err;
 
+       for (i = 0; i < enic->intr_count; i++)
+               vnic_intr_mask(&enic->intr[i]);
+
+       enic_synchronize_irqs(enic);
+
        del_timer_sync(&enic->notify_timer);
 
        spin_lock(&enic->devcmd_lock);
        vnic_dev_disable(enic->vdev);
        spin_unlock(&enic->devcmd_lock);
        napi_disable(&enic->napi);
-       netif_stop_queue(netdev);
-
-       for (i = 0; i < enic->intr_count; i++)
-               vnic_intr_mask(&enic->intr[i]);
+       netif_carrier_off(netdev);
+       netif_tx_disable(netdev);
 
        for (i = 0; i < enic->wq_count; i++) {
                err = vnic_wq_disable(&enic->wq[i]);
@@ -1449,11 +1441,6 @@ static int enic_stop(struct net_device *netdev)
        spin_unlock(&enic->devcmd_lock);
        enic_free_intr(enic);
 
-       (void)vnic_cq_service(&enic->cq[ENIC_CQ_RQ],
-               -1, enic_rq_service_drop, NULL);
-       (void)vnic_cq_service(&enic->cq[ENIC_CQ_WQ],
-               -1, enic_wq_service, NULL);
-
        for (i = 0; i < enic->wq_count; i++)
                vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
        for (i = 0; i < enic->rq_count; i++)
@@ -1741,6 +1728,88 @@ static const struct net_device_ops enic_netdev_ops = {
 #endif
 };
 
+void enic_dev_deinit(struct enic *enic)
+{
+       netif_napi_del(&enic->napi);
+       enic_free_vnic_resources(enic);
+       enic_clear_intr_mode(enic);
+}
+
+int enic_dev_init(struct enic *enic)
+{
+       struct net_device *netdev = enic->netdev;
+       int err;
+
+       /* Get vNIC configuration
+        */
+
+       err = enic_get_vnic_config(enic);
+       if (err) {
+               printk(KERN_ERR PFX
+                       "Get vNIC configuration failed, aborting.\n");
+               return err;
+       }
+
+       /* Get available resource counts
+        */
+
+       enic_get_res_counts(enic);
+
+       /* Set interrupt mode based on resource counts and system
+        * capabilities
+        */
+
+       err = enic_set_intr_mode(enic);
+       if (err) {
+               printk(KERN_ERR PFX
+                       "Failed to set intr mode, aborting.\n");
+               return err;
+       }
+
+       /* Allocate and configure vNIC resources
+        */
+
+       err = enic_alloc_vnic_resources(enic);
+       if (err) {
+               printk(KERN_ERR PFX
+                       "Failed to alloc vNIC resources, aborting.\n");
+               goto err_out_free_vnic_resources;
+       }
+
+       enic_init_vnic_resources(enic);
+
+       err = enic_set_rq_alloc_buf(enic);
+       if (err) {
+               printk(KERN_ERR PFX
+                       "Failed to set RQ buffer allocator, aborting.\n");
+               goto err_out_free_vnic_resources;
+       }
+
+       err = enic_set_niccfg(enic);
+       if (err) {
+               printk(KERN_ERR PFX
+                       "Failed to config nic, aborting.\n");
+               goto err_out_free_vnic_resources;
+       }
+
+       switch (vnic_dev_get_intr_mode(enic->vdev)) {
+       default:
+               netif_napi_add(netdev, &enic->napi, enic_poll, 64);
+               break;
+       case VNIC_DEV_INTR_MODE_MSIX:
+               netif_napi_add(netdev, &enic->napi, enic_poll_msix, 64);
+               break;
+       }
+
+       return 0;
+
+err_out_free_vnic_resources:
+       enic_clear_intr_mode(enic);
+       enic_free_vnic_resources(enic);
+
+       return err;
+}
+
 static void enic_iounmap(struct enic *enic)
 {
        unsigned int i;
@@ -1883,51 +1952,13 @@ static int __devinit enic_probe(struct pci_dev *pdev,
                goto err_out_dev_close;
        }
 
-       /* Get vNIC configuration
-        */
-
-       err = enic_get_vnic_config(enic);
-       if (err) {
-               printk(KERN_ERR PFX
-                       "Get vNIC configuration failed, aborting.\n");
-               goto err_out_dev_close;
-       }
-
-       /* Get available resource counts
-        */
-
-       enic_get_res_counts(enic);
-
-       /* Set interrupt mode based on resource counts and system
-        * capabilities
-        */
-
-       err = enic_set_intr_mode(enic);
+       err = enic_dev_init(enic);
        if (err) {
                printk(KERN_ERR PFX
-                       "Failed to set intr mode, aborting.\n");
+                       "Device initialization failed, aborting.\n");
                goto err_out_dev_close;
        }
 
-       /* Allocate and configure vNIC resources
-        */
-
-       err = enic_alloc_vnic_resources(enic);
-       if (err) {
-               printk(KERN_ERR PFX
-                       "Failed to alloc vNIC resources, aborting.\n");
-               goto err_out_free_vnic_resources;
-       }
-
-       enic_init_vnic_resources(enic);
-
-       err = enic_set_niccfg(enic);
-       if (err) {
-               printk(KERN_ERR PFX
-                       "Failed to config nic, aborting.\n");
-               goto err_out_free_vnic_resources;
-       }
-
        /* Setup notification timer, HW reset task, and locks
         */
 
@@ -1952,23 +1983,15 @@ static int __devinit enic_probe(struct pci_dev *pdev,
        if (err) {
                printk(KERN_ERR PFX
                        "Invalid MAC address, aborting.\n");
-               goto err_out_free_vnic_resources;
+               goto err_out_dev_deinit;
        }
 
        netdev->netdev_ops = &enic_netdev_ops;
        netdev->watchdog_timeo = 2 * HZ;
        netdev->ethtool_ops = &enic_ethtool_ops;
 
-       switch (vnic_dev_get_intr_mode(enic->vdev)) {
-       default:
-               netif_napi_add(netdev, &enic->napi, enic_poll, 64);
-               break;
-       case VNIC_DEV_INTR_MODE_MSIX:
-               netif_napi_add(netdev, &enic->napi, enic_poll_msix, 64);
-               break;
-       }
-
-       netdev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
+       netdev->features |= NETIF_F_HW_VLAN_TX |
+               NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER;
        if (ENIC_SETTING(enic, TXCSUM))
                netdev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
        if (ENIC_SETTING(enic, TSO))
@@ -1994,17 +2017,16 @@ static int __devinit enic_probe(struct pci_dev *pdev,
        if (err) {
                printk(KERN_ERR PFX
                        "Cannot register net device, aborting.\n");
-               goto err_out_free_vnic_resources;
+               goto err_out_dev_deinit;
        }
 
        return 0;
 
-err_out_free_vnic_resources:
-       enic_free_vnic_resources(enic);
+err_out_dev_deinit:
+       enic_dev_deinit(enic);
 err_out_dev_close:
        vnic_dev_close(enic->vdev);
 err_out_vnic_unregister:
-       enic_clear_intr_mode(enic);
        vnic_dev_unregister(enic->vdev);
 err_out_iounmap:
        enic_iounmap(enic);
@@ -2028,9 +2050,8 @@ static void __devexit enic_remove(struct pci_dev *pdev)
 
                flush_scheduled_work();
                unregister_netdev(netdev);
-               enic_free_vnic_resources(enic);
+               enic_dev_deinit(enic);
                vnic_dev_close(enic->vdev);
-               enic_clear_intr_mode(enic);
                vnic_dev_unregister(enic->vdev);
                enic_iounmap(enic);
                pci_release_regions(pdev);