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