Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / drivers / infiniband / ulp / ipoib / ipoib_ib.c
index 8238766..8406839 100644 (file)
@@ -1,5 +1,8 @@
 /*
  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
+ * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
+ * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
+ * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
  *
  * This software is available to you under a choice of one of two
  * licenses.  You may choose to be licensed under the terms of the GNU
@@ -35,7 +38,7 @@
 #include <linux/delay.h>
 #include <linux/dma-mapping.h>
 
-#include <ib_cache.h>
+#include <rdma/ib_cache.h>
 
 #include "ipoib.h"
 
@@ -49,7 +52,7 @@ MODULE_PARM_DESC(data_debug_level,
 
 #define        IPOIB_OP_RECV   (1ul << 31)
 
-static DECLARE_MUTEX(pkey_sem);
+static DEFINE_MUTEX(pkey_mutex);
 
 struct ipoib_ah *ipoib_create_ah(struct net_device *dev,
                                 struct ib_pd *pd, struct ib_ah_attr *attr)
@@ -81,7 +84,7 @@ void ipoib_free_ah(struct kref *kref)
 
        unsigned long flags;
 
-       if (ah->last_send <= priv->tx_tail) {
+       if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
                ipoib_dbg(priv, "Freeing ah %p\n", ah->ah);
                ib_destroy_ah(ah->ah);
                kfree(ah);
@@ -92,57 +95,65 @@ void ipoib_free_ah(struct kref *kref)
        }
 }
 
-static inline int ipoib_ib_receive(struct ipoib_dev_priv *priv,
-                                  unsigned int wr_id,
-                                  dma_addr_t addr)
+static int ipoib_ib_post_receive(struct net_device *dev, int id)
 {
-       struct ib_sge list = {
-               .addr    = addr,
-               .length  = IPOIB_BUF_SIZE,
-               .lkey    = priv->mr->lkey,
-       };
-       struct ib_recv_wr param = {
-               .wr_id      = wr_id | IPOIB_OP_RECV,
-               .sg_list    = &list,
-               .num_sge    = 1,
-       };
+       struct ipoib_dev_priv *priv = netdev_priv(dev);
+       struct ib_sge list;
+       struct ib_recv_wr param;
        struct ib_recv_wr *bad_wr;
+       int ret;
+
+       list.addr     = priv->rx_ring[id].mapping;
+       list.length   = IPOIB_BUF_SIZE;
+       list.lkey     = priv->mr->lkey;
+
+       param.next    = NULL;
+       param.wr_id   = id | IPOIB_OP_RECV;
+       param.sg_list = &list;
+       param.num_sge = 1;
+
+       ret = ib_post_recv(priv->qp, &param, &bad_wr);
+       if (unlikely(ret)) {
+               ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret);
+               dma_unmap_single(priv->ca->dma_device,
+                                priv->rx_ring[id].mapping,
+                                IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
+               dev_kfree_skb_any(priv->rx_ring[id].skb);
+               priv->rx_ring[id].skb = NULL;
+       }
 
-       return ib_post_recv(priv->qp, &param, &bad_wr);
+       return ret;
 }
 
-static int ipoib_ib_post_receive(struct net_device *dev, int id)
+static int ipoib_alloc_rx_skb(struct net_device *dev, int id)
 {
        struct ipoib_dev_priv *priv = netdev_priv(dev);
        struct sk_buff *skb;
        dma_addr_t addr;
-       int ret;
 
        skb = dev_alloc_skb(IPOIB_BUF_SIZE + 4);
-       if (!skb) {
-               ipoib_warn(priv, "failed to allocate receive buffer\n");
-
-               priv->rx_ring[id].skb = NULL;
+       if (!skb)
                return -ENOMEM;
-       }
-       skb_reserve(skb, 4);    /* 16 byte align IP header */
-       priv->rx_ring[id].skb = skb;
+
+       /*
+        * IB will leave a 40 byte gap for a GRH and IPoIB adds a 4 byte
+        * header.  So we need 4 more bytes to get to 48 and align the
+        * IP header to a multiple of 16.
+        */
+       skb_reserve(skb, 4);
+
        addr = dma_map_single(priv->ca->dma_device,
                              skb->data, IPOIB_BUF_SIZE,
                              DMA_FROM_DEVICE);
-       pci_unmap_addr_set(&priv->rx_ring[id], mapping, addr);
-
-       ret = ipoib_ib_receive(priv, id, addr);
-       if (ret) {
-               ipoib_warn(priv, "ipoib_ib_receive failed for buf %d (%d)\n",
-                          id, ret);
-               dma_unmap_single(priv->ca->dma_device, addr,
-                                IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
+       if (unlikely(dma_mapping_error(addr))) {
                dev_kfree_skb_any(skb);
-               priv->rx_ring[id].skb = NULL;
+               return -EIO;
        }
 
-       return ret;
+       priv->rx_ring[id].skb     = skb;
+       priv->rx_ring[id].mapping = addr;
+
+       return 0;
 }
 
 static int ipoib_ib_post_receives(struct net_device *dev)
@@ -150,7 +161,11 @@ static int ipoib_ib_post_receives(struct net_device *dev)
        struct ipoib_dev_priv *priv = netdev_priv(dev);
        int i;
 
-       for (i = 0; i < IPOIB_RX_RING_SIZE; ++i) {
+       for (i = 0; i < ipoib_recvq_size; ++i) {
+               if (ipoib_alloc_rx_skb(dev, i)) {
+                       ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
+                       return -ENOMEM;
+               }
                if (ipoib_ib_post_receive(dev, i)) {
                        ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
                        return -EIO;
@@ -172,29 +187,37 @@ static void ipoib_ib_handle_wc(struct net_device *dev,
        if (wr_id & IPOIB_OP_RECV) {
                wr_id &= ~IPOIB_OP_RECV;
 
-               if (wr_id < IPOIB_RX_RING_SIZE) {
-                       struct sk_buff *skb = priv->rx_ring[wr_id].skb;
-
-                       priv->rx_ring[wr_id].skb = NULL;
-
-                       dma_unmap_single(priv->ca->dma_device,
-                                        pci_unmap_addr(&priv->rx_ring[wr_id],
-                                                       mapping),
-                                        IPOIB_BUF_SIZE,
-                                        DMA_FROM_DEVICE);
+               if (wr_id < ipoib_recvq_size) {
+                       struct sk_buff *skb  = priv->rx_ring[wr_id].skb;
+                       dma_addr_t      addr = priv->rx_ring[wr_id].mapping;
 
-                       if (wc->status != IB_WC_SUCCESS) {
+                       if (unlikely(wc->status != IB_WC_SUCCESS)) {
                                if (wc->status != IB_WC_WR_FLUSH_ERR)
                                        ipoib_warn(priv, "failed recv event "
                                                   "(status=%d, wrid=%d vend_err %x)\n",
                                                   wc->status, wr_id, wc->vendor_err);
+                               dma_unmap_single(priv->ca->dma_device, addr,
+                                                IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
                                dev_kfree_skb_any(skb);
+                               priv->rx_ring[wr_id].skb = NULL;
                                return;
                        }
 
+                       /*
+                        * If we can't allocate a new RX buffer, dump
+                        * this packet and reuse the old buffer.
+                        */
+                       if (unlikely(ipoib_alloc_rx_skb(dev, wr_id))) {
+                               ++priv->stats.rx_dropped;
+                               goto repost;
+                       }
+
                        ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
                                       wc->byte_len, wc->slid);
 
+                       dma_unmap_single(priv->ca->dma_device, addr,
+                                        IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
+
                        skb_put(skb, wc->byte_len);
                        skb_pull(skb, IB_GRH_BYTES);
 
@@ -217,8 +240,8 @@ static void ipoib_ib_handle_wc(struct net_device *dev,
                                dev_kfree_skb_any(skb);
                        }
 
-                       /* repost receive */
-                       if (ipoib_ib_post_receive(dev, wr_id))
+               repost:
+                       if (unlikely(ipoib_ib_post_receive(dev, wr_id)))
                                ipoib_warn(priv, "ipoib_ib_post_receive failed "
                                           "for buf %d\n", wr_id);
                } else
@@ -226,12 +249,12 @@ static void ipoib_ib_handle_wc(struct net_device *dev,
                                   wr_id);
 
        } else {
-               struct ipoib_buf *tx_req;
+               struct ipoib_tx_buf *tx_req;
                unsigned long flags;
 
-               if (wr_id >= IPOIB_TX_RING_SIZE) {
+               if (wr_id >= ipoib_sendq_size) {
                        ipoib_warn(priv, "completion event with wrid %d (> %d)\n",
-                                  wr_id, IPOIB_TX_RING_SIZE);
+                                  wr_id, ipoib_sendq_size);
                        return;
                }
 
@@ -252,7 +275,8 @@ static void ipoib_ib_handle_wc(struct net_device *dev,
                spin_lock_irqsave(&priv->tx_lock, flags);
                ++priv->tx_tail;
                if (netif_queue_stopped(dev) &&
-                   priv->tx_head - priv->tx_tail <= IPOIB_TX_RING_SIZE / 2)
+                   test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags) &&
+                   priv->tx_head - priv->tx_tail <= ipoib_sendq_size >> 1)
                        netif_wake_queue(dev);
                spin_unlock_irqrestore(&priv->tx_lock, flags);
 
@@ -299,7 +323,7 @@ void ipoib_send(struct net_device *dev, struct sk_buff *skb,
                struct ipoib_ah *address, u32 qpn)
 {
        struct ipoib_dev_priv *priv = netdev_priv(dev);
-       struct ipoib_buf *tx_req;
+       struct ipoib_tx_buf *tx_req;
        dma_addr_t addr;
 
        if (skb->len > dev->mtu + INFINIBAND_ALEN) {
@@ -321,13 +345,13 @@ void ipoib_send(struct net_device *dev, struct sk_buff *skb,
         * means we have to make sure everything is properly recorded and
         * our state is consistent before we call post_send().
         */
-       tx_req = &priv->tx_ring[priv->tx_head & (IPOIB_TX_RING_SIZE - 1)];
+       tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
        tx_req->skb = skb;
        addr = dma_map_single(priv->ca->dma_device, skb->data, skb->len,
                              DMA_TO_DEVICE);
        pci_unmap_addr_set(tx_req, mapping, addr);
 
-       if (unlikely(post_send(priv, priv->tx_head & (IPOIB_TX_RING_SIZE - 1),
+       if (unlikely(post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
                               address->ah, qpn, addr, skb->len))) {
                ipoib_warn(priv, "post_send failed\n");
                ++priv->stats.tx_errors;
@@ -340,7 +364,7 @@ void ipoib_send(struct net_device *dev, struct sk_buff *skb,
                address->last_send = priv->tx_head;
                ++priv->tx_head;
 
-               if (priv->tx_head - priv->tx_tail == IPOIB_TX_RING_SIZE) {
+               if (priv->tx_head - priv->tx_tail == ipoib_sendq_size) {
                        ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
                        netif_stop_queue(dev);
                }
@@ -355,7 +379,7 @@ static void __ipoib_reap_ah(struct net_device *dev)
 
        spin_lock_irq(&priv->lock);
        list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
-               if (ah->last_send <= priv->tx_tail) {
+               if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
                        list_del(&ah->list);
                        list_add_tail(&ah->list, &remove_list);
                }
@@ -384,34 +408,55 @@ int ipoib_ib_dev_open(struct net_device *dev)
        struct ipoib_dev_priv *priv = netdev_priv(dev);
        int ret;
 
-       ret = ipoib_qp_create(dev);
+       ret = ipoib_init_qp(dev);
        if (ret) {
-               ipoib_warn(priv, "ipoib_qp_create returned %d\n", ret);
+               ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
                return -1;
        }
 
        ret = ipoib_ib_post_receives(dev);
        if (ret) {
                ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
+               ipoib_ib_dev_stop(dev);
                return -1;
        }
 
        clear_bit(IPOIB_STOP_REAPER, &priv->flags);
        queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);
 
+       set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
+
        return 0;
 }
 
+static void ipoib_pkey_dev_check_presence(struct net_device *dev)
+{
+       struct ipoib_dev_priv *priv = netdev_priv(dev);
+       u16 pkey_index = 0;
+
+       if (ib_find_cached_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
+               clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
+       else
+               set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
+}
+
 int ipoib_ib_dev_up(struct net_device *dev)
 {
        struct ipoib_dev_priv *priv = netdev_priv(dev);
 
+       ipoib_pkey_dev_check_presence(dev);
+
+       if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
+               ipoib_dbg(priv, "PKEY is not assigned.\n");
+               return 0;
+       }
+
        set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
 
        return ipoib_mcast_start_thread(dev);
 }
 
-int ipoib_ib_dev_down(struct net_device *dev)
+int ipoib_ib_dev_down(struct net_device *dev, int flush)
 {
        struct ipoib_dev_priv *priv = netdev_priv(dev);
 
@@ -422,25 +467,17 @@ int ipoib_ib_dev_down(struct net_device *dev)
 
        /* Shutdown the P_Key thread if still active */
        if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
-               down(&pkey_sem);
+               mutex_lock(&pkey_mutex);
                set_bit(IPOIB_PKEY_STOP, &priv->flags);
                cancel_delayed_work(&priv->pkey_task);
-               up(&pkey_sem);
-               flush_workqueue(ipoib_workqueue);
+               mutex_unlock(&pkey_mutex);
+               if (flush)
+                       flush_workqueue(ipoib_workqueue);
        }
 
-       ipoib_mcast_stop_thread(dev);
-
-       /*
-        * Flush the multicast groups first so we stop any multicast joins. The
-        * completion thread may have already died and we may deadlock waiting
-        * for the completion thread to finish some multicast joins.
-        */
+       ipoib_mcast_stop_thread(dev, flush);
        ipoib_mcast_dev_flush(dev);
 
-       /* Delete broadcast and local addresses since they will be recreated */
-       ipoib_mcast_dev_down(dev);
-
        ipoib_flush_paths(dev);
 
        return 0;
@@ -452,7 +489,7 @@ static int recvs_pending(struct net_device *dev)
        int pending = 0;
        int i;
 
-       for (i = 0; i < IPOIB_RX_RING_SIZE; ++i)
+       for (i = 0; i < ipoib_recvq_size; ++i)
                if (priv->rx_ring[i].skb)
                        ++pending;
 
@@ -463,15 +500,18 @@ int ipoib_ib_dev_stop(struct net_device *dev)
 {
        struct ipoib_dev_priv *priv = netdev_priv(dev);
        struct ib_qp_attr qp_attr;
-       int attr_mask;
        unsigned long begin;
-       struct ipoib_buf *tx_req;
+       struct ipoib_tx_buf *tx_req;
        int i;
 
-       /* Kill the existing QP and allocate a new one */
+       clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
+
+       /*
+        * Move our QP to the error state and then reinitialize in
+        * when all work requests have completed or have been flushed.
+        */
        qp_attr.qp_state = IB_QPS_ERR;
-       attr_mask        = IB_QP_STATE;
-       if (ib_modify_qp(priv->qp, &qp_attr, attr_mask))
+       if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
                ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
 
        /* Wait for all sends and receives to complete */
@@ -486,9 +526,9 @@ int ipoib_ib_dev_stop(struct net_device *dev)
                         * assume the HW is wedged and just free up
                         * all our pending work requests.
                         */
-                       while (priv->tx_tail < priv->tx_head) {
+                       while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
                                tx_req = &priv->tx_ring[priv->tx_tail &
-                                                       (IPOIB_TX_RING_SIZE - 1)];
+                                                       (ipoib_sendq_size - 1)];
                                dma_unmap_single(priv->ca->dma_device,
                                                 pci_unmap_addr(tx_req, mapping),
                                                 tx_req->skb->len,
@@ -497,7 +537,7 @@ int ipoib_ib_dev_stop(struct net_device *dev)
                                ++priv->tx_tail;
                        }
 
-                       for (i = 0; i < IPOIB_RX_RING_SIZE; ++i)
+                       for (i = 0; i < ipoib_recvq_size; ++i)
                                if (priv->rx_ring[i].skb) {
                                        dma_unmap_single(priv->ca->dma_device,
                                                         pci_unmap_addr(&priv->rx_ring[i],
@@ -518,8 +558,7 @@ int ipoib_ib_dev_stop(struct net_device *dev)
 
 timeout:
        qp_attr.qp_state = IB_QPS_RESET;
-       attr_mask        = IB_QP_STATE;
-       if (ib_modify_qp(priv->qp, &qp_attr, attr_mask))
+       if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
                ipoib_warn(priv, "Failed to modify QP to RESET state\n");
 
        /* Wait for all AHs to be reaped */
@@ -571,12 +610,19 @@ void ipoib_ib_dev_flush(void *_dev)
        struct net_device *dev = (struct net_device *)_dev;
        struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv;
 
-       if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
+       if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags) ) {
+               ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
                return;
+       }
+
+       if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
+               ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
+               return;
+       }
 
        ipoib_dbg(priv, "flushing\n");
 
-       ipoib_ib_dev_down(dev);
+       ipoib_ib_dev_down(dev, 0);
 
        /*
         * The device could have been brought down between the start and when
@@ -585,9 +631,13 @@ void ipoib_ib_dev_flush(void *_dev)
        if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
                ipoib_ib_dev_up(dev);
 
+       mutex_lock(&priv->vlan_mutex);
+
        /* Flush any child interfaces too */
        list_for_each_entry(cpriv, &priv->child_intfs, list)
-               ipoib_ib_dev_flush(&cpriv->dev);
+               ipoib_ib_dev_flush(cpriv->dev);
+
+       mutex_unlock(&priv->vlan_mutex);
 }
 
 void ipoib_ib_dev_cleanup(struct net_device *dev)
@@ -596,10 +646,8 @@ void ipoib_ib_dev_cleanup(struct net_device *dev)
 
        ipoib_dbg(priv, "cleaning up ib_dev\n");
 
-       ipoib_mcast_stop_thread(dev);
-
-       /* Delete the broadcast address and the local address */
-       ipoib_mcast_dev_down(dev);
+       ipoib_mcast_stop_thread(dev, 1);
+       ipoib_mcast_dev_flush(dev);
 
        ipoib_transport_dev_cleanup(dev);
 }
@@ -613,18 +661,6 @@ void ipoib_ib_dev_cleanup(struct net_device *dev)
  * Bug #2507. This implementation will probably be removed when the P_Key
  * change async notification is available.
  */
-int ipoib_open(struct net_device *dev);
-
-static void ipoib_pkey_dev_check_presence(struct net_device *dev)
-{
-       struct ipoib_dev_priv *priv = netdev_priv(dev);
-       u16 pkey_index = 0;
-
-       if (ib_find_cached_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
-               clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
-       else
-               set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
-}
 
 void ipoib_pkey_poll(void *dev_ptr)
 {
@@ -636,12 +672,12 @@ void ipoib_pkey_poll(void *dev_ptr)
        if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
                ipoib_open(dev);
        else {
-               down(&pkey_sem);
+               mutex_lock(&pkey_mutex);
                if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
                        queue_delayed_work(ipoib_workqueue,
                                           &priv->pkey_task,
                                           HZ);
-               up(&pkey_sem);
+               mutex_unlock(&pkey_mutex);
        }
 }
 
@@ -655,12 +691,12 @@ int ipoib_pkey_dev_delay_open(struct net_device *dev)
 
        /* P_Key value not assigned yet - start polling */
        if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
-               down(&pkey_sem);
+               mutex_lock(&pkey_mutex);
                clear_bit(IPOIB_PKEY_STOP, &priv->flags);
                queue_delayed_work(ipoib_workqueue,
                                   &priv->pkey_task,
                                   HZ);
-               up(&pkey_sem);
+               mutex_unlock(&pkey_mutex);
                return 1;
        }