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
34 * $Id: ipoib_multicast.c 1362 2004-12-18 15:56:29Z roland $
37 #include <linux/skbuff.h>
38 #include <linux/rtnetlink.h>
41 #include <linux/igmp.h>
42 #include <linux/inetdevice.h>
43 #include <linux/delay.h>
44 #include <linux/completion.h>
50 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
51 static int mcast_debug_level;
53 module_param(mcast_debug_level, int, 0644);
54 MODULE_PARM_DESC(mcast_debug_level,
55 "Enable multicast debug tracing if > 0");
58 static DEFINE_MUTEX(mcast_mutex);
60 /* Used for all multicast joins (broadcast, IPv4 mcast and IPv6 mcast) */
62 struct ib_sa_mcmember_rec mcmember;
65 struct rb_node rb_node;
66 struct list_head list;
67 struct completion done;
70 struct ib_sa_query *query;
72 unsigned long created;
73 unsigned long backoff;
76 unsigned char logcount;
78 struct list_head neigh_list;
80 struct sk_buff_head pkt_queue;
82 struct net_device *dev;
85 struct ipoib_mcast_iter {
86 struct net_device *dev;
88 unsigned long created;
89 unsigned int queuelen;
90 unsigned int complete;
91 unsigned int send_only;
94 static void ipoib_mcast_free(struct ipoib_mcast *mcast)
96 struct net_device *dev = mcast->dev;
97 struct ipoib_dev_priv *priv = netdev_priv(dev);
98 struct ipoib_neigh *neigh, *tmp;
101 ipoib_dbg_mcast(netdev_priv(dev),
102 "deleting multicast group " IPOIB_GID_FMT "\n",
103 IPOIB_GID_ARG(mcast->mcmember.mgid));
105 spin_lock_irqsave(&priv->lock, flags);
107 list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
109 * It's safe to call ipoib_put_ah() inside priv->lock
110 * here, because we know that mcast->ah will always
111 * hold one more reference, so ipoib_put_ah() will
112 * never do more than decrement the ref count.
115 ipoib_put_ah(neigh->ah);
116 *to_ipoib_neigh(neigh->neighbour) = NULL;
117 neigh->neighbour->ops->destructor = NULL;
121 spin_unlock_irqrestore(&priv->lock, flags);
124 ipoib_put_ah(mcast->ah);
126 while (!skb_queue_empty(&mcast->pkt_queue))
127 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
132 static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
135 struct ipoib_mcast *mcast;
137 mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
142 mcast->created = jiffies;
145 INIT_LIST_HEAD(&mcast->list);
146 INIT_LIST_HEAD(&mcast->neigh_list);
147 skb_queue_head_init(&mcast->pkt_queue);
152 static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, union ib_gid *mgid)
154 struct ipoib_dev_priv *priv = netdev_priv(dev);
155 struct rb_node *n = priv->multicast_tree.rb_node;
158 struct ipoib_mcast *mcast;
161 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
163 ret = memcmp(mgid->raw, mcast->mcmember.mgid.raw,
164 sizeof (union ib_gid));
176 static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
178 struct ipoib_dev_priv *priv = netdev_priv(dev);
179 struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
182 struct ipoib_mcast *tmcast;
186 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
188 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
189 sizeof (union ib_gid));
198 rb_link_node(&mcast->rb_node, pn, n);
199 rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
204 static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
205 struct ib_sa_mcmember_rec *mcmember)
207 struct net_device *dev = mcast->dev;
208 struct ipoib_dev_priv *priv = netdev_priv(dev);
211 mcast->mcmember = *mcmember;
213 /* Set the cached Q_Key before we attach if it's the broadcast group */
214 if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
215 sizeof (union ib_gid))) {
216 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
217 priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
220 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
221 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
222 ipoib_warn(priv, "multicast group " IPOIB_GID_FMT
223 " already attached\n",
224 IPOIB_GID_ARG(mcast->mcmember.mgid));
229 ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
230 &mcast->mcmember.mgid);
232 ipoib_warn(priv, "couldn't attach QP to multicast group "
234 IPOIB_GID_ARG(mcast->mcmember.mgid));
236 clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
242 struct ib_ah_attr av = {
243 .dlid = be16_to_cpu(mcast->mcmember.mlid),
244 .port_num = priv->port,
245 .sl = mcast->mcmember.sl,
246 .ah_flags = IB_AH_GRH,
248 .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
249 .hop_limit = mcast->mcmember.hop_limit,
251 .traffic_class = mcast->mcmember.traffic_class
254 int path_rate = ib_sa_rate_enum_to_int(mcast->mcmember.rate);
256 av.grh.dgid = mcast->mcmember.mgid;
258 if (path_rate > 0 && priv->local_rate > path_rate)
259 av.static_rate = (priv->local_rate - 1) / path_rate;
261 ipoib_dbg_mcast(priv, "static_rate %d for local port %dX, mcmember %dX\n",
262 av.static_rate, priv->local_rate,
263 ib_sa_rate_enum_to_int(mcast->mcmember.rate));
265 mcast->ah = ipoib_create_ah(dev, priv->pd, &av);
267 ipoib_warn(priv, "ib_address_create failed\n");
269 ipoib_dbg_mcast(priv, "MGID " IPOIB_GID_FMT
270 " AV %p, LID 0x%04x, SL %d\n",
271 IPOIB_GID_ARG(mcast->mcmember.mgid),
273 be16_to_cpu(mcast->mcmember.mlid),
278 /* actually send any queued packets */
279 while (!skb_queue_empty(&mcast->pkt_queue)) {
280 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
284 if (!skb->dst || !skb->dst->neighbour) {
285 /* put pseudoheader back on for next time */
286 skb_push(skb, sizeof (struct ipoib_pseudoheader));
289 if (dev_queue_xmit(skb))
290 ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
297 ipoib_mcast_sendonly_join_complete(int status,
298 struct ib_sa_mcmember_rec *mcmember,
301 struct ipoib_mcast *mcast = mcast_ptr;
302 struct net_device *dev = mcast->dev;
305 ipoib_mcast_join_finish(mcast, mcmember);
307 if (mcast->logcount++ < 20)
308 ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for "
309 IPOIB_GID_FMT ", status %d\n",
310 IPOIB_GID_ARG(mcast->mcmember.mgid), status);
312 /* Flush out any queued packets */
313 while (!skb_queue_empty(&mcast->pkt_queue))
314 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
316 /* Clear the busy flag so we try again */
317 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
320 complete(&mcast->done);
323 static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
325 struct net_device *dev = mcast->dev;
326 struct ipoib_dev_priv *priv = netdev_priv(dev);
327 struct ib_sa_mcmember_rec rec = {
328 #if 0 /* Some SMs don't support send-only yet */
336 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
337 ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
341 if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
342 ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
346 rec.mgid = mcast->mcmember.mgid;
347 rec.port_gid = priv->local_gid;
348 rec.pkey = cpu_to_be16(priv->pkey);
350 init_completion(&mcast->done);
352 ret = ib_sa_mcmember_rec_set(priv->ca, priv->port, &rec,
353 IB_SA_MCMEMBER_REC_MGID |
354 IB_SA_MCMEMBER_REC_PORT_GID |
355 IB_SA_MCMEMBER_REC_PKEY |
356 IB_SA_MCMEMBER_REC_JOIN_STATE,
358 ipoib_mcast_sendonly_join_complete,
359 mcast, &mcast->query);
361 ipoib_warn(priv, "ib_sa_mcmember_rec_set failed (ret = %d)\n",
364 ipoib_dbg_mcast(priv, "no multicast record for " IPOIB_GID_FMT
366 IPOIB_GID_ARG(mcast->mcmember.mgid));
368 mcast->query_id = ret;
374 static void ipoib_mcast_join_complete(int status,
375 struct ib_sa_mcmember_rec *mcmember,
378 struct ipoib_mcast *mcast = mcast_ptr;
379 struct net_device *dev = mcast->dev;
380 struct ipoib_dev_priv *priv = netdev_priv(dev);
382 ipoib_dbg_mcast(priv, "join completion for " IPOIB_GID_FMT
384 IPOIB_GID_ARG(mcast->mcmember.mgid), status);
386 if (!status && !ipoib_mcast_join_finish(mcast, mcmember)) {
388 mutex_lock(&mcast_mutex);
389 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
390 queue_work(ipoib_workqueue, &priv->mcast_task);
391 mutex_unlock(&mcast_mutex);
392 complete(&mcast->done);
396 if (status == -EINTR) {
397 complete(&mcast->done);
401 if (status && mcast->logcount++ < 20) {
402 if (status == -ETIMEDOUT || status == -EINTR) {
403 ipoib_dbg_mcast(priv, "multicast join failed for " IPOIB_GID_FMT
405 IPOIB_GID_ARG(mcast->mcmember.mgid),
408 ipoib_warn(priv, "multicast join failed for "
409 IPOIB_GID_FMT ", status %d\n",
410 IPOIB_GID_ARG(mcast->mcmember.mgid),
416 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
417 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
421 mutex_lock(&mcast_mutex);
422 if (test_bit(IPOIB_MCAST_RUN, &priv->flags)) {
423 if (status == -ETIMEDOUT)
424 queue_work(ipoib_workqueue, &priv->mcast_task);
426 queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
427 mcast->backoff * HZ);
429 complete(&mcast->done);
430 mutex_unlock(&mcast_mutex);
435 static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
438 struct ipoib_dev_priv *priv = netdev_priv(dev);
439 struct ib_sa_mcmember_rec rec = {
442 ib_sa_comp_mask comp_mask;
445 ipoib_dbg_mcast(priv, "joining MGID " IPOIB_GID_FMT "\n",
446 IPOIB_GID_ARG(mcast->mcmember.mgid));
448 rec.mgid = mcast->mcmember.mgid;
449 rec.port_gid = priv->local_gid;
450 rec.pkey = cpu_to_be16(priv->pkey);
453 IB_SA_MCMEMBER_REC_MGID |
454 IB_SA_MCMEMBER_REC_PORT_GID |
455 IB_SA_MCMEMBER_REC_PKEY |
456 IB_SA_MCMEMBER_REC_JOIN_STATE;
460 IB_SA_MCMEMBER_REC_QKEY |
461 IB_SA_MCMEMBER_REC_SL |
462 IB_SA_MCMEMBER_REC_FLOW_LABEL |
463 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
465 rec.qkey = priv->broadcast->mcmember.qkey;
466 rec.sl = priv->broadcast->mcmember.sl;
467 rec.flow_label = priv->broadcast->mcmember.flow_label;
468 rec.traffic_class = priv->broadcast->mcmember.traffic_class;
471 init_completion(&mcast->done);
473 ret = ib_sa_mcmember_rec_set(priv->ca, priv->port, &rec, comp_mask,
474 mcast->backoff * 1000, GFP_ATOMIC,
475 ipoib_mcast_join_complete,
476 mcast, &mcast->query);
479 ipoib_warn(priv, "ib_sa_mcmember_rec_set failed, status %d\n", ret);
482 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
483 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
485 mutex_lock(&mcast_mutex);
486 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
487 queue_delayed_work(ipoib_workqueue,
489 mcast->backoff * HZ);
490 mutex_unlock(&mcast_mutex);
492 mcast->query_id = ret;
495 void ipoib_mcast_join_task(void *dev_ptr)
497 struct net_device *dev = dev_ptr;
498 struct ipoib_dev_priv *priv = netdev_priv(dev);
500 if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
503 if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
504 ipoib_warn(priv, "ib_gid_entry_get() failed\n");
506 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
509 struct ib_port_attr attr;
511 if (!ib_query_port(priv->ca, priv->port, &attr)) {
512 priv->local_lid = attr.lid;
513 priv->local_rate = attr.active_speed *
514 ib_width_enum_to_int(attr.active_width);
516 ipoib_warn(priv, "ib_query_port failed\n");
519 if (!priv->broadcast) {
520 priv->broadcast = ipoib_mcast_alloc(dev, 1);
521 if (!priv->broadcast) {
522 ipoib_warn(priv, "failed to allocate broadcast group\n");
523 mutex_lock(&mcast_mutex);
524 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
525 queue_delayed_work(ipoib_workqueue,
526 &priv->mcast_task, HZ);
527 mutex_unlock(&mcast_mutex);
531 memcpy(priv->broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
532 sizeof (union ib_gid));
534 spin_lock_irq(&priv->lock);
535 __ipoib_mcast_add(dev, priv->broadcast);
536 spin_unlock_irq(&priv->lock);
539 if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
540 ipoib_mcast_join(dev, priv->broadcast, 0);
545 struct ipoib_mcast *mcast = NULL;
547 spin_lock_irq(&priv->lock);
548 list_for_each_entry(mcast, &priv->multicast_list, list) {
549 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
550 && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
551 && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
552 /* Found the next unjoined group */
556 spin_unlock_irq(&priv->lock);
558 if (&mcast->list == &priv->multicast_list) {
563 ipoib_mcast_join(dev, mcast, 1);
567 priv->mcast_mtu = ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu) -
569 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
571 ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
573 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
574 netif_carrier_on(dev);
577 int ipoib_mcast_start_thread(struct net_device *dev)
579 struct ipoib_dev_priv *priv = netdev_priv(dev);
581 ipoib_dbg_mcast(priv, "starting multicast thread\n");
583 mutex_lock(&mcast_mutex);
584 if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
585 queue_work(ipoib_workqueue, &priv->mcast_task);
586 mutex_unlock(&mcast_mutex);
591 int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
593 struct ipoib_dev_priv *priv = netdev_priv(dev);
594 struct ipoib_mcast *mcast;
596 ipoib_dbg_mcast(priv, "stopping multicast thread\n");
598 mutex_lock(&mcast_mutex);
599 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
600 cancel_delayed_work(&priv->mcast_task);
601 mutex_unlock(&mcast_mutex);
604 flush_workqueue(ipoib_workqueue);
606 if (priv->broadcast && priv->broadcast->query) {
607 ib_sa_cancel_query(priv->broadcast->query_id, priv->broadcast->query);
608 priv->broadcast->query = NULL;
609 ipoib_dbg_mcast(priv, "waiting for bcast\n");
610 wait_for_completion(&priv->broadcast->done);
613 list_for_each_entry(mcast, &priv->multicast_list, list) {
615 ib_sa_cancel_query(mcast->query_id, mcast->query);
617 ipoib_dbg_mcast(priv, "waiting for MGID " IPOIB_GID_FMT "\n",
618 IPOIB_GID_ARG(mcast->mcmember.mgid));
619 wait_for_completion(&mcast->done);
626 static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
628 struct ipoib_dev_priv *priv = netdev_priv(dev);
629 struct ib_sa_mcmember_rec rec = {
634 if (!test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags))
637 ipoib_dbg_mcast(priv, "leaving MGID " IPOIB_GID_FMT "\n",
638 IPOIB_GID_ARG(mcast->mcmember.mgid));
640 rec.mgid = mcast->mcmember.mgid;
641 rec.port_gid = priv->local_gid;
642 rec.pkey = cpu_to_be16(priv->pkey);
644 /* Remove ourselves from the multicast group */
645 ret = ipoib_mcast_detach(dev, be16_to_cpu(mcast->mcmember.mlid),
646 &mcast->mcmember.mgid);
648 ipoib_warn(priv, "ipoib_mcast_detach failed (result = %d)\n", ret);
651 * Just make one shot at leaving and don't wait for a reply;
652 * if we fail, too bad.
654 ret = ib_sa_mcmember_rec_delete(priv->ca, priv->port, &rec,
655 IB_SA_MCMEMBER_REC_MGID |
656 IB_SA_MCMEMBER_REC_PORT_GID |
657 IB_SA_MCMEMBER_REC_PKEY |
658 IB_SA_MCMEMBER_REC_JOIN_STATE,
660 mcast, &mcast->query);
662 ipoib_warn(priv, "ib_sa_mcmember_rec_delete failed "
663 "for leave (result = %d)\n", ret);
668 void ipoib_mcast_send(struct net_device *dev, union ib_gid *mgid,
671 struct ipoib_dev_priv *priv = netdev_priv(dev);
672 struct ipoib_mcast *mcast;
675 * We can only be called from ipoib_start_xmit, so we're
676 * inside tx_lock -- no need to save/restore flags.
678 spin_lock(&priv->lock);
680 mcast = __ipoib_mcast_find(dev, mgid);
682 /* Let's create a new send only group now */
683 ipoib_dbg_mcast(priv, "setting up send only multicast group for "
684 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(*mgid));
686 mcast = ipoib_mcast_alloc(dev, 0);
688 ipoib_warn(priv, "unable to allocate memory for "
689 "multicast structure\n");
690 dev_kfree_skb_any(skb);
694 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
695 mcast->mcmember.mgid = *mgid;
696 __ipoib_mcast_add(dev, mcast);
697 list_add_tail(&mcast->list, &priv->multicast_list);
701 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
702 skb_queue_tail(&mcast->pkt_queue, skb);
704 dev_kfree_skb_any(skb);
707 ipoib_dbg_mcast(priv, "no address vector, "
708 "but multicast join already started\n");
709 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
710 ipoib_mcast_sendonly_join(mcast);
713 * If lookup completes between here and out:, don't
714 * want to send packet twice.
720 if (mcast && mcast->ah) {
722 skb->dst->neighbour &&
723 !*to_ipoib_neigh(skb->dst->neighbour)) {
724 struct ipoib_neigh *neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
727 kref_get(&mcast->ah->ref);
728 neigh->ah = mcast->ah;
729 neigh->neighbour = skb->dst->neighbour;
730 *to_ipoib_neigh(skb->dst->neighbour) = neigh;
731 list_add_tail(&neigh->list, &mcast->neigh_list);
735 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
738 spin_unlock(&priv->lock);
741 void ipoib_mcast_dev_flush(struct net_device *dev)
743 struct ipoib_dev_priv *priv = netdev_priv(dev);
744 LIST_HEAD(remove_list);
745 struct ipoib_mcast *mcast, *tmcast;
748 ipoib_dbg_mcast(priv, "flushing multicast list\n");
750 spin_lock_irqsave(&priv->lock, flags);
752 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
753 list_del(&mcast->list);
754 rb_erase(&mcast->rb_node, &priv->multicast_tree);
755 list_add_tail(&mcast->list, &remove_list);
758 if (priv->broadcast) {
759 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
760 list_add_tail(&priv->broadcast->list, &remove_list);
761 priv->broadcast = NULL;
764 spin_unlock_irqrestore(&priv->lock, flags);
766 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
767 ipoib_mcast_leave(dev, mcast);
768 ipoib_mcast_free(mcast);
772 void ipoib_mcast_restart_task(void *dev_ptr)
774 struct net_device *dev = dev_ptr;
775 struct ipoib_dev_priv *priv = netdev_priv(dev);
776 struct dev_mc_list *mclist;
777 struct ipoib_mcast *mcast, *tmcast;
778 LIST_HEAD(remove_list);
781 ipoib_dbg_mcast(priv, "restarting multicast task\n");
783 ipoib_mcast_stop_thread(dev, 0);
785 spin_lock_irqsave(&dev->xmit_lock, flags);
786 spin_lock(&priv->lock);
789 * Unfortunately, the networking core only gives us a list of all of
790 * the multicast hardware addresses. We need to figure out which ones
791 * are new and which ones have been removed
794 /* Clear out the found flag */
795 list_for_each_entry(mcast, &priv->multicast_list, list)
796 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
798 /* Mark all of the entries that are found or don't exist */
799 for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
802 memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
804 /* Add in the P_Key */
805 mgid.raw[4] = (priv->pkey >> 8) & 0xff;
806 mgid.raw[5] = priv->pkey & 0xff;
808 mcast = __ipoib_mcast_find(dev, &mgid);
809 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
810 struct ipoib_mcast *nmcast;
812 /* Not found or send-only group, let's add a new entry */
813 ipoib_dbg_mcast(priv, "adding multicast entry for mgid "
814 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
816 nmcast = ipoib_mcast_alloc(dev, 0);
818 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
822 set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
824 nmcast->mcmember.mgid = mgid;
827 /* Destroy the send only entry */
828 list_del(&mcast->list);
829 list_add_tail(&mcast->list, &remove_list);
831 rb_replace_node(&mcast->rb_node,
833 &priv->multicast_tree);
835 __ipoib_mcast_add(dev, nmcast);
837 list_add_tail(&nmcast->list, &priv->multicast_list);
841 set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
844 /* Remove all of the entries don't exist anymore */
845 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
846 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
847 !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
848 ipoib_dbg_mcast(priv, "deleting multicast group " IPOIB_GID_FMT "\n",
849 IPOIB_GID_ARG(mcast->mcmember.mgid));
851 rb_erase(&mcast->rb_node, &priv->multicast_tree);
853 /* Move to the remove list */
854 list_del(&mcast->list);
855 list_add_tail(&mcast->list, &remove_list);
859 spin_unlock(&priv->lock);
860 spin_unlock_irqrestore(&dev->xmit_lock, flags);
862 /* We have to cancel outside of the spinlock */
863 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
864 ipoib_mcast_leave(mcast->dev, mcast);
865 ipoib_mcast_free(mcast);
868 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
869 ipoib_mcast_start_thread(dev);
872 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
874 struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
876 struct ipoib_mcast_iter *iter;
878 iter = kmalloc(sizeof *iter, GFP_KERNEL);
883 memset(iter->mgid.raw, 0, 16);
885 if (ipoib_mcast_iter_next(iter)) {
893 int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
895 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
897 struct ipoib_mcast *mcast;
900 spin_lock_irq(&priv->lock);
902 n = rb_first(&priv->multicast_tree);
905 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
907 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
908 sizeof (union ib_gid)) < 0) {
909 iter->mgid = mcast->mcmember.mgid;
910 iter->created = mcast->created;
911 iter->queuelen = skb_queue_len(&mcast->pkt_queue);
912 iter->complete = !!mcast->ah;
913 iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
923 spin_unlock_irq(&priv->lock);
928 void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
930 unsigned long *created,
931 unsigned int *queuelen,
932 unsigned int *complete,
933 unsigned int *send_only)
936 *created = iter->created;
937 *queuelen = iter->queuelen;
938 *complete = iter->complete;
939 *send_only = iter->send_only;
942 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */