2 * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
4 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
5 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
6 * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
8 * This software is available to you under a choice of one of two
9 * licenses. You may choose to be licensed under the terms of the GNU
10 * General Public License (GPL) Version 2, available from the file
11 * COPYING in the main directory of this source tree, or the
12 * OpenIB.org BSD license below:
14 * Redistribution and use in source and binary forms, with or
15 * without modification, are permitted provided that the following
18 * - Redistributions of source code must retain the above
19 * copyright notice, this list of conditions and the following
22 * - Redistributions in binary form must reproduce the above
23 * copyright notice, this list of conditions and the following
24 * disclaimer in the documentation and/or other materials
25 * provided with the distribution.
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
31 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
32 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
33 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
42 #include <linux/delay.h>
43 #include <linux/completion.h>
45 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
46 static int mcast_debug_level = 1;
48 module_param(mcast_debug_level, int, 0644);
49 MODULE_PARM_DESC(mcast_debug_level,
50 "Enable multicast debug tracing if > 0");
53 static DEFINE_MUTEX(mcast_mutex);
55 struct ipoib_mcast_iter {
56 struct ipoib_dev_priv *priv;
58 unsigned long created;
59 unsigned int queuelen;
60 unsigned int complete;
61 unsigned int send_only;
64 static void ipoib_mcast_free(struct ipoib_mcast *mcast)
66 struct ifnet *dev = mcast->priv->dev;
69 ipoib_dbg_mcast(mcast->priv, "deleting multicast group %16D\n",
70 mcast->mcmember.mgid.raw, ":");
73 ipoib_put_ah(mcast->ah);
75 tx_dropped = mcast->pkt_queue.ifq_len;
76 _IF_DRAIN(&mcast->pkt_queue); /* XXX Locking. */
78 if_inc_counter(dev, IFCOUNTER_OERRORS, tx_dropped);
83 static struct ipoib_mcast *ipoib_mcast_alloc(struct ipoib_dev_priv *priv,
86 struct ipoib_mcast *mcast;
88 mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
93 mcast->created = jiffies;
96 INIT_LIST_HEAD(&mcast->list);
97 bzero(&mcast->pkt_queue, sizeof(mcast->pkt_queue));
102 static struct ipoib_mcast *__ipoib_mcast_find(struct ipoib_dev_priv *priv,
105 struct rb_node *n = priv->multicast_tree.rb_node;
108 struct ipoib_mcast *mcast;
111 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
113 ret = memcmp(mgid, mcast->mcmember.mgid.raw,
114 sizeof (union ib_gid));
126 static int __ipoib_mcast_add(struct ipoib_dev_priv *priv,
127 struct ipoib_mcast *mcast)
129 struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
132 struct ipoib_mcast *tmcast;
136 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
138 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
139 sizeof (union ib_gid));
148 rb_link_node(&mcast->rb_node, pn, n);
149 rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
154 static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
155 struct ib_sa_mcmember_rec *mcmember)
157 struct ipoib_dev_priv *priv = mcast->priv;
158 struct ifnet *dev = priv->dev;
163 mcast->mcmember = *mcmember;
165 /* Set the cached Q_Key before we attach if it's the broadcast group */
166 if (!memcmp(mcast->mcmember.mgid.raw, dev->if_broadcastaddr + 4,
167 sizeof (union ib_gid))) {
168 spin_lock_irq(&priv->lock);
169 if (!priv->broadcast) {
170 spin_unlock_irq(&priv->lock);
173 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
174 spin_unlock_irq(&priv->lock);
175 priv->tx_wr.remote_qkey = priv->qkey;
179 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
180 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
181 ipoib_warn(priv, "multicast group %16D already attached\n",
182 mcast->mcmember.mgid.raw, ":");
187 ret = ipoib_mcast_attach(priv, be16_to_cpu(mcast->mcmember.mlid),
188 &mcast->mcmember.mgid, set_qkey);
190 ipoib_warn(priv, "couldn't attach QP to multicast group %16D\n",
191 mcast->mcmember.mgid.raw, ":");
193 clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
199 struct ib_ah_attr av = {
200 .dlid = be16_to_cpu(mcast->mcmember.mlid),
201 .port_num = priv->port,
202 .sl = mcast->mcmember.sl,
203 .ah_flags = IB_AH_GRH,
204 .static_rate = mcast->mcmember.rate,
206 .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
207 .hop_limit = mcast->mcmember.hop_limit,
209 .traffic_class = mcast->mcmember.traffic_class
212 av.grh.dgid = mcast->mcmember.mgid;
214 ah = ipoib_create_ah(priv, priv->pd, &av);
216 ipoib_warn(priv, "ib_address_create failed\n");
218 spin_lock_irq(&priv->lock);
220 spin_unlock_irq(&priv->lock);
222 ipoib_dbg_mcast(priv, "MGID %16D AV %p, LID 0x%04x, SL %d\n",
223 mcast->mcmember.mgid.raw, ":",
225 be16_to_cpu(mcast->mcmember.mlid),
230 /* actually send any queued packets */
231 while (mcast->pkt_queue.ifq_len) {
233 _IF_DEQUEUE(&mcast->pkt_queue, mb);
234 mb->m_pkthdr.rcvif = dev;
236 if (dev->if_transmit(dev, mb))
237 ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
244 ipoib_mcast_sendonly_join_complete(int status,
245 struct ib_sa_multicast *multicast)
247 struct ipoib_mcast *mcast = multicast->context;
248 struct ipoib_dev_priv *priv = mcast->priv;
250 /* We trap for port events ourselves. */
251 if (status == -ENETRESET)
255 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
258 if (mcast->logcount++ < 20)
259 ipoib_dbg_mcast(priv, "multicast join failed for %16D, status %d\n",
260 mcast->mcmember.mgid.raw, ":", status);
262 /* Flush out any queued packets */
263 if_inc_counter(priv->dev, IFCOUNTER_OERRORS, mcast->pkt_queue.ifq_len);
264 _IF_DRAIN(&mcast->pkt_queue);
266 /* Clear the busy flag so we try again */
267 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY,
273 static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
275 struct ipoib_dev_priv *priv = mcast->priv;
276 struct ib_sa_mcmember_rec rec = {
277 #if 0 /* Some SMs don't support send-only yet */
285 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
286 ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
290 if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
291 ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
295 rec.mgid = mcast->mcmember.mgid;
296 rec.port_gid = priv->local_gid;
297 rec.pkey = cpu_to_be16(priv->pkey);
299 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca,
301 IB_SA_MCMEMBER_REC_MGID |
302 IB_SA_MCMEMBER_REC_PORT_GID |
303 IB_SA_MCMEMBER_REC_PKEY |
304 IB_SA_MCMEMBER_REC_JOIN_STATE,
306 ipoib_mcast_sendonly_join_complete,
308 if (IS_ERR(mcast->mc)) {
309 ret = PTR_ERR(mcast->mc);
310 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
311 ipoib_warn(priv, "ib_sa_join_multicast failed (ret = %d)\n",
314 ipoib_dbg_mcast(priv, "no multicast record for %16D, starting join\n",
315 mcast->mcmember.mgid.raw, ":");
321 void ipoib_mcast_carrier_on_task(struct work_struct *work)
323 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
325 struct ib_port_attr attr;
328 * Take rtnl_lock to avoid racing with ipoib_stop() and
329 * turning the carrier back on while a device is being
332 if (ib_query_port(priv->ca, priv->port, &attr) ||
333 attr.state != IB_PORT_ACTIVE) {
334 ipoib_dbg(priv, "Keeping carrier off until IB port is active\n");
337 if_link_state_change(priv->dev, LINK_STATE_UP);
340 static int ipoib_mcast_join_complete(int status,
341 struct ib_sa_multicast *multicast)
343 struct ipoib_mcast *mcast = multicast->context;
344 struct ipoib_dev_priv *priv = mcast->priv;
346 ipoib_dbg_mcast(priv, "join completion for %16D (status %d)\n",
347 mcast->mcmember.mgid.raw, ":", status);
349 /* We trap for port events ourselves. */
350 if (status == -ENETRESET)
354 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
358 mutex_lock(&mcast_mutex);
359 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
360 queue_delayed_work(ipoib_workqueue,
361 &priv->mcast_task, 0);
362 mutex_unlock(&mcast_mutex);
365 * Defer carrier on work to ipoib_workqueue to avoid a
366 * deadlock on rtnl_lock here.
368 if (mcast == priv->broadcast)
369 queue_work(ipoib_workqueue, &priv->carrier_on_task);
374 if (mcast->logcount++ < 20) {
375 if (status == -ETIMEDOUT || status == -EAGAIN) {
376 ipoib_dbg_mcast(priv, "multicast join failed for %16D, status %d\n",
377 mcast->mcmember.mgid.raw, ":", status);
379 ipoib_warn(priv, "multicast join failed for %16D, status %d\n",
380 mcast->mcmember.mgid.raw, ":", status);
385 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
386 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
388 /* Clear the busy flag so we try again */
389 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
391 mutex_lock(&mcast_mutex);
392 spin_lock_irq(&priv->lock);
393 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
394 queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
395 mcast->backoff * HZ);
396 spin_unlock_irq(&priv->lock);
397 mutex_unlock(&mcast_mutex);
402 static void ipoib_mcast_join(struct ipoib_dev_priv *priv,
403 struct ipoib_mcast *mcast, int create)
405 struct ib_sa_mcmember_rec rec = {
408 ib_sa_comp_mask comp_mask;
411 ipoib_dbg_mcast(priv, "joining MGID %16D\n",
412 mcast->mcmember.mgid.raw, ":");
414 rec.mgid = mcast->mcmember.mgid;
415 rec.port_gid = priv->local_gid;
416 rec.pkey = cpu_to_be16(priv->pkey);
419 IB_SA_MCMEMBER_REC_MGID |
420 IB_SA_MCMEMBER_REC_PORT_GID |
421 IB_SA_MCMEMBER_REC_PKEY |
422 IB_SA_MCMEMBER_REC_JOIN_STATE;
426 IB_SA_MCMEMBER_REC_QKEY |
427 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
428 IB_SA_MCMEMBER_REC_MTU |
429 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS |
430 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
431 IB_SA_MCMEMBER_REC_RATE |
432 IB_SA_MCMEMBER_REC_SL |
433 IB_SA_MCMEMBER_REC_FLOW_LABEL |
434 IB_SA_MCMEMBER_REC_HOP_LIMIT;
436 rec.qkey = priv->broadcast->mcmember.qkey;
437 rec.mtu_selector = IB_SA_EQ;
438 rec.mtu = priv->broadcast->mcmember.mtu;
439 rec.traffic_class = priv->broadcast->mcmember.traffic_class;
440 rec.rate_selector = IB_SA_EQ;
441 rec.rate = priv->broadcast->mcmember.rate;
442 rec.sl = priv->broadcast->mcmember.sl;
443 rec.flow_label = priv->broadcast->mcmember.flow_label;
444 rec.hop_limit = priv->broadcast->mcmember.hop_limit;
447 set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
448 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
449 &rec, comp_mask, GFP_KERNEL,
450 ipoib_mcast_join_complete, mcast);
451 if (IS_ERR(mcast->mc)) {
452 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
453 ret = PTR_ERR(mcast->mc);
454 ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
457 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
458 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
460 mutex_lock(&mcast_mutex);
461 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
462 queue_delayed_work(ipoib_workqueue,
464 mcast->backoff * HZ);
465 mutex_unlock(&mcast_mutex);
469 void ipoib_mcast_join_task(struct work_struct *work)
471 struct ipoib_dev_priv *priv =
472 container_of(work, struct ipoib_dev_priv, mcast_task.work);
473 struct ifnet *dev = priv->dev;
474 struct ib_port_attr attr;
476 ipoib_dbg_mcast(priv, "Running join task. flags 0x%lX\n", priv->flags);
478 if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
481 if (ib_query_port(priv->ca, priv->port, &attr) ||
482 attr.state != IB_PORT_ACTIVE) {
483 ipoib_dbg(priv, "%s: port state is not ACTIVE (state = %d) suspend task.\n",
484 __func__, attr.state);
488 if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid, NULL))
489 ipoib_warn(priv, "ib_query_gid() failed\n");
491 memcpy(IF_LLADDR(dev) + 4, priv->local_gid.raw, sizeof (union ib_gid));
494 struct ib_port_attr attr;
496 if (!ib_query_port(priv->ca, priv->port, &attr))
497 priv->local_lid = attr.lid;
499 ipoib_warn(priv, "ib_query_port failed\n");
502 if (!priv->broadcast) {
503 struct ipoib_mcast *broadcast;
505 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
508 broadcast = ipoib_mcast_alloc(priv, 1);
510 ipoib_warn(priv, "failed to allocate broadcast group\n");
511 mutex_lock(&mcast_mutex);
512 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
513 queue_delayed_work(ipoib_workqueue,
514 &priv->mcast_task, HZ);
515 mutex_unlock(&mcast_mutex);
519 spin_lock_irq(&priv->lock);
520 memcpy(broadcast->mcmember.mgid.raw, dev->if_broadcastaddr + 4,
521 sizeof (union ib_gid));
522 priv->broadcast = broadcast;
524 __ipoib_mcast_add(priv, priv->broadcast);
525 spin_unlock_irq(&priv->lock);
528 if (priv->broadcast &&
529 !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
530 if (priv->broadcast &&
531 !test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
532 ipoib_mcast_join(priv, priv->broadcast, 0);
537 struct ipoib_mcast *mcast = NULL;
539 spin_lock_irq(&priv->lock);
540 list_for_each_entry(mcast, &priv->multicast_list, list) {
541 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
542 && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
543 && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
544 /* Found the next unjoined group */
548 spin_unlock_irq(&priv->lock);
550 if (&mcast->list == &priv->multicast_list) {
555 ipoib_mcast_join(priv, mcast, 1);
559 spin_lock_irq(&priv->lock);
561 priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
563 priv->mcast_mtu = priv->admin_mtu;
564 spin_unlock_irq(&priv->lock);
566 if (!ipoib_cm_admin_enabled(priv))
567 ipoib_change_mtu(priv, min(priv->mcast_mtu, priv->admin_mtu));
569 ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
571 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
574 int ipoib_mcast_start_thread(struct ipoib_dev_priv *priv)
576 ipoib_dbg_mcast(priv, "starting multicast thread flags 0x%lX\n",
579 mutex_lock(&mcast_mutex);
580 if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
581 queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
582 mutex_unlock(&mcast_mutex);
587 int ipoib_mcast_stop_thread(struct ipoib_dev_priv *priv, int flush)
590 ipoib_dbg_mcast(priv, "stopping multicast thread\n");
592 mutex_lock(&mcast_mutex);
593 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
594 cancel_delayed_work(&priv->mcast_task);
595 mutex_unlock(&mcast_mutex);
598 flush_workqueue(ipoib_workqueue);
603 static int ipoib_mcast_leave(struct ipoib_dev_priv *priv, struct ipoib_mcast *mcast)
607 if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
608 ib_sa_free_multicast(mcast->mc);
610 if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
611 ipoib_dbg_mcast(priv, "leaving MGID %16D\n",
612 mcast->mcmember.mgid.raw, ":");
614 /* Remove ourselves from the multicast group */
615 ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
616 be16_to_cpu(mcast->mcmember.mlid));
618 ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
625 ipoib_mcast_send(struct ipoib_dev_priv *priv, void *mgid, struct mbuf *mb)
627 struct ifnet *dev = priv->dev;
628 struct ipoib_mcast *mcast;
630 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags) ||
632 !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
633 if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
638 mcast = __ipoib_mcast_find(priv, mgid);
640 /* Let's create a new send only group now */
641 ipoib_dbg_mcast(priv, "setting up send only multicast group for %16D\n",
644 mcast = ipoib_mcast_alloc(priv, 0);
646 ipoib_warn(priv, "unable to allocate memory for "
647 "multicast structure\n");
648 if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
653 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
654 memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
655 __ipoib_mcast_add(priv, mcast);
656 list_add_tail(&mcast->list, &priv->multicast_list);
660 if (mcast->pkt_queue.ifq_len < IPOIB_MAX_MCAST_QUEUE) {
661 _IF_ENQUEUE(&mcast->pkt_queue, mb);
663 if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
667 if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
668 ipoib_dbg_mcast(priv, "no address vector, "
669 "but multicast join already started\n");
670 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
671 ipoib_mcast_sendonly_join(mcast);
674 * If lookup completes between here and out:, don't
675 * want to send packet twice.
681 if (mcast && mcast->ah)
682 ipoib_send(priv, mb, mcast->ah, IB_MULTICAST_QPN);
685 void ipoib_mcast_dev_flush(struct ipoib_dev_priv *priv)
687 LIST_HEAD(remove_list);
688 struct ipoib_mcast *mcast, *tmcast;
691 ipoib_dbg_mcast(priv, "flushing multicast list\n");
693 spin_lock_irqsave(&priv->lock, flags);
695 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
696 list_del(&mcast->list);
697 rb_erase(&mcast->rb_node, &priv->multicast_tree);
698 list_add_tail(&mcast->list, &remove_list);
701 if (priv->broadcast) {
702 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
703 list_add_tail(&priv->broadcast->list, &remove_list);
704 priv->broadcast = NULL;
707 spin_unlock_irqrestore(&priv->lock, flags);
709 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
710 ipoib_mcast_leave(priv, mcast);
711 ipoib_mcast_free(mcast);
715 static int ipoib_mcast_addr_is_valid(const u8 *addr, unsigned int addrlen,
718 if (addrlen != INFINIBAND_ALEN)
720 /* reserved QPN, prefix, scope */
721 if (memcmp(addr, broadcast, 6))
723 /* signature lower, pkey */
724 if (memcmp(addr + 7, broadcast + 7, 3))
729 void ipoib_mcast_restart_task(struct work_struct *work)
731 struct ipoib_dev_priv *priv =
732 container_of(work, struct ipoib_dev_priv, restart_task);
733 ipoib_mcast_restart(priv);
736 void ipoib_mcast_restart(struct ipoib_dev_priv *priv)
738 struct ifnet *dev = priv->dev;
739 struct ifmultiaddr *ifma;
740 struct ipoib_mcast *mcast, *tmcast;
741 LIST_HEAD(remove_list);
742 struct ib_sa_mcmember_rec rec;
745 ipoib_dbg_mcast(priv, "restarting multicast task flags 0x%lX\n",
748 ipoib_mcast_stop_thread(priv, 0);
751 spin_lock(&priv->lock);
754 * Unfortunately, the networking core only gives us a list of all of
755 * the multicast hardware addresses. We need to figure out which ones
756 * are new and which ones have been removed
759 /* Clear out the found flag */
760 list_for_each_entry(mcast, &priv->multicast_list, list)
761 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
763 /* Mark all of the entries that are found or don't exist */
766 CK_STAILQ_FOREACH(ifma, &dev->if_multiaddrs, ifma_link) {
770 if (ifma->ifma_addr->sa_family != AF_LINK)
772 addr = LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
773 addrlen = ((struct sockaddr_dl *)ifma->ifma_addr)->sdl_alen;
774 if (!ipoib_mcast_addr_is_valid(addr, addrlen,
775 dev->if_broadcastaddr))
778 memcpy(mgid.raw, addr + 4, sizeof mgid);
780 mcast = __ipoib_mcast_find(priv, &mgid);
781 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
782 struct ipoib_mcast *nmcast;
784 /* ignore group which is directly joined by userspace */
785 if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
786 !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
787 ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %16D\n",
792 /* Not found or send-only group, let's add a new entry */
793 ipoib_dbg_mcast(priv, "adding multicast entry for mgid %16D\n",
796 nmcast = ipoib_mcast_alloc(priv, 0);
798 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
802 set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
804 nmcast->mcmember.mgid = mgid;
807 /* Destroy the send only entry */
808 list_move_tail(&mcast->list, &remove_list);
810 rb_replace_node(&mcast->rb_node,
812 &priv->multicast_tree);
814 __ipoib_mcast_add(priv, nmcast);
816 list_add_tail(&nmcast->list, &priv->multicast_list);
820 set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
823 /* Remove all of the entries don't exist anymore */
824 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
825 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
826 !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
827 ipoib_dbg_mcast(priv, "deleting multicast group %16D\n",
828 mcast->mcmember.mgid.raw, ":");
830 rb_erase(&mcast->rb_node, &priv->multicast_tree);
832 /* Move to the remove list */
833 list_move_tail(&mcast->list, &remove_list);
837 spin_unlock(&priv->lock);
838 if_maddr_runlock(dev);
840 /* We have to cancel outside of the spinlock */
841 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
842 ipoib_mcast_leave(mcast->priv, mcast);
843 ipoib_mcast_free(mcast);
846 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
847 ipoib_mcast_start_thread(priv);
850 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
852 struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct ipoib_dev_priv *priv)
854 struct ipoib_mcast_iter *iter;
856 iter = kmalloc(sizeof *iter, GFP_KERNEL);
861 memset(iter->mgid.raw, 0, 16);
863 if (ipoib_mcast_iter_next(iter)) {
871 int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
873 struct ipoib_dev_priv *priv = iter->priv;
875 struct ipoib_mcast *mcast;
878 spin_lock_irq(&priv->lock);
880 n = rb_first(&priv->multicast_tree);
883 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
885 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
886 sizeof (union ib_gid)) < 0) {
887 iter->mgid = mcast->mcmember.mgid;
888 iter->created = mcast->created;
889 iter->queuelen = mcast->pkt_queue.ifq_len;
890 iter->complete = !!mcast->ah;
891 iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
901 spin_unlock_irq(&priv->lock);
906 void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
908 unsigned long *created,
909 unsigned int *queuelen,
910 unsigned int *complete,
911 unsigned int *send_only)
914 *created = iter->created;
915 *queuelen = iter->queuelen;
916 *complete = iter->complete;
917 *send_only = iter->send_only;
920 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */