[PATCH] W1: Change the type 'unsigned long' member of 'struct w1_bus_master' to ...
[linux-2.6/verdex.git] / drivers / infiniband / ulp / ipoib / ipoib_multicast.c
blob93c462eaf4fd781c2ae93411c04c55878cc37325
1 /*
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
14 * conditions are met:
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
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
32 * SOFTWARE.
34 * $Id: ipoib_multicast.c 1362 2004-12-18 15:56:29Z roland $
37 #include <linux/skbuff.h>
38 #include <linux/rtnetlink.h>
39 #include <linux/ip.h>
40 #include <linux/in.h>
41 #include <linux/igmp.h>
42 #include <linux/inetdevice.h>
43 #include <linux/delay.h>
44 #include <linux/completion.h>
46 #include <net/dst.h>
48 #include "ipoib.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");
56 #endif
58 static DEFINE_MUTEX(mcast_mutex);
60 /* Used for all multicast joins (broadcast, IPv4 mcast and IPv6 mcast) */
61 struct ipoib_mcast {
62 struct ib_sa_mcmember_rec mcmember;
63 struct ipoib_ah *ah;
65 struct rb_node rb_node;
66 struct list_head list;
67 struct completion done;
69 int query_id;
70 struct ib_sa_query *query;
72 unsigned long created;
73 unsigned long backoff;
75 unsigned long flags;
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;
87 union ib_gid mgid;
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;
99 unsigned long flags;
100 int tx_dropped = 0;
102 ipoib_dbg_mcast(netdev_priv(dev),
103 "deleting multicast group " IPOIB_GID_FMT "\n",
104 IPOIB_GID_ARG(mcast->mcmember.mgid));
106 spin_lock_irqsave(&priv->lock, flags);
108 list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
110 * It's safe to call ipoib_put_ah() inside priv->lock
111 * here, because we know that mcast->ah will always
112 * hold one more reference, so ipoib_put_ah() will
113 * never do more than decrement the ref count.
115 if (neigh->ah)
116 ipoib_put_ah(neigh->ah);
117 *to_ipoib_neigh(neigh->neighbour) = NULL;
118 kfree(neigh);
121 spin_unlock_irqrestore(&priv->lock, flags);
123 if (mcast->ah)
124 ipoib_put_ah(mcast->ah);
126 while (!skb_queue_empty(&mcast->pkt_queue)) {
127 ++tx_dropped;
128 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
131 spin_lock_irqsave(&priv->tx_lock, flags);
132 priv->stats.tx_dropped += tx_dropped;
133 spin_unlock_irqrestore(&priv->tx_lock, flags);
135 kfree(mcast);
138 static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
139 int can_sleep)
141 struct ipoib_mcast *mcast;
143 mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
144 if (!mcast)
145 return NULL;
147 mcast->dev = dev;
148 mcast->created = jiffies;
149 mcast->backoff = 1;
151 INIT_LIST_HEAD(&mcast->list);
152 INIT_LIST_HEAD(&mcast->neigh_list);
153 skb_queue_head_init(&mcast->pkt_queue);
155 return mcast;
158 static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, union ib_gid *mgid)
160 struct ipoib_dev_priv *priv = netdev_priv(dev);
161 struct rb_node *n = priv->multicast_tree.rb_node;
163 while (n) {
164 struct ipoib_mcast *mcast;
165 int ret;
167 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
169 ret = memcmp(mgid->raw, mcast->mcmember.mgid.raw,
170 sizeof (union ib_gid));
171 if (ret < 0)
172 n = n->rb_left;
173 else if (ret > 0)
174 n = n->rb_right;
175 else
176 return mcast;
179 return NULL;
182 static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
184 struct ipoib_dev_priv *priv = netdev_priv(dev);
185 struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
187 while (*n) {
188 struct ipoib_mcast *tmcast;
189 int ret;
191 pn = *n;
192 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
194 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
195 sizeof (union ib_gid));
196 if (ret < 0)
197 n = &pn->rb_left;
198 else if (ret > 0)
199 n = &pn->rb_right;
200 else
201 return -EEXIST;
204 rb_link_node(&mcast->rb_node, pn, n);
205 rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
207 return 0;
210 static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
211 struct ib_sa_mcmember_rec *mcmember)
213 struct net_device *dev = mcast->dev;
214 struct ipoib_dev_priv *priv = netdev_priv(dev);
215 struct ipoib_ah *ah;
216 int ret;
218 mcast->mcmember = *mcmember;
220 /* Set the cached Q_Key before we attach if it's the broadcast group */
221 if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
222 sizeof (union ib_gid))) {
223 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
224 priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
227 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
228 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
229 ipoib_warn(priv, "multicast group " IPOIB_GID_FMT
230 " already attached\n",
231 IPOIB_GID_ARG(mcast->mcmember.mgid));
233 return 0;
236 ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
237 &mcast->mcmember.mgid);
238 if (ret < 0) {
239 ipoib_warn(priv, "couldn't attach QP to multicast group "
240 IPOIB_GID_FMT "\n",
241 IPOIB_GID_ARG(mcast->mcmember.mgid));
243 clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
244 return ret;
249 struct ib_ah_attr av = {
250 .dlid = be16_to_cpu(mcast->mcmember.mlid),
251 .port_num = priv->port,
252 .sl = mcast->mcmember.sl,
253 .ah_flags = IB_AH_GRH,
254 .grh = {
255 .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
256 .hop_limit = mcast->mcmember.hop_limit,
257 .sgid_index = 0,
258 .traffic_class = mcast->mcmember.traffic_class
261 int path_rate = ib_sa_rate_enum_to_int(mcast->mcmember.rate);
263 av.grh.dgid = mcast->mcmember.mgid;
265 if (path_rate > 0 && priv->local_rate > path_rate)
266 av.static_rate = (priv->local_rate - 1) / path_rate;
268 ipoib_dbg_mcast(priv, "static_rate %d for local port %dX, mcmember %dX\n",
269 av.static_rate, priv->local_rate,
270 ib_sa_rate_enum_to_int(mcast->mcmember.rate));
272 ah = ipoib_create_ah(dev, priv->pd, &av);
273 if (!ah) {
274 ipoib_warn(priv, "ib_address_create failed\n");
275 } else {
276 ipoib_dbg_mcast(priv, "MGID " IPOIB_GID_FMT
277 " AV %p, LID 0x%04x, SL %d\n",
278 IPOIB_GID_ARG(mcast->mcmember.mgid),
279 mcast->ah->ah,
280 be16_to_cpu(mcast->mcmember.mlid),
281 mcast->mcmember.sl);
284 spin_lock_irq(&priv->lock);
285 mcast->ah = ah;
286 spin_unlock_irq(&priv->lock);
289 /* actually send any queued packets */
290 spin_lock_irq(&priv->tx_lock);
291 while (!skb_queue_empty(&mcast->pkt_queue)) {
292 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
293 spin_unlock_irq(&priv->tx_lock);
295 skb->dev = dev;
297 if (!skb->dst || !skb->dst->neighbour) {
298 /* put pseudoheader back on for next time */
299 skb_push(skb, sizeof (struct ipoib_pseudoheader));
302 if (dev_queue_xmit(skb))
303 ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
304 spin_lock_irq(&priv->tx_lock);
306 spin_unlock_irq(&priv->tx_lock);
308 return 0;
311 static void
312 ipoib_mcast_sendonly_join_complete(int status,
313 struct ib_sa_mcmember_rec *mcmember,
314 void *mcast_ptr)
316 struct ipoib_mcast *mcast = mcast_ptr;
317 struct net_device *dev = mcast->dev;
318 struct ipoib_dev_priv *priv = netdev_priv(dev);
320 if (!status)
321 ipoib_mcast_join_finish(mcast, mcmember);
322 else {
323 if (mcast->logcount++ < 20)
324 ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for "
325 IPOIB_GID_FMT ", status %d\n",
326 IPOIB_GID_ARG(mcast->mcmember.mgid), status);
328 /* Flush out any queued packets */
329 spin_lock_irq(&priv->tx_lock);
330 while (!skb_queue_empty(&mcast->pkt_queue)) {
331 ++priv->stats.tx_dropped;
332 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
334 spin_unlock_irq(&priv->tx_lock);
336 /* Clear the busy flag so we try again */
337 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
340 complete(&mcast->done);
343 static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
345 struct net_device *dev = mcast->dev;
346 struct ipoib_dev_priv *priv = netdev_priv(dev);
347 struct ib_sa_mcmember_rec rec = {
348 #if 0 /* Some SMs don't support send-only yet */
349 .join_state = 4
350 #else
351 .join_state = 1
352 #endif
354 int ret = 0;
356 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
357 ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
358 return -ENODEV;
361 if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
362 ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
363 return -EBUSY;
366 rec.mgid = mcast->mcmember.mgid;
367 rec.port_gid = priv->local_gid;
368 rec.pkey = cpu_to_be16(priv->pkey);
370 init_completion(&mcast->done);
372 ret = ib_sa_mcmember_rec_set(priv->ca, priv->port, &rec,
373 IB_SA_MCMEMBER_REC_MGID |
374 IB_SA_MCMEMBER_REC_PORT_GID |
375 IB_SA_MCMEMBER_REC_PKEY |
376 IB_SA_MCMEMBER_REC_JOIN_STATE,
377 1000, GFP_ATOMIC,
378 ipoib_mcast_sendonly_join_complete,
379 mcast, &mcast->query);
380 if (ret < 0) {
381 ipoib_warn(priv, "ib_sa_mcmember_rec_set failed (ret = %d)\n",
382 ret);
383 } else {
384 ipoib_dbg_mcast(priv, "no multicast record for " IPOIB_GID_FMT
385 ", starting join\n",
386 IPOIB_GID_ARG(mcast->mcmember.mgid));
388 mcast->query_id = ret;
391 return ret;
394 static void ipoib_mcast_join_complete(int status,
395 struct ib_sa_mcmember_rec *mcmember,
396 void *mcast_ptr)
398 struct ipoib_mcast *mcast = mcast_ptr;
399 struct net_device *dev = mcast->dev;
400 struct ipoib_dev_priv *priv = netdev_priv(dev);
402 ipoib_dbg_mcast(priv, "join completion for " IPOIB_GID_FMT
403 " (status %d)\n",
404 IPOIB_GID_ARG(mcast->mcmember.mgid), status);
406 if (!status && !ipoib_mcast_join_finish(mcast, mcmember)) {
407 mcast->backoff = 1;
408 mutex_lock(&mcast_mutex);
409 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
410 queue_work(ipoib_workqueue, &priv->mcast_task);
411 mutex_unlock(&mcast_mutex);
412 complete(&mcast->done);
413 return;
416 if (status == -EINTR) {
417 complete(&mcast->done);
418 return;
421 if (status && mcast->logcount++ < 20) {
422 if (status == -ETIMEDOUT || status == -EINTR) {
423 ipoib_dbg_mcast(priv, "multicast join failed for " IPOIB_GID_FMT
424 ", status %d\n",
425 IPOIB_GID_ARG(mcast->mcmember.mgid),
426 status);
427 } else {
428 ipoib_warn(priv, "multicast join failed for "
429 IPOIB_GID_FMT ", status %d\n",
430 IPOIB_GID_ARG(mcast->mcmember.mgid),
431 status);
435 mcast->backoff *= 2;
436 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
437 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
439 mutex_lock(&mcast_mutex);
441 spin_lock_irq(&priv->lock);
442 mcast->query = NULL;
444 if (test_bit(IPOIB_MCAST_RUN, &priv->flags)) {
445 if (status == -ETIMEDOUT)
446 queue_work(ipoib_workqueue, &priv->mcast_task);
447 else
448 queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
449 mcast->backoff * HZ);
450 } else
451 complete(&mcast->done);
452 spin_unlock_irq(&priv->lock);
453 mutex_unlock(&mcast_mutex);
455 return;
458 static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
459 int create)
461 struct ipoib_dev_priv *priv = netdev_priv(dev);
462 struct ib_sa_mcmember_rec rec = {
463 .join_state = 1
465 ib_sa_comp_mask comp_mask;
466 int ret = 0;
468 ipoib_dbg_mcast(priv, "joining MGID " IPOIB_GID_FMT "\n",
469 IPOIB_GID_ARG(mcast->mcmember.mgid));
471 rec.mgid = mcast->mcmember.mgid;
472 rec.port_gid = priv->local_gid;
473 rec.pkey = cpu_to_be16(priv->pkey);
475 comp_mask =
476 IB_SA_MCMEMBER_REC_MGID |
477 IB_SA_MCMEMBER_REC_PORT_GID |
478 IB_SA_MCMEMBER_REC_PKEY |
479 IB_SA_MCMEMBER_REC_JOIN_STATE;
481 if (create) {
482 comp_mask |=
483 IB_SA_MCMEMBER_REC_QKEY |
484 IB_SA_MCMEMBER_REC_SL |
485 IB_SA_MCMEMBER_REC_FLOW_LABEL |
486 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
488 rec.qkey = priv->broadcast->mcmember.qkey;
489 rec.sl = priv->broadcast->mcmember.sl;
490 rec.flow_label = priv->broadcast->mcmember.flow_label;
491 rec.traffic_class = priv->broadcast->mcmember.traffic_class;
494 init_completion(&mcast->done);
496 ret = ib_sa_mcmember_rec_set(priv->ca, priv->port, &rec, comp_mask,
497 mcast->backoff * 1000, GFP_ATOMIC,
498 ipoib_mcast_join_complete,
499 mcast, &mcast->query);
501 if (ret < 0) {
502 ipoib_warn(priv, "ib_sa_mcmember_rec_set failed, status %d\n", ret);
504 mcast->backoff *= 2;
505 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
506 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
508 mutex_lock(&mcast_mutex);
509 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
510 queue_delayed_work(ipoib_workqueue,
511 &priv->mcast_task,
512 mcast->backoff * HZ);
513 mutex_unlock(&mcast_mutex);
514 } else
515 mcast->query_id = ret;
518 void ipoib_mcast_join_task(void *dev_ptr)
520 struct net_device *dev = dev_ptr;
521 struct ipoib_dev_priv *priv = netdev_priv(dev);
523 if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
524 return;
526 if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
527 ipoib_warn(priv, "ib_gid_entry_get() failed\n");
528 else
529 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
532 struct ib_port_attr attr;
534 if (!ib_query_port(priv->ca, priv->port, &attr)) {
535 priv->local_lid = attr.lid;
536 priv->local_rate = attr.active_speed *
537 ib_width_enum_to_int(attr.active_width);
538 } else
539 ipoib_warn(priv, "ib_query_port failed\n");
542 if (!priv->broadcast) {
543 struct ipoib_mcast *broadcast;
545 broadcast = ipoib_mcast_alloc(dev, 1);
546 if (!broadcast) {
547 ipoib_warn(priv, "failed to allocate broadcast group\n");
548 mutex_lock(&mcast_mutex);
549 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
550 queue_delayed_work(ipoib_workqueue,
551 &priv->mcast_task, HZ);
552 mutex_unlock(&mcast_mutex);
553 return;
556 spin_lock_irq(&priv->lock);
557 memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
558 sizeof (union ib_gid));
559 priv->broadcast = broadcast;
561 __ipoib_mcast_add(dev, priv->broadcast);
562 spin_unlock_irq(&priv->lock);
565 if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
566 ipoib_mcast_join(dev, priv->broadcast, 0);
567 return;
570 while (1) {
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 */
579 break;
582 spin_unlock_irq(&priv->lock);
584 if (&mcast->list == &priv->multicast_list) {
585 /* All done */
586 break;
589 ipoib_mcast_join(dev, mcast, 1);
590 return;
593 priv->mcast_mtu = ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu) -
594 IPOIB_ENCAP_LEN;
595 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
597 ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
599 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
600 netif_carrier_on(dev);
603 int ipoib_mcast_start_thread(struct net_device *dev)
605 struct ipoib_dev_priv *priv = netdev_priv(dev);
607 ipoib_dbg_mcast(priv, "starting multicast thread\n");
609 mutex_lock(&mcast_mutex);
610 if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
611 queue_work(ipoib_workqueue, &priv->mcast_task);
612 mutex_unlock(&mcast_mutex);
614 spin_lock_irq(&priv->lock);
615 set_bit(IPOIB_MCAST_STARTED, &priv->flags);
616 spin_unlock_irq(&priv->lock);
618 return 0;
621 int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
623 struct ipoib_dev_priv *priv = netdev_priv(dev);
624 struct ipoib_mcast *mcast;
626 ipoib_dbg_mcast(priv, "stopping multicast thread\n");
628 spin_lock_irq(&priv->lock);
629 clear_bit(IPOIB_MCAST_STARTED, &priv->flags);
630 spin_unlock_irq(&priv->lock);
632 mutex_lock(&mcast_mutex);
633 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
634 cancel_delayed_work(&priv->mcast_task);
635 mutex_unlock(&mcast_mutex);
637 if (flush)
638 flush_workqueue(ipoib_workqueue);
640 spin_lock_irq(&priv->lock);
641 if (priv->broadcast && priv->broadcast->query) {
642 ib_sa_cancel_query(priv->broadcast->query_id, priv->broadcast->query);
643 priv->broadcast->query = NULL;
644 spin_unlock_irq(&priv->lock);
645 ipoib_dbg_mcast(priv, "waiting for bcast\n");
646 wait_for_completion(&priv->broadcast->done);
647 } else
648 spin_unlock_irq(&priv->lock);
650 list_for_each_entry(mcast, &priv->multicast_list, list) {
651 spin_lock_irq(&priv->lock);
652 if (mcast->query) {
653 ib_sa_cancel_query(mcast->query_id, mcast->query);
654 mcast->query = NULL;
655 spin_unlock_irq(&priv->lock);
656 ipoib_dbg_mcast(priv, "waiting for MGID " IPOIB_GID_FMT "\n",
657 IPOIB_GID_ARG(mcast->mcmember.mgid));
658 wait_for_completion(&mcast->done);
659 } else
660 spin_unlock_irq(&priv->lock);
663 return 0;
666 static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
668 struct ipoib_dev_priv *priv = netdev_priv(dev);
669 struct ib_sa_mcmember_rec rec = {
670 .join_state = 1
672 int ret = 0;
674 if (!test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags))
675 return 0;
677 ipoib_dbg_mcast(priv, "leaving MGID " IPOIB_GID_FMT "\n",
678 IPOIB_GID_ARG(mcast->mcmember.mgid));
680 rec.mgid = mcast->mcmember.mgid;
681 rec.port_gid = priv->local_gid;
682 rec.pkey = cpu_to_be16(priv->pkey);
684 /* Remove ourselves from the multicast group */
685 ret = ipoib_mcast_detach(dev, be16_to_cpu(mcast->mcmember.mlid),
686 &mcast->mcmember.mgid);
687 if (ret)
688 ipoib_warn(priv, "ipoib_mcast_detach failed (result = %d)\n", ret);
691 * Just make one shot at leaving and don't wait for a reply;
692 * if we fail, too bad.
694 ret = ib_sa_mcmember_rec_delete(priv->ca, priv->port, &rec,
695 IB_SA_MCMEMBER_REC_MGID |
696 IB_SA_MCMEMBER_REC_PORT_GID |
697 IB_SA_MCMEMBER_REC_PKEY |
698 IB_SA_MCMEMBER_REC_JOIN_STATE,
699 0, GFP_ATOMIC, NULL,
700 mcast, &mcast->query);
701 if (ret < 0)
702 ipoib_warn(priv, "ib_sa_mcmember_rec_delete failed "
703 "for leave (result = %d)\n", ret);
705 return 0;
708 void ipoib_mcast_send(struct net_device *dev, union ib_gid *mgid,
709 struct sk_buff *skb)
711 struct ipoib_dev_priv *priv = netdev_priv(dev);
712 struct ipoib_mcast *mcast;
715 * We can only be called from ipoib_start_xmit, so we're
716 * inside tx_lock -- no need to save/restore flags.
718 spin_lock(&priv->lock);
720 if (!test_bit(IPOIB_MCAST_STARTED, &priv->flags) ||
721 !priv->broadcast ||
722 !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
723 ++priv->stats.tx_dropped;
724 dev_kfree_skb_any(skb);
725 goto unlock;
728 mcast = __ipoib_mcast_find(dev, mgid);
729 if (!mcast) {
730 /* Let's create a new send only group now */
731 ipoib_dbg_mcast(priv, "setting up send only multicast group for "
732 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(*mgid));
734 mcast = ipoib_mcast_alloc(dev, 0);
735 if (!mcast) {
736 ipoib_warn(priv, "unable to allocate memory for "
737 "multicast structure\n");
738 ++priv->stats.tx_dropped;
739 dev_kfree_skb_any(skb);
740 goto out;
743 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
744 mcast->mcmember.mgid = *mgid;
745 __ipoib_mcast_add(dev, mcast);
746 list_add_tail(&mcast->list, &priv->multicast_list);
749 if (!mcast->ah) {
750 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
751 skb_queue_tail(&mcast->pkt_queue, skb);
752 else {
753 ++priv->stats.tx_dropped;
754 dev_kfree_skb_any(skb);
757 if (mcast->query)
758 ipoib_dbg_mcast(priv, "no address vector, "
759 "but multicast join already started\n");
760 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
761 ipoib_mcast_sendonly_join(mcast);
764 * If lookup completes between here and out:, don't
765 * want to send packet twice.
767 mcast = NULL;
770 out:
771 if (mcast && mcast->ah) {
772 if (skb->dst &&
773 skb->dst->neighbour &&
774 !*to_ipoib_neigh(skb->dst->neighbour)) {
775 struct ipoib_neigh *neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
777 if (neigh) {
778 kref_get(&mcast->ah->ref);
779 neigh->ah = mcast->ah;
780 neigh->neighbour = skb->dst->neighbour;
781 *to_ipoib_neigh(skb->dst->neighbour) = neigh;
782 list_add_tail(&neigh->list, &mcast->neigh_list);
786 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
789 unlock:
790 spin_unlock(&priv->lock);
793 void ipoib_mcast_dev_flush(struct net_device *dev)
795 struct ipoib_dev_priv *priv = netdev_priv(dev);
796 LIST_HEAD(remove_list);
797 struct ipoib_mcast *mcast, *tmcast;
798 unsigned long flags;
800 ipoib_dbg_mcast(priv, "flushing multicast list\n");
802 spin_lock_irqsave(&priv->lock, flags);
804 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
805 list_del(&mcast->list);
806 rb_erase(&mcast->rb_node, &priv->multicast_tree);
807 list_add_tail(&mcast->list, &remove_list);
810 if (priv->broadcast) {
811 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
812 list_add_tail(&priv->broadcast->list, &remove_list);
813 priv->broadcast = NULL;
816 spin_unlock_irqrestore(&priv->lock, flags);
818 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
819 ipoib_mcast_leave(dev, mcast);
820 ipoib_mcast_free(mcast);
824 void ipoib_mcast_restart_task(void *dev_ptr)
826 struct net_device *dev = dev_ptr;
827 struct ipoib_dev_priv *priv = netdev_priv(dev);
828 struct dev_mc_list *mclist;
829 struct ipoib_mcast *mcast, *tmcast;
830 LIST_HEAD(remove_list);
831 unsigned long flags;
833 ipoib_dbg_mcast(priv, "restarting multicast task\n");
835 ipoib_mcast_stop_thread(dev, 0);
837 spin_lock_irqsave(&dev->xmit_lock, flags);
838 spin_lock(&priv->lock);
841 * Unfortunately, the networking core only gives us a list of all of
842 * the multicast hardware addresses. We need to figure out which ones
843 * are new and which ones have been removed
846 /* Clear out the found flag */
847 list_for_each_entry(mcast, &priv->multicast_list, list)
848 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
850 /* Mark all of the entries that are found or don't exist */
851 for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
852 union ib_gid mgid;
854 memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
856 /* Add in the P_Key */
857 mgid.raw[4] = (priv->pkey >> 8) & 0xff;
858 mgid.raw[5] = priv->pkey & 0xff;
860 mcast = __ipoib_mcast_find(dev, &mgid);
861 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
862 struct ipoib_mcast *nmcast;
864 /* Not found or send-only group, let's add a new entry */
865 ipoib_dbg_mcast(priv, "adding multicast entry for mgid "
866 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
868 nmcast = ipoib_mcast_alloc(dev, 0);
869 if (!nmcast) {
870 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
871 continue;
874 set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
876 nmcast->mcmember.mgid = mgid;
878 if (mcast) {
879 /* Destroy the send only entry */
880 list_del(&mcast->list);
881 list_add_tail(&mcast->list, &remove_list);
883 rb_replace_node(&mcast->rb_node,
884 &nmcast->rb_node,
885 &priv->multicast_tree);
886 } else
887 __ipoib_mcast_add(dev, nmcast);
889 list_add_tail(&nmcast->list, &priv->multicast_list);
892 if (mcast)
893 set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
896 /* Remove all of the entries don't exist anymore */
897 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
898 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
899 !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
900 ipoib_dbg_mcast(priv, "deleting multicast group " IPOIB_GID_FMT "\n",
901 IPOIB_GID_ARG(mcast->mcmember.mgid));
903 rb_erase(&mcast->rb_node, &priv->multicast_tree);
905 /* Move to the remove list */
906 list_del(&mcast->list);
907 list_add_tail(&mcast->list, &remove_list);
911 spin_unlock(&priv->lock);
912 spin_unlock_irqrestore(&dev->xmit_lock, flags);
914 /* We have to cancel outside of the spinlock */
915 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
916 ipoib_mcast_leave(mcast->dev, mcast);
917 ipoib_mcast_free(mcast);
920 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
921 ipoib_mcast_start_thread(dev);
924 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
926 struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
928 struct ipoib_mcast_iter *iter;
930 iter = kmalloc(sizeof *iter, GFP_KERNEL);
931 if (!iter)
932 return NULL;
934 iter->dev = dev;
935 memset(iter->mgid.raw, 0, 16);
937 if (ipoib_mcast_iter_next(iter)) {
938 kfree(iter);
939 return NULL;
942 return iter;
945 int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
947 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
948 struct rb_node *n;
949 struct ipoib_mcast *mcast;
950 int ret = 1;
952 spin_lock_irq(&priv->lock);
954 n = rb_first(&priv->multicast_tree);
956 while (n) {
957 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
959 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
960 sizeof (union ib_gid)) < 0) {
961 iter->mgid = mcast->mcmember.mgid;
962 iter->created = mcast->created;
963 iter->queuelen = skb_queue_len(&mcast->pkt_queue);
964 iter->complete = !!mcast->ah;
965 iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
967 ret = 0;
969 break;
972 n = rb_next(n);
975 spin_unlock_irq(&priv->lock);
977 return ret;
980 void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
981 union ib_gid *mgid,
982 unsigned long *created,
983 unsigned int *queuelen,
984 unsigned int *complete,
985 unsigned int *send_only)
987 *mgid = iter->mgid;
988 *created = iter->created;
989 *queuelen = iter->queuelen;
990 *complete = iter->complete;
991 *send_only = iter->send_only;
994 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */