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
39 #include <linux/delay.h>
40 #include <linux/completion.h>
42 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
43 static int mcast_debug_level = 1;
45 module_param(mcast_debug_level, int, 0644);
46 MODULE_PARM_DESC(mcast_debug_level,
47 "Enable multicast debug tracing if > 0");
50 static DEFINE_MUTEX(mcast_mutex);
52 struct ipoib_mcast_iter {
53 struct ipoib_dev_priv *priv;
55 unsigned long created;
56 unsigned int queuelen;
57 unsigned int complete;
58 unsigned int send_only;
61 static void ipoib_mcast_free(struct ipoib_mcast *mcast)
63 struct ifnet *dev = mcast->priv->dev;
66 ipoib_dbg_mcast(mcast->priv, "deleting multicast group %16D\n",
67 mcast->mcmember.mgid.raw, ":");
70 ipoib_put_ah(mcast->ah);
72 tx_dropped = mcast->pkt_queue.ifq_len;
73 _IF_DRAIN(&mcast->pkt_queue); /* XXX Locking. */
75 if_inc_counter(dev, IFCOUNTER_OERRORS, tx_dropped);
80 static struct ipoib_mcast *ipoib_mcast_alloc(struct ipoib_dev_priv *priv,
83 struct ipoib_mcast *mcast;
85 mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
90 mcast->created = jiffies;
93 INIT_LIST_HEAD(&mcast->list);
94 bzero(&mcast->pkt_queue, sizeof(mcast->pkt_queue));
99 static struct ipoib_mcast *__ipoib_mcast_find(struct ipoib_dev_priv *priv,
102 struct rb_node *n = priv->multicast_tree.rb_node;
105 struct ipoib_mcast *mcast;
108 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
110 ret = memcmp(mgid, mcast->mcmember.mgid.raw,
111 sizeof (union ib_gid));
123 static int __ipoib_mcast_add(struct ipoib_dev_priv *priv,
124 struct ipoib_mcast *mcast)
126 struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
129 struct ipoib_mcast *tmcast;
133 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
135 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
136 sizeof (union ib_gid));
145 rb_link_node(&mcast->rb_node, pn, n);
146 rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
151 static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
152 struct ib_sa_mcmember_rec *mcmember)
154 struct ipoib_dev_priv *priv = mcast->priv;
155 struct ifnet *dev = priv->dev;
160 mcast->mcmember = *mcmember;
162 /* Set the cached Q_Key before we attach if it's the broadcast group */
163 if (!memcmp(mcast->mcmember.mgid.raw, dev->if_broadcastaddr + 4,
164 sizeof (union ib_gid))) {
165 spin_lock_irq(&priv->lock);
166 if (!priv->broadcast) {
167 spin_unlock_irq(&priv->lock);
170 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
171 spin_unlock_irq(&priv->lock);
172 priv->tx_wr.remote_qkey = priv->qkey;
176 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
177 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
178 ipoib_warn(priv, "multicast group %16D already attached\n",
179 mcast->mcmember.mgid.raw, ":");
184 ret = ipoib_mcast_attach(priv, be16_to_cpu(mcast->mcmember.mlid),
185 &mcast->mcmember.mgid, set_qkey);
187 ipoib_warn(priv, "couldn't attach QP to multicast group %16D\n",
188 mcast->mcmember.mgid.raw, ":");
190 clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
196 struct ib_ah_attr av = {
197 .dlid = be16_to_cpu(mcast->mcmember.mlid),
198 .port_num = priv->port,
199 .sl = mcast->mcmember.sl,
200 .ah_flags = IB_AH_GRH,
201 .static_rate = mcast->mcmember.rate,
203 .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
204 .hop_limit = mcast->mcmember.hop_limit,
206 .traffic_class = mcast->mcmember.traffic_class
209 av.grh.dgid = mcast->mcmember.mgid;
211 ah = ipoib_create_ah(priv, priv->pd, &av);
213 ipoib_warn(priv, "ib_address_create failed\n");
215 spin_lock_irq(&priv->lock);
217 spin_unlock_irq(&priv->lock);
219 ipoib_dbg_mcast(priv, "MGID %16D AV %p, LID 0x%04x, SL %d\n",
220 mcast->mcmember.mgid.raw, ":",
222 be16_to_cpu(mcast->mcmember.mlid),
227 /* actually send any queued packets */
228 while (mcast->pkt_queue.ifq_len) {
230 _IF_DEQUEUE(&mcast->pkt_queue, mb);
231 mb->m_pkthdr.rcvif = dev;
233 if (dev->if_transmit(dev, mb))
234 ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
241 ipoib_mcast_sendonly_join_complete(int status,
242 struct ib_sa_multicast *multicast)
244 struct ipoib_mcast *mcast = multicast->context;
245 struct ipoib_dev_priv *priv = mcast->priv;
247 /* We trap for port events ourselves. */
248 if (status == -ENETRESET)
252 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
255 if (mcast->logcount++ < 20)
256 ipoib_dbg_mcast(priv, "multicast join failed for %16D, status %d\n",
257 mcast->mcmember.mgid.raw, ":", status);
259 /* Flush out any queued packets */
260 if_inc_counter(priv->dev, IFCOUNTER_OERRORS, mcast->pkt_queue.ifq_len);
261 _IF_DRAIN(&mcast->pkt_queue);
263 /* Clear the busy flag so we try again */
264 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY,
270 static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
272 struct ipoib_dev_priv *priv = mcast->priv;
273 struct ib_sa_mcmember_rec rec = {
274 #if 0 /* Some SMs don't support send-only yet */
282 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
283 ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
287 if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
288 ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
292 rec.mgid = mcast->mcmember.mgid;
293 rec.port_gid = priv->local_gid;
294 rec.pkey = cpu_to_be16(priv->pkey);
296 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca,
298 IB_SA_MCMEMBER_REC_MGID |
299 IB_SA_MCMEMBER_REC_PORT_GID |
300 IB_SA_MCMEMBER_REC_PKEY |
301 IB_SA_MCMEMBER_REC_JOIN_STATE,
303 ipoib_mcast_sendonly_join_complete,
305 if (IS_ERR(mcast->mc)) {
306 ret = PTR_ERR(mcast->mc);
307 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
308 ipoib_warn(priv, "ib_sa_join_multicast failed (ret = %d)\n",
311 ipoib_dbg_mcast(priv, "no multicast record for %16D, starting join\n",
312 mcast->mcmember.mgid.raw, ":");
318 void ipoib_mcast_carrier_on_task(struct work_struct *work)
320 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
322 struct ib_port_attr attr;
325 * Take rtnl_lock to avoid racing with ipoib_stop() and
326 * turning the carrier back on while a device is being
329 if (ib_query_port(priv->ca, priv->port, &attr) ||
330 attr.state != IB_PORT_ACTIVE) {
331 ipoib_dbg(priv, "Keeping carrier off until IB port is active\n");
334 if_link_state_change(priv->dev, LINK_STATE_UP);
337 static int ipoib_mcast_join_complete(int status,
338 struct ib_sa_multicast *multicast)
340 struct ipoib_mcast *mcast = multicast->context;
341 struct ipoib_dev_priv *priv = mcast->priv;
343 ipoib_dbg_mcast(priv, "join completion for %16D (status %d)\n",
344 mcast->mcmember.mgid.raw, ":", status);
346 /* We trap for port events ourselves. */
347 if (status == -ENETRESET)
351 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
355 mutex_lock(&mcast_mutex);
356 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
357 queue_delayed_work(ipoib_workqueue,
358 &priv->mcast_task, 0);
359 mutex_unlock(&mcast_mutex);
362 * Defer carrier on work to ipoib_workqueue to avoid a
363 * deadlock on rtnl_lock here.
365 if (mcast == priv->broadcast)
366 queue_work(ipoib_workqueue, &priv->carrier_on_task);
371 if (mcast->logcount++ < 20) {
372 if (status == -ETIMEDOUT || status == -EAGAIN) {
373 ipoib_dbg_mcast(priv, "multicast join failed for %16D, status %d\n",
374 mcast->mcmember.mgid.raw, ":", status);
376 ipoib_warn(priv, "multicast join failed for %16D, status %d\n",
377 mcast->mcmember.mgid.raw, ":", status);
382 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
383 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
385 /* Clear the busy flag so we try again */
386 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
388 mutex_lock(&mcast_mutex);
389 spin_lock_irq(&priv->lock);
390 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
391 queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
392 mcast->backoff * HZ);
393 spin_unlock_irq(&priv->lock);
394 mutex_unlock(&mcast_mutex);
399 static void ipoib_mcast_join(struct ipoib_dev_priv *priv,
400 struct ipoib_mcast *mcast, int create)
402 struct ib_sa_mcmember_rec rec = {
405 ib_sa_comp_mask comp_mask;
408 ipoib_dbg_mcast(priv, "joining MGID %16D\n",
409 mcast->mcmember.mgid.raw, ":");
411 rec.mgid = mcast->mcmember.mgid;
412 rec.port_gid = priv->local_gid;
413 rec.pkey = cpu_to_be16(priv->pkey);
416 IB_SA_MCMEMBER_REC_MGID |
417 IB_SA_MCMEMBER_REC_PORT_GID |
418 IB_SA_MCMEMBER_REC_PKEY |
419 IB_SA_MCMEMBER_REC_JOIN_STATE;
423 IB_SA_MCMEMBER_REC_QKEY |
424 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
425 IB_SA_MCMEMBER_REC_MTU |
426 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS |
427 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
428 IB_SA_MCMEMBER_REC_RATE |
429 IB_SA_MCMEMBER_REC_SL |
430 IB_SA_MCMEMBER_REC_FLOW_LABEL |
431 IB_SA_MCMEMBER_REC_HOP_LIMIT;
433 rec.qkey = priv->broadcast->mcmember.qkey;
434 rec.mtu_selector = IB_SA_EQ;
435 rec.mtu = priv->broadcast->mcmember.mtu;
436 rec.traffic_class = priv->broadcast->mcmember.traffic_class;
437 rec.rate_selector = IB_SA_EQ;
438 rec.rate = priv->broadcast->mcmember.rate;
439 rec.sl = priv->broadcast->mcmember.sl;
440 rec.flow_label = priv->broadcast->mcmember.flow_label;
441 rec.hop_limit = priv->broadcast->mcmember.hop_limit;
444 set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
445 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
446 &rec, comp_mask, GFP_KERNEL,
447 ipoib_mcast_join_complete, mcast);
448 if (IS_ERR(mcast->mc)) {
449 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
450 ret = PTR_ERR(mcast->mc);
451 ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
454 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
455 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
457 mutex_lock(&mcast_mutex);
458 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
459 queue_delayed_work(ipoib_workqueue,
461 mcast->backoff * HZ);
462 mutex_unlock(&mcast_mutex);
466 void ipoib_mcast_join_task(struct work_struct *work)
468 struct ipoib_dev_priv *priv =
469 container_of(work, struct ipoib_dev_priv, mcast_task.work);
470 struct ifnet *dev = priv->dev;
471 struct ib_port_attr attr;
473 ipoib_dbg_mcast(priv, "Running join task. flags 0x%lX\n", priv->flags);
475 if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
478 if (ib_query_port(priv->ca, priv->port, &attr) ||
479 attr.state != IB_PORT_ACTIVE) {
480 ipoib_dbg(priv, "%s: port state is not ACTIVE (state = %d) suspend task.\n",
481 __func__, attr.state);
485 if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid, NULL))
486 ipoib_warn(priv, "ib_query_gid() failed\n");
488 memcpy(IF_LLADDR(dev) + 4, priv->local_gid.raw, sizeof (union ib_gid));
491 struct ib_port_attr attr;
493 if (!ib_query_port(priv->ca, priv->port, &attr))
494 priv->local_lid = attr.lid;
496 ipoib_warn(priv, "ib_query_port failed\n");
499 if (!priv->broadcast) {
500 struct ipoib_mcast *broadcast;
502 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
505 broadcast = ipoib_mcast_alloc(priv, 1);
507 ipoib_warn(priv, "failed to allocate broadcast group\n");
508 mutex_lock(&mcast_mutex);
509 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
510 queue_delayed_work(ipoib_workqueue,
511 &priv->mcast_task, HZ);
512 mutex_unlock(&mcast_mutex);
516 spin_lock_irq(&priv->lock);
517 memcpy(broadcast->mcmember.mgid.raw, dev->if_broadcastaddr + 4,
518 sizeof (union ib_gid));
519 priv->broadcast = broadcast;
521 __ipoib_mcast_add(priv, priv->broadcast);
522 spin_unlock_irq(&priv->lock);
525 if (priv->broadcast &&
526 !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
527 if (priv->broadcast &&
528 !test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
529 ipoib_mcast_join(priv, priv->broadcast, 0);
534 struct ipoib_mcast *mcast = NULL;
536 spin_lock_irq(&priv->lock);
537 list_for_each_entry(mcast, &priv->multicast_list, list) {
538 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
539 && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
540 && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
541 /* Found the next unjoined group */
545 spin_unlock_irq(&priv->lock);
547 if (&mcast->list == &priv->multicast_list) {
552 ipoib_mcast_join(priv, mcast, 1);
556 spin_lock_irq(&priv->lock);
558 priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
560 priv->mcast_mtu = priv->admin_mtu;
561 spin_unlock_irq(&priv->lock);
563 if (!ipoib_cm_admin_enabled(priv))
564 ipoib_change_mtu(priv, min(priv->mcast_mtu, priv->admin_mtu));
566 ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
568 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
571 int ipoib_mcast_start_thread(struct ipoib_dev_priv *priv)
573 ipoib_dbg_mcast(priv, "starting multicast thread flags 0x%lX\n",
576 mutex_lock(&mcast_mutex);
577 if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
578 queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
579 mutex_unlock(&mcast_mutex);
584 int ipoib_mcast_stop_thread(struct ipoib_dev_priv *priv, int flush)
587 ipoib_dbg_mcast(priv, "stopping multicast thread\n");
589 mutex_lock(&mcast_mutex);
590 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
591 cancel_delayed_work(&priv->mcast_task);
592 mutex_unlock(&mcast_mutex);
595 flush_workqueue(ipoib_workqueue);
600 static int ipoib_mcast_leave(struct ipoib_dev_priv *priv, struct ipoib_mcast *mcast)
604 if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
605 ib_sa_free_multicast(mcast->mc);
607 if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
608 ipoib_dbg_mcast(priv, "leaving MGID %16D\n",
609 mcast->mcmember.mgid.raw, ":");
611 /* Remove ourselves from the multicast group */
612 ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
613 be16_to_cpu(mcast->mcmember.mlid));
615 ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
622 ipoib_mcast_send(struct ipoib_dev_priv *priv, void *mgid, struct mbuf *mb)
624 struct ifnet *dev = priv->dev;
625 struct ipoib_mcast *mcast;
627 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags) ||
629 !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
630 if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
635 mcast = __ipoib_mcast_find(priv, mgid);
637 /* Let's create a new send only group now */
638 ipoib_dbg_mcast(priv, "setting up send only multicast group for %16D\n",
641 mcast = ipoib_mcast_alloc(priv, 0);
643 ipoib_warn(priv, "unable to allocate memory for "
644 "multicast structure\n");
645 if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
650 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
651 memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
652 __ipoib_mcast_add(priv, mcast);
653 list_add_tail(&mcast->list, &priv->multicast_list);
657 if (mcast->pkt_queue.ifq_len < IPOIB_MAX_MCAST_QUEUE) {
658 _IF_ENQUEUE(&mcast->pkt_queue, mb);
660 if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
664 if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
665 ipoib_dbg_mcast(priv, "no address vector, "
666 "but multicast join already started\n");
667 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
668 ipoib_mcast_sendonly_join(mcast);
671 * If lookup completes between here and out:, don't
672 * want to send packet twice.
678 if (mcast && mcast->ah)
679 ipoib_send(priv, mb, mcast->ah, IB_MULTICAST_QPN);
682 void ipoib_mcast_dev_flush(struct ipoib_dev_priv *priv)
684 LIST_HEAD(remove_list);
685 struct ipoib_mcast *mcast, *tmcast;
688 ipoib_dbg_mcast(priv, "flushing multicast list\n");
690 spin_lock_irqsave(&priv->lock, flags);
692 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
693 list_del(&mcast->list);
694 rb_erase(&mcast->rb_node, &priv->multicast_tree);
695 list_add_tail(&mcast->list, &remove_list);
698 if (priv->broadcast) {
699 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
700 list_add_tail(&priv->broadcast->list, &remove_list);
701 priv->broadcast = NULL;
704 spin_unlock_irqrestore(&priv->lock, flags);
706 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
707 ipoib_mcast_leave(priv, mcast);
708 ipoib_mcast_free(mcast);
712 static int ipoib_mcast_addr_is_valid(const u8 *addr, unsigned int addrlen,
715 if (addrlen != INFINIBAND_ALEN)
717 /* reserved QPN, prefix, scope */
718 if (memcmp(addr, broadcast, 6))
720 /* signature lower, pkey */
721 if (memcmp(addr + 7, broadcast + 7, 3))
726 void ipoib_mcast_restart_task(struct work_struct *work)
728 struct ipoib_dev_priv *priv =
729 container_of(work, struct ipoib_dev_priv, restart_task);
730 ipoib_mcast_restart(priv);
733 void ipoib_mcast_restart(struct ipoib_dev_priv *priv)
735 struct ifnet *dev = priv->dev;
736 struct ifmultiaddr *ifma;
737 struct ipoib_mcast *mcast, *tmcast;
738 LIST_HEAD(remove_list);
739 struct ib_sa_mcmember_rec rec;
742 ipoib_dbg_mcast(priv, "restarting multicast task flags 0x%lX\n",
745 ipoib_mcast_stop_thread(priv, 0);
748 spin_lock(&priv->lock);
751 * Unfortunately, the networking core only gives us a list of all of
752 * the multicast hardware addresses. We need to figure out which ones
753 * are new and which ones have been removed
756 /* Clear out the found flag */
757 list_for_each_entry(mcast, &priv->multicast_list, list)
758 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
760 /* Mark all of the entries that are found or don't exist */
763 TAILQ_FOREACH(ifma, &dev->if_multiaddrs, ifma_link) {
767 if (ifma->ifma_addr->sa_family != AF_LINK)
769 addr = LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
770 addrlen = ((struct sockaddr_dl *)ifma->ifma_addr)->sdl_alen;
771 if (!ipoib_mcast_addr_is_valid(addr, addrlen,
772 dev->if_broadcastaddr))
775 memcpy(mgid.raw, addr + 4, sizeof mgid);
777 mcast = __ipoib_mcast_find(priv, &mgid);
778 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
779 struct ipoib_mcast *nmcast;
781 /* ignore group which is directly joined by userspace */
782 if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
783 !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
784 ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %16D\n",
789 /* Not found or send-only group, let's add a new entry */
790 ipoib_dbg_mcast(priv, "adding multicast entry for mgid %16D\n",
793 nmcast = ipoib_mcast_alloc(priv, 0);
795 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
799 set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
801 nmcast->mcmember.mgid = mgid;
804 /* Destroy the send only entry */
805 list_move_tail(&mcast->list, &remove_list);
807 rb_replace_node(&mcast->rb_node,
809 &priv->multicast_tree);
811 __ipoib_mcast_add(priv, nmcast);
813 list_add_tail(&nmcast->list, &priv->multicast_list);
817 set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
820 /* Remove all of the entries don't exist anymore */
821 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
822 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
823 !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
824 ipoib_dbg_mcast(priv, "deleting multicast group %16D\n",
825 mcast->mcmember.mgid.raw, ":");
827 rb_erase(&mcast->rb_node, &priv->multicast_tree);
829 /* Move to the remove list */
830 list_move_tail(&mcast->list, &remove_list);
834 spin_unlock(&priv->lock);
835 if_maddr_runlock(dev);
837 /* We have to cancel outside of the spinlock */
838 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
839 ipoib_mcast_leave(mcast->priv, mcast);
840 ipoib_mcast_free(mcast);
843 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
844 ipoib_mcast_start_thread(priv);
847 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
849 struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct ipoib_dev_priv *priv)
851 struct ipoib_mcast_iter *iter;
853 iter = kmalloc(sizeof *iter, GFP_KERNEL);
858 memset(iter->mgid.raw, 0, 16);
860 if (ipoib_mcast_iter_next(iter)) {
868 int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
870 struct ipoib_dev_priv *priv = iter->priv;
872 struct ipoib_mcast *mcast;
875 spin_lock_irq(&priv->lock);
877 n = rb_first(&priv->multicast_tree);
880 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
882 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
883 sizeof (union ib_gid)) < 0) {
884 iter->mgid = mcast->mcmember.mgid;
885 iter->created = mcast->created;
886 iter->queuelen = mcast->pkt_queue.ifq_len;
887 iter->complete = !!mcast->ah;
888 iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
898 spin_unlock_irq(&priv->lock);
903 void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
905 unsigned long *created,
906 unsigned int *queuelen,
907 unsigned int *complete,
908 unsigned int *send_only)
911 *created = iter->created;
912 *queuelen = iter->queuelen;
913 *complete = iter->complete;
914 *send_only = iter->send_only;
917 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */