2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4 * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 #include <linux/skbuff.h>
36 #include <linux/rtnetlink.h>
39 #include <linux/igmp.h>
40 #include <linux/inetdevice.h>
41 #include <linux/delay.h>
42 #include <linux/completion.h>
48 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
49 static int mcast_debug_level;
51 module_param(mcast_debug_level, int, 0644);
52 MODULE_PARM_DESC(mcast_debug_level,
53 "Enable multicast debug tracing if > 0");
56 static DEFINE_MUTEX(mcast_mutex);
58 struct ipoib_mcast_iter {
59 struct net_device *dev;
61 unsigned long created;
62 unsigned int queuelen;
63 unsigned int complete;
64 unsigned int send_only;
67 static void ipoib_mcast_free(struct ipoib_mcast *mcast)
69 struct net_device *dev = mcast->dev;
70 struct ipoib_dev_priv *priv = netdev_priv(dev);
71 struct ipoib_neigh *neigh, *tmp;
74 ipoib_dbg_mcast(netdev_priv(dev),
75 "deleting multicast group " IPOIB_GID_FMT "\n",
76 IPOIB_GID_ARG(mcast->mcmember.mgid));
78 spin_lock_irq(&priv->lock);
80 list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
82 * It's safe to call ipoib_put_ah() inside priv->lock
83 * here, because we know that mcast->ah will always
84 * hold one more reference, so ipoib_put_ah() will
85 * never do more than decrement the ref count.
88 ipoib_put_ah(neigh->ah);
89 ipoib_neigh_free(dev, neigh);
92 spin_unlock_irq(&priv->lock);
95 ipoib_put_ah(mcast->ah);
97 while (!skb_queue_empty(&mcast->pkt_queue)) {
99 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
102 netif_tx_lock_bh(dev);
103 dev->stats.tx_dropped += tx_dropped;
104 netif_tx_unlock_bh(dev);
109 static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
112 struct ipoib_mcast *mcast;
114 mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
119 mcast->created = jiffies;
122 INIT_LIST_HEAD(&mcast->list);
123 INIT_LIST_HEAD(&mcast->neigh_list);
124 skb_queue_head_init(&mcast->pkt_queue);
129 static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
131 struct ipoib_dev_priv *priv = netdev_priv(dev);
132 struct rb_node *n = priv->multicast_tree.rb_node;
135 struct ipoib_mcast *mcast;
138 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
140 ret = memcmp(mgid, mcast->mcmember.mgid.raw,
141 sizeof (union ib_gid));
153 static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
155 struct ipoib_dev_priv *priv = netdev_priv(dev);
156 struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
159 struct ipoib_mcast *tmcast;
163 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
165 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
166 sizeof (union ib_gid));
175 rb_link_node(&mcast->rb_node, pn, n);
176 rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
181 static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
182 struct ib_sa_mcmember_rec *mcmember)
184 struct net_device *dev = mcast->dev;
185 struct ipoib_dev_priv *priv = netdev_priv(dev);
190 mcast->mcmember = *mcmember;
192 /* Set the cached Q_Key before we attach if it's the broadcast group */
193 if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
194 sizeof (union ib_gid))) {
195 spin_lock_irq(&priv->lock);
196 if (!priv->broadcast) {
197 spin_unlock_irq(&priv->lock);
200 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
201 spin_unlock_irq(&priv->lock);
202 priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
206 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
207 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
208 ipoib_warn(priv, "multicast group " IPOIB_GID_FMT
209 " already attached\n",
210 IPOIB_GID_ARG(mcast->mcmember.mgid));
215 ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
216 &mcast->mcmember.mgid, set_qkey);
218 ipoib_warn(priv, "couldn't attach QP to multicast group "
220 IPOIB_GID_ARG(mcast->mcmember.mgid));
222 clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
228 struct ib_ah_attr av = {
229 .dlid = be16_to_cpu(mcast->mcmember.mlid),
230 .port_num = priv->port,
231 .sl = mcast->mcmember.sl,
232 .ah_flags = IB_AH_GRH,
233 .static_rate = mcast->mcmember.rate,
235 .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
236 .hop_limit = mcast->mcmember.hop_limit,
238 .traffic_class = mcast->mcmember.traffic_class
241 av.grh.dgid = mcast->mcmember.mgid;
243 ah = ipoib_create_ah(dev, priv->pd, &av);
245 ipoib_warn(priv, "ib_address_create failed\n");
247 spin_lock_irq(&priv->lock);
249 spin_unlock_irq(&priv->lock);
251 ipoib_dbg_mcast(priv, "MGID " IPOIB_GID_FMT
252 " AV %p, LID 0x%04x, SL %d\n",
253 IPOIB_GID_ARG(mcast->mcmember.mgid),
255 be16_to_cpu(mcast->mcmember.mlid),
260 /* actually send any queued packets */
261 netif_tx_lock_bh(dev);
262 while (!skb_queue_empty(&mcast->pkt_queue)) {
263 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
264 netif_tx_unlock_bh(dev);
268 if (!skb->dst || !skb->dst->neighbour) {
269 /* put pseudoheader back on for next time */
270 skb_push(skb, sizeof (struct ipoib_pseudoheader));
273 if (dev_queue_xmit(skb))
274 ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
275 netif_tx_lock_bh(dev);
277 netif_tx_unlock_bh(dev);
283 ipoib_mcast_sendonly_join_complete(int status,
284 struct ib_sa_multicast *multicast)
286 struct ipoib_mcast *mcast = multicast->context;
287 struct net_device *dev = mcast->dev;
289 /* We trap for port events ourselves. */
290 if (status == -ENETRESET)
294 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
297 if (mcast->logcount++ < 20)
298 ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for "
299 IPOIB_GID_FMT ", status %d\n",
300 IPOIB_GID_ARG(mcast->mcmember.mgid), status);
302 /* Flush out any queued packets */
303 netif_tx_lock_bh(dev);
304 while (!skb_queue_empty(&mcast->pkt_queue)) {
305 ++dev->stats.tx_dropped;
306 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
308 netif_tx_unlock_bh(dev);
310 /* Clear the busy flag so we try again */
311 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY,
317 static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
319 struct net_device *dev = mcast->dev;
320 struct ipoib_dev_priv *priv = netdev_priv(dev);
321 struct ib_sa_mcmember_rec rec = {
322 #if 0 /* Some SMs don't support send-only yet */
330 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
331 ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
335 if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
336 ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
340 rec.mgid = mcast->mcmember.mgid;
341 rec.port_gid = priv->local_gid;
342 rec.pkey = cpu_to_be16(priv->pkey);
344 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca,
346 IB_SA_MCMEMBER_REC_MGID |
347 IB_SA_MCMEMBER_REC_PORT_GID |
348 IB_SA_MCMEMBER_REC_PKEY |
349 IB_SA_MCMEMBER_REC_JOIN_STATE,
351 ipoib_mcast_sendonly_join_complete,
353 if (IS_ERR(mcast->mc)) {
354 ret = PTR_ERR(mcast->mc);
355 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
356 ipoib_warn(priv, "ib_sa_join_multicast failed (ret = %d)\n",
359 ipoib_dbg_mcast(priv, "no multicast record for " IPOIB_GID_FMT
361 IPOIB_GID_ARG(mcast->mcmember.mgid));
367 void ipoib_mcast_carrier_on_task(struct work_struct *work)
369 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
373 * Take rtnl_lock to avoid racing with ipoib_stop() and
374 * turning the carrier back on while a device is being
378 netif_carrier_on(priv->dev);
382 static int ipoib_mcast_join_complete(int status,
383 struct ib_sa_multicast *multicast)
385 struct ipoib_mcast *mcast = multicast->context;
386 struct net_device *dev = mcast->dev;
387 struct ipoib_dev_priv *priv = netdev_priv(dev);
389 ipoib_dbg_mcast(priv, "join completion for " IPOIB_GID_FMT
391 IPOIB_GID_ARG(mcast->mcmember.mgid), status);
393 /* We trap for port events ourselves. */
394 if (status == -ENETRESET)
398 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
402 mutex_lock(&mcast_mutex);
403 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
404 queue_delayed_work(ipoib_workqueue,
405 &priv->mcast_task, 0);
406 mutex_unlock(&mcast_mutex);
409 * Defer carrier on work to ipoib_workqueue to avoid a
410 * deadlock on rtnl_lock here.
412 if (mcast == priv->broadcast)
413 queue_work(ipoib_workqueue, &priv->carrier_on_task);
418 if (mcast->logcount++ < 20) {
419 if (status == -ETIMEDOUT) {
420 ipoib_dbg_mcast(priv, "multicast join failed for " IPOIB_GID_FMT
422 IPOIB_GID_ARG(mcast->mcmember.mgid),
425 ipoib_warn(priv, "multicast join failed for "
426 IPOIB_GID_FMT ", status %d\n",
427 IPOIB_GID_ARG(mcast->mcmember.mgid),
433 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
434 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
436 /* Clear the busy flag so we try again */
437 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
439 mutex_lock(&mcast_mutex);
440 spin_lock_irq(&priv->lock);
441 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
442 queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
443 mcast->backoff * HZ);
444 spin_unlock_irq(&priv->lock);
445 mutex_unlock(&mcast_mutex);
450 static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
453 struct ipoib_dev_priv *priv = netdev_priv(dev);
454 struct ib_sa_mcmember_rec rec = {
457 ib_sa_comp_mask comp_mask;
460 ipoib_dbg_mcast(priv, "joining MGID " IPOIB_GID_FMT "\n",
461 IPOIB_GID_ARG(mcast->mcmember.mgid));
463 rec.mgid = mcast->mcmember.mgid;
464 rec.port_gid = priv->local_gid;
465 rec.pkey = cpu_to_be16(priv->pkey);
468 IB_SA_MCMEMBER_REC_MGID |
469 IB_SA_MCMEMBER_REC_PORT_GID |
470 IB_SA_MCMEMBER_REC_PKEY |
471 IB_SA_MCMEMBER_REC_JOIN_STATE;
475 IB_SA_MCMEMBER_REC_QKEY |
476 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
477 IB_SA_MCMEMBER_REC_MTU |
478 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS |
479 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
480 IB_SA_MCMEMBER_REC_RATE |
481 IB_SA_MCMEMBER_REC_SL |
482 IB_SA_MCMEMBER_REC_FLOW_LABEL |
483 IB_SA_MCMEMBER_REC_HOP_LIMIT;
485 rec.qkey = priv->broadcast->mcmember.qkey;
486 rec.mtu_selector = IB_SA_EQ;
487 rec.mtu = priv->broadcast->mcmember.mtu;
488 rec.traffic_class = priv->broadcast->mcmember.traffic_class;
489 rec.rate_selector = IB_SA_EQ;
490 rec.rate = priv->broadcast->mcmember.rate;
491 rec.sl = priv->broadcast->mcmember.sl;
492 rec.flow_label = priv->broadcast->mcmember.flow_label;
493 rec.hop_limit = priv->broadcast->mcmember.hop_limit;
496 set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
497 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
498 &rec, comp_mask, GFP_KERNEL,
499 ipoib_mcast_join_complete, mcast);
500 if (IS_ERR(mcast->mc)) {
501 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
502 ret = PTR_ERR(mcast->mc);
503 ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
506 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
507 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
509 mutex_lock(&mcast_mutex);
510 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
511 queue_delayed_work(ipoib_workqueue,
513 mcast->backoff * HZ);
514 mutex_unlock(&mcast_mutex);
518 void ipoib_mcast_join_task(struct work_struct *work)
520 struct ipoib_dev_priv *priv =
521 container_of(work, struct ipoib_dev_priv, mcast_task.work);
522 struct net_device *dev = priv->dev;
524 if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
527 if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
528 ipoib_warn(priv, "ib_query_gid() failed\n");
530 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
533 struct ib_port_attr attr;
535 if (!ib_query_port(priv->ca, priv->port, &attr))
536 priv->local_lid = attr.lid;
538 ipoib_warn(priv, "ib_query_port failed\n");
541 if (!priv->broadcast) {
542 struct ipoib_mcast *broadcast;
544 broadcast = ipoib_mcast_alloc(dev, 1);
546 ipoib_warn(priv, "failed to allocate broadcast group\n");
547 mutex_lock(&mcast_mutex);
548 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
549 queue_delayed_work(ipoib_workqueue,
550 &priv->mcast_task, HZ);
551 mutex_unlock(&mcast_mutex);
555 spin_lock_irq(&priv->lock);
556 memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
557 sizeof (union ib_gid));
558 priv->broadcast = broadcast;
560 __ipoib_mcast_add(dev, priv->broadcast);
561 spin_unlock_irq(&priv->lock);
564 if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
565 if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
566 ipoib_mcast_join(dev, priv->broadcast, 0);
571 struct ipoib_mcast *mcast = NULL;
573 spin_lock_irq(&priv->lock);
574 list_for_each_entry(mcast, &priv->multicast_list, list) {
575 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
576 && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
577 && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
578 /* Found the next unjoined group */
582 spin_unlock_irq(&priv->lock);
584 if (&mcast->list == &priv->multicast_list) {
589 ipoib_mcast_join(dev, mcast, 1);
593 priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
595 if (!ipoib_cm_admin_enabled(dev)) {
597 dev_set_mtu(dev, min(priv->mcast_mtu, priv->admin_mtu));
601 ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
603 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
606 int ipoib_mcast_start_thread(struct net_device *dev)
608 struct ipoib_dev_priv *priv = netdev_priv(dev);
610 ipoib_dbg_mcast(priv, "starting multicast thread\n");
612 mutex_lock(&mcast_mutex);
613 if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
614 queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
615 mutex_unlock(&mcast_mutex);
620 int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
622 struct ipoib_dev_priv *priv = netdev_priv(dev);
624 ipoib_dbg_mcast(priv, "stopping multicast thread\n");
626 mutex_lock(&mcast_mutex);
627 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
628 cancel_delayed_work(&priv->mcast_task);
629 mutex_unlock(&mcast_mutex);
632 flush_workqueue(ipoib_workqueue);
637 static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
639 struct ipoib_dev_priv *priv = netdev_priv(dev);
642 if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
643 ib_sa_free_multicast(mcast->mc);
645 if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
646 ipoib_dbg_mcast(priv, "leaving MGID " IPOIB_GID_FMT "\n",
647 IPOIB_GID_ARG(mcast->mcmember.mgid));
649 /* Remove ourselves from the multicast group */
650 ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
651 be16_to_cpu(mcast->mcmember.mlid));
653 ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
659 void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb)
661 struct ipoib_dev_priv *priv = netdev_priv(dev);
662 struct ipoib_mcast *mcast;
665 spin_lock_irqsave(&priv->lock, flags);
667 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags) ||
669 !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
670 ++dev->stats.tx_dropped;
671 dev_kfree_skb_any(skb);
675 mcast = __ipoib_mcast_find(dev, mgid);
677 /* Let's create a new send only group now */
678 ipoib_dbg_mcast(priv, "setting up send only multicast group for "
679 IPOIB_GID_FMT "\n", IPOIB_GID_RAW_ARG(mgid));
681 mcast = ipoib_mcast_alloc(dev, 0);
683 ipoib_warn(priv, "unable to allocate memory for "
684 "multicast structure\n");
685 ++dev->stats.tx_dropped;
686 dev_kfree_skb_any(skb);
690 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
691 memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
692 __ipoib_mcast_add(dev, mcast);
693 list_add_tail(&mcast->list, &priv->multicast_list);
697 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
698 skb_queue_tail(&mcast->pkt_queue, skb);
700 ++dev->stats.tx_dropped;
701 dev_kfree_skb_any(skb);
704 if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
705 ipoib_dbg_mcast(priv, "no address vector, "
706 "but multicast join already started\n");
707 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
708 ipoib_mcast_sendonly_join(mcast);
711 * If lookup completes between here and out:, don't
712 * want to send packet twice.
718 if (mcast && mcast->ah) {
720 skb->dst->neighbour &&
721 !*to_ipoib_neigh(skb->dst->neighbour)) {
722 struct ipoib_neigh *neigh = ipoib_neigh_alloc(skb->dst->neighbour,
726 kref_get(&mcast->ah->ref);
727 neigh->ah = mcast->ah;
728 list_add_tail(&neigh->list, &mcast->neigh_list);
732 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
736 spin_unlock_irqrestore(&priv->lock, flags);
739 void ipoib_mcast_dev_flush(struct net_device *dev)
741 struct ipoib_dev_priv *priv = netdev_priv(dev);
742 LIST_HEAD(remove_list);
743 struct ipoib_mcast *mcast, *tmcast;
746 ipoib_dbg_mcast(priv, "flushing multicast list\n");
748 spin_lock_irqsave(&priv->lock, flags);
750 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
751 list_del(&mcast->list);
752 rb_erase(&mcast->rb_node, &priv->multicast_tree);
753 list_add_tail(&mcast->list, &remove_list);
756 if (priv->broadcast) {
757 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
758 list_add_tail(&priv->broadcast->list, &remove_list);
759 priv->broadcast = NULL;
762 spin_unlock_irqrestore(&priv->lock, flags);
764 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
765 ipoib_mcast_leave(dev, mcast);
766 ipoib_mcast_free(mcast);
770 void ipoib_mcast_restart_task(struct work_struct *work)
772 struct ipoib_dev_priv *priv =
773 container_of(work, struct ipoib_dev_priv, restart_task);
774 struct net_device *dev = priv->dev;
775 struct dev_mc_list *mclist;
776 struct ipoib_mcast *mcast, *tmcast;
777 LIST_HEAD(remove_list);
779 struct ib_sa_mcmember_rec rec;
781 ipoib_dbg_mcast(priv, "restarting multicast task\n");
783 ipoib_mcast_stop_thread(dev, 0);
785 local_irq_save(flags);
786 netif_addr_lock(dev);
787 spin_lock(&priv->lock);
790 * Unfortunately, the networking core only gives us a list of all of
791 * the multicast hardware addresses. We need to figure out which ones
792 * are new and which ones have been removed
795 /* Clear out the found flag */
796 list_for_each_entry(mcast, &priv->multicast_list, list)
797 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
799 /* Mark all of the entries that are found or don't exist */
800 for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
803 memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
805 mcast = __ipoib_mcast_find(dev, &mgid);
806 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
807 struct ipoib_mcast *nmcast;
809 /* ignore group which is directly joined by userspace */
810 if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
811 !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
812 ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid "
813 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
817 /* Not found or send-only group, let's add a new entry */
818 ipoib_dbg_mcast(priv, "adding multicast entry for mgid "
819 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
821 nmcast = ipoib_mcast_alloc(dev, 0);
823 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
827 set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
829 nmcast->mcmember.mgid = mgid;
832 /* Destroy the send only entry */
833 list_move_tail(&mcast->list, &remove_list);
835 rb_replace_node(&mcast->rb_node,
837 &priv->multicast_tree);
839 __ipoib_mcast_add(dev, nmcast);
841 list_add_tail(&nmcast->list, &priv->multicast_list);
845 set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
848 /* Remove all of the entries don't exist anymore */
849 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
850 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
851 !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
852 ipoib_dbg_mcast(priv, "deleting multicast group " IPOIB_GID_FMT "\n",
853 IPOIB_GID_ARG(mcast->mcmember.mgid));
855 rb_erase(&mcast->rb_node, &priv->multicast_tree);
857 /* Move to the remove list */
858 list_move_tail(&mcast->list, &remove_list);
862 spin_unlock(&priv->lock);
863 netif_addr_unlock(dev);
864 local_irq_restore(flags);
866 /* We have to cancel outside of the spinlock */
867 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
868 ipoib_mcast_leave(mcast->dev, mcast);
869 ipoib_mcast_free(mcast);
872 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
873 ipoib_mcast_start_thread(dev);
876 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
878 struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
880 struct ipoib_mcast_iter *iter;
882 iter = kmalloc(sizeof *iter, GFP_KERNEL);
887 memset(iter->mgid.raw, 0, 16);
889 if (ipoib_mcast_iter_next(iter)) {
897 int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
899 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
901 struct ipoib_mcast *mcast;
904 spin_lock_irq(&priv->lock);
906 n = rb_first(&priv->multicast_tree);
909 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
911 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
912 sizeof (union ib_gid)) < 0) {
913 iter->mgid = mcast->mcmember.mgid;
914 iter->created = mcast->created;
915 iter->queuelen = skb_queue_len(&mcast->pkt_queue);
916 iter->complete = !!mcast->ah;
917 iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
927 spin_unlock_irq(&priv->lock);
932 void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
934 unsigned long *created,
935 unsigned int *queuelen,
936 unsigned int *complete,
937 unsigned int *send_only)
940 *created = iter->created;
941 *queuelen = iter->queuelen;
942 *complete = iter->complete;
943 *send_only = iter->send_only;
946 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */