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