IPoIB: Use netif_tx_lock() and get rid of private tx_lock, LLTX
[linux-2.6-block.git] / drivers / infiniband / ulp / ipoib / ipoib_multicast.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
2a1d9b7f
RD
3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4 * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
1da177e4
LT
5 *
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.
1da177e4
LT
33 */
34
35#include <linux/skbuff.h>
36#include <linux/rtnetlink.h>
37#include <linux/ip.h>
38#include <linux/in.h>
39#include <linux/igmp.h>
40#include <linux/inetdevice.h>
41#include <linux/delay.h>
42#include <linux/completion.h>
43
14c85021
ACM
44#include <net/dst.h>
45
1da177e4
LT
46#include "ipoib.h"
47
48#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
49static int mcast_debug_level;
50
51module_param(mcast_debug_level, int, 0644);
52MODULE_PARM_DESC(mcast_debug_level,
53 "Enable multicast debug tracing if > 0");
54#endif
55
95ed644f 56static DEFINE_MUTEX(mcast_mutex);
1da177e4 57
1da177e4
LT
58struct ipoib_mcast_iter {
59 struct net_device *dev;
60 union ib_gid mgid;
61 unsigned long created;
62 unsigned int queuelen;
63 unsigned int complete;
64 unsigned int send_only;
65};
66
67static void ipoib_mcast_free(struct ipoib_mcast *mcast)
68{
69 struct net_device *dev = mcast->dev;
70 struct ipoib_dev_priv *priv = netdev_priv(dev);
71 struct ipoib_neigh *neigh, *tmp;
b36f170b 72 int tx_dropped = 0;
1da177e4
LT
73
74 ipoib_dbg_mcast(netdev_priv(dev),
75 "deleting multicast group " IPOIB_GID_FMT "\n",
76 IPOIB_GID_ARG(mcast->mcmember.mgid));
77
943c246e 78 spin_lock_irq(&priv->lock);
1da177e4
LT
79
80 list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
97460df3
EC
81 /*
82 * It's safe to call ipoib_put_ah() inside priv->lock
83 * here, because we know that mcast->ah will always
84 * hold one more reference, so ipoib_put_ah() will
85 * never do more than decrement the ref count.
86 */
1da177e4 87 if (neigh->ah)
97460df3 88 ipoib_put_ah(neigh->ah);
2745b5b7 89 ipoib_neigh_free(dev, neigh);
1da177e4
LT
90 }
91
943c246e 92 spin_unlock_irq(&priv->lock);
1da177e4 93
1da177e4
LT
94 if (mcast->ah)
95 ipoib_put_ah(mcast->ah);
96
b36f170b
MT
97 while (!skb_queue_empty(&mcast->pkt_queue)) {
98 ++tx_dropped;
8c608a32 99 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
b36f170b
MT
100 }
101
943c246e 102 netif_tx_lock_bh(dev);
de903512 103 dev->stats.tx_dropped += tx_dropped;
943c246e 104 netif_tx_unlock_bh(dev);
1da177e4
LT
105
106 kfree(mcast);
107}
108
109static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
110 int can_sleep)
111{
112 struct ipoib_mcast *mcast;
113
de6eb66b 114 mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
1da177e4
LT
115 if (!mcast)
116 return NULL;
117
1da177e4
LT
118 mcast->dev = dev;
119 mcast->created = jiffies;
ce5b65cc 120 mcast->backoff = 1;
1da177e4
LT
121
122 INIT_LIST_HEAD(&mcast->list);
123 INIT_LIST_HEAD(&mcast->neigh_list);
124 skb_queue_head_init(&mcast->pkt_queue);
125
1da177e4
LT
126 return mcast;
127}
128
37c22a77 129static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
1da177e4
LT
130{
131 struct ipoib_dev_priv *priv = netdev_priv(dev);
132 struct rb_node *n = priv->multicast_tree.rb_node;
133
134 while (n) {
135 struct ipoib_mcast *mcast;
136 int ret;
137
138 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
139
37c22a77 140 ret = memcmp(mgid, mcast->mcmember.mgid.raw,
1da177e4
LT
141 sizeof (union ib_gid));
142 if (ret < 0)
143 n = n->rb_left;
144 else if (ret > 0)
145 n = n->rb_right;
146 else
147 return mcast;
148 }
149
150 return NULL;
151}
152
153static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
154{
155 struct ipoib_dev_priv *priv = netdev_priv(dev);
156 struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
157
158 while (*n) {
159 struct ipoib_mcast *tmcast;
160 int ret;
161
162 pn = *n;
163 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
164
165 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
166 sizeof (union ib_gid));
167 if (ret < 0)
168 n = &pn->rb_left;
169 else if (ret > 0)
170 n = &pn->rb_right;
171 else
172 return -EEXIST;
173 }
174
175 rb_link_node(&mcast->rb_node, pn, n);
176 rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
177
178 return 0;
179}
180
181static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
182 struct ib_sa_mcmember_rec *mcmember)
183{
184 struct net_device *dev = mcast->dev;
185 struct ipoib_dev_priv *priv = netdev_priv(dev);
7343b231 186 struct ipoib_ah *ah;
1da177e4 187 int ret;
d0de1362 188 int set_qkey = 0;
1da177e4
LT
189
190 mcast->mcmember = *mcmember;
191
192 /* Set the cached Q_Key before we attach if it's the broadcast group */
193 if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
194 sizeof (union ib_gid))) {
e1d50dce
JM
195 spin_lock_irq(&priv->lock);
196 if (!priv->broadcast) {
197 spin_unlock_irq(&priv->lock);
198 return -EAGAIN;
199 }
1da177e4 200 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
e1d50dce 201 spin_unlock_irq(&priv->lock);
1da177e4 202 priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
d0de1362 203 set_qkey = 1;
1da177e4
LT
204 }
205
206 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
207 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
208 ipoib_warn(priv, "multicast group " IPOIB_GID_FMT
209 " already attached\n",
210 IPOIB_GID_ARG(mcast->mcmember.mgid));
211
212 return 0;
213 }
214
215 ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
d0de1362 216 &mcast->mcmember.mgid, set_qkey);
1da177e4
LT
217 if (ret < 0) {
218 ipoib_warn(priv, "couldn't attach QP to multicast group "
219 IPOIB_GID_FMT "\n",
220 IPOIB_GID_ARG(mcast->mcmember.mgid));
221
222 clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
223 return ret;
224 }
225 }
226
227 {
228 struct ib_ah_attr av = {
229 .dlid = be16_to_cpu(mcast->mcmember.mlid),
230 .port_num = priv->port,
231 .sl = mcast->mcmember.sl,
232 .ah_flags = IB_AH_GRH,
bf6a9e31 233 .static_rate = mcast->mcmember.rate,
1da177e4
LT
234 .grh = {
235 .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
236 .hop_limit = mcast->mcmember.hop_limit,
237 .sgid_index = 0,
238 .traffic_class = mcast->mcmember.traffic_class
239 }
240 };
1da177e4
LT
241 av.grh.dgid = mcast->mcmember.mgid;
242
7343b231
EC
243 ah = ipoib_create_ah(dev, priv->pd, &av);
244 if (!ah) {
1da177e4
LT
245 ipoib_warn(priv, "ib_address_create failed\n");
246 } else {
624d01f8
OG
247 spin_lock_irq(&priv->lock);
248 mcast->ah = ah;
249 spin_unlock_irq(&priv->lock);
250
1da177e4
LT
251 ipoib_dbg_mcast(priv, "MGID " IPOIB_GID_FMT
252 " AV %p, LID 0x%04x, SL %d\n",
253 IPOIB_GID_ARG(mcast->mcmember.mgid),
254 mcast->ah->ah,
255 be16_to_cpu(mcast->mcmember.mlid),
256 mcast->mcmember.sl);
257 }
258 }
259
260 /* actually send any queued packets */
943c246e 261 netif_tx_lock_bh(dev);
1da177e4
LT
262 while (!skb_queue_empty(&mcast->pkt_queue)) {
263 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
943c246e 264 netif_tx_unlock_bh(dev);
1da177e4
LT
265
266 skb->dev = dev;
267
268 if (!skb->dst || !skb->dst->neighbour) {
269 /* put pseudoheader back on for next time */
270 skb_push(skb, sizeof (struct ipoib_pseudoheader));
271 }
272
273 if (dev_queue_xmit(skb))
274 ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
943c246e 275 netif_tx_lock_bh(dev);
1da177e4 276 }
943c246e 277 netif_tx_unlock_bh(dev);
1da177e4
LT
278
279 return 0;
280}
281
faec2f7b 282static int
1da177e4 283ipoib_mcast_sendonly_join_complete(int status,
faec2f7b 284 struct ib_sa_multicast *multicast)
1da177e4 285{
faec2f7b 286 struct ipoib_mcast *mcast = multicast->context;
1da177e4
LT
287 struct net_device *dev = mcast->dev;
288
faec2f7b
SH
289 /* We trap for port events ourselves. */
290 if (status == -ENETRESET)
291 return 0;
292
1da177e4 293 if (!status)
faec2f7b
SH
294 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
295
296 if (status) {
1da177e4
LT
297 if (mcast->logcount++ < 20)
298 ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for "
299 IPOIB_GID_FMT ", status %d\n",
300 IPOIB_GID_ARG(mcast->mcmember.mgid), status);
301
302 /* Flush out any queued packets */
943c246e 303 netif_tx_lock_bh(dev);
b36f170b 304 while (!skb_queue_empty(&mcast->pkt_queue)) {
de903512 305 ++dev->stats.tx_dropped;
8c608a32 306 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
b36f170b 307 }
943c246e 308 netif_tx_unlock_bh(dev);
1da177e4
LT
309
310 /* Clear the busy flag so we try again */
faec2f7b
SH
311 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY,
312 &mcast->flags);
1da177e4 313 }
faec2f7b 314 return status;
1da177e4
LT
315}
316
317static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
318{
319 struct net_device *dev = mcast->dev;
320 struct ipoib_dev_priv *priv = netdev_priv(dev);
321 struct ib_sa_mcmember_rec rec = {
322#if 0 /* Some SMs don't support send-only yet */
323 .join_state = 4
324#else
325 .join_state = 1
326#endif
327 };
328 int ret = 0;
329
330 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
331 ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
332 return -ENODEV;
333 }
334
335 if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
336 ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
337 return -EBUSY;
338 }
339
340 rec.mgid = mcast->mcmember.mgid;
341 rec.port_gid = priv->local_gid;
97f52eb4 342 rec.pkey = cpu_to_be16(priv->pkey);
1da177e4 343
faec2f7b
SH
344 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca,
345 priv->port, &rec,
346 IB_SA_MCMEMBER_REC_MGID |
347 IB_SA_MCMEMBER_REC_PORT_GID |
348 IB_SA_MCMEMBER_REC_PKEY |
349 IB_SA_MCMEMBER_REC_JOIN_STATE,
350 GFP_ATOMIC,
351 ipoib_mcast_sendonly_join_complete,
352 mcast);
353 if (IS_ERR(mcast->mc)) {
354 ret = PTR_ERR(mcast->mc);
355 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
356 ipoib_warn(priv, "ib_sa_join_multicast failed (ret = %d)\n",
1da177e4
LT
357 ret);
358 } else {
359 ipoib_dbg_mcast(priv, "no multicast record for " IPOIB_GID_FMT
360 ", starting join\n",
361 IPOIB_GID_ARG(mcast->mcmember.mgid));
1da177e4
LT
362 }
363
364 return ret;
365}
366
e8224e4b
YE
367void ipoib_mcast_carrier_on_task(struct work_struct *work)
368{
369 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
370 carrier_on_task);
371
372 /*
373 * Take rtnl_lock to avoid racing with ipoib_stop() and
374 * turning the carrier back on while a device is being
375 * removed.
376 */
377 rtnl_lock();
378 netif_carrier_on(priv->dev);
379 rtnl_unlock();
380}
381
faec2f7b
SH
382static int ipoib_mcast_join_complete(int status,
383 struct ib_sa_multicast *multicast)
1da177e4 384{
faec2f7b 385 struct ipoib_mcast *mcast = multicast->context;
1da177e4
LT
386 struct net_device *dev = mcast->dev;
387 struct ipoib_dev_priv *priv = netdev_priv(dev);
388
389 ipoib_dbg_mcast(priv, "join completion for " IPOIB_GID_FMT
390 " (status %d)\n",
391 IPOIB_GID_ARG(mcast->mcmember.mgid), status);
392
faec2f7b
SH
393 /* We trap for port events ourselves. */
394 if (status == -ENETRESET)
395 return 0;
396
397 if (!status)
398 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
399
400 if (!status) {
ce5b65cc 401 mcast->backoff = 1;
95ed644f 402 mutex_lock(&mcast_mutex);
1da177e4 403 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
c4028958
DH
404 queue_delayed_work(ipoib_workqueue,
405 &priv->mcast_task, 0);
95ed644f 406 mutex_unlock(&mcast_mutex);
55c9adde 407
e8224e4b
YE
408 /*
409 * Defer carrier on work to ipoib_workqueue to avoid a
410 * deadlock on rtnl_lock here.
411 */
412 if (mcast == priv->broadcast)
413 queue_work(ipoib_workqueue, &priv->carrier_on_task);
55c9adde 414
faec2f7b 415 return 0;
1da177e4
LT
416 }
417
faec2f7b
SH
418 if (mcast->logcount++ < 20) {
419 if (status == -ETIMEDOUT) {
1da177e4
LT
420 ipoib_dbg_mcast(priv, "multicast join failed for " IPOIB_GID_FMT
421 ", status %d\n",
422 IPOIB_GID_ARG(mcast->mcmember.mgid),
423 status);
424 } else {
425 ipoib_warn(priv, "multicast join failed for "
426 IPOIB_GID_FMT ", status %d\n",
427 IPOIB_GID_ARG(mcast->mcmember.mgid),
428 status);
429 }
430 }
431
432 mcast->backoff *= 2;
433 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
434 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
435
faec2f7b
SH
436 /* Clear the busy flag so we try again */
437 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
9acf6a85 438
faec2f7b 439 mutex_lock(&mcast_mutex);
9acf6a85 440 spin_lock_irq(&priv->lock);
faec2f7b
SH
441 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
442 queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
443 mcast->backoff * HZ);
9acf6a85 444 spin_unlock_irq(&priv->lock);
95ed644f 445 mutex_unlock(&mcast_mutex);
1da177e4 446
faec2f7b 447 return status;
1da177e4
LT
448}
449
450static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
451 int create)
452{
453 struct ipoib_dev_priv *priv = netdev_priv(dev);
454 struct ib_sa_mcmember_rec rec = {
455 .join_state = 1
456 };
457 ib_sa_comp_mask comp_mask;
458 int ret = 0;
459
460 ipoib_dbg_mcast(priv, "joining MGID " IPOIB_GID_FMT "\n",
461 IPOIB_GID_ARG(mcast->mcmember.mgid));
462
463 rec.mgid = mcast->mcmember.mgid;
464 rec.port_gid = priv->local_gid;
97f52eb4 465 rec.pkey = cpu_to_be16(priv->pkey);
1da177e4
LT
466
467 comp_mask =
468 IB_SA_MCMEMBER_REC_MGID |
469 IB_SA_MCMEMBER_REC_PORT_GID |
470 IB_SA_MCMEMBER_REC_PKEY |
471 IB_SA_MCMEMBER_REC_JOIN_STATE;
472
473 if (create) {
474 comp_mask |=
d0df6d6d
RD
475 IB_SA_MCMEMBER_REC_QKEY |
476 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
477 IB_SA_MCMEMBER_REC_MTU |
478 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS |
479 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
480 IB_SA_MCMEMBER_REC_RATE |
481 IB_SA_MCMEMBER_REC_SL |
482 IB_SA_MCMEMBER_REC_FLOW_LABEL |
483 IB_SA_MCMEMBER_REC_HOP_LIMIT;
1da177e4
LT
484
485 rec.qkey = priv->broadcast->mcmember.qkey;
d0df6d6d
RD
486 rec.mtu_selector = IB_SA_EQ;
487 rec.mtu = priv->broadcast->mcmember.mtu;
488 rec.traffic_class = priv->broadcast->mcmember.traffic_class;
489 rec.rate_selector = IB_SA_EQ;
490 rec.rate = priv->broadcast->mcmember.rate;
1da177e4
LT
491 rec.sl = priv->broadcast->mcmember.sl;
492 rec.flow_label = priv->broadcast->mcmember.flow_label;
d0df6d6d 493 rec.hop_limit = priv->broadcast->mcmember.hop_limit;
1da177e4
LT
494 }
495
faec2f7b
SH
496 set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
497 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
498 &rec, comp_mask, GFP_KERNEL,
499 ipoib_mcast_join_complete, mcast);
500 if (IS_ERR(mcast->mc)) {
501 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
502 ret = PTR_ERR(mcast->mc);
503 ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
1da177e4
LT
504
505 mcast->backoff *= 2;
506 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
507 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
508
95ed644f 509 mutex_lock(&mcast_mutex);
1da177e4
LT
510 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
511 queue_delayed_work(ipoib_workqueue,
512 &priv->mcast_task,
ce5b65cc 513 mcast->backoff * HZ);
95ed644f 514 mutex_unlock(&mcast_mutex);
faec2f7b 515 }
1da177e4
LT
516}
517
c4028958 518void ipoib_mcast_join_task(struct work_struct *work)
1da177e4 519{
c4028958
DH
520 struct ipoib_dev_priv *priv =
521 container_of(work, struct ipoib_dev_priv, mcast_task.work);
522 struct net_device *dev = priv->dev;
1da177e4
LT
523
524 if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
525 return;
526
527 if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
24bd1e4e 528 ipoib_warn(priv, "ib_query_gid() failed\n");
1da177e4
LT
529 else
530 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
531
532 {
533 struct ib_port_attr attr;
534
658bcef6
RD
535 if (!ib_query_port(priv->ca, priv->port, &attr))
536 priv->local_lid = attr.lid;
537 else
faec2f7b 538 ipoib_warn(priv, "ib_query_port failed\n");
1da177e4
LT
539 }
540
541 if (!priv->broadcast) {
20b83382
RD
542 struct ipoib_mcast *broadcast;
543
544 broadcast = ipoib_mcast_alloc(dev, 1);
545 if (!broadcast) {
1da177e4 546 ipoib_warn(priv, "failed to allocate broadcast group\n");
95ed644f 547 mutex_lock(&mcast_mutex);
1da177e4
LT
548 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
549 queue_delayed_work(ipoib_workqueue,
550 &priv->mcast_task, HZ);
95ed644f 551 mutex_unlock(&mcast_mutex);
1da177e4
LT
552 return;
553 }
554
20b83382
RD
555 spin_lock_irq(&priv->lock);
556 memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
1da177e4 557 sizeof (union ib_gid));
20b83382 558 priv->broadcast = broadcast;
1da177e4 559
1da177e4
LT
560 __ipoib_mcast_add(dev, priv->broadcast);
561 spin_unlock_irq(&priv->lock);
562 }
563
564 if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
faec2f7b
SH
565 if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
566 ipoib_mcast_join(dev, priv->broadcast, 0);
1da177e4
LT
567 return;
568 }
569
570 while (1) {
571 struct ipoib_mcast *mcast = NULL;
572
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;
580 }
581 }
582 spin_unlock_irq(&priv->lock);
583
584 if (&mcast->list == &priv->multicast_list) {
585 /* All done */
586 break;
587 }
588
589 ipoib_mcast_join(dev, mcast, 1);
590 return;
591 }
592
bc7b3a36 593 priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
839fcaba 594
c8c2afe3
EC
595 if (!ipoib_cm_admin_enabled(dev)) {
596 rtnl_lock();
bd360671 597 dev_set_mtu(dev, min(priv->mcast_mtu, priv->admin_mtu));
c8c2afe3
EC
598 rtnl_unlock();
599 }
1da177e4
LT
600
601 ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
602
603 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
1da177e4
LT
604}
605
606int ipoib_mcast_start_thread(struct net_device *dev)
607{
608 struct ipoib_dev_priv *priv = netdev_priv(dev);
609
610 ipoib_dbg_mcast(priv, "starting multicast thread\n");
611
95ed644f 612 mutex_lock(&mcast_mutex);
1da177e4 613 if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
c4028958 614 queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
95ed644f 615 mutex_unlock(&mcast_mutex);
1da177e4
LT
616
617 return 0;
618}
619
8d2cae06 620int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
1da177e4
LT
621{
622 struct ipoib_dev_priv *priv = netdev_priv(dev);
1da177e4
LT
623
624 ipoib_dbg_mcast(priv, "stopping multicast thread\n");
625
95ed644f 626 mutex_lock(&mcast_mutex);
1da177e4
LT
627 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
628 cancel_delayed_work(&priv->mcast_task);
95ed644f 629 mutex_unlock(&mcast_mutex);
1da177e4 630
8d2cae06
RD
631 if (flush)
632 flush_workqueue(ipoib_workqueue);
1da177e4 633
1da177e4
LT
634 return 0;
635}
636
637static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
638{
639 struct ipoib_dev_priv *priv = netdev_priv(dev);
1da177e4
LT
640 int ret = 0;
641
e07832b6
SH
642 if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
643 ib_sa_free_multicast(mcast->mc);
644
faec2f7b
SH
645 if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
646 ipoib_dbg_mcast(priv, "leaving MGID " IPOIB_GID_FMT "\n",
647 IPOIB_GID_ARG(mcast->mcmember.mgid));
1da177e4 648
faec2f7b 649 /* Remove ourselves from the multicast group */
9eae554c
RD
650 ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
651 be16_to_cpu(mcast->mcmember.mlid));
faec2f7b 652 if (ret)
9eae554c 653 ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
faec2f7b 654 }
1da177e4 655
1da177e4
LT
656 return 0;
657}
658
37c22a77 659void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb)
1da177e4
LT
660{
661 struct ipoib_dev_priv *priv = netdev_priv(dev);
662 struct ipoib_mcast *mcast;
943c246e 663 unsigned long flags;
1da177e4 664
943c246e 665 spin_lock_irqsave(&priv->lock, flags);
1da177e4 666
b3e2749b 667 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags) ||
20b83382
RD
668 !priv->broadcast ||
669 !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
de903512 670 ++dev->stats.tx_dropped;
479a0796
MT
671 dev_kfree_skb_any(skb);
672 goto unlock;
673 }
674
1da177e4
LT
675 mcast = __ipoib_mcast_find(dev, mgid);
676 if (!mcast) {
677 /* Let's create a new send only group now */
678 ipoib_dbg_mcast(priv, "setting up send only multicast group for "
37c22a77 679 IPOIB_GID_FMT "\n", IPOIB_GID_RAW_ARG(mgid));
1da177e4
LT
680
681 mcast = ipoib_mcast_alloc(dev, 0);
682 if (!mcast) {
683 ipoib_warn(priv, "unable to allocate memory for "
684 "multicast structure\n");
de903512 685 ++dev->stats.tx_dropped;
1da177e4
LT
686 dev_kfree_skb_any(skb);
687 goto out;
688 }
689
690 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
37c22a77 691 memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
1da177e4
LT
692 __ipoib_mcast_add(dev, mcast);
693 list_add_tail(&mcast->list, &priv->multicast_list);
694 }
695
696 if (!mcast->ah) {
697 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
698 skb_queue_tail(&mcast->pkt_queue, skb);
b36f170b 699 else {
de903512 700 ++dev->stats.tx_dropped;
1da177e4 701 dev_kfree_skb_any(skb);
b36f170b 702 }
1da177e4 703
faec2f7b 704 if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
1da177e4
LT
705 ipoib_dbg_mcast(priv, "no address vector, "
706 "but multicast join already started\n");
707 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
708 ipoib_mcast_sendonly_join(mcast);
709
710 /*
711 * If lookup completes between here and out:, don't
712 * want to send packet twice.
713 */
714 mcast = NULL;
715 }
716
717out:
718 if (mcast && mcast->ah) {
2337f809 719 if (skb->dst &&
1da177e4
LT
720 skb->dst->neighbour &&
721 !*to_ipoib_neigh(skb->dst->neighbour)) {
732a2170
MS
722 struct ipoib_neigh *neigh = ipoib_neigh_alloc(skb->dst->neighbour,
723 skb->dev);
1da177e4
LT
724
725 if (neigh) {
726 kref_get(&mcast->ah->ref);
2337f809 727 neigh->ah = mcast->ah;
1da177e4
LT
728 list_add_tail(&neigh->list, &mcast->neigh_list);
729 }
730 }
731
732 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
733 }
734
479a0796 735unlock:
943c246e 736 spin_unlock_irqrestore(&priv->lock, flags);
1da177e4
LT
737}
738
739void ipoib_mcast_dev_flush(struct net_device *dev)
740{
741 struct ipoib_dev_priv *priv = netdev_priv(dev);
742 LIST_HEAD(remove_list);
988bd503 743 struct ipoib_mcast *mcast, *tmcast;
1da177e4
LT
744 unsigned long flags;
745
746 ipoib_dbg_mcast(priv, "flushing multicast list\n");
747
748 spin_lock_irqsave(&priv->lock, flags);
1da177e4 749
988bd503
EC
750 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
751 list_del(&mcast->list);
752 rb_erase(&mcast->rb_node, &priv->multicast_tree);
753 list_add_tail(&mcast->list, &remove_list);
1da177e4
LT
754 }
755
756 if (priv->broadcast) {
3cd96564 757 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
988bd503
EC
758 list_add_tail(&priv->broadcast->list, &remove_list);
759 priv->broadcast = NULL;
1da177e4
LT
760 }
761
762 spin_unlock_irqrestore(&priv->lock, flags);
763
764 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
765 ipoib_mcast_leave(dev, mcast);
766 ipoib_mcast_free(mcast);
767 }
768}
769
c4028958 770void ipoib_mcast_restart_task(struct work_struct *work)
1da177e4 771{
c4028958
DH
772 struct ipoib_dev_priv *priv =
773 container_of(work, struct ipoib_dev_priv, restart_task);
774 struct net_device *dev = priv->dev;
1da177e4
LT
775 struct dev_mc_list *mclist;
776 struct ipoib_mcast *mcast, *tmcast;
777 LIST_HEAD(remove_list);
778 unsigned long flags;
335a64a5 779 struct ib_sa_mcmember_rec rec;
1da177e4
LT
780
781 ipoib_dbg_mcast(priv, "restarting multicast task\n");
782
8d2cae06 783 ipoib_mcast_stop_thread(dev, 0);
1da177e4 784
932ff279 785 local_irq_save(flags);
e308a5d8 786 netif_addr_lock(dev);
78bfe0b5 787 spin_lock(&priv->lock);
1da177e4
LT
788
789 /*
790 * Unfortunately, the networking core only gives us a list of all of
791 * the multicast hardware addresses. We need to figure out which ones
792 * are new and which ones have been removed
793 */
794
795 /* Clear out the found flag */
796 list_for_each_entry(mcast, &priv->multicast_list, list)
797 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
798
799 /* Mark all of the entries that are found or don't exist */
800 for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
801 union ib_gid mgid;
802
803 memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
804
1da177e4
LT
805 mcast = __ipoib_mcast_find(dev, &mgid);
806 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
807 struct ipoib_mcast *nmcast;
808
335a64a5
OG
809 /* ignore group which is directly joined by userspace */
810 if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
811 !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
812 ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid "
813 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
814 continue;
815 }
816
1da177e4
LT
817 /* Not found or send-only group, let's add a new entry */
818 ipoib_dbg_mcast(priv, "adding multicast entry for mgid "
819 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
820
821 nmcast = ipoib_mcast_alloc(dev, 0);
822 if (!nmcast) {
823 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
824 continue;
825 }
826
827 set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
828
829 nmcast->mcmember.mgid = mgid;
830
831 if (mcast) {
832 /* Destroy the send only entry */
179e0917 833 list_move_tail(&mcast->list, &remove_list);
1da177e4
LT
834
835 rb_replace_node(&mcast->rb_node,
836 &nmcast->rb_node,
837 &priv->multicast_tree);
838 } else
839 __ipoib_mcast_add(dev, nmcast);
840
841 list_add_tail(&nmcast->list, &priv->multicast_list);
842 }
843
844 if (mcast)
845 set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
846 }
847
848 /* Remove all of the entries don't exist anymore */
849 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
850 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
851 !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
852 ipoib_dbg_mcast(priv, "deleting multicast group " IPOIB_GID_FMT "\n",
853 IPOIB_GID_ARG(mcast->mcmember.mgid));
854
855 rb_erase(&mcast->rb_node, &priv->multicast_tree);
856
857 /* Move to the remove list */
179e0917 858 list_move_tail(&mcast->list, &remove_list);
1da177e4
LT
859 }
860 }
78bfe0b5
MT
861
862 spin_unlock(&priv->lock);
e308a5d8 863 netif_addr_unlock(dev);
932ff279 864 local_irq_restore(flags);
1da177e4
LT
865
866 /* We have to cancel outside of the spinlock */
867 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
868 ipoib_mcast_leave(mcast->dev, mcast);
869 ipoib_mcast_free(mcast);
870 }
871
872 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
873 ipoib_mcast_start_thread(dev);
874}
875
8ae5a8a2
RD
876#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
877
1da177e4
LT
878struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
879{
880 struct ipoib_mcast_iter *iter;
881
882 iter = kmalloc(sizeof *iter, GFP_KERNEL);
883 if (!iter)
884 return NULL;
885
886 iter->dev = dev;
1732b0ef 887 memset(iter->mgid.raw, 0, 16);
1da177e4
LT
888
889 if (ipoib_mcast_iter_next(iter)) {
1732b0ef 890 kfree(iter);
1da177e4
LT
891 return NULL;
892 }
893
894 return iter;
895}
896
1da177e4
LT
897int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
898{
899 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
900 struct rb_node *n;
901 struct ipoib_mcast *mcast;
902 int ret = 1;
903
904 spin_lock_irq(&priv->lock);
905
906 n = rb_first(&priv->multicast_tree);
907
908 while (n) {
909 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
910
911 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
912 sizeof (union ib_gid)) < 0) {
913 iter->mgid = mcast->mcmember.mgid;
914 iter->created = mcast->created;
915 iter->queuelen = skb_queue_len(&mcast->pkt_queue);
916 iter->complete = !!mcast->ah;
917 iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
918
919 ret = 0;
920
921 break;
922 }
923
924 n = rb_next(n);
925 }
926
927 spin_unlock_irq(&priv->lock);
928
929 return ret;
930}
931
932void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
933 union ib_gid *mgid,
934 unsigned long *created,
935 unsigned int *queuelen,
936 unsigned int *complete,
937 unsigned int *send_only)
938{
939 *mgid = iter->mgid;
940 *created = iter->created;
941 *queuelen = iter->queuelen;
942 *complete = iter->complete;
943 *send_only = iter->send_only;
944}
8ae5a8a2
RD
945
946#endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */