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