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