xref: /freebsd/sys/ofed/drivers/infiniband/ulp/ipoib/ipoib_multicast.c (revision 389e4940069316fe667ffa263fa7d6390d0a960f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3  *
4  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
5  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
6  * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
7  *
8  * This software is available to you under a choice of one of two
9  * licenses.  You may choose to be licensed under the terms of the GNU
10  * General Public License (GPL) Version 2, available from the file
11  * COPYING in the main directory of this source tree, or the
12  * OpenIB.org BSD license below:
13  *
14  *     Redistribution and use in source and binary forms, with or
15  *     without modification, are permitted provided that the following
16  *     conditions are met:
17  *
18  *      - Redistributions of source code must retain the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer.
21  *
22  *      - Redistributions in binary form must reproduce the above
23  *        copyright notice, this list of conditions and the following
24  *        disclaimer in the documentation and/or other materials
25  *        provided with the distribution.
26  *
27  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
31  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
32  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
33  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34  * SOFTWARE.
35  */
36 
37 #include "ipoib.h"
38 
39 #include <linux/delay.h>
40 #include <linux/completion.h>
41 
42 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
43 static int mcast_debug_level = 1;
44 
45 module_param(mcast_debug_level, int, 0644);
46 MODULE_PARM_DESC(mcast_debug_level,
47 		 "Enable multicast debug tracing if > 0");
48 #endif
49 
50 static DEFINE_MUTEX(mcast_mutex);
51 
52 struct ipoib_mcast_iter {
53 	struct ipoib_dev_priv *priv;
54 	union ib_gid       mgid;
55 	unsigned long      created;
56 	unsigned int       queuelen;
57 	unsigned int       complete;
58 	unsigned int       send_only;
59 };
60 
61 static void ipoib_mcast_free(struct ipoib_mcast *mcast)
62 {
63 	struct ifnet *dev = mcast->priv->dev;
64 	int tx_dropped = 0;
65 
66 	ipoib_dbg_mcast(mcast->priv, "deleting multicast group %16D\n",
67 			mcast->mcmember.mgid.raw, ":");
68 
69 	if (mcast->ah)
70 		ipoib_put_ah(mcast->ah);
71 
72 	tx_dropped = mcast->pkt_queue.ifq_len;
73 	_IF_DRAIN(&mcast->pkt_queue);	/* XXX Locking. */
74 
75 	if_inc_counter(dev, IFCOUNTER_OERRORS, tx_dropped);
76 
77 	kfree(mcast);
78 }
79 
80 static struct ipoib_mcast *ipoib_mcast_alloc(struct ipoib_dev_priv *priv,
81 					     int can_sleep)
82 {
83 	struct ipoib_mcast *mcast;
84 
85 	mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
86 	if (!mcast)
87 		return NULL;
88 
89 	mcast->priv = priv;
90 	mcast->created = jiffies;
91 	mcast->backoff = 1;
92 
93 	INIT_LIST_HEAD(&mcast->list);
94 	bzero(&mcast->pkt_queue, sizeof(mcast->pkt_queue));
95 
96 	return mcast;
97 }
98 
99 static struct ipoib_mcast *__ipoib_mcast_find(struct ipoib_dev_priv *priv,
100     void *mgid)
101 {
102 	struct rb_node *n = priv->multicast_tree.rb_node;
103 
104 	while (n) {
105 		struct ipoib_mcast *mcast;
106 		int ret;
107 
108 		mcast = rb_entry(n, struct ipoib_mcast, rb_node);
109 
110 		ret = memcmp(mgid, mcast->mcmember.mgid.raw,
111 			     sizeof (union ib_gid));
112 		if (ret < 0)
113 			n = n->rb_left;
114 		else if (ret > 0)
115 			n = n->rb_right;
116 		else
117 			return mcast;
118 	}
119 
120 	return NULL;
121 }
122 
123 static int __ipoib_mcast_add(struct ipoib_dev_priv *priv,
124     struct ipoib_mcast *mcast)
125 {
126 	struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
127 
128 	while (*n) {
129 		struct ipoib_mcast *tmcast;
130 		int ret;
131 
132 		pn = *n;
133 		tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
134 
135 		ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
136 			     sizeof (union ib_gid));
137 		if (ret < 0)
138 			n = &pn->rb_left;
139 		else if (ret > 0)
140 			n = &pn->rb_right;
141 		else
142 			return -EEXIST;
143 	}
144 
145 	rb_link_node(&mcast->rb_node, pn, n);
146 	rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
147 
148 	return 0;
149 }
150 
151 static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
152 				   struct ib_sa_mcmember_rec *mcmember)
153 {
154 	struct ipoib_dev_priv *priv = mcast->priv;
155 	struct ifnet *dev = priv->dev;
156 	struct ipoib_ah *ah;
157 	int ret;
158 	int set_qkey = 0;
159 
160 	mcast->mcmember = *mcmember;
161 
162 	/* Set the cached Q_Key before we attach if it's the broadcast group */
163 	if (!memcmp(mcast->mcmember.mgid.raw, dev->if_broadcastaddr + 4,
164 		    sizeof (union ib_gid))) {
165 		spin_lock_irq(&priv->lock);
166 		if (!priv->broadcast) {
167 			spin_unlock_irq(&priv->lock);
168 			return -EAGAIN;
169 		}
170 		priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
171 		spin_unlock_irq(&priv->lock);
172 		priv->tx_wr.remote_qkey = priv->qkey;
173 		set_qkey = 1;
174 	}
175 
176 	if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
177 		if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
178 			ipoib_warn(priv, "multicast group %16D already attached\n",
179 				   mcast->mcmember.mgid.raw, ":");
180 
181 			return 0;
182 		}
183 
184 		ret = ipoib_mcast_attach(priv, be16_to_cpu(mcast->mcmember.mlid),
185 					 &mcast->mcmember.mgid, set_qkey);
186 		if (ret < 0) {
187 			ipoib_warn(priv, "couldn't attach QP to multicast group %16D\n",
188 				   mcast->mcmember.mgid.raw, ":");
189 
190 			clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
191 			return ret;
192 		}
193 	}
194 
195 	{
196 		struct ib_ah_attr av = {
197 			.dlid	       = be16_to_cpu(mcast->mcmember.mlid),
198 			.port_num      = priv->port,
199 			.sl	       = mcast->mcmember.sl,
200 			.ah_flags      = IB_AH_GRH,
201 			.static_rate   = mcast->mcmember.rate,
202 			.grh	       = {
203 				.flow_label    = be32_to_cpu(mcast->mcmember.flow_label),
204 				.hop_limit     = mcast->mcmember.hop_limit,
205 				.sgid_index    = 0,
206 				.traffic_class = mcast->mcmember.traffic_class
207 			}
208 		};
209 		av.grh.dgid = mcast->mcmember.mgid;
210 
211 		ah = ipoib_create_ah(priv, priv->pd, &av);
212 		if (!ah) {
213 			ipoib_warn(priv, "ib_address_create failed\n");
214 		} else {
215 			spin_lock_irq(&priv->lock);
216 			mcast->ah = ah;
217 			spin_unlock_irq(&priv->lock);
218 
219 			ipoib_dbg_mcast(priv, "MGID %16D AV %p, LID 0x%04x, SL %d\n",
220 					mcast->mcmember.mgid.raw, ":",
221 					mcast->ah->ah,
222 					be16_to_cpu(mcast->mcmember.mlid),
223 					mcast->mcmember.sl);
224 		}
225 	}
226 
227 	/* actually send any queued packets */
228 	while (mcast->pkt_queue.ifq_len) {
229 		struct mbuf *mb;
230 		_IF_DEQUEUE(&mcast->pkt_queue, mb);
231 		mb->m_pkthdr.rcvif = dev;
232 
233 		if (dev->if_transmit(dev, mb))
234 			ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
235 	}
236 
237 	return 0;
238 }
239 
240 static int
241 ipoib_mcast_sendonly_join_complete(int status,
242 				   struct ib_sa_multicast *multicast)
243 {
244 	struct ipoib_mcast *mcast = multicast->context;
245 	struct ipoib_dev_priv *priv = mcast->priv;
246 
247 	/* We trap for port events ourselves. */
248 	if (status == -ENETRESET)
249 		return 0;
250 
251 	if (!status)
252 		status = ipoib_mcast_join_finish(mcast, &multicast->rec);
253 
254 	if (status) {
255 		if (mcast->logcount++ < 20)
256 			ipoib_dbg_mcast(priv, "multicast join failed for %16D, status %d\n",
257 					mcast->mcmember.mgid.raw, ":", status);
258 
259 		/* Flush out any queued packets */
260 		if_inc_counter(priv->dev, IFCOUNTER_OERRORS, mcast->pkt_queue.ifq_len);
261 		_IF_DRAIN(&mcast->pkt_queue);
262 
263 		/* Clear the busy flag so we try again */
264 		status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY,
265 					    &mcast->flags);
266 	}
267 	return status;
268 }
269 
270 static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
271 {
272 	struct ipoib_dev_priv *priv = mcast->priv;
273 	struct ib_sa_mcmember_rec rec = {
274 #if 0				/* Some SMs don't support send-only yet */
275 		.join_state = 4
276 #else
277 		.join_state = 1
278 #endif
279 	};
280 	int ret = 0;
281 
282 	if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
283 		ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
284 		return -ENODEV;
285 	}
286 
287 	if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
288 		ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
289 		return -EBUSY;
290 	}
291 
292 	rec.mgid     = mcast->mcmember.mgid;
293 	rec.port_gid = priv->local_gid;
294 	rec.pkey     = cpu_to_be16(priv->pkey);
295 
296 	mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca,
297 					 priv->port, &rec,
298 					 IB_SA_MCMEMBER_REC_MGID	|
299 					 IB_SA_MCMEMBER_REC_PORT_GID	|
300 					 IB_SA_MCMEMBER_REC_PKEY	|
301 					 IB_SA_MCMEMBER_REC_JOIN_STATE,
302 					 GFP_ATOMIC,
303 					 ipoib_mcast_sendonly_join_complete,
304 					 mcast);
305 	if (IS_ERR(mcast->mc)) {
306 		ret = PTR_ERR(mcast->mc);
307 		clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
308 		ipoib_warn(priv, "ib_sa_join_multicast failed (ret = %d)\n",
309 			   ret);
310 	} else {
311 		ipoib_dbg_mcast(priv, "no multicast record for %16D, starting join\n",
312 				mcast->mcmember.mgid.raw, ":");
313 	}
314 
315 	return ret;
316 }
317 
318 void ipoib_mcast_carrier_on_task(struct work_struct *work)
319 {
320 	struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
321 						   carrier_on_task);
322 	struct ib_port_attr attr;
323 
324 	/*
325 	 * Take rtnl_lock to avoid racing with ipoib_stop() and
326 	 * turning the carrier back on while a device is being
327 	 * removed.
328 	 */
329 	if (ib_query_port(priv->ca, priv->port, &attr) ||
330 	    attr.state != IB_PORT_ACTIVE) {
331 		ipoib_dbg(priv, "Keeping carrier off until IB port is active\n");
332 		return;
333 	}
334 	if_link_state_change(priv->dev, LINK_STATE_UP);
335 }
336 
337 static int ipoib_mcast_join_complete(int status,
338 				     struct ib_sa_multicast *multicast)
339 {
340 	struct ipoib_mcast *mcast = multicast->context;
341 	struct ipoib_dev_priv *priv = mcast->priv;
342 
343 	ipoib_dbg_mcast(priv, "join completion for %16D (status %d)\n",
344 			mcast->mcmember.mgid.raw, ":", status);
345 
346 	/* We trap for port events ourselves. */
347 	if (status == -ENETRESET)
348 		return 0;
349 
350 	if (!status)
351 		status = ipoib_mcast_join_finish(mcast, &multicast->rec);
352 
353 	if (!status) {
354 		mcast->backoff = 1;
355 		mutex_lock(&mcast_mutex);
356 		if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
357 			queue_delayed_work(ipoib_workqueue,
358 					   &priv->mcast_task, 0);
359 		mutex_unlock(&mcast_mutex);
360 
361 		/*
362 		 * Defer carrier on work to ipoib_workqueue to avoid a
363 		 * deadlock on rtnl_lock here.
364 		 */
365 		if (mcast == priv->broadcast)
366 			queue_work(ipoib_workqueue, &priv->carrier_on_task);
367 
368 		return 0;
369 	}
370 
371 	if (mcast->logcount++ < 20) {
372 		if (status == -ETIMEDOUT || status == -EAGAIN) {
373 			ipoib_dbg_mcast(priv, "multicast join failed for %16D, status %d\n",
374 					mcast->mcmember.mgid.raw, ":", status);
375 		} else {
376 			ipoib_warn(priv, "multicast join failed for %16D, status %d\n",
377 				   mcast->mcmember.mgid.raw, ":", status);
378 		}
379 	}
380 
381 	mcast->backoff *= 2;
382 	if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
383 		mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
384 
385 	/* Clear the busy flag so we try again */
386 	status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
387 
388 	mutex_lock(&mcast_mutex);
389 	spin_lock_irq(&priv->lock);
390 	if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
391 		queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
392 				   mcast->backoff * HZ);
393 	spin_unlock_irq(&priv->lock);
394 	mutex_unlock(&mcast_mutex);
395 
396 	return status;
397 }
398 
399 static void ipoib_mcast_join(struct ipoib_dev_priv *priv,
400     struct ipoib_mcast *mcast, int create)
401 {
402 	struct ib_sa_mcmember_rec rec = {
403 		.join_state = 1
404 	};
405 	ib_sa_comp_mask comp_mask;
406 	int ret = 0;
407 
408 	ipoib_dbg_mcast(priv, "joining MGID %16D\n",
409 	    mcast->mcmember.mgid.raw, ":");
410 
411 	rec.mgid     = mcast->mcmember.mgid;
412 	rec.port_gid = priv->local_gid;
413 	rec.pkey     = cpu_to_be16(priv->pkey);
414 
415 	comp_mask =
416 		IB_SA_MCMEMBER_REC_MGID		|
417 		IB_SA_MCMEMBER_REC_PORT_GID	|
418 		IB_SA_MCMEMBER_REC_PKEY		|
419 		IB_SA_MCMEMBER_REC_JOIN_STATE;
420 
421 	if (create) {
422 		comp_mask |=
423 			IB_SA_MCMEMBER_REC_QKEY			|
424 			IB_SA_MCMEMBER_REC_MTU_SELECTOR		|
425 			IB_SA_MCMEMBER_REC_MTU			|
426 			IB_SA_MCMEMBER_REC_TRAFFIC_CLASS	|
427 			IB_SA_MCMEMBER_REC_RATE_SELECTOR	|
428 			IB_SA_MCMEMBER_REC_RATE			|
429 			IB_SA_MCMEMBER_REC_SL			|
430 			IB_SA_MCMEMBER_REC_FLOW_LABEL		|
431 			IB_SA_MCMEMBER_REC_HOP_LIMIT;
432 
433 		rec.qkey	  = priv->broadcast->mcmember.qkey;
434 		rec.mtu_selector  = IB_SA_EQ;
435 		rec.mtu		  = priv->broadcast->mcmember.mtu;
436 		rec.traffic_class = priv->broadcast->mcmember.traffic_class;
437 		rec.rate_selector = IB_SA_EQ;
438 		rec.rate	  = priv->broadcast->mcmember.rate;
439 		rec.sl		  = priv->broadcast->mcmember.sl;
440 		rec.flow_label	  = priv->broadcast->mcmember.flow_label;
441 		rec.hop_limit	  = priv->broadcast->mcmember.hop_limit;
442 	}
443 
444 	set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
445 	mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
446 					 &rec, comp_mask, GFP_KERNEL,
447 					 ipoib_mcast_join_complete, mcast);
448 	if (IS_ERR(mcast->mc)) {
449 		clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
450 		ret = PTR_ERR(mcast->mc);
451 		ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
452 
453 		mcast->backoff *= 2;
454 		if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
455 			mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
456 
457 		mutex_lock(&mcast_mutex);
458 		if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
459 			queue_delayed_work(ipoib_workqueue,
460 					   &priv->mcast_task,
461 					   mcast->backoff * HZ);
462 		mutex_unlock(&mcast_mutex);
463 	}
464 }
465 
466 void ipoib_mcast_join_task(struct work_struct *work)
467 {
468 	struct ipoib_dev_priv *priv =
469 		container_of(work, struct ipoib_dev_priv, mcast_task.work);
470 	struct ifnet *dev = priv->dev;
471 	struct ib_port_attr attr;
472 
473 	ipoib_dbg_mcast(priv, "Running join task. flags 0x%lX\n", priv->flags);
474 
475 	if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
476 		return;
477 
478 	if (ib_query_port(priv->ca, priv->port, &attr) ||
479             attr.state != IB_PORT_ACTIVE) {
480 		ipoib_dbg(priv, "%s: port state is not ACTIVE (state = %d) suspend task.\n",
481                           __func__, attr.state);
482 		return;
483 	}
484 
485 	if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid, NULL))
486 		ipoib_warn(priv, "ib_query_gid() failed\n");
487 	else
488 		memcpy(IF_LLADDR(dev) + 4, priv->local_gid.raw, sizeof (union ib_gid));
489 
490 	{
491 		struct ib_port_attr attr;
492 
493 		if (!ib_query_port(priv->ca, priv->port, &attr))
494 			priv->local_lid = attr.lid;
495 		else
496 			ipoib_warn(priv, "ib_query_port failed\n");
497 	}
498 
499 	if (!priv->broadcast) {
500 		struct ipoib_mcast *broadcast;
501 
502 		if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
503 			return;
504 
505 		broadcast = ipoib_mcast_alloc(priv, 1);
506 		if (!broadcast) {
507 			ipoib_warn(priv, "failed to allocate broadcast group\n");
508 			mutex_lock(&mcast_mutex);
509 			if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
510 				queue_delayed_work(ipoib_workqueue,
511 						   &priv->mcast_task, HZ);
512 			mutex_unlock(&mcast_mutex);
513 			return;
514 		}
515 
516 		spin_lock_irq(&priv->lock);
517 		memcpy(broadcast->mcmember.mgid.raw, dev->if_broadcastaddr + 4,
518 		       sizeof (union ib_gid));
519 		priv->broadcast = broadcast;
520 
521 		__ipoib_mcast_add(priv, priv->broadcast);
522 		spin_unlock_irq(&priv->lock);
523 	}
524 
525 	if (priv->broadcast &&
526 	    !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
527 		if (priv->broadcast &&
528 		    !test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
529 			ipoib_mcast_join(priv, priv->broadcast, 0);
530 		return;
531 	}
532 
533 	while (1) {
534 		struct ipoib_mcast *mcast = NULL;
535 
536 		spin_lock_irq(&priv->lock);
537 		list_for_each_entry(mcast, &priv->multicast_list, list) {
538 			if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
539 			    && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
540 			    && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
541 				/* Found the next unjoined group */
542 				break;
543 			}
544 		}
545 		spin_unlock_irq(&priv->lock);
546 
547 		if (&mcast->list == &priv->multicast_list) {
548 			/* All done */
549 			break;
550 		}
551 
552 		ipoib_mcast_join(priv, mcast, 1);
553 		return;
554 	}
555 
556 	spin_lock_irq(&priv->lock);
557 	if (priv->broadcast)
558 		priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
559 	else
560 		priv->mcast_mtu = priv->admin_mtu;
561 	spin_unlock_irq(&priv->lock);
562 
563 	if (!ipoib_cm_admin_enabled(priv))
564 		ipoib_change_mtu(priv, min(priv->mcast_mtu, priv->admin_mtu));
565 
566 	ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
567 
568 	clear_bit(IPOIB_MCAST_RUN, &priv->flags);
569 }
570 
571 int ipoib_mcast_start_thread(struct ipoib_dev_priv *priv)
572 {
573 	ipoib_dbg_mcast(priv, "starting multicast thread flags 0x%lX\n",
574 	    priv->flags);
575 
576 	mutex_lock(&mcast_mutex);
577 	if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
578 		queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
579 	mutex_unlock(&mcast_mutex);
580 
581 	return 0;
582 }
583 
584 int ipoib_mcast_stop_thread(struct ipoib_dev_priv *priv, int flush)
585 {
586 
587 	ipoib_dbg_mcast(priv, "stopping multicast thread\n");
588 
589 	mutex_lock(&mcast_mutex);
590 	clear_bit(IPOIB_MCAST_RUN, &priv->flags);
591 	cancel_delayed_work(&priv->mcast_task);
592 	mutex_unlock(&mcast_mutex);
593 
594 	if (flush)
595 		flush_workqueue(ipoib_workqueue);
596 
597 	return 0;
598 }
599 
600 static int ipoib_mcast_leave(struct ipoib_dev_priv *priv, struct ipoib_mcast *mcast)
601 {
602 	int ret = 0;
603 
604 	if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
605 		ib_sa_free_multicast(mcast->mc);
606 
607 	if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
608 		ipoib_dbg_mcast(priv, "leaving MGID %16D\n",
609 				mcast->mcmember.mgid.raw, ":");
610 
611 		/* Remove ourselves from the multicast group */
612 		ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
613 				      be16_to_cpu(mcast->mcmember.mlid));
614 		if (ret)
615 			ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
616 	}
617 
618 	return 0;
619 }
620 
621 void
622 ipoib_mcast_send(struct ipoib_dev_priv *priv, void *mgid, struct mbuf *mb)
623 {
624 	struct ifnet *dev = priv->dev;
625 	struct ipoib_mcast *mcast;
626 
627 	if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)		||
628 	    !priv->broadcast					||
629 	    !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
630 		if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
631 		m_freem(mb);
632 		return;
633 	}
634 
635 	mcast = __ipoib_mcast_find(priv, mgid);
636 	if (!mcast) {
637 		/* Let's create a new send only group now */
638 		ipoib_dbg_mcast(priv, "setting up send only multicast group for %16D\n",
639 				mgid, ":");
640 
641 		mcast = ipoib_mcast_alloc(priv, 0);
642 		if (!mcast) {
643 			ipoib_warn(priv, "unable to allocate memory for "
644 				   "multicast structure\n");
645 			if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
646 			m_freem(mb);
647 			goto out;
648 		}
649 
650 		set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
651 		memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
652 		__ipoib_mcast_add(priv, mcast);
653 		list_add_tail(&mcast->list, &priv->multicast_list);
654 	}
655 
656 	if (!mcast->ah) {
657 		if (mcast->pkt_queue.ifq_len < IPOIB_MAX_MCAST_QUEUE) {
658 			_IF_ENQUEUE(&mcast->pkt_queue, mb);
659 		} else {
660 			if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
661 			m_freem(mb);
662 		}
663 
664 		if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
665 			ipoib_dbg_mcast(priv, "no address vector, "
666 					"but multicast join already started\n");
667 		else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
668 			ipoib_mcast_sendonly_join(mcast);
669 
670 		/*
671 		 * If lookup completes between here and out:, don't
672 		 * want to send packet twice.
673 		 */
674 		mcast = NULL;
675 	}
676 
677 out:
678 	if (mcast && mcast->ah)
679 		ipoib_send(priv, mb, mcast->ah, IB_MULTICAST_QPN);
680 }
681 
682 void ipoib_mcast_dev_flush(struct ipoib_dev_priv *priv)
683 {
684 	LIST_HEAD(remove_list);
685 	struct ipoib_mcast *mcast, *tmcast;
686 	unsigned long flags;
687 
688 	ipoib_dbg_mcast(priv, "flushing multicast list\n");
689 
690 	spin_lock_irqsave(&priv->lock, flags);
691 
692 	list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
693 		list_del(&mcast->list);
694 		rb_erase(&mcast->rb_node, &priv->multicast_tree);
695 		list_add_tail(&mcast->list, &remove_list);
696 	}
697 
698 	if (priv->broadcast) {
699 		rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
700 		list_add_tail(&priv->broadcast->list, &remove_list);
701 		priv->broadcast = NULL;
702 	}
703 
704 	spin_unlock_irqrestore(&priv->lock, flags);
705 
706 	list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
707 		ipoib_mcast_leave(priv, mcast);
708 		ipoib_mcast_free(mcast);
709 	}
710 }
711 
712 static int ipoib_mcast_addr_is_valid(const u8 *addr, unsigned int addrlen,
713 				     const u8 *broadcast)
714 {
715 	if (addrlen != INFINIBAND_ALEN)
716 		return 0;
717 	/* reserved QPN, prefix, scope */
718 	if (memcmp(addr, broadcast, 6))
719 		return 0;
720 	/* signature lower, pkey */
721 	if (memcmp(addr + 7, broadcast + 7, 3))
722 		return 0;
723 	return 1;
724 }
725 
726 void ipoib_mcast_restart_task(struct work_struct *work)
727 {
728 	struct ipoib_dev_priv *priv =
729 		container_of(work, struct ipoib_dev_priv, restart_task);
730 	ipoib_mcast_restart(priv);
731 }
732 
733 void ipoib_mcast_restart(struct ipoib_dev_priv *priv)
734 {
735 	struct ifnet *dev = priv->dev;
736 	struct ifmultiaddr *ifma;
737 	struct ipoib_mcast *mcast, *tmcast;
738 	LIST_HEAD(remove_list);
739 	struct ib_sa_mcmember_rec rec;
740 	int addrlen;
741 
742 	ipoib_dbg_mcast(priv, "restarting multicast task flags 0x%lX\n",
743 	    priv->flags);
744 
745 	ipoib_mcast_stop_thread(priv, 0);
746 
747 	if_maddr_rlock(dev);
748 	spin_lock(&priv->lock);
749 
750 	/*
751 	 * Unfortunately, the networking core only gives us a list of all of
752 	 * the multicast hardware addresses. We need to figure out which ones
753 	 * are new and which ones have been removed
754 	 */
755 
756 	/* Clear out the found flag */
757 	list_for_each_entry(mcast, &priv->multicast_list, list)
758 		clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
759 
760 	/* Mark all of the entries that are found or don't exist */
761 
762 
763 	CK_STAILQ_FOREACH(ifma, &dev->if_multiaddrs, ifma_link) {
764 		union ib_gid mgid;
765 		uint8_t *addr;
766 
767 		if (ifma->ifma_addr->sa_family != AF_LINK)
768 			continue;
769 		addr = LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
770 		addrlen = ((struct sockaddr_dl *)ifma->ifma_addr)->sdl_alen;
771 		if (!ipoib_mcast_addr_is_valid(addr, addrlen,
772 					       dev->if_broadcastaddr))
773 			continue;
774 
775 		memcpy(mgid.raw, addr + 4, sizeof mgid);
776 
777 		mcast = __ipoib_mcast_find(priv, &mgid);
778 		if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
779 			struct ipoib_mcast *nmcast;
780 
781 			/* ignore group which is directly joined by userspace */
782 			if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
783 			    !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
784 				ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %16D\n",
785 						mgid.raw, ":");
786 				continue;
787 			}
788 
789 			/* Not found or send-only group, let's add a new entry */
790 			ipoib_dbg_mcast(priv, "adding multicast entry for mgid %16D\n",
791 					mgid.raw, ":");
792 
793 			nmcast = ipoib_mcast_alloc(priv, 0);
794 			if (!nmcast) {
795 				ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
796 				continue;
797 			}
798 
799 			set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
800 
801 			nmcast->mcmember.mgid = mgid;
802 
803 			if (mcast) {
804 				/* Destroy the send only entry */
805 				list_move_tail(&mcast->list, &remove_list);
806 
807 				rb_replace_node(&mcast->rb_node,
808 						&nmcast->rb_node,
809 						&priv->multicast_tree);
810 			} else
811 				__ipoib_mcast_add(priv, nmcast);
812 
813 			list_add_tail(&nmcast->list, &priv->multicast_list);
814 		}
815 
816 		if (mcast)
817 			set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
818 	}
819 
820 	/* Remove all of the entries don't exist anymore */
821 	list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
822 		if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
823 		    !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
824 			ipoib_dbg_mcast(priv, "deleting multicast group %16D\n",
825 					mcast->mcmember.mgid.raw, ":");
826 
827 			rb_erase(&mcast->rb_node, &priv->multicast_tree);
828 
829 			/* Move to the remove list */
830 			list_move_tail(&mcast->list, &remove_list);
831 		}
832 	}
833 
834 	spin_unlock(&priv->lock);
835 	if_maddr_runlock(dev);
836 
837 	/* We have to cancel outside of the spinlock */
838 	list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
839 		ipoib_mcast_leave(mcast->priv, mcast);
840 		ipoib_mcast_free(mcast);
841 	}
842 
843 	if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
844 		ipoib_mcast_start_thread(priv);
845 }
846 
847 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
848 
849 struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct ipoib_dev_priv *priv)
850 {
851 	struct ipoib_mcast_iter *iter;
852 
853 	iter = kmalloc(sizeof *iter, GFP_KERNEL);
854 	if (!iter)
855 		return NULL;
856 
857 	iter->priv = priv;
858 	memset(iter->mgid.raw, 0, 16);
859 
860 	if (ipoib_mcast_iter_next(iter)) {
861 		kfree(iter);
862 		return NULL;
863 	}
864 
865 	return iter;
866 }
867 
868 int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
869 {
870 	struct ipoib_dev_priv *priv = iter->priv;
871 	struct rb_node *n;
872 	struct ipoib_mcast *mcast;
873 	int ret = 1;
874 
875 	spin_lock_irq(&priv->lock);
876 
877 	n = rb_first(&priv->multicast_tree);
878 
879 	while (n) {
880 		mcast = rb_entry(n, struct ipoib_mcast, rb_node);
881 
882 		if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
883 			   sizeof (union ib_gid)) < 0) {
884 			iter->mgid      = mcast->mcmember.mgid;
885 			iter->created   = mcast->created;
886 			iter->queuelen  = mcast->pkt_queue.ifq_len;
887 			iter->complete  = !!mcast->ah;
888 			iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
889 
890 			ret = 0;
891 
892 			break;
893 		}
894 
895 		n = rb_next(n);
896 	}
897 
898 	spin_unlock_irq(&priv->lock);
899 
900 	return ret;
901 }
902 
903 void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
904 			   union ib_gid *mgid,
905 			   unsigned long *created,
906 			   unsigned int *queuelen,
907 			   unsigned int *complete,
908 			   unsigned int *send_only)
909 {
910 	*mgid      = iter->mgid;
911 	*created   = iter->created;
912 	*queuelen  = iter->queuelen;
913 	*complete  = iter->complete;
914 	*send_only = iter->send_only;
915 }
916 
917 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
918