blob: d9d1223c3fd5f7dc1609764c9a1db16e3a20018f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
Roland Dreier2a1d9b72005-08-10 23:03:10 -07003 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4 * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
35#include <linux/skbuff.h>
36#include <linux/rtnetlink.h>
37#include <linux/ip.h>
38#include <linux/in.h>
39#include <linux/igmp.h>
40#include <linux/inetdevice.h>
41#include <linux/delay.h>
42#include <linux/completion.h>
43
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020044#include <net/dst.h>
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "ipoib.h"
47
48#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
49static int mcast_debug_level;
50
51module_param(mcast_debug_level, int, 0644);
52MODULE_PARM_DESC(mcast_debug_level,
53 "Enable multicast debug tracing if > 0");
54#endif
55
Ingo Molnar95ed6442006-01-13 14:51:39 -080056static DEFINE_MUTEX(mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058struct ipoib_mcast_iter {
59 struct net_device *dev;
60 union ib_gid mgid;
61 unsigned long created;
62 unsigned int queuelen;
63 unsigned int complete;
64 unsigned int send_only;
65};
66
67static void ipoib_mcast_free(struct ipoib_mcast *mcast)
68{
69 struct net_device *dev = mcast->dev;
70 struct ipoib_dev_priv *priv = netdev_priv(dev);
71 struct ipoib_neigh *neigh, *tmp;
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -080072 int tx_dropped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 ipoib_dbg_mcast(netdev_priv(dev),
75 "deleting multicast group " IPOIB_GID_FMT "\n",
76 IPOIB_GID_ARG(mcast->mcmember.mgid));
77
Roland Dreier943c2462008-09-30 10:36:21 -070078 spin_lock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
Eli Cohen97460df2006-01-10 07:43:02 -080081 /*
82 * It's safe to call ipoib_put_ah() inside priv->lock
83 * here, because we know that mcast->ah will always
84 * hold one more reference, so ipoib_put_ah() will
85 * never do more than decrement the ref count.
86 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (neigh->ah)
Eli Cohen97460df2006-01-10 07:43:02 -080088 ipoib_put_ah(neigh->ah);
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +020089 ipoib_neigh_free(dev, neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
91
Roland Dreier943c2462008-09-30 10:36:21 -070092 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 if (mcast->ah)
95 ipoib_put_ah(mcast->ah);
96
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -080097 while (!skb_queue_empty(&mcast->pkt_queue)) {
98 ++tx_dropped;
Roland Dreier8c608a32005-11-07 10:49:38 -080099 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -0800100 }
101
Roland Dreier943c2462008-09-30 10:36:21 -0700102 netif_tx_lock_bh(dev);
Roland Dreierde903512007-09-28 15:33:51 -0700103 dev->stats.tx_dropped += tx_dropped;
Roland Dreier943c2462008-09-30 10:36:21 -0700104 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 kfree(mcast);
107}
108
109static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
110 int can_sleep)
111{
112 struct ipoib_mcast *mcast;
113
Roland Dreierde6eb662005-11-02 07:23:14 -0800114 mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (!mcast)
116 return NULL;
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 mcast->dev = dev;
119 mcast->created = jiffies;
Hal Rosenstockce5b65c2005-09-18 13:47:53 -0700120 mcast->backoff = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 INIT_LIST_HEAD(&mcast->list);
123 INIT_LIST_HEAD(&mcast->neigh_list);
124 skb_queue_head_init(&mcast->pkt_queue);
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return mcast;
127}
128
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300129static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 struct ipoib_dev_priv *priv = netdev_priv(dev);
132 struct rb_node *n = priv->multicast_tree.rb_node;
133
134 while (n) {
135 struct ipoib_mcast *mcast;
136 int ret;
137
138 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
139
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300140 ret = memcmp(mgid, mcast->mcmember.mgid.raw,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 sizeof (union ib_gid));
142 if (ret < 0)
143 n = n->rb_left;
144 else if (ret > 0)
145 n = n->rb_right;
146 else
147 return mcast;
148 }
149
150 return NULL;
151}
152
153static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
154{
155 struct ipoib_dev_priv *priv = netdev_priv(dev);
156 struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
157
158 while (*n) {
159 struct ipoib_mcast *tmcast;
160 int ret;
161
162 pn = *n;
163 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
164
165 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
166 sizeof (union ib_gid));
167 if (ret < 0)
168 n = &pn->rb_left;
169 else if (ret > 0)
170 n = &pn->rb_right;
171 else
172 return -EEXIST;
173 }
174
175 rb_link_node(&mcast->rb_node, pn, n);
176 rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
177
178 return 0;
179}
180
181static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
182 struct ib_sa_mcmember_rec *mcmember)
183{
184 struct net_device *dev = mcast->dev;
185 struct ipoib_dev_priv *priv = netdev_priv(dev);
Eli Cohen7343b232006-02-27 20:47:43 -0800186 struct ipoib_ah *ah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 int ret;
Eli Cohend0de1362008-07-14 23:48:50 -0700188 int set_qkey = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 mcast->mcmember = *mcmember;
191
192 /* Set the cached Q_Key before we attach if it's the broadcast group */
193 if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
194 sizeof (union ib_gid))) {
Jack Morgensteine1d50dc2008-05-20 15:41:09 -0700195 spin_lock_irq(&priv->lock);
196 if (!priv->broadcast) {
197 spin_unlock_irq(&priv->lock);
198 return -EAGAIN;
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
Jack Morgensteine1d50dc2008-05-20 15:41:09 -0700201 spin_unlock_irq(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
Eli Cohend0de1362008-07-14 23:48:50 -0700203 set_qkey = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205
206 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
207 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
208 ipoib_warn(priv, "multicast group " IPOIB_GID_FMT
209 " already attached\n",
210 IPOIB_GID_ARG(mcast->mcmember.mgid));
211
212 return 0;
213 }
214
215 ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
Eli Cohend0de1362008-07-14 23:48:50 -0700216 &mcast->mcmember.mgid, set_qkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (ret < 0) {
218 ipoib_warn(priv, "couldn't attach QP to multicast group "
219 IPOIB_GID_FMT "\n",
220 IPOIB_GID_ARG(mcast->mcmember.mgid));
221
222 clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
223 return ret;
224 }
225 }
226
227 {
228 struct ib_ah_attr av = {
229 .dlid = be16_to_cpu(mcast->mcmember.mlid),
230 .port_num = priv->port,
231 .sl = mcast->mcmember.sl,
232 .ah_flags = IB_AH_GRH,
Jack Morgensteinbf6a9e32006-04-10 09:43:47 -0700233 .static_rate = mcast->mcmember.rate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 .grh = {
235 .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
236 .hop_limit = mcast->mcmember.hop_limit,
237 .sgid_index = 0,
238 .traffic_class = mcast->mcmember.traffic_class
239 }
240 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 av.grh.dgid = mcast->mcmember.mgid;
242
Eli Cohen7343b232006-02-27 20:47:43 -0800243 ah = ipoib_create_ah(dev, priv->pd, &av);
244 if (!ah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 ipoib_warn(priv, "ib_address_create failed\n");
246 } else {
Or Gerlitz624d01f2006-07-24 10:42:00 +0300247 spin_lock_irq(&priv->lock);
248 mcast->ah = ah;
249 spin_unlock_irq(&priv->lock);
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 ipoib_dbg_mcast(priv, "MGID " IPOIB_GID_FMT
252 " AV %p, LID 0x%04x, SL %d\n",
253 IPOIB_GID_ARG(mcast->mcmember.mgid),
254 mcast->ah->ah,
255 be16_to_cpu(mcast->mcmember.mlid),
256 mcast->mcmember.sl);
257 }
258 }
259
260 /* actually send any queued packets */
Roland Dreier943c2462008-09-30 10:36:21 -0700261 netif_tx_lock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 while (!skb_queue_empty(&mcast->pkt_queue)) {
263 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
Roland Dreier943c2462008-09-30 10:36:21 -0700264 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 skb->dev = dev;
267
268 if (!skb->dst || !skb->dst->neighbour) {
269 /* put pseudoheader back on for next time */
270 skb_push(skb, sizeof (struct ipoib_pseudoheader));
271 }
272
273 if (dev_queue_xmit(skb))
274 ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
Roland Dreier943c2462008-09-30 10:36:21 -0700275 netif_tx_lock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
Roland Dreier943c2462008-09-30 10:36:21 -0700277 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 return 0;
280}
281
Sean Heftyfaec2f72007-02-15 17:00:17 -0800282static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283ipoib_mcast_sendonly_join_complete(int status,
Sean Heftyfaec2f72007-02-15 17:00:17 -0800284 struct ib_sa_multicast *multicast)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Sean Heftyfaec2f72007-02-15 17:00:17 -0800286 struct ipoib_mcast *mcast = multicast->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 struct net_device *dev = mcast->dev;
288
Sean Heftyfaec2f72007-02-15 17:00:17 -0800289 /* We trap for port events ourselves. */
290 if (status == -ENETRESET)
291 return 0;
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (!status)
Sean Heftyfaec2f72007-02-15 17:00:17 -0800294 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
295
296 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (mcast->logcount++ < 20)
298 ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for "
299 IPOIB_GID_FMT ", status %d\n",
300 IPOIB_GID_ARG(mcast->mcmember.mgid), status);
301
302 /* Flush out any queued packets */
Roland Dreier943c2462008-09-30 10:36:21 -0700303 netif_tx_lock_bh(dev);
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -0800304 while (!skb_queue_empty(&mcast->pkt_queue)) {
Roland Dreierde903512007-09-28 15:33:51 -0700305 ++dev->stats.tx_dropped;
Roland Dreier8c608a32005-11-07 10:49:38 -0800306 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -0800307 }
Roland Dreier943c2462008-09-30 10:36:21 -0700308 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 /* Clear the busy flag so we try again */
Sean Heftyfaec2f72007-02-15 17:00:17 -0800311 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY,
312 &mcast->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
Sean Heftyfaec2f72007-02-15 17:00:17 -0800314 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
317static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
318{
319 struct net_device *dev = mcast->dev;
320 struct ipoib_dev_priv *priv = netdev_priv(dev);
321 struct ib_sa_mcmember_rec rec = {
322#if 0 /* Some SMs don't support send-only yet */
323 .join_state = 4
324#else
325 .join_state = 1
326#endif
327 };
328 int ret = 0;
329
330 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
331 ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
332 return -ENODEV;
333 }
334
335 if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
336 ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
337 return -EBUSY;
338 }
339
340 rec.mgid = mcast->mcmember.mgid;
341 rec.port_gid = priv->local_gid;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700342 rec.pkey = cpu_to_be16(priv->pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Sean Heftyfaec2f72007-02-15 17:00:17 -0800344 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca,
345 priv->port, &rec,
346 IB_SA_MCMEMBER_REC_MGID |
347 IB_SA_MCMEMBER_REC_PORT_GID |
348 IB_SA_MCMEMBER_REC_PKEY |
349 IB_SA_MCMEMBER_REC_JOIN_STATE,
350 GFP_ATOMIC,
351 ipoib_mcast_sendonly_join_complete,
352 mcast);
353 if (IS_ERR(mcast->mc)) {
354 ret = PTR_ERR(mcast->mc);
355 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
356 ipoib_warn(priv, "ib_sa_join_multicast failed (ret = %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 ret);
358 } else {
359 ipoib_dbg_mcast(priv, "no multicast record for " IPOIB_GID_FMT
360 ", starting join\n",
361 IPOIB_GID_ARG(mcast->mcmember.mgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363
364 return ret;
365}
366
Yossi Etigine8224e42008-09-16 11:57:45 -0700367void ipoib_mcast_carrier_on_task(struct work_struct *work)
368{
369 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
370 carrier_on_task);
371
372 /*
373 * Take rtnl_lock to avoid racing with ipoib_stop() and
374 * turning the carrier back on while a device is being
375 * removed.
376 */
377 rtnl_lock();
378 netif_carrier_on(priv->dev);
379 rtnl_unlock();
380}
381
Sean Heftyfaec2f72007-02-15 17:00:17 -0800382static int ipoib_mcast_join_complete(int status,
383 struct ib_sa_multicast *multicast)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Sean Heftyfaec2f72007-02-15 17:00:17 -0800385 struct ipoib_mcast *mcast = multicast->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 struct net_device *dev = mcast->dev;
387 struct ipoib_dev_priv *priv = netdev_priv(dev);
388
389 ipoib_dbg_mcast(priv, "join completion for " IPOIB_GID_FMT
390 " (status %d)\n",
391 IPOIB_GID_ARG(mcast->mcmember.mgid), status);
392
Sean Heftyfaec2f72007-02-15 17:00:17 -0800393 /* We trap for port events ourselves. */
394 if (status == -ENETRESET)
395 return 0;
396
397 if (!status)
398 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
399
400 if (!status) {
Hal Rosenstockce5b65c2005-09-18 13:47:53 -0700401 mcast->backoff = 1;
Ingo Molnar95ed6442006-01-13 14:51:39 -0800402 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
David Howellsc4028952006-11-22 14:57:56 +0000404 queue_delayed_work(ipoib_workqueue,
405 &priv->mcast_task, 0);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800406 mutex_unlock(&mcast_mutex);
Shirley Ma55c9add2007-03-08 14:59:30 -0800407
Yossi Etigine8224e42008-09-16 11:57:45 -0700408 /*
409 * Defer carrier on work to ipoib_workqueue to avoid a
410 * deadlock on rtnl_lock here.
411 */
412 if (mcast == priv->broadcast)
413 queue_work(ipoib_workqueue, &priv->carrier_on_task);
Shirley Ma55c9add2007-03-08 14:59:30 -0800414
Sean Heftyfaec2f72007-02-15 17:00:17 -0800415 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417
Sean Heftyfaec2f72007-02-15 17:00:17 -0800418 if (mcast->logcount++ < 20) {
419 if (status == -ETIMEDOUT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 ipoib_dbg_mcast(priv, "multicast join failed for " IPOIB_GID_FMT
421 ", status %d\n",
422 IPOIB_GID_ARG(mcast->mcmember.mgid),
423 status);
424 } else {
425 ipoib_warn(priv, "multicast join failed for "
426 IPOIB_GID_FMT ", status %d\n",
427 IPOIB_GID_ARG(mcast->mcmember.mgid),
428 status);
429 }
430 }
431
432 mcast->backoff *= 2;
433 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
434 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
435
Sean Heftyfaec2f72007-02-15 17:00:17 -0800436 /* Clear the busy flag so we try again */
437 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
438
Michael S. Tsirkin9acf6a82006-03-02 11:07:47 -0800439 mutex_lock(&mcast_mutex);
Michael S. Tsirkin9acf6a82006-03-02 11:07:47 -0800440 spin_lock_irq(&priv->lock);
Sean Heftyfaec2f72007-02-15 17:00:17 -0800441 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
442 queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
443 mcast->backoff * HZ);
Michael S. Tsirkin9acf6a82006-03-02 11:07:47 -0800444 spin_unlock_irq(&priv->lock);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800445 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Sean Heftyfaec2f72007-02-15 17:00:17 -0800447 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
450static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
451 int create)
452{
453 struct ipoib_dev_priv *priv = netdev_priv(dev);
454 struct ib_sa_mcmember_rec rec = {
455 .join_state = 1
456 };
457 ib_sa_comp_mask comp_mask;
458 int ret = 0;
459
460 ipoib_dbg_mcast(priv, "joining MGID " IPOIB_GID_FMT "\n",
461 IPOIB_GID_ARG(mcast->mcmember.mgid));
462
463 rec.mgid = mcast->mcmember.mgid;
464 rec.port_gid = priv->local_gid;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700465 rec.pkey = cpu_to_be16(priv->pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 comp_mask =
468 IB_SA_MCMEMBER_REC_MGID |
469 IB_SA_MCMEMBER_REC_PORT_GID |
470 IB_SA_MCMEMBER_REC_PKEY |
471 IB_SA_MCMEMBER_REC_JOIN_STATE;
472
473 if (create) {
474 comp_mask |=
Roland Dreierd0df6d62006-09-22 15:22:56 -0700475 IB_SA_MCMEMBER_REC_QKEY |
476 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
477 IB_SA_MCMEMBER_REC_MTU |
478 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS |
479 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
480 IB_SA_MCMEMBER_REC_RATE |
481 IB_SA_MCMEMBER_REC_SL |
482 IB_SA_MCMEMBER_REC_FLOW_LABEL |
483 IB_SA_MCMEMBER_REC_HOP_LIMIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 rec.qkey = priv->broadcast->mcmember.qkey;
Roland Dreierd0df6d62006-09-22 15:22:56 -0700486 rec.mtu_selector = IB_SA_EQ;
487 rec.mtu = priv->broadcast->mcmember.mtu;
488 rec.traffic_class = priv->broadcast->mcmember.traffic_class;
489 rec.rate_selector = IB_SA_EQ;
490 rec.rate = priv->broadcast->mcmember.rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 rec.sl = priv->broadcast->mcmember.sl;
492 rec.flow_label = priv->broadcast->mcmember.flow_label;
Roland Dreierd0df6d62006-09-22 15:22:56 -0700493 rec.hop_limit = priv->broadcast->mcmember.hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495
Sean Heftyfaec2f72007-02-15 17:00:17 -0800496 set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
497 mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
498 &rec, comp_mask, GFP_KERNEL,
499 ipoib_mcast_join_complete, mcast);
500 if (IS_ERR(mcast->mc)) {
501 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
502 ret = PTR_ERR(mcast->mc);
503 ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 mcast->backoff *= 2;
506 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
507 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
508
Ingo Molnar95ed6442006-01-13 14:51:39 -0800509 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
511 queue_delayed_work(ipoib_workqueue,
512 &priv->mcast_task,
Hal Rosenstockce5b65c2005-09-18 13:47:53 -0700513 mcast->backoff * HZ);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800514 mutex_unlock(&mcast_mutex);
Sean Heftyfaec2f72007-02-15 17:00:17 -0800515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
David Howellsc4028952006-11-22 14:57:56 +0000518void ipoib_mcast_join_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
David Howellsc4028952006-11-22 14:57:56 +0000520 struct ipoib_dev_priv *priv =
521 container_of(work, struct ipoib_dev_priv, mcast_task.work);
522 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
525 return;
526
527 if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
Michael S. Tsirkin24bd1e42007-05-18 16:12:54 +0300528 ipoib_warn(priv, "ib_query_gid() failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 else
530 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
531
532 {
533 struct ib_port_attr attr;
534
Roland Dreier658bcef2007-02-21 20:28:05 -0800535 if (!ib_query_port(priv->ca, priv->port, &attr))
536 priv->local_lid = attr.lid;
537 else
Sean Heftyfaec2f72007-02-15 17:00:17 -0800538 ipoib_warn(priv, "ib_query_port failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540
541 if (!priv->broadcast) {
Roland Dreier20b83382006-02-11 12:22:12 -0800542 struct ipoib_mcast *broadcast;
543
544 broadcast = ipoib_mcast_alloc(dev, 1);
545 if (!broadcast) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 ipoib_warn(priv, "failed to allocate broadcast group\n");
Ingo Molnar95ed6442006-01-13 14:51:39 -0800547 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
549 queue_delayed_work(ipoib_workqueue,
550 &priv->mcast_task, HZ);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800551 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return;
553 }
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 spin_lock_irq(&priv->lock);
Roland Dreier20b83382006-02-11 12:22:12 -0800556 memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
557 sizeof (union ib_gid));
558 priv->broadcast = broadcast;
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 __ipoib_mcast_add(dev, priv->broadcast);
561 spin_unlock_irq(&priv->lock);
562 }
563
564 if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
Sean Heftyfaec2f72007-02-15 17:00:17 -0800565 if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
566 ipoib_mcast_join(dev, priv->broadcast, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return;
568 }
569
570 while (1) {
571 struct ipoib_mcast *mcast = NULL;
572
573 spin_lock_irq(&priv->lock);
574 list_for_each_entry(mcast, &priv->multicast_list, list) {
575 if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
576 && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
577 && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
578 /* Found the next unjoined group */
579 break;
580 }
581 }
582 spin_unlock_irq(&priv->lock);
583
584 if (&mcast->list == &priv->multicast_list) {
585 /* All done */
586 break;
587 }
588
589 ipoib_mcast_join(dev, mcast, 1);
590 return;
591 }
592
Shirley Mabc7b3a32008-04-23 11:55:45 -0700593 priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200594
Eli Cohenc8c2afe2008-07-14 23:48:51 -0700595 if (!ipoib_cm_admin_enabled(dev)) {
596 rtnl_lock();
Eli Cohenbd360672008-07-14 23:48:51 -0700597 dev_set_mtu(dev, min(priv->mcast_mtu, priv->admin_mtu));
Eli Cohenc8c2afe2008-07-14 23:48:51 -0700598 rtnl_unlock();
599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
602
603 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
606int ipoib_mcast_start_thread(struct net_device *dev)
607{
608 struct ipoib_dev_priv *priv = netdev_priv(dev);
609
610 ipoib_dbg_mcast(priv, "starting multicast thread\n");
611
Ingo Molnar95ed6442006-01-13 14:51:39 -0800612 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
David Howellsc4028952006-11-22 14:57:56 +0000614 queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800615 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 return 0;
618}
619
Roland Dreier8d2cae02005-09-20 10:52:04 -0700620int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 struct ipoib_dev_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 ipoib_dbg_mcast(priv, "stopping multicast thread\n");
625
Ingo Molnar95ed6442006-01-13 14:51:39 -0800626 mutex_lock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 clear_bit(IPOIB_MCAST_RUN, &priv->flags);
628 cancel_delayed_work(&priv->mcast_task);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800629 mutex_unlock(&mcast_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Roland Dreier8d2cae02005-09-20 10:52:04 -0700631 if (flush)
632 flush_workqueue(ipoib_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return 0;
635}
636
637static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
638{
639 struct ipoib_dev_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 int ret = 0;
641
Sean Heftye07832b2007-03-19 14:31:36 -0800642 if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
643 ib_sa_free_multicast(mcast->mc);
644
Sean Heftyfaec2f72007-02-15 17:00:17 -0800645 if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
646 ipoib_dbg_mcast(priv, "leaving MGID " IPOIB_GID_FMT "\n",
647 IPOIB_GID_ARG(mcast->mcmember.mgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Sean Heftyfaec2f72007-02-15 17:00:17 -0800649 /* Remove ourselves from the multicast group */
Roland Dreier9eae5542008-07-14 23:48:50 -0700650 ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
651 be16_to_cpu(mcast->mcmember.mlid));
Sean Heftyfaec2f72007-02-15 17:00:17 -0800652 if (ret)
Roland Dreier9eae5542008-07-14 23:48:50 -0700653 ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
Sean Heftyfaec2f72007-02-15 17:00:17 -0800654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return 0;
657}
658
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300659void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 struct ipoib_dev_priv *priv = netdev_priv(dev);
662 struct ipoib_mcast *mcast;
Roland Dreier943c2462008-09-30 10:36:21 -0700663 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Roland Dreier943c2462008-09-30 10:36:21 -0700665 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Or Gerlitzb3e27492008-03-11 16:10:02 +0200667 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags) ||
Roland Dreier20b83382006-02-11 12:22:12 -0800668 !priv->broadcast ||
669 !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
Roland Dreierde903512007-09-28 15:33:51 -0700670 ++dev->stats.tx_dropped;
Michael S. Tsirkin479a0792006-02-07 16:37:08 -0800671 dev_kfree_skb_any(skb);
672 goto unlock;
673 }
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 mcast = __ipoib_mcast_find(dev, mgid);
676 if (!mcast) {
677 /* Let's create a new send only group now */
678 ipoib_dbg_mcast(priv, "setting up send only multicast group for "
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300679 IPOIB_GID_FMT "\n", IPOIB_GID_RAW_ARG(mgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 mcast = ipoib_mcast_alloc(dev, 0);
682 if (!mcast) {
683 ipoib_warn(priv, "unable to allocate memory for "
684 "multicast structure\n");
Roland Dreierde903512007-09-28 15:33:51 -0700685 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 dev_kfree_skb_any(skb);
687 goto out;
688 }
689
690 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300691 memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 __ipoib_mcast_add(dev, mcast);
693 list_add_tail(&mcast->list, &priv->multicast_list);
694 }
695
696 if (!mcast->ah) {
697 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
698 skb_queue_tail(&mcast->pkt_queue, skb);
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -0800699 else {
Roland Dreierde903512007-09-28 15:33:51 -0700700 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 dev_kfree_skb_any(skb);
Michael S. Tsirkinb36f170b62006-01-17 12:19:40 -0800702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Sean Heftyfaec2f72007-02-15 17:00:17 -0800704 if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 ipoib_dbg_mcast(priv, "no address vector, "
706 "but multicast join already started\n");
707 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
708 ipoib_mcast_sendonly_join(mcast);
709
710 /*
711 * If lookup completes between here and out:, don't
712 * want to send packet twice.
713 */
714 mcast = NULL;
715 }
716
717out:
718 if (mcast && mcast->ah) {
Roland Dreier2337f802007-10-23 19:57:54 -0700719 if (skb->dst &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 skb->dst->neighbour &&
721 !*to_ipoib_neigh(skb->dst->neighbour)) {
Moni Shoua732a2172007-10-09 19:43:36 -0700722 struct ipoib_neigh *neigh = ipoib_neigh_alloc(skb->dst->neighbour,
723 skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 if (neigh) {
726 kref_get(&mcast->ah->ref);
Roland Dreier2337f802007-10-23 19:57:54 -0700727 neigh->ah = mcast->ah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 list_add_tail(&neigh->list, &mcast->neigh_list);
729 }
730 }
731
732 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
733 }
734
Michael S. Tsirkin479a0792006-02-07 16:37:08 -0800735unlock:
Roland Dreier943c2462008-09-30 10:36:21 -0700736 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
739void ipoib_mcast_dev_flush(struct net_device *dev)
740{
741 struct ipoib_dev_priv *priv = netdev_priv(dev);
742 LIST_HEAD(remove_list);
Eli Cohen988bd502006-01-12 14:32:20 -0800743 struct ipoib_mcast *mcast, *tmcast;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 unsigned long flags;
745
746 ipoib_dbg_mcast(priv, "flushing multicast list\n");
747
748 spin_lock_irqsave(&priv->lock, flags);
Eli Cohen988bd502006-01-12 14:32:20 -0800749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
Eli Cohen988bd502006-01-12 14:32:20 -0800751 list_del(&mcast->list);
752 rb_erase(&mcast->rb_node, &priv->multicast_tree);
753 list_add_tail(&mcast->list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755
756 if (priv->broadcast) {
Roland Dreier3cd96562006-09-22 15:22:46 -0700757 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
Eli Cohen988bd502006-01-12 14:32:20 -0800758 list_add_tail(&priv->broadcast->list, &remove_list);
759 priv->broadcast = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
761
762 spin_unlock_irqrestore(&priv->lock, flags);
763
764 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
765 ipoib_mcast_leave(dev, mcast);
766 ipoib_mcast_free(mcast);
767 }
768}
769
David Howellsc4028952006-11-22 14:57:56 +0000770void ipoib_mcast_restart_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
David Howellsc4028952006-11-22 14:57:56 +0000772 struct ipoib_dev_priv *priv =
773 container_of(work, struct ipoib_dev_priv, restart_task);
774 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 struct dev_mc_list *mclist;
776 struct ipoib_mcast *mcast, *tmcast;
777 LIST_HEAD(remove_list);
778 unsigned long flags;
Or Gerlitz335a64a5a2007-10-08 10:13:00 +0200779 struct ib_sa_mcmember_rec rec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 ipoib_dbg_mcast(priv, "restarting multicast task\n");
782
Roland Dreier8d2cae02005-09-20 10:52:04 -0700783 ipoib_mcast_stop_thread(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Herbert Xu932ff272006-06-09 12:20:56 -0700785 local_irq_save(flags);
David S. Millere308a5d2008-07-15 00:13:44 -0700786 netif_addr_lock(dev);
Michael S. Tsirkin78bfe0b2006-01-11 11:47:34 -0800787 spin_lock(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 /*
790 * Unfortunately, the networking core only gives us a list of all of
791 * the multicast hardware addresses. We need to figure out which ones
792 * are new and which ones have been removed
793 */
794
795 /* Clear out the found flag */
796 list_for_each_entry(mcast, &priv->multicast_list, list)
797 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
798
799 /* Mark all of the entries that are found or don't exist */
800 for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
801 union ib_gid mgid;
802
803 memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 mcast = __ipoib_mcast_find(dev, &mgid);
806 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
807 struct ipoib_mcast *nmcast;
808
Or Gerlitz335a64a5a2007-10-08 10:13:00 +0200809 /* ignore group which is directly joined by userspace */
810 if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
811 !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
812 ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid "
813 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
814 continue;
815 }
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 /* Not found or send-only group, let's add a new entry */
818 ipoib_dbg_mcast(priv, "adding multicast entry for mgid "
819 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
820
821 nmcast = ipoib_mcast_alloc(dev, 0);
822 if (!nmcast) {
823 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
824 continue;
825 }
826
827 set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
828
829 nmcast->mcmember.mgid = mgid;
830
831 if (mcast) {
832 /* Destroy the send only entry */
Akinobu Mita179e0912006-06-26 00:24:41 -0700833 list_move_tail(&mcast->list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 rb_replace_node(&mcast->rb_node,
836 &nmcast->rb_node,
837 &priv->multicast_tree);
838 } else
839 __ipoib_mcast_add(dev, nmcast);
840
841 list_add_tail(&nmcast->list, &priv->multicast_list);
842 }
843
844 if (mcast)
845 set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
846 }
847
848 /* Remove all of the entries don't exist anymore */
849 list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
850 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
851 !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
852 ipoib_dbg_mcast(priv, "deleting multicast group " IPOIB_GID_FMT "\n",
853 IPOIB_GID_ARG(mcast->mcmember.mgid));
854
855 rb_erase(&mcast->rb_node, &priv->multicast_tree);
856
857 /* Move to the remove list */
Akinobu Mita179e0912006-06-26 00:24:41 -0700858 list_move_tail(&mcast->list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860 }
Michael S. Tsirkin78bfe0b2006-01-11 11:47:34 -0800861
862 spin_unlock(&priv->lock);
David S. Millere308a5d2008-07-15 00:13:44 -0700863 netif_addr_unlock(dev);
Herbert Xu932ff272006-06-09 12:20:56 -0700864 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 /* We have to cancel outside of the spinlock */
867 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
868 ipoib_mcast_leave(mcast->dev, mcast);
869 ipoib_mcast_free(mcast);
870 }
871
872 if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
873 ipoib_mcast_start_thread(dev);
874}
875
Roland Dreier8ae5a8a2005-11-02 20:51:01 -0800876#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
879{
880 struct ipoib_mcast_iter *iter;
881
882 iter = kmalloc(sizeof *iter, GFP_KERNEL);
883 if (!iter)
884 return NULL;
885
886 iter->dev = dev;
Roland Dreier1732b0e2005-11-07 10:33:11 -0800887 memset(iter->mgid.raw, 0, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 if (ipoib_mcast_iter_next(iter)) {
Roland Dreier1732b0e2005-11-07 10:33:11 -0800890 kfree(iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return NULL;
892 }
893
894 return iter;
895}
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
898{
899 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
900 struct rb_node *n;
901 struct ipoib_mcast *mcast;
902 int ret = 1;
903
904 spin_lock_irq(&priv->lock);
905
906 n = rb_first(&priv->multicast_tree);
907
908 while (n) {
909 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
910
911 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
912 sizeof (union ib_gid)) < 0) {
913 iter->mgid = mcast->mcmember.mgid;
914 iter->created = mcast->created;
915 iter->queuelen = skb_queue_len(&mcast->pkt_queue);
916 iter->complete = !!mcast->ah;
917 iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
918
919 ret = 0;
920
921 break;
922 }
923
924 n = rb_next(n);
925 }
926
927 spin_unlock_irq(&priv->lock);
928
929 return ret;
930}
931
932void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
933 union ib_gid *mgid,
934 unsigned long *created,
935 unsigned int *queuelen,
936 unsigned int *complete,
937 unsigned int *send_only)
938{
939 *mgid = iter->mgid;
940 *created = iter->created;
941 *queuelen = iter->queuelen;
942 *complete = iter->complete;
943 *send_only = iter->send_only;
944}
Roland Dreier8ae5a8a2005-11-02 20:51:01 -0800945
946#endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */