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