]> nv-tegra.nvidia Code Review - linux-2.6.git/blobdiff - drivers/infiniband/ulp/ipoib/ipoib_multicast.c
Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6.git] / drivers / infiniband / ulp / ipoib / ipoib_multicast.c
index ccaa0c387076e8b0a36dc90dbb830528b09b93da..3871ac663554339ac09766a5b3bdcc7c758a65e3 100644 (file)
@@ -30,8 +30,6 @@
  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
- *
- * $Id: ipoib_multicast.c 1362 2004-12-18 15:56:29Z roland $
  */
 
 #include <linux/skbuff.h>
@@ -42,6 +40,7 @@
 #include <linux/inetdevice.h>
 #include <linux/delay.h>
 #include <linux/completion.h>
+#include <linux/slab.h>
 
 #include <net/dst.h>
 
@@ -57,31 +56,6 @@ MODULE_PARM_DESC(mcast_debug_level,
 
 static DEFINE_MUTEX(mcast_mutex);
 
-/* Used for all multicast joins (broadcast, IPv4 mcast and IPv6 mcast) */
-struct ipoib_mcast {
-       struct ib_sa_mcmember_rec mcmember;
-       struct ipoib_ah          *ah;
-
-       struct rb_node    rb_node;
-       struct list_head  list;
-       struct completion done;
-
-       int                 query_id;
-       struct ib_sa_query *query;
-
-       unsigned long created;
-       unsigned long backoff;
-
-       unsigned long flags;
-       unsigned char logcount;
-
-       struct list_head  neigh_list;
-
-       struct sk_buff_head pkt_queue;
-
-       struct net_device *dev;
-};
-
 struct ipoib_mcast_iter {
        struct net_device *dev;
        union ib_gid       mgid;
@@ -96,14 +70,12 @@ static void ipoib_mcast_free(struct ipoib_mcast *mcast)
        struct net_device *dev = mcast->dev;
        struct ipoib_dev_priv *priv = netdev_priv(dev);
        struct ipoib_neigh *neigh, *tmp;
-       unsigned long flags;
        int tx_dropped = 0;
 
-       ipoib_dbg_mcast(netdev_priv(dev),
-                       "deleting multicast group " IPOIB_GID_FMT "\n",
-                       IPOIB_GID_ARG(mcast->mcmember.mgid));
+       ipoib_dbg_mcast(netdev_priv(dev), "deleting multicast group %pI6\n",
+                       mcast->mcmember.mgid.raw);
 
-       spin_lock_irqsave(&priv->lock, flags);
+       spin_lock_irq(&priv->lock);
 
        list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
                /*
@@ -114,12 +86,10 @@ static void ipoib_mcast_free(struct ipoib_mcast *mcast)
                 */
                if (neigh->ah)
                        ipoib_put_ah(neigh->ah);
-               *to_ipoib_neigh(neigh->neighbour) = NULL;
-               neigh->neighbour->ops->destructor = NULL;
-               kfree(neigh);
+               ipoib_neigh_free(dev, neigh);
        }
 
-       spin_unlock_irqrestore(&priv->lock, flags);
+       spin_unlock_irq(&priv->lock);
 
        if (mcast->ah)
                ipoib_put_ah(mcast->ah);
@@ -129,9 +99,9 @@ static void ipoib_mcast_free(struct ipoib_mcast *mcast)
                dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
        }
 
-       spin_lock_irqsave(&priv->tx_lock, flags);
-       priv->stats.tx_dropped += tx_dropped;
-       spin_unlock_irqrestore(&priv->tx_lock, flags);
+       netif_tx_lock_bh(dev);
+       dev->stats.tx_dropped += tx_dropped;
+       netif_tx_unlock_bh(dev);
 
        kfree(mcast);
 }
@@ -156,7 +126,7 @@ static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
        return mcast;
 }
 
-static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, union ib_gid *mgid)
+static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
 {
        struct ipoib_dev_priv *priv = netdev_priv(dev);
        struct rb_node *n = priv->multicast_tree.rb_node;
@@ -167,7 +137,7 @@ static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, union ib_g
 
                mcast = rb_entry(n, struct ipoib_mcast, rb_node);
 
-               ret = memcmp(mgid->raw, mcast->mcmember.mgid.raw,
+               ret = memcmp(mgid, mcast->mcmember.mgid.raw,
                             sizeof (union ib_gid));
                if (ret < 0)
                        n = n->rb_left;
@@ -213,32 +183,39 @@ static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
 {
        struct net_device *dev = mcast->dev;
        struct ipoib_dev_priv *priv = netdev_priv(dev);
+       struct ipoib_ah *ah;
        int ret;
+       int set_qkey = 0;
 
        mcast->mcmember = *mcmember;
 
        /* Set the cached Q_Key before we attach if it's the broadcast group */
        if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
                    sizeof (union ib_gid))) {
+               spin_lock_irq(&priv->lock);
+               if (!priv->broadcast) {
+                       spin_unlock_irq(&priv->lock);
+                       return -EAGAIN;
+               }
                priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
+               spin_unlock_irq(&priv->lock);
                priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
+               set_qkey = 1;
        }
 
        if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
                if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
-                       ipoib_warn(priv, "multicast group " IPOIB_GID_FMT
-                                  " already attached\n",
-                                  IPOIB_GID_ARG(mcast->mcmember.mgid));
+                       ipoib_warn(priv, "multicast group %pI6 already attached\n",
+                                  mcast->mcmember.mgid.raw);
 
                        return 0;
                }
 
                ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
-                                        &mcast->mcmember.mgid);
+                                        &mcast->mcmember.mgid, set_qkey);
                if (ret < 0) {
-                       ipoib_warn(priv, "couldn't attach QP to multicast group "
-                                  IPOIB_GID_FMT "\n",
-                                  IPOIB_GID_ARG(mcast->mcmember.mgid));
+                       ipoib_warn(priv, "couldn't attach QP to multicast group %pI6\n",
+                                  mcast->mcmember.mgid.raw);
 
                        clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
                        return ret;
@@ -251,6 +228,7 @@ static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
                        .port_num      = priv->port,
                        .sl            = mcast->mcmember.sl,
                        .ah_flags      = IB_AH_GRH,
+                       .static_rate   = mcast->mcmember.rate,
                        .grh           = {
                                .flow_label    = be32_to_cpu(mcast->mcmember.flow_label),
                                .hop_limit     = mcast->mcmember.hop_limit,
@@ -258,24 +236,18 @@ static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
                                .traffic_class = mcast->mcmember.traffic_class
                        }
                };
-               int path_rate = ib_sa_rate_enum_to_int(mcast->mcmember.rate);
-
                av.grh.dgid = mcast->mcmember.mgid;
 
-               if (path_rate > 0 && priv->local_rate > path_rate)
-                       av.static_rate = (priv->local_rate - 1) / path_rate;
-
-               ipoib_dbg_mcast(priv, "static_rate %d for local port %dX, mcmember %dX\n",
-                               av.static_rate, priv->local_rate,
-                               ib_sa_rate_enum_to_int(mcast->mcmember.rate));
-
-               mcast->ah = ipoib_create_ah(dev, priv->pd, &av);
-               if (!mcast->ah) {
+               ah = ipoib_create_ah(dev, priv->pd, &av);
+               if (!ah) {
                        ipoib_warn(priv, "ib_address_create failed\n");
                } else {
-                       ipoib_dbg_mcast(priv, "MGID " IPOIB_GID_FMT
-                                       " AV %p, LID 0x%04x, SL %d\n",
-                                       IPOIB_GID_ARG(mcast->mcmember.mgid),
+                       spin_lock_irq(&priv->lock);
+                       mcast->ah = ah;
+                       spin_unlock_irq(&priv->lock);
+
+                       ipoib_dbg_mcast(priv, "MGID %pI6 AV %p, LID 0x%04x, SL %d\n",
+                                       mcast->mcmember.mgid.raw,
                                        mcast->ah->ah,
                                        be16_to_cpu(mcast->mcmember.mlid),
                                        mcast->mcmember.sl);
@@ -283,57 +255,59 @@ static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
        }
 
        /* actually send any queued packets */
-       spin_lock_irq(&priv->tx_lock);
+       netif_tx_lock_bh(dev);
        while (!skb_queue_empty(&mcast->pkt_queue)) {
                struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
-               spin_unlock_irq(&priv->tx_lock);
+               netif_tx_unlock_bh(dev);
 
                skb->dev = dev;
 
-               if (!skb->dst || !skb->dst->neighbour) {
+               if (!skb_dst(skb) || !skb_dst(skb)->neighbour) {
                        /* put pseudoheader back on for next time */
                        skb_push(skb, sizeof (struct ipoib_pseudoheader));
                }
 
                if (dev_queue_xmit(skb))
                        ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
-               spin_lock_irq(&priv->tx_lock);
+               netif_tx_lock_bh(dev);
        }
-       spin_unlock_irq(&priv->tx_lock);
+       netif_tx_unlock_bh(dev);
 
        return 0;
 }
 
-static void
+static int
 ipoib_mcast_sendonly_join_complete(int status,
-                                  struct ib_sa_mcmember_rec *mcmember,
-                                  void *mcast_ptr)
+                                  struct ib_sa_multicast *multicast)
 {
-       struct ipoib_mcast *mcast = mcast_ptr;
+       struct ipoib_mcast *mcast = multicast->context;
        struct net_device *dev = mcast->dev;
-       struct ipoib_dev_priv *priv = netdev_priv(dev);
+
+       /* We trap for port events ourselves. */
+       if (status == -ENETRESET)
+               return 0;
 
        if (!status)
-               ipoib_mcast_join_finish(mcast, mcmember);
-       else {
+               status = ipoib_mcast_join_finish(mcast, &multicast->rec);
+
+       if (status) {
                if (mcast->logcount++ < 20)
-                       ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for "
-                                       IPOIB_GID_FMT ", status %d\n",
-                                       IPOIB_GID_ARG(mcast->mcmember.mgid), status);
+                       ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for %pI6, status %d\n",
+                                       mcast->mcmember.mgid.raw, status);
 
                /* Flush out any queued packets */
-               spin_lock_irq(&priv->tx_lock);
+               netif_tx_lock_bh(dev);
                while (!skb_queue_empty(&mcast->pkt_queue)) {
-                       ++priv->stats.tx_dropped;
+                       ++dev->stats.tx_dropped;
                        dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
                }
-               spin_unlock_irq(&priv->tx_lock);
+               netif_tx_unlock_bh(dev);
 
                /* Clear the busy flag so we try again */
-               clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
+               status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY,
+                                           &mcast->flags);
        }
-
-       complete(&mcast->done);
+       return status;
 }
 
 static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
@@ -363,68 +337,92 @@ static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
        rec.port_gid = priv->local_gid;
        rec.pkey     = cpu_to_be16(priv->pkey);
 
-       init_completion(&mcast->done);
-
-       ret = ib_sa_mcmember_rec_set(priv->ca, priv->port, &rec,
-                                    IB_SA_MCMEMBER_REC_MGID            |
-                                    IB_SA_MCMEMBER_REC_PORT_GID        |
-                                    IB_SA_MCMEMBER_REC_PKEY            |
-                                    IB_SA_MCMEMBER_REC_JOIN_STATE,
-                                    1000, GFP_ATOMIC,
-                                    ipoib_mcast_sendonly_join_complete,
-                                    mcast, &mcast->query);
-       if (ret < 0) {
-               ipoib_warn(priv, "ib_sa_mcmember_rec_set failed (ret = %d)\n",
+       mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca,
+                                        priv->port, &rec,
+                                        IB_SA_MCMEMBER_REC_MGID        |
+                                        IB_SA_MCMEMBER_REC_PORT_GID    |
+                                        IB_SA_MCMEMBER_REC_PKEY        |
+                                        IB_SA_MCMEMBER_REC_JOIN_STATE,
+                                        GFP_ATOMIC,
+                                        ipoib_mcast_sendonly_join_complete,
+                                        mcast);
+       if (IS_ERR(mcast->mc)) {
+               ret = PTR_ERR(mcast->mc);
+               clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
+               ipoib_warn(priv, "ib_sa_join_multicast failed (ret = %d)\n",
                           ret);
        } else {
-               ipoib_dbg_mcast(priv, "no multicast record for " IPOIB_GID_FMT
-                               ", starting join\n",
-                               IPOIB_GID_ARG(mcast->mcmember.mgid));
-
-               mcast->query_id = ret;
+               ipoib_dbg_mcast(priv, "no multicast record for %pI6, starting join\n",
+                               mcast->mcmember.mgid.raw);
        }
 
        return ret;
 }
 
-static void ipoib_mcast_join_complete(int status,
-                                     struct ib_sa_mcmember_rec *mcmember,
-                                     void *mcast_ptr)
+void ipoib_mcast_carrier_on_task(struct work_struct *work)
+{
+       struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
+                                                  carrier_on_task);
+       struct ib_port_attr attr;
+
+       /*
+        * Take rtnl_lock to avoid racing with ipoib_stop() and
+        * turning the carrier back on while a device is being
+        * removed.
+        */
+       if (ib_query_port(priv->ca, priv->port, &attr) ||
+           attr.state != IB_PORT_ACTIVE) {
+               ipoib_dbg(priv, "Keeping carrier off until IB port is active\n");
+               return;
+       }
+
+       rtnl_lock();
+       netif_carrier_on(priv->dev);
+       rtnl_unlock();
+}
+
+static int ipoib_mcast_join_complete(int status,
+                                    struct ib_sa_multicast *multicast)
 {
-       struct ipoib_mcast *mcast = mcast_ptr;
+       struct ipoib_mcast *mcast = multicast->context;
        struct net_device *dev = mcast->dev;
        struct ipoib_dev_priv *priv = netdev_priv(dev);
 
-       ipoib_dbg_mcast(priv, "join completion for " IPOIB_GID_FMT
-                       " (status %d)\n",
-                       IPOIB_GID_ARG(mcast->mcmember.mgid), status);
+       ipoib_dbg_mcast(priv, "join completion for %pI6 (status %d)\n",
+                       mcast->mcmember.mgid.raw, status);
 
-       if (!status && !ipoib_mcast_join_finish(mcast, mcmember)) {
+       /* We trap for port events ourselves. */
+       if (status == -ENETRESET)
+               return 0;
+
+       if (!status)
+               status = ipoib_mcast_join_finish(mcast, &multicast->rec);
+
+       if (!status) {
                mcast->backoff = 1;
                mutex_lock(&mcast_mutex);
                if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
-                       queue_work(ipoib_workqueue, &priv->mcast_task);
+                       queue_delayed_work(ipoib_workqueue,
+                                          &priv->mcast_task, 0);
                mutex_unlock(&mcast_mutex);
-               complete(&mcast->done);
-               return;
-       }
 
-       if (status == -EINTR) {
-               complete(&mcast->done);
-               return;
+               /*
+                * Defer carrier on work to ipoib_workqueue to avoid a
+                * deadlock on rtnl_lock here.
+                */
+               if (mcast == priv->broadcast)
+                       queue_work(ipoib_workqueue, &priv->carrier_on_task);
+
+               return 0;
        }
 
-       if (status && mcast->logcount++ < 20) {
-               if (status == -ETIMEDOUT || status == -EINTR) {
-                       ipoib_dbg_mcast(priv, "multicast join failed for " IPOIB_GID_FMT
-                                       ", status %d\n",
-                                       IPOIB_GID_ARG(mcast->mcmember.mgid),
-                                       status);
+       if (mcast->logcount++ < 20) {
+               if (status == -ETIMEDOUT || status == -EAGAIN) {
+                       ipoib_dbg_mcast(priv, "multicast join failed for %pI6, status %d\n",
+                                       mcast->mcmember.mgid.raw, status);
                } else {
-                       ipoib_warn(priv, "multicast join failed for "
-                                  IPOIB_GID_FMT ", status %d\n",
-                                  IPOIB_GID_ARG(mcast->mcmember.mgid),
-                                  status);
+                       ipoib_warn(priv, "multicast join failed for %pI6, status %d\n",
+                                  mcast->mcmember.mgid.raw, status);
                }
        }
 
@@ -432,20 +430,18 @@ static void ipoib_mcast_join_complete(int status,
        if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
                mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
 
-       mcast->query = NULL;
+       /* Clear the busy flag so we try again */
+       status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
 
        mutex_lock(&mcast_mutex);
-       if (test_bit(IPOIB_MCAST_RUN, &priv->flags)) {
-               if (status == -ETIMEDOUT)
-                       queue_work(ipoib_workqueue, &priv->mcast_task);
-               else
-                       queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
-                                          mcast->backoff * HZ);
-       } else
-               complete(&mcast->done);
+       spin_lock_irq(&priv->lock);
+       if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
+               queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
+                                  mcast->backoff * HZ);
+       spin_unlock_irq(&priv->lock);
        mutex_unlock(&mcast_mutex);
 
-       return;
+       return status;
 }
 
 static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
@@ -458,8 +454,7 @@ static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
        ib_sa_comp_mask comp_mask;
        int ret = 0;
 
-       ipoib_dbg_mcast(priv, "joining MGID " IPOIB_GID_FMT "\n",
-                       IPOIB_GID_ARG(mcast->mcmember.mgid));
+       ipoib_dbg_mcast(priv, "joining MGID %pI6\n", mcast->mcmember.mgid.raw);
 
        rec.mgid     = mcast->mcmember.mgid;
        rec.port_gid = priv->local_gid;
@@ -473,26 +468,35 @@ static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
 
        if (create) {
                comp_mask |=
-                       IB_SA_MCMEMBER_REC_QKEY         |
-                       IB_SA_MCMEMBER_REC_SL           |
-                       IB_SA_MCMEMBER_REC_FLOW_LABEL   |
-                       IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
+                       IB_SA_MCMEMBER_REC_QKEY                 |
+                       IB_SA_MCMEMBER_REC_MTU_SELECTOR         |
+                       IB_SA_MCMEMBER_REC_MTU                  |
+                       IB_SA_MCMEMBER_REC_TRAFFIC_CLASS        |
+                       IB_SA_MCMEMBER_REC_RATE_SELECTOR        |
+                       IB_SA_MCMEMBER_REC_RATE                 |
+                       IB_SA_MCMEMBER_REC_SL                   |
+                       IB_SA_MCMEMBER_REC_FLOW_LABEL           |
+                       IB_SA_MCMEMBER_REC_HOP_LIMIT;
 
                rec.qkey          = priv->broadcast->mcmember.qkey;
+               rec.mtu_selector  = IB_SA_EQ;
+               rec.mtu           = priv->broadcast->mcmember.mtu;
+               rec.traffic_class = priv->broadcast->mcmember.traffic_class;
+               rec.rate_selector = IB_SA_EQ;
+               rec.rate          = priv->broadcast->mcmember.rate;
                rec.sl            = priv->broadcast->mcmember.sl;
                rec.flow_label    = priv->broadcast->mcmember.flow_label;
-               rec.traffic_class = priv->broadcast->mcmember.traffic_class;
+               rec.hop_limit     = priv->broadcast->mcmember.hop_limit;
        }
 
-       init_completion(&mcast->done);
-
-       ret = ib_sa_mcmember_rec_set(priv->ca, priv->port, &rec, comp_mask,
-                                    mcast->backoff * 1000, GFP_ATOMIC,
-                                    ipoib_mcast_join_complete,
-                                    mcast, &mcast->query);
-
-       if (ret < 0) {
-               ipoib_warn(priv, "ib_sa_mcmember_rec_set failed, status %d\n", ret);
+       set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
+       mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
+                                        &rec, comp_mask, GFP_KERNEL,
+                                        ipoib_mcast_join_complete, mcast);
+       if (IS_ERR(mcast->mc)) {
+               clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
+               ret = PTR_ERR(mcast->mc);
+               ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
 
                mcast->backoff *= 2;
                if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
@@ -504,37 +508,40 @@ static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
                                           &priv->mcast_task,
                                           mcast->backoff * HZ);
                mutex_unlock(&mcast_mutex);
-       } else
-               mcast->query_id = ret;
+       }
 }
 
-void ipoib_mcast_join_task(void *dev_ptr)
+void ipoib_mcast_join_task(struct work_struct *work)
 {
-       struct net_device *dev = dev_ptr;
-       struct ipoib_dev_priv *priv = netdev_priv(dev);
+       struct ipoib_dev_priv *priv =
+               container_of(work, struct ipoib_dev_priv, mcast_task.work);
+       struct net_device *dev = priv->dev;
 
        if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
                return;
 
        if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
-               ipoib_warn(priv, "ib_gid_entry_get() failed\n");
+               ipoib_warn(priv, "ib_query_gid() failed\n");
        else
                memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
 
        {
                struct ib_port_attr attr;
 
-               if (!ib_query_port(priv->ca, priv->port, &attr)) {
-                       priv->local_lid  = attr.lid;
-                       priv->local_rate = attr.active_speed *
-                               ib_width_enum_to_int(attr.active_width);
-               } else
+               if (!ib_query_port(priv->ca, priv->port, &attr))
+                       priv->local_lid = attr.lid;
+               else
                        ipoib_warn(priv, "ib_query_port failed\n");
        }
 
        if (!priv->broadcast) {
-               priv->broadcast = ipoib_mcast_alloc(dev, 1);
-               if (!priv->broadcast) {
+               struct ipoib_mcast *broadcast;
+
+               if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
+                       return;
+
+               broadcast = ipoib_mcast_alloc(dev, 1);
+               if (!broadcast) {
                        ipoib_warn(priv, "failed to allocate broadcast group\n");
                        mutex_lock(&mcast_mutex);
                        if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
@@ -544,16 +551,18 @@ void ipoib_mcast_join_task(void *dev_ptr)
                        return;
                }
 
-               memcpy(priv->broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
+               spin_lock_irq(&priv->lock);
+               memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
                       sizeof (union ib_gid));
+               priv->broadcast = broadcast;
 
-               spin_lock_irq(&priv->lock);
                __ipoib_mcast_add(dev, priv->broadcast);
                spin_unlock_irq(&priv->lock);
        }
 
        if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
-               ipoib_mcast_join(dev, priv->broadcast, 0);
+               if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
+                       ipoib_mcast_join(dev, priv->broadcast, 0);
                return;
        }
 
@@ -580,14 +589,17 @@ void ipoib_mcast_join_task(void *dev_ptr)
                return;
        }
 
-       priv->mcast_mtu = ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu) -
-               IPOIB_ENCAP_LEN;
-       dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
+       priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
+
+       if (!ipoib_cm_admin_enabled(dev)) {
+               rtnl_lock();
+               dev_set_mtu(dev, min(priv->mcast_mtu, priv->admin_mtu));
+               rtnl_unlock();
+       }
 
        ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
 
        clear_bit(IPOIB_MCAST_RUN, &priv->flags);
-       netif_carrier_on(dev);
 }
 
 int ipoib_mcast_start_thread(struct net_device *dev)
@@ -598,7 +610,7 @@ int ipoib_mcast_start_thread(struct net_device *dev)
 
        mutex_lock(&mcast_mutex);
        if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
-               queue_work(ipoib_workqueue, &priv->mcast_task);
+               queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
        mutex_unlock(&mcast_mutex);
 
        return 0;
@@ -607,7 +619,6 @@ int ipoib_mcast_start_thread(struct net_device *dev)
 int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
 {
        struct ipoib_dev_priv *priv = netdev_priv(dev);
-       struct ipoib_mcast *mcast;
 
        ipoib_dbg_mcast(priv, "stopping multicast thread\n");
 
@@ -619,97 +630,64 @@ int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
        if (flush)
                flush_workqueue(ipoib_workqueue);
 
-       if (priv->broadcast && priv->broadcast->query) {
-               ib_sa_cancel_query(priv->broadcast->query_id, priv->broadcast->query);
-               priv->broadcast->query = NULL;
-               ipoib_dbg_mcast(priv, "waiting for bcast\n");
-               wait_for_completion(&priv->broadcast->done);
-       }
-
-       list_for_each_entry(mcast, &priv->multicast_list, list) {
-               if (mcast->query) {
-                       ib_sa_cancel_query(mcast->query_id, mcast->query);
-                       mcast->query = NULL;
-                       ipoib_dbg_mcast(priv, "waiting for MGID " IPOIB_GID_FMT "\n",
-                                       IPOIB_GID_ARG(mcast->mcmember.mgid));
-                       wait_for_completion(&mcast->done);
-               }
-       }
-
        return 0;
 }
 
 static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
 {
        struct ipoib_dev_priv *priv = netdev_priv(dev);
-       struct ib_sa_mcmember_rec rec = {
-               .join_state = 1
-       };
        int ret = 0;
 
-       if (!test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags))
-               return 0;
+       if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
+               ib_sa_free_multicast(mcast->mc);
 
-       ipoib_dbg_mcast(priv, "leaving MGID " IPOIB_GID_FMT "\n",
-                       IPOIB_GID_ARG(mcast->mcmember.mgid));
+       if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
+               ipoib_dbg_mcast(priv, "leaving MGID %pI6\n",
+                               mcast->mcmember.mgid.raw);
 
-       rec.mgid     = mcast->mcmember.mgid;
-       rec.port_gid = priv->local_gid;
-       rec.pkey     = cpu_to_be16(priv->pkey);
-
-       /* Remove ourselves from the multicast group */
-       ret = ipoib_mcast_detach(dev, be16_to_cpu(mcast->mcmember.mlid),
-                                &mcast->mcmember.mgid);
-       if (ret)
-               ipoib_warn(priv, "ipoib_mcast_detach failed (result = %d)\n", ret);
-
-       /*
-        * Just make one shot at leaving and don't wait for a reply;
-        * if we fail, too bad.
-        */
-       ret = ib_sa_mcmember_rec_delete(priv->ca, priv->port, &rec,
-                                       IB_SA_MCMEMBER_REC_MGID         |
-                                       IB_SA_MCMEMBER_REC_PORT_GID     |
-                                       IB_SA_MCMEMBER_REC_PKEY         |
-                                       IB_SA_MCMEMBER_REC_JOIN_STATE,
-                                       0, GFP_ATOMIC, NULL,
-                                       mcast, &mcast->query);
-       if (ret < 0)
-               ipoib_warn(priv, "ib_sa_mcmember_rec_delete failed "
-                          "for leave (result = %d)\n", ret);
+               /* Remove ourselves from the multicast group */
+               ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
+                                     be16_to_cpu(mcast->mcmember.mlid));
+               if (ret)
+                       ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
+       }
 
        return 0;
 }
 
-void ipoib_mcast_send(struct net_device *dev, union ib_gid *mgid,
-                     struct sk_buff *skb)
+void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb)
 {
        struct ipoib_dev_priv *priv = netdev_priv(dev);
        struct ipoib_mcast *mcast;
+       unsigned long flags;
 
-       /*
-        * We can only be called from ipoib_start_xmit, so we're
-        * inside tx_lock -- no need to save/restore flags.
-        */
-       spin_lock(&priv->lock);
+       spin_lock_irqsave(&priv->lock, flags);
+
+       if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)         ||
+           !priv->broadcast                                    ||
+           !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
+               ++dev->stats.tx_dropped;
+               dev_kfree_skb_any(skb);
+               goto unlock;
+       }
 
        mcast = __ipoib_mcast_find(dev, mgid);
        if (!mcast) {
                /* Let's create a new send only group now */
-               ipoib_dbg_mcast(priv, "setting up send only multicast group for "
-                               IPOIB_GID_FMT "\n", IPOIB_GID_ARG(*mgid));
+               ipoib_dbg_mcast(priv, "setting up send only multicast group for %pI6\n",
+                               mgid);
 
                mcast = ipoib_mcast_alloc(dev, 0);
                if (!mcast) {
                        ipoib_warn(priv, "unable to allocate memory for "
                                   "multicast structure\n");
-                       ++priv->stats.tx_dropped;
+                       ++dev->stats.tx_dropped;
                        dev_kfree_skb_any(skb);
                        goto out;
                }
 
                set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
-               mcast->mcmember.mgid = *mgid;
+               memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
                __ipoib_mcast_add(dev, mcast);
                list_add_tail(&mcast->list, &priv->multicast_list);
        }
@@ -718,11 +696,11 @@ void ipoib_mcast_send(struct net_device *dev, union ib_gid *mgid,
                if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
                        skb_queue_tail(&mcast->pkt_queue, skb);
                else {
-                       ++priv->stats.tx_dropped;
+                       ++dev->stats.tx_dropped;
                        dev_kfree_skb_any(skb);
                }
 
-               if (mcast->query)
+               if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
                        ipoib_dbg_mcast(priv, "no address vector, "
                                        "but multicast join already started\n");
                else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
@@ -737,24 +715,26 @@ void ipoib_mcast_send(struct net_device *dev, union ib_gid *mgid,
 
 out:
        if (mcast && mcast->ah) {
-               if (skb->dst            &&
-                   skb->dst->neighbour &&
-                   !*to_ipoib_neigh(skb->dst->neighbour)) {
-                       struct ipoib_neigh *neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
+               if (skb_dst(skb)                &&
+                   skb_dst(skb)->neighbour &&
+                   !*to_ipoib_neigh(skb_dst(skb)->neighbour)) {
+                       struct ipoib_neigh *neigh = ipoib_neigh_alloc(skb_dst(skb)->neighbour,
+                                                                       skb->dev);
 
                        if (neigh) {
                                kref_get(&mcast->ah->ref);
-                               neigh->ah       = mcast->ah;
-                               neigh->neighbour = skb->dst->neighbour;
-                               *to_ipoib_neigh(skb->dst->neighbour) = neigh;
+                               neigh->ah       = mcast->ah;
                                list_add_tail(&neigh->list, &mcast->neigh_list);
                        }
                }
 
+               spin_unlock_irqrestore(&priv->lock, flags);
                ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
+               return;
        }
 
-       spin_unlock(&priv->lock);
+unlock:
+       spin_unlock_irqrestore(&priv->lock, flags);
 }
 
 void ipoib_mcast_dev_flush(struct net_device *dev)
@@ -775,7 +755,7 @@ void ipoib_mcast_dev_flush(struct net_device *dev)
        }
 
        if (priv->broadcast) {
-               rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
+               rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
                list_add_tail(&priv->broadcast->list, &remove_list);
                priv->broadcast = NULL;
        }
@@ -788,20 +768,34 @@ void ipoib_mcast_dev_flush(struct net_device *dev)
        }
 }
 
-void ipoib_mcast_restart_task(void *dev_ptr)
+static int ipoib_mcast_addr_is_valid(const u8 *addr, const u8 *broadcast)
 {
-       struct net_device *dev = dev_ptr;
-       struct ipoib_dev_priv *priv = netdev_priv(dev);
-       struct dev_mc_list *mclist;
+       /* reserved QPN, prefix, scope */
+       if (memcmp(addr, broadcast, 6))
+               return 0;
+       /* signature lower, pkey */
+       if (memcmp(addr + 7, broadcast + 7, 3))
+               return 0;
+       return 1;
+}
+
+void ipoib_mcast_restart_task(struct work_struct *work)
+{
+       struct ipoib_dev_priv *priv =
+               container_of(work, struct ipoib_dev_priv, restart_task);
+       struct net_device *dev = priv->dev;
+       struct netdev_hw_addr *ha;
        struct ipoib_mcast *mcast, *tmcast;
        LIST_HEAD(remove_list);
        unsigned long flags;
+       struct ib_sa_mcmember_rec rec;
 
        ipoib_dbg_mcast(priv, "restarting multicast task\n");
 
        ipoib_mcast_stop_thread(dev, 0);
 
-       spin_lock_irqsave(&dev->xmit_lock, flags);
+       local_irq_save(flags);
+       netif_addr_lock(dev);
        spin_lock(&priv->lock);
 
        /*
@@ -815,22 +809,29 @@ void ipoib_mcast_restart_task(void *dev_ptr)
                clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
 
        /* Mark all of the entries that are found or don't exist */
-       for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
+       netdev_for_each_mc_addr(ha, dev) {
                union ib_gid mgid;
 
-               memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
+               if (!ipoib_mcast_addr_is_valid(ha->addr, dev->broadcast))
+                       continue;
 
-               /* Add in the P_Key */
-               mgid.raw[4] = (priv->pkey >> 8) & 0xff;
-               mgid.raw[5] = priv->pkey & 0xff;
+               memcpy(mgid.raw, ha->addr + 4, sizeof mgid);
 
                mcast = __ipoib_mcast_find(dev, &mgid);
                if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
                        struct ipoib_mcast *nmcast;
 
+                       /* ignore group which is directly joined by userspace */
+                       if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
+                           !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
+                               ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %pI6\n",
+                                               mgid.raw);
+                               continue;
+                       }
+
                        /* Not found or send-only group, let's add a new entry */
-                       ipoib_dbg_mcast(priv, "adding multicast entry for mgid "
-                                       IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
+                       ipoib_dbg_mcast(priv, "adding multicast entry for mgid %pI6\n",
+                                       mgid.raw);
 
                        nmcast = ipoib_mcast_alloc(dev, 0);
                        if (!nmcast) {
@@ -844,8 +845,7 @@ void ipoib_mcast_restart_task(void *dev_ptr)
 
                        if (mcast) {
                                /* Destroy the send only entry */
-                               list_del(&mcast->list);
-                               list_add_tail(&mcast->list, &remove_list);
+                               list_move_tail(&mcast->list, &remove_list);
 
                                rb_replace_node(&mcast->rb_node,
                                                &nmcast->rb_node,
@@ -864,19 +864,19 @@ void ipoib_mcast_restart_task(void *dev_ptr)
        list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
                if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
                    !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
-                       ipoib_dbg_mcast(priv, "deleting multicast group " IPOIB_GID_FMT "\n",
-                                       IPOIB_GID_ARG(mcast->mcmember.mgid));
+                       ipoib_dbg_mcast(priv, "deleting multicast group %pI6\n",
+                                       mcast->mcmember.mgid.raw);
 
                        rb_erase(&mcast->rb_node, &priv->multicast_tree);
 
                        /* Move to the remove list */
-                       list_del(&mcast->list);
-                       list_add_tail(&mcast->list, &remove_list);
+                       list_move_tail(&mcast->list, &remove_list);
                }
        }
 
        spin_unlock(&priv->lock);
-       spin_unlock_irqrestore(&dev->xmit_lock, flags);
+       netif_addr_unlock(dev);
+       local_irq_restore(flags);
 
        /* We have to cancel outside of the spinlock */
        list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {