blob: 98039da0caf0e4ff67e958c3679540f4030b12a9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
Roland Dreier2a1d9b72005-08-10 23:03:10 -07003 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4 * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
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.
24 *
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.
33 *
34 * $Id: ipoib_multicast.c 1362 2004-12-18 15:56:29Z roland $
35 */
36
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>
45
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020046#include <net/dst.h>
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "ipoib.h"
49
50#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
51static int mcast_debug_level;
52
53module_param(mcast_debug_level, int, 0644);
54MODULE_PARM_DESC(mcast_debug_level,
55 "Enable multicast debug tracing if > 0");
56#endif
57
Ingo Molnar95ed6442006-01-13 14:51:39 -080058static DEFINE_MUTEX(mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/* Used for all multicast joins (broadcast, IPv4 mcast and IPv6 mcast) */
61struct ipoib_mcast {
62 struct ib_sa_mcmember_rec mcmember;
63 struct ipoib_ah *ah;
64
65 struct rb_node rb_node;
66 struct list_head list;
67 struct completion done;
68
69 int query_id;
70 struct ib_sa_query *query;
71
72 unsigned long created;
73 unsigned long backoff;
74
75 unsigned long flags;
76 unsigned char logcount;
77
78 struct list_head neigh_list;
79
80 struct sk_buff_head pkt_queue;
81
82 struct net_device *dev;
83};
84
85struct 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;
92};
93
94static void ipoib_mcast_free(struct ipoib_mcast *mcast)
95{
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 ipoib_dbg_mcast(netdev_priv(dev),
102 "deleting multicast group " IPOIB_GID_FMT "\n",
103 IPOIB_GID_ARG(mcast->mcmember.mgid));
104
105 spin_lock_irqsave(&priv->lock, flags);
106
107 list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
Eli Cohen97460df2006-01-10 07:43:02 -0800108 /*
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.
113 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (neigh->ah)
Eli Cohen97460df2006-01-10 07:43:02 -0800115 ipoib_put_ah(neigh->ah);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 *to_ipoib_neigh(neigh->neighbour) = NULL;
117 neigh->neighbour->ops->destructor = NULL;
118 kfree(neigh);
119 }
120
121 spin_unlock_irqrestore(&priv->lock, flags);
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (mcast->ah)
124 ipoib_put_ah(mcast->ah);
125
Roland Dreier8c608a32005-11-07 10:49:38 -0800126 while (!skb_queue_empty(&mcast->pkt_queue))
127 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 kfree(mcast);
130}
131
132static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
133 int can_sleep)
134{
135 struct ipoib_mcast *mcast;
136
Roland Dreierde6eb662005-11-02 07:23:14 -0800137 mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (!mcast)
139 return NULL;
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 mcast->dev = dev;
142 mcast->created = jiffies;
Hal Rosenstockce5b65c2005-09-18 13:47:53 -0700143 mcast->backoff = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 INIT_LIST_HEAD(&mcast->list);
146 INIT_LIST_HEAD(&mcast->neigh_list);
147 skb_queue_head_init(&mcast->pkt_queue);
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return mcast;
150}
151
152static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, union ib_gid *mgid)
153{
154 struct ipoib_dev_priv *priv = netdev_priv(dev);
155 struct rb_node *n = priv->multicast_tree.rb_node;
156
157 while (n) {
158 struct ipoib_mcast *mcast;
159 int ret;
160
161 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
162
163 ret = memcmp(mgid->raw, mcast->mcmember.mgid.raw,
164 sizeof (union ib_gid));
165 if (ret < 0)
166 n = n->rb_left;
167 else if (ret > 0)
168 n = n->rb_right;
169 else
170 return mcast;
171 }
172
173 return NULL;
174}
175
176static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
177{
178 struct ipoib_dev_priv *priv = netdev_priv(dev);
179 struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
180
181 while (*n) {
182 struct ipoib_mcast *tmcast;
183 int ret;
184
185 pn = *n;
186 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
187
188 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
189 sizeof (union ib_gid));
190 if (ret < 0)
191 n = &pn->rb_left;
192 else if (ret > 0)
193 n = &pn->rb_right;
194 else
195 return -EEXIST;
196 }
197
198 rb_link_node(&mcast->rb_node, pn, n);
199 rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
200
201 return 0;
202}
203
204static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
205 struct ib_sa_mcmember_rec *mcmember)
206{
207 struct net_device *dev = mcast->dev;
208 struct ipoib_dev_priv *priv = netdev_priv(dev);
209 int ret;
210
211 mcast->mcmember = *mcmember;
212
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;
218 }
219
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));
225
226 return 0;
227 }
228
229 ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
230 &mcast->mcmember.mgid);
231 if (ret < 0) {
232 ipoib_warn(priv, "couldn't attach QP to multicast group "
233 IPOIB_GID_FMT "\n",
234 IPOIB_GID_ARG(mcast->mcmember.mgid));
235
236 clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
237 return ret;
238 }
239 }
240
241 {
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,
247 .grh = {
248 .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
249 .hop_limit = mcast->mcmember.hop_limit,
250 .sgid_index = 0,
251 .traffic_class = mcast->mcmember.traffic_class
252 }
253 };
Roland Dreiere6ded992005-04-16 15:26:06 -0700254 int path_rate = ib_sa_rate_enum_to_int(mcast->mcmember.rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 av.grh.dgid = mcast->mcmember.mgid;
257
Roland Dreiere6ded992005-04-16 15:26:06 -0700258 if (path_rate > 0 && priv->local_rate > path_rate)
259 av.static_rate = (priv->local_rate - 1) / path_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
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));
264
265 mcast->ah = ipoib_create_ah(dev, priv->pd, &av);
266 if (!mcast->ah) {
267 ipoib_warn(priv, "ib_address_create failed\n");
268 } else {
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),
272 mcast->ah->ah,
273 be16_to_cpu(mcast->mcmember.mlid),
274 mcast->mcmember.sl);
275 }
276 }
277
278 /* actually send any queued packets */
279 while (!skb_queue_empty(&mcast->pkt_queue)) {
280 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
281
282 skb->dev = dev;
283
284 if (!skb->dst || !skb->dst->neighbour) {
285 /* put pseudoheader back on for next time */
286 skb_push(skb, sizeof (struct ipoib_pseudoheader));
287 }
288
289 if (dev_queue_xmit(skb))
290 ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
291 }
292
293 return 0;
294}
295
296static void
297ipoib_mcast_sendonly_join_complete(int status,
298 struct ib_sa_mcmember_rec *mcmember,
299 void *mcast_ptr)
300{
301 struct ipoib_mcast *mcast = mcast_ptr;
302 struct net_device *dev = mcast->dev;
303
304 if (!status)
305 ipoib_mcast_join_finish(mcast, mcmember);
306 else {
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);
311
312 /* Flush out any queued packets */
Roland Dreier8c608a32005-11-07 10:49:38 -0800313 while (!skb_queue_empty(&mcast->pkt_queue))
314 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 /* Clear the busy flag so we try again */
317 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
318 }
319
320 complete(&mcast->done);
321}
322
323static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
324{
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 */
329 .join_state = 4
330#else
331 .join_state = 1
332#endif
333 };
334 int ret = 0;
335
336 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
337 ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
338 return -ENODEV;
339 }
340
341 if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
342 ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
343 return -EBUSY;
344 }
345
346 rec.mgid = mcast->mcmember.mgid;
347 rec.port_gid = priv->local_gid;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700348 rec.pkey = cpu_to_be16(priv->pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Michael S. Tsirkinde922482005-11-29 10:18:45 -0800350 init_completion(&mcast->done);
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 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,
357 1000, GFP_ATOMIC,
358 ipoib_mcast_sendonly_join_complete,
359 mcast, &mcast->query);
360 if (ret < 0) {
361 ipoib_warn(priv, "ib_sa_mcmember_rec_set failed (ret = %d)\n",
362 ret);
363 } else {
364 ipoib_dbg_mcast(priv, "no multicast record for " IPOIB_GID_FMT
365 ", starting join\n",
366 IPOIB_GID_ARG(mcast->mcmember.mgid));
367
368 mcast->query_id = ret;
369 }
370
371 return ret;
372}
373
374static void ipoib_mcast_join_complete(int status,
375 struct ib_sa_mcmember_rec *mcmember,
376 void *mcast_ptr)
377{
378 struct ipoib_mcast *mcast = mcast_ptr;
379 struct net_device *dev = mcast->dev;
380 struct ipoib_dev_priv *priv = netdev_priv(dev);
381
382 ipoib_dbg_mcast(priv, "join completion for " IPOIB_GID_FMT
383 " (status %d)\n",
384 IPOIB_GID_ARG(mcast->mcmember.mgid), status);
385
386 if (!status && !ipoib_mcast_join_finish(mcast, mcmember)) {
Hal Rosenstockce5b65c2005-09-18 13:47:53 -0700387 mcast->backoff = 1;
Ingo Molnar95ed6442006-01-13 14:51:39 -0800388 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
390 queue_work(ipoib_workqueue, &priv->mcast_task);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800391 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 complete(&mcast->done);
393 return;
394 }
395
396 if (status == -EINTR) {
397 complete(&mcast->done);
398 return;
399 }
400
401 if (status && mcast->logcount++ < 20) {
402 if (status == -ETIMEDOUT || status == -EINTR) {
403 ipoib_dbg_mcast(priv, "multicast join failed for " IPOIB_GID_FMT
404 ", status %d\n",
405 IPOIB_GID_ARG(mcast->mcmember.mgid),
406 status);
407 } else {
408 ipoib_warn(priv, "multicast join failed for "
409 IPOIB_GID_FMT ", status %d\n",
410 IPOIB_GID_ARG(mcast->mcmember.mgid),
411 status);
412 }
413 }
414
415 mcast->backoff *= 2;
416 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
417 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
418
419 mcast->query = NULL;
420
Ingo Molnar95ed6442006-01-13 14:51:39 -0800421 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (test_bit(IPOIB_MCAST_RUN, &priv->flags)) {
423 if (status == -ETIMEDOUT)
424 queue_work(ipoib_workqueue, &priv->mcast_task);
425 else
426 queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
427 mcast->backoff * HZ);
428 } else
429 complete(&mcast->done);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800430 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 return;
433}
434
435static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
436 int create)
437{
438 struct ipoib_dev_priv *priv = netdev_priv(dev);
439 struct ib_sa_mcmember_rec rec = {
440 .join_state = 1
441 };
442 ib_sa_comp_mask comp_mask;
443 int ret = 0;
444
445 ipoib_dbg_mcast(priv, "joining MGID " IPOIB_GID_FMT "\n",
446 IPOIB_GID_ARG(mcast->mcmember.mgid));
447
448 rec.mgid = mcast->mcmember.mgid;
449 rec.port_gid = priv->local_gid;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700450 rec.pkey = cpu_to_be16(priv->pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 comp_mask =
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;
457
458 if (create) {
459 comp_mask |=
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;
464
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;
469 }
470
Michael S. Tsirkinde922482005-11-29 10:18:45 -0800471 init_completion(&mcast->done);
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 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);
477
478 if (ret < 0) {
479 ipoib_warn(priv, "ib_sa_mcmember_rec_set failed, status %d\n", ret);
480
481 mcast->backoff *= 2;
482 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
483 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
484
Ingo Molnar95ed6442006-01-13 14:51:39 -0800485 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
487 queue_delayed_work(ipoib_workqueue,
488 &priv->mcast_task,
Hal Rosenstockce5b65c2005-09-18 13:47:53 -0700489 mcast->backoff * HZ);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800490 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 } else
492 mcast->query_id = ret;
493}
494
495void ipoib_mcast_join_task(void *dev_ptr)
496{
497 struct net_device *dev = dev_ptr;
498 struct ipoib_dev_priv *priv = netdev_priv(dev);
499
500 if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
501 return;
502
503 if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
504 ipoib_warn(priv, "ib_gid_entry_get() failed\n");
505 else
506 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
507
508 {
509 struct ib_port_attr attr;
510
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);
515 } else
516 ipoib_warn(priv, "ib_query_port failed\n");
517 }
518
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");
Ingo Molnar95ed6442006-01-13 14:51:39 -0800523 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
525 queue_delayed_work(ipoib_workqueue,
526 &priv->mcast_task, HZ);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800527 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return;
529 }
530
531 memcpy(priv->broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
532 sizeof (union ib_gid));
533
534 spin_lock_irq(&priv->lock);
535 __ipoib_mcast_add(dev, priv->broadcast);
536 spin_unlock_irq(&priv->lock);
537 }
538
539 if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
540 ipoib_mcast_join(dev, priv->broadcast, 0);
541 return;
542 }
543
544 while (1) {
545 struct ipoib_mcast *mcast = NULL;
546
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 */
553 break;
554 }
555 }
556 spin_unlock_irq(&priv->lock);
557
558 if (&mcast->list == &priv->multicast_list) {
559 /* All done */
560 break;
561 }
562
563 ipoib_mcast_join(dev, mcast, 1);
564 return;
565 }
566
567 priv->mcast_mtu = ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu) -
568 IPOIB_ENCAP_LEN;
569 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
570
571 ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
572
573 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
574 netif_carrier_on(dev);
575}
576
577int ipoib_mcast_start_thread(struct net_device *dev)
578{
579 struct ipoib_dev_priv *priv = netdev_priv(dev);
580
581 ipoib_dbg_mcast(priv, "starting multicast thread\n");
582
Ingo Molnar95ed6442006-01-13 14:51:39 -0800583 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
585 queue_work(ipoib_workqueue, &priv->mcast_task);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800586 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 return 0;
589}
590
Roland Dreier8d2cae02005-09-20 10:52:04 -0700591int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 struct ipoib_dev_priv *priv = netdev_priv(dev);
594 struct ipoib_mcast *mcast;
595
596 ipoib_dbg_mcast(priv, "stopping multicast thread\n");
597
Ingo Molnar95ed6442006-01-13 14:51:39 -0800598 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
600 cancel_delayed_work(&priv->mcast_task);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800601 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Roland Dreier8d2cae02005-09-20 10:52:04 -0700603 if (flush)
604 flush_workqueue(ipoib_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
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);
611 }
612
613 list_for_each_entry(mcast, &priv->multicast_list, list) {
614 if (mcast->query) {
615 ib_sa_cancel_query(mcast->query_id, mcast->query);
616 mcast->query = NULL;
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);
620 }
621 }
622
623 return 0;
624}
625
626static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
627{
628 struct ipoib_dev_priv *priv = netdev_priv(dev);
629 struct ib_sa_mcmember_rec rec = {
630 .join_state = 1
631 };
632 int ret = 0;
633
634 if (!test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags))
635 return 0;
636
637 ipoib_dbg_mcast(priv, "leaving MGID " IPOIB_GID_FMT "\n",
638 IPOIB_GID_ARG(mcast->mcmember.mgid));
639
640 rec.mgid = mcast->mcmember.mgid;
641 rec.port_gid = priv->local_gid;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700642 rec.pkey = cpu_to_be16(priv->pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 /* Remove ourselves from the multicast group */
645 ret = ipoib_mcast_detach(dev, be16_to_cpu(mcast->mcmember.mlid),
646 &mcast->mcmember.mgid);
647 if (ret)
648 ipoib_warn(priv, "ipoib_mcast_detach failed (result = %d)\n", ret);
649
650 /*
651 * Just make one shot at leaving and don't wait for a reply;
652 * if we fail, too bad.
653 */
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,
659 0, GFP_ATOMIC, NULL,
660 mcast, &mcast->query);
661 if (ret < 0)
662 ipoib_warn(priv, "ib_sa_mcmember_rec_delete failed "
663 "for leave (result = %d)\n", ret);
664
665 return 0;
666}
667
668void ipoib_mcast_send(struct net_device *dev, union ib_gid *mgid,
669 struct sk_buff *skb)
670{
671 struct ipoib_dev_priv *priv = netdev_priv(dev);
672 struct ipoib_mcast *mcast;
673
674 /*
675 * We can only be called from ipoib_start_xmit, so we're
676 * inside tx_lock -- no need to save/restore flags.
677 */
678 spin_lock(&priv->lock);
679
680 mcast = __ipoib_mcast_find(dev, mgid);
681 if (!mcast) {
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));
685
686 mcast = ipoib_mcast_alloc(dev, 0);
687 if (!mcast) {
688 ipoib_warn(priv, "unable to allocate memory for "
689 "multicast structure\n");
690 dev_kfree_skb_any(skb);
691 goto out;
692 }
693
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);
698 }
699
700 if (!mcast->ah) {
701 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
702 skb_queue_tail(&mcast->pkt_queue, skb);
703 else
704 dev_kfree_skb_any(skb);
705
706 if (mcast->query)
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);
711
712 /*
713 * If lookup completes between here and out:, don't
714 * want to send packet twice.
715 */
716 mcast = NULL;
717 }
718
719out:
720 if (mcast && mcast->ah) {
721 if (skb->dst &&
722 skb->dst->neighbour &&
723 !*to_ipoib_neigh(skb->dst->neighbour)) {
724 struct ipoib_neigh *neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
725
726 if (neigh) {
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);
732 }
733 }
734
735 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
736 }
737
738 spin_unlock(&priv->lock);
739}
740
741void ipoib_mcast_dev_flush(struct net_device *dev)
742{
743 struct ipoib_dev_priv *priv = netdev_priv(dev);
744 LIST_HEAD(remove_list);
Eli Cohen988bd502006-01-12 14:32:20 -0800745 struct ipoib_mcast *mcast, *tmcast;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 unsigned long flags;
747
748 ipoib_dbg_mcast(priv, "flushing multicast list\n");
749
750 spin_lock_irqsave(&priv->lock, flags);
Eli Cohen988bd502006-01-12 14:32:20 -0800751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
Eli Cohen988bd502006-01-12 14:32:20 -0800753 list_del(&mcast->list);
754 rb_erase(&mcast->rb_node, &priv->multicast_tree);
755 list_add_tail(&mcast->list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
757
758 if (priv->broadcast) {
Eli Cohen988bd502006-01-12 14:32:20 -0800759 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
760 list_add_tail(&priv->broadcast->list, &remove_list);
761 priv->broadcast = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
763
764 spin_unlock_irqrestore(&priv->lock, flags);
765
766 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
767 ipoib_mcast_leave(dev, mcast);
768 ipoib_mcast_free(mcast);
769 }
770}
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772void ipoib_mcast_restart_task(void *dev_ptr)
773{
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);
779 unsigned long flags;
780
781 ipoib_dbg_mcast(priv, "restarting multicast task\n");
782
Roland Dreier8d2cae02005-09-20 10:52:04 -0700783 ipoib_mcast_stop_thread(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Michael S. Tsirkin78bfe0b2006-01-11 11:47:34 -0800785 spin_lock_irqsave(&dev->xmit_lock, flags);
786 spin_lock(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 /*
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
792 */
793
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);
797
798 /* Mark all of the entries that are found or don't exist */
799 for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
800 union ib_gid mgid;
801
802 memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
803
804 /* Add in the P_Key */
805 mgid.raw[4] = (priv->pkey >> 8) & 0xff;
806 mgid.raw[5] = priv->pkey & 0xff;
807
808 mcast = __ipoib_mcast_find(dev, &mgid);
809 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
810 struct ipoib_mcast *nmcast;
811
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));
815
816 nmcast = ipoib_mcast_alloc(dev, 0);
817 if (!nmcast) {
818 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
819 continue;
820 }
821
822 set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
823
824 nmcast->mcmember.mgid = mgid;
825
826 if (mcast) {
827 /* Destroy the send only entry */
828 list_del(&mcast->list);
829 list_add_tail(&mcast->list, &remove_list);
830
831 rb_replace_node(&mcast->rb_node,
832 &nmcast->rb_node,
833 &priv->multicast_tree);
834 } else
835 __ipoib_mcast_add(dev, nmcast);
836
837 list_add_tail(&nmcast->list, &priv->multicast_list);
838 }
839
840 if (mcast)
841 set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
842 }
843
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));
850
851 rb_erase(&mcast->rb_node, &priv->multicast_tree);
852
853 /* Move to the remove list */
854 list_del(&mcast->list);
855 list_add_tail(&mcast->list, &remove_list);
856 }
857 }
Michael S. Tsirkin78bfe0b2006-01-11 11:47:34 -0800858
859 spin_unlock(&priv->lock);
860 spin_unlock_irqrestore(&dev->xmit_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
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);
866 }
867
868 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
869 ipoib_mcast_start_thread(dev);
870}
871
Roland Dreier8ae5a8a2005-11-02 20:51:01 -0800872#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
875{
876 struct ipoib_mcast_iter *iter;
877
878 iter = kmalloc(sizeof *iter, GFP_KERNEL);
879 if (!iter)
880 return NULL;
881
882 iter->dev = dev;
Roland Dreier1732b0e2005-11-07 10:33:11 -0800883 memset(iter->mgid.raw, 0, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 if (ipoib_mcast_iter_next(iter)) {
Roland Dreier1732b0e2005-11-07 10:33:11 -0800886 kfree(iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return NULL;
888 }
889
890 return iter;
891}
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
894{
895 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
896 struct rb_node *n;
897 struct ipoib_mcast *mcast;
898 int ret = 1;
899
900 spin_lock_irq(&priv->lock);
901
902 n = rb_first(&priv->multicast_tree);
903
904 while (n) {
905 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
906
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));
914
915 ret = 0;
916
917 break;
918 }
919
920 n = rb_next(n);
921 }
922
923 spin_unlock_irq(&priv->lock);
924
925 return ret;
926}
927
928void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
929 union ib_gid *mgid,
930 unsigned long *created,
931 unsigned int *queuelen,
932 unsigned int *complete,
933 unsigned int *send_only)
934{
935 *mgid = iter->mgid;
936 *created = iter->created;
937 *queuelen = iter->queuelen;
938 *complete = iter->complete;
939 *send_only = iter->send_only;
940}
Roland Dreier8ae5a8a2005-11-02 20:51:01 -0800941
942#endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */