1 /*
2  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
3  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4  * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
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.
33  */
34 
35 #include <linux/skbuff.h>
36 #include <linux/rtnetlink.h>
37 #include <linux/moduleparam.h>
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>
44 #include <linux/slab.h>
45 
46 #include <net/dst.h>
47 
48 #include "ipoib.h"
49 
50 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
51 static int mcast_debug_level;
52 
53 module_param(mcast_debug_level, int, 0644);
54 MODULE_PARM_DESC(mcast_debug_level,
55 		 "Enable multicast debug tracing if > 0");
56 #endif
57 
58 static DEFINE_MUTEX(mcast_mutex);
59 
60 struct 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 
69 static void ipoib_mcast_free(struct ipoib_mcast *mcast)
70 {
71 	struct net_device *dev = mcast->dev;
72 	int tx_dropped = 0;
73 
74 	ipoib_dbg_mcast(netdev_priv(dev), "deleting multicast group %pI6\n",
75 			mcast->mcmember.mgid.raw);
76 
77 	/* remove all neigh connected to this mcast */
78 	ipoib_del_neighs_by_gid(dev, mcast->mcmember.mgid.raw);
79 
80 	if (mcast->ah)
81 		ipoib_put_ah(mcast->ah);
82 
83 	while (!skb_queue_empty(&mcast->pkt_queue)) {
84 		++tx_dropped;
85 		dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
86 	}
87 
88 	netif_tx_lock_bh(dev);
89 	dev->stats.tx_dropped += tx_dropped;
90 	netif_tx_unlock_bh(dev);
91 
92 	kfree(mcast);
93 }
94 
95 static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
96 					     int can_sleep)
97 {
98 	struct ipoib_mcast *mcast;
99 
100 	mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
101 	if (!mcast)
102 		return NULL;
103 
104 	mcast->dev = dev;
105 	mcast->created = jiffies;
106 	mcast->backoff = 1;
107 
108 	INIT_LIST_HEAD(&mcast->list);
109 	INIT_LIST_HEAD(&mcast->neigh_list);
110 	skb_queue_head_init(&mcast->pkt_queue);
111 
112 	return mcast;
113 }
114 
115 static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
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 
126 		ret = memcmp(mgid, mcast->mcmember.mgid.raw,
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 
139 static 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 
167 static 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);
172 	struct ipoib_ah *ah;
173 	int ret;
174 	int set_qkey = 0;
175 
176 	mcast->mcmember = *mcmember;
177 
178 	/* Set the multicast MTU and cached Q_Key before we attach if it's
179 	 * the broadcast group.
180 	 */
181 	if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
182 		    sizeof (union ib_gid))) {
183 		spin_lock_irq(&priv->lock);
184 		if (!priv->broadcast) {
185 			spin_unlock_irq(&priv->lock);
186 			return -EAGAIN;
187 		}
188 		priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
189 		priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
190 		spin_unlock_irq(&priv->lock);
191 		priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
192 		set_qkey = 1;
193 	}
194 
195 	if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
196 		if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
197 			ipoib_warn(priv, "multicast group %pI6 already attached\n",
198 				   mcast->mcmember.mgid.raw);
199 
200 			return 0;
201 		}
202 
203 		ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
204 					 &mcast->mcmember.mgid, set_qkey);
205 		if (ret < 0) {
206 			ipoib_warn(priv, "couldn't attach QP to multicast group %pI6\n",
207 				   mcast->mcmember.mgid.raw);
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,
220 			.static_rate   = mcast->mcmember.rate,
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 		};
228 		av.grh.dgid = mcast->mcmember.mgid;
229 
230 		ah = ipoib_create_ah(dev, priv->pd, &av);
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);
236 		} else {
237 			spin_lock_irq(&priv->lock);
238 			mcast->ah = ah;
239 			spin_unlock_irq(&priv->lock);
240 
241 			ipoib_dbg_mcast(priv, "MGID %pI6 AV %p, LID 0x%04x, SL %d\n",
242 					mcast->mcmember.mgid.raw,
243 					mcast->ah->ah,
244 					be16_to_cpu(mcast->mcmember.mlid),
245 					mcast->mcmember.sl);
246 		}
247 	}
248 
249 	/* actually send any queued packets */
250 	netif_tx_lock_bh(dev);
251 	while (!skb_queue_empty(&mcast->pkt_queue)) {
252 		struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
253 
254 		netif_tx_unlock_bh(dev);
255 
256 		skb->dev = dev;
257 		if (dev_queue_xmit(skb))
258 			ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
259 
260 		netif_tx_lock_bh(dev);
261 	}
262 	netif_tx_unlock_bh(dev);
263 
264 	return 0;
265 }
266 
267 static int
268 ipoib_mcast_sendonly_join_complete(int status,
269 				   struct ib_sa_multicast *multicast)
270 {
271 	struct ipoib_mcast *mcast = multicast->context;
272 	struct net_device *dev = mcast->dev;
273 
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 
284 	/* We trap for port events ourselves. */
285 	if (status == -ENETRESET)
286 		goto out;
287 
288 	if (!status)
289 		status = ipoib_mcast_join_finish(mcast, &multicast->rec);
290 
291 	if (status) {
292 		if (mcast->logcount++ < 20)
293 			ipoib_dbg_mcast(netdev_priv(dev), "sendonly multicast "
294 					"join failed for %pI6, status %d\n",
295 					mcast->mcmember.mgid.raw, status);
296 
297 		/* Flush out any queued packets */
298 		netif_tx_lock_bh(dev);
299 		while (!skb_queue_empty(&mcast->pkt_queue)) {
300 			++dev->stats.tx_dropped;
301 			dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
302 		}
303 		netif_tx_unlock_bh(dev);
304 	}
305 out:
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);
313 	return status;
314 }
315 
316 static 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)) {
330 		ipoib_dbg_mcast(priv, "device shutting down, no sendonly "
331 				"multicast joins\n");
332 		return -ENODEV;
333 	}
334 
335 	if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
336 		ipoib_dbg_mcast(priv, "multicast entry busy, skipping "
337 				"sendonly join\n");
338 		return -EBUSY;
339 	}
340 
341 	rec.mgid     = mcast->mcmember.mgid;
342 	rec.port_gid = priv->local_gid;
343 	rec.pkey     = cpu_to_be16(priv->pkey);
344 
345 	mutex_lock(&mcast_mutex);
346 	init_completion(&mcast->done);
347 	set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
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);
360 		complete(&mcast->done);
361 		ipoib_warn(priv, "ib_sa_join_multicast for sendonly join "
362 			   "failed (ret = %d)\n", ret);
363 	} else {
364 		ipoib_dbg_mcast(priv, "no multicast record for %pI6, starting "
365 				"sendonly join\n", mcast->mcmember.mgid.raw);
366 	}
367 	mutex_unlock(&mcast_mutex);
368 
369 	return ret;
370 }
371 
372 void 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);
376 	struct ib_port_attr attr;
377 
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 
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
391 	 * and can safely ignore the carrier on work.
392 	 */
393 	while (!rtnl_trylock()) {
394 		if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
395 			return;
396 		else
397 			msleep(20);
398 	}
399 	if (!ipoib_cm_admin_enabled(priv->dev))
400 		dev_set_mtu(priv->dev, min(priv->mcast_mtu, priv->admin_mtu));
401 	netif_carrier_on(priv->dev);
402 	rtnl_unlock();
403 }
404 
405 static int ipoib_mcast_join_complete(int status,
406 				     struct ib_sa_multicast *multicast)
407 {
408 	struct ipoib_mcast *mcast = multicast->context;
409 	struct net_device *dev = mcast->dev;
410 	struct ipoib_dev_priv *priv = netdev_priv(dev);
411 
412 	ipoib_dbg_mcast(priv, "join completion for %pI6 (status %d)\n",
413 			mcast->mcmember.mgid.raw, status);
414 
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 
425 	/* We trap for port events ourselves. */
426 	if (status == -ENETRESET)
427 		goto out;
428 
429 	if (!status)
430 		status = ipoib_mcast_join_finish(mcast, &multicast->rec);
431 
432 	if (!status) {
433 		mcast->backoff = 1;
434 		if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
435 			queue_delayed_work(priv->wq, &priv->mcast_task, 0);
436 
437 		/*
438 		 * Defer carrier on work to priv->wq to avoid a
439 		 * deadlock on rtnl_lock here.
440 		 */
441 		if (mcast == priv->broadcast)
442 			queue_work(priv->wq, &priv->carrier_on_task);
443 	} else {
444 		if (mcast->logcount++ < 20) {
445 			if (status == -ETIMEDOUT || status == -EAGAIN) {
446 				ipoib_dbg_mcast(priv, "multicast join failed for %pI6, status %d\n",
447 						mcast->mcmember.mgid.raw, status);
448 			} else {
449 				ipoib_warn(priv, "multicast join failed for %pI6, status %d\n",
450 					   mcast->mcmember.mgid.raw, status);
451 			}
452 		}
453 
454 		mcast->backoff *= 2;
455 		if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
456 			mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
457 	}
458 out:
459 	spin_lock_irq(&priv->lock);
460 	clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
461 	if (status)
462 		mcast->mc = NULL;
463 	complete(&mcast->done);
464 	if (status == -ENETRESET)
465 		status = 0;
466 	if (status && test_bit(IPOIB_MCAST_RUN, &priv->flags))
467 		queue_delayed_work(priv->wq, &priv->mcast_task,
468 				   mcast->backoff * HZ);
469 	spin_unlock_irq(&priv->lock);
470 	mutex_unlock(&mcast_mutex);
471 
472 	return status;
473 }
474 
475 static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
476 			     int create)
477 {
478 	struct ipoib_dev_priv *priv = netdev_priv(dev);
479 	struct ib_sa_mcmember_rec rec = {
480 		.join_state = 1
481 	};
482 	ib_sa_comp_mask comp_mask;
483 	int ret = 0;
484 
485 	ipoib_dbg_mcast(priv, "joining MGID %pI6\n", mcast->mcmember.mgid.raw);
486 
487 	rec.mgid     = mcast->mcmember.mgid;
488 	rec.port_gid = priv->local_gid;
489 	rec.pkey     = cpu_to_be16(priv->pkey);
490 
491 	comp_mask =
492 		IB_SA_MCMEMBER_REC_MGID		|
493 		IB_SA_MCMEMBER_REC_PORT_GID	|
494 		IB_SA_MCMEMBER_REC_PKEY		|
495 		IB_SA_MCMEMBER_REC_JOIN_STATE;
496 
497 	if (create) {
498 		comp_mask |=
499 			IB_SA_MCMEMBER_REC_QKEY			|
500 			IB_SA_MCMEMBER_REC_MTU_SELECTOR		|
501 			IB_SA_MCMEMBER_REC_MTU			|
502 			IB_SA_MCMEMBER_REC_TRAFFIC_CLASS	|
503 			IB_SA_MCMEMBER_REC_RATE_SELECTOR	|
504 			IB_SA_MCMEMBER_REC_RATE			|
505 			IB_SA_MCMEMBER_REC_SL			|
506 			IB_SA_MCMEMBER_REC_FLOW_LABEL		|
507 			IB_SA_MCMEMBER_REC_HOP_LIMIT;
508 
509 		rec.qkey	  = priv->broadcast->mcmember.qkey;
510 		rec.mtu_selector  = IB_SA_EQ;
511 		rec.mtu		  = priv->broadcast->mcmember.mtu;
512 		rec.traffic_class = priv->broadcast->mcmember.traffic_class;
513 		rec.rate_selector = IB_SA_EQ;
514 		rec.rate	  = priv->broadcast->mcmember.rate;
515 		rec.sl		  = priv->broadcast->mcmember.sl;
516 		rec.flow_label	  = priv->broadcast->mcmember.flow_label;
517 		rec.hop_limit	  = priv->broadcast->mcmember.hop_limit;
518 	}
519 
520 	mutex_lock(&mcast_mutex);
521 	init_completion(&mcast->done);
522 	set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
523 	mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
524 					 &rec, comp_mask, GFP_KERNEL,
525 					 ipoib_mcast_join_complete, mcast);
526 	if (IS_ERR(mcast->mc)) {
527 		clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
528 		complete(&mcast->done);
529 		ret = PTR_ERR(mcast->mc);
530 		ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
531 
532 		mcast->backoff *= 2;
533 		if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
534 			mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
535 
536 		if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
537 			queue_delayed_work(priv->wq, &priv->mcast_task,
538 					   mcast->backoff * HZ);
539 	}
540 	mutex_unlock(&mcast_mutex);
541 }
542 
543 void ipoib_mcast_join_task(struct work_struct *work)
544 {
545 	struct ipoib_dev_priv *priv =
546 		container_of(work, struct ipoib_dev_priv, mcast_task.work);
547 	struct net_device *dev = priv->dev;
548 	struct ib_port_attr port_attr;
549 
550 	if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
551 		return;
552 
553 	if (ib_query_port(priv->ca, priv->port, &port_attr) ||
554 	    port_attr.state != IB_PORT_ACTIVE) {
555 		ipoib_dbg(priv, "port state is not ACTIVE (state = %d) suspending join task\n",
556 			  port_attr.state);
557 		return;
558 	}
559 	priv->local_lid = port_attr.lid;
560 
561 	if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
562 		ipoib_warn(priv, "ib_query_gid() failed\n");
563 	else
564 		memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
565 
566 	if (!priv->broadcast) {
567 		struct ipoib_mcast *broadcast;
568 
569 		if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
570 			return;
571 
572 		broadcast = ipoib_mcast_alloc(dev, 1);
573 		if (!broadcast) {
574 			ipoib_warn(priv, "failed to allocate broadcast group\n");
575 			mutex_lock(&mcast_mutex);
576 			if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
577 				queue_delayed_work(priv->wq, &priv->mcast_task,
578 						   HZ);
579 			mutex_unlock(&mcast_mutex);
580 			return;
581 		}
582 
583 		spin_lock_irq(&priv->lock);
584 		memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
585 		       sizeof (union ib_gid));
586 		priv->broadcast = broadcast;
587 
588 		__ipoib_mcast_add(dev, priv->broadcast);
589 		spin_unlock_irq(&priv->lock);
590 	}
591 
592 	if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
593 		if (IS_ERR_OR_NULL(priv->broadcast->mc) &&
594 		    !test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
595 			ipoib_mcast_join(dev, priv->broadcast, 0);
596 		return;
597 	}
598 
599 	while (1) {
600 		struct ipoib_mcast *mcast = NULL;
601 
602 		/*
603 		 * Need the mutex so our flags are consistent, need the
604 		 * priv->lock so we don't race with list removals in either
605 		 * mcast_dev_flush or mcast_restart_task
606 		 */
607 		mutex_lock(&mcast_mutex);
608 		spin_lock_irq(&priv->lock);
609 		list_for_each_entry(mcast, &priv->multicast_list, list) {
610 			if (IS_ERR_OR_NULL(mcast->mc) &&
611 			    !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags) &&
612 			    !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
613 				/* Found the next unjoined group */
614 				break;
615 			}
616 		}
617 		spin_unlock_irq(&priv->lock);
618 		mutex_unlock(&mcast_mutex);
619 
620 		if (&mcast->list == &priv->multicast_list) {
621 			/* All done */
622 			break;
623 		}
624 
625 		if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
626 			ipoib_mcast_sendonly_join(mcast);
627 		else
628 			ipoib_mcast_join(dev, mcast, 1);
629 		return;
630 	}
631 
632 	ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
633 
634 	clear_bit(IPOIB_MCAST_RUN, &priv->flags);
635 }
636 
637 int ipoib_mcast_start_thread(struct net_device *dev)
638 {
639 	struct ipoib_dev_priv *priv = netdev_priv(dev);
640 
641 	ipoib_dbg_mcast(priv, "starting multicast thread\n");
642 
643 	mutex_lock(&mcast_mutex);
644 	if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
645 		queue_delayed_work(priv->wq, &priv->mcast_task, 0);
646 	mutex_unlock(&mcast_mutex);
647 
648 	return 0;
649 }
650 
651 int ipoib_mcast_stop_thread(struct net_device *dev)
652 {
653 	struct ipoib_dev_priv *priv = netdev_priv(dev);
654 
655 	ipoib_dbg_mcast(priv, "stopping multicast thread\n");
656 
657 	mutex_lock(&mcast_mutex);
658 	clear_bit(IPOIB_MCAST_RUN, &priv->flags);
659 	cancel_delayed_work(&priv->mcast_task);
660 	mutex_unlock(&mcast_mutex);
661 
662 	flush_workqueue(priv->wq);
663 
664 	return 0;
665 }
666 
667 static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
668 {
669 	struct ipoib_dev_priv *priv = netdev_priv(dev);
670 	int ret = 0;
671 
672 	if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
673 		ipoib_warn(priv, "ipoib_mcast_leave on an in-flight join\n");
674 
675 	if (!IS_ERR_OR_NULL(mcast->mc))
676 		ib_sa_free_multicast(mcast->mc);
677 
678 	if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
679 		ipoib_dbg_mcast(priv, "leaving MGID %pI6\n",
680 				mcast->mcmember.mgid.raw);
681 
682 		/* Remove ourselves from the multicast group */
683 		ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
684 				      be16_to_cpu(mcast->mcmember.mlid));
685 		if (ret)
686 			ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
687 	}
688 
689 	return 0;
690 }
691 
692 void ipoib_mcast_send(struct net_device *dev, u8 *daddr, struct sk_buff *skb)
693 {
694 	struct ipoib_dev_priv *priv = netdev_priv(dev);
695 	struct ipoib_mcast *mcast;
696 	unsigned long flags;
697 	void *mgid = daddr + 4;
698 
699 	spin_lock_irqsave(&priv->lock, flags);
700 
701 	if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)		||
702 	    !priv->broadcast					||
703 	    !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
704 		++dev->stats.tx_dropped;
705 		dev_kfree_skb_any(skb);
706 		goto unlock;
707 	}
708 
709 	mcast = __ipoib_mcast_find(dev, mgid);
710 	if (!mcast) {
711 		/* Let's create a new send only group now */
712 		ipoib_dbg_mcast(priv, "setting up send only multicast group for %pI6\n",
713 				mgid);
714 
715 		mcast = ipoib_mcast_alloc(dev, 0);
716 		if (!mcast) {
717 			ipoib_warn(priv, "unable to allocate memory for "
718 				   "multicast structure\n");
719 			++dev->stats.tx_dropped;
720 			dev_kfree_skb_any(skb);
721 			goto out;
722 		}
723 
724 		set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
725 		memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
726 		__ipoib_mcast_add(dev, mcast);
727 		list_add_tail(&mcast->list, &priv->multicast_list);
728 		if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
729 			queue_delayed_work(priv->wq, &priv->mcast_task, 0);
730 	}
731 
732 	if (!mcast->ah) {
733 		if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
734 			skb_queue_tail(&mcast->pkt_queue, skb);
735 		else {
736 			++dev->stats.tx_dropped;
737 			dev_kfree_skb_any(skb);
738 		}
739 
740 		if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
741 			ipoib_dbg_mcast(priv, "no address vector, "
742 					"but multicast join already started\n");
743 
744 		/*
745 		 * If lookup completes between here and out:, don't
746 		 * want to send packet twice.
747 		 */
748 		mcast = NULL;
749 	}
750 
751 out:
752 	if (mcast && mcast->ah) {
753 		struct ipoib_neigh *neigh;
754 
755 		spin_unlock_irqrestore(&priv->lock, flags);
756 		neigh = ipoib_neigh_get(dev, daddr);
757 		spin_lock_irqsave(&priv->lock, flags);
758 		if (!neigh) {
759 			neigh = ipoib_neigh_alloc(daddr, dev);
760 			if (neigh) {
761 				kref_get(&mcast->ah->ref);
762 				neigh->ah	= mcast->ah;
763 				list_add_tail(&neigh->list, &mcast->neigh_list);
764 			}
765 		}
766 		spin_unlock_irqrestore(&priv->lock, flags);
767 		ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
768 		if (neigh)
769 			ipoib_neigh_put(neigh);
770 		return;
771 	}
772 
773 unlock:
774 	spin_unlock_irqrestore(&priv->lock, flags);
775 }
776 
777 void ipoib_mcast_dev_flush(struct net_device *dev)
778 {
779 	struct ipoib_dev_priv *priv = netdev_priv(dev);
780 	LIST_HEAD(remove_list);
781 	struct ipoib_mcast *mcast, *tmcast;
782 	unsigned long flags;
783 
784 	ipoib_dbg_mcast(priv, "flushing multicast list\n");
785 
786 	spin_lock_irqsave(&priv->lock, flags);
787 
788 	list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
789 		list_del(&mcast->list);
790 		rb_erase(&mcast->rb_node, &priv->multicast_tree);
791 		list_add_tail(&mcast->list, &remove_list);
792 	}
793 
794 	if (priv->broadcast) {
795 		rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
796 		list_add_tail(&priv->broadcast->list, &remove_list);
797 		priv->broadcast = NULL;
798 	}
799 
800 	spin_unlock_irqrestore(&priv->lock, flags);
801 
802 	/*
803 	 * make sure the in-flight joins have finished before we attempt
804 	 * to leave
805 	 */
806 	list_for_each_entry_safe(mcast, tmcast, &remove_list, list)
807 		if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
808 			wait_for_completion(&mcast->done);
809 
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 
816 static int ipoib_mcast_addr_is_valid(const u8 *addr, const u8 *broadcast)
817 {
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 
827 void ipoib_mcast_restart_task(struct work_struct *work)
828 {
829 	struct ipoib_dev_priv *priv =
830 		container_of(work, struct ipoib_dev_priv, restart_task);
831 	struct net_device *dev = priv->dev;
832 	struct netdev_hw_addr *ha;
833 	struct ipoib_mcast *mcast, *tmcast;
834 	LIST_HEAD(remove_list);
835 	unsigned long flags;
836 	struct ib_sa_mcmember_rec rec;
837 
838 	ipoib_dbg_mcast(priv, "restarting multicast task\n");
839 
840 	local_irq_save(flags);
841 	netif_addr_lock(dev);
842 	spin_lock(&priv->lock);
843 
844 	/*
845 	 * Unfortunately, the networking core only gives us a list of all of
846 	 * the multicast hardware addresses. We need to figure out which ones
847 	 * are new and which ones have been removed
848 	 */
849 
850 	/* Clear out the found flag */
851 	list_for_each_entry(mcast, &priv->multicast_list, list)
852 		clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
853 
854 	/* Mark all of the entries that are found or don't exist */
855 	netdev_for_each_mc_addr(ha, dev) {
856 		union ib_gid mgid;
857 
858 		if (!ipoib_mcast_addr_is_valid(ha->addr, dev->broadcast))
859 			continue;
860 
861 		memcpy(mgid.raw, ha->addr + 4, sizeof mgid);
862 
863 		mcast = __ipoib_mcast_find(dev, &mgid);
864 		if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
865 			struct ipoib_mcast *nmcast;
866 
867 			/* ignore group which is directly joined by userspace */
868 			if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
869 			    !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
870 				ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %pI6\n",
871 						mgid.raw);
872 				continue;
873 			}
874 
875 			/* Not found or send-only group, let's add a new entry */
876 			ipoib_dbg_mcast(priv, "adding multicast entry for mgid %pI6\n",
877 					mgid.raw);
878 
879 			nmcast = ipoib_mcast_alloc(dev, 0);
880 			if (!nmcast) {
881 				ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
882 				continue;
883 			}
884 
885 			set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
886 
887 			nmcast->mcmember.mgid = mgid;
888 
889 			if (mcast) {
890 				/* Destroy the send only entry */
891 				list_move_tail(&mcast->list, &remove_list);
892 
893 				rb_replace_node(&mcast->rb_node,
894 						&nmcast->rb_node,
895 						&priv->multicast_tree);
896 			} else
897 				__ipoib_mcast_add(dev, nmcast);
898 
899 			list_add_tail(&nmcast->list, &priv->multicast_list);
900 		}
901 
902 		if (mcast)
903 			set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
904 	}
905 
906 	/* Remove all of the entries don't exist anymore */
907 	list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
908 		if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
909 		    !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
910 			ipoib_dbg_mcast(priv, "deleting multicast group %pI6\n",
911 					mcast->mcmember.mgid.raw);
912 
913 			rb_erase(&mcast->rb_node, &priv->multicast_tree);
914 
915 			/* Move to the remove list */
916 			list_move_tail(&mcast->list, &remove_list);
917 		}
918 	}
919 
920 	spin_unlock(&priv->lock);
921 	netif_addr_unlock(dev);
922 	local_irq_restore(flags);
923 
924 	/*
925 	 * make sure the in-flight joins have finished before we attempt
926 	 * to leave
927 	 */
928 	list_for_each_entry_safe(mcast, tmcast, &remove_list, list)
929 		if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
930 			wait_for_completion(&mcast->done);
931 
932 	/*
933 	 * We have to cancel outside of the spinlock, but we have to
934 	 * take the rtnl lock or else we race with the removal of
935 	 * entries from the remove list in mcast_dev_flush as part
936 	 * of ipoib_stop().  We detect the drop of the ADMIN_UP flag
937 	 * to signal that we have hit this particular race, and we
938 	 * return since we know we don't need to do anything else
939 	 * anyway.
940 	 */
941 	while (!rtnl_trylock()) {
942 		if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
943 			return;
944 		else
945 			msleep(20);
946 	}
947 	list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
948 		ipoib_mcast_leave(mcast->dev, mcast);
949 		ipoib_mcast_free(mcast);
950 	}
951 	/*
952 	 * Restart our join task if needed
953 	 */
954 	ipoib_mcast_start_thread(dev);
955 	rtnl_unlock();
956 }
957 
958 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
959 
960 struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
961 {
962 	struct ipoib_mcast_iter *iter;
963 
964 	iter = kmalloc(sizeof *iter, GFP_KERNEL);
965 	if (!iter)
966 		return NULL;
967 
968 	iter->dev = dev;
969 	memset(iter->mgid.raw, 0, 16);
970 
971 	if (ipoib_mcast_iter_next(iter)) {
972 		kfree(iter);
973 		return NULL;
974 	}
975 
976 	return iter;
977 }
978 
979 int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
980 {
981 	struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
982 	struct rb_node *n;
983 	struct ipoib_mcast *mcast;
984 	int ret = 1;
985 
986 	spin_lock_irq(&priv->lock);
987 
988 	n = rb_first(&priv->multicast_tree);
989 
990 	while (n) {
991 		mcast = rb_entry(n, struct ipoib_mcast, rb_node);
992 
993 		if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
994 			   sizeof (union ib_gid)) < 0) {
995 			iter->mgid      = mcast->mcmember.mgid;
996 			iter->created   = mcast->created;
997 			iter->queuelen  = skb_queue_len(&mcast->pkt_queue);
998 			iter->complete  = !!mcast->ah;
999 			iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
1000 
1001 			ret = 0;
1002 
1003 			break;
1004 		}
1005 
1006 		n = rb_next(n);
1007 	}
1008 
1009 	spin_unlock_irq(&priv->lock);
1010 
1011 	return ret;
1012 }
1013 
1014 void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
1015 			   union ib_gid *mgid,
1016 			   unsigned long *created,
1017 			   unsigned int *queuelen,
1018 			   unsigned int *complete,
1019 			   unsigned int *send_only)
1020 {
1021 	*mgid      = iter->mgid;
1022 	*created   = iter->created;
1023 	*queuelen  = iter->queuelen;
1024 	*complete  = iter->complete;
1025 	*send_only = iter->send_only;
1026 }
1027 
1028 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
1029