xref: /linux/drivers/infiniband/core/multicast.c (revision d1fa82ef992bc18850d27bc0bdc7a5cd0821d7b3)
1 /*
2  * Copyright (c) 2006 Intel Corporation.  All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #include <linux/completion.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/err.h>
36 #include <linux/interrupt.h>
37 #include <linux/export.h>
38 #include <linux/slab.h>
39 #include <linux/bitops.h>
40 #include <linux/random.h>
41 
42 #include <rdma/ib_cache.h>
43 #include "sa.h"
44 
45 static int mcast_add_one(struct ib_device *device);
46 static void mcast_remove_one(struct ib_device *device, void *client_data);
47 
48 static struct ib_client mcast_client = {
49 	.name   = "ib_multicast",
50 	.add    = mcast_add_one,
51 	.remove = mcast_remove_one
52 };
53 
54 static struct ib_sa_client	sa_client;
55 static struct workqueue_struct	*mcast_wq;
56 static union ib_gid mgid0;
57 
58 struct mcast_device;
59 
60 struct mcast_port {
61 	struct mcast_device	*dev;
62 	spinlock_t		lock;
63 	struct rb_root		table;
64 	refcount_t		refcount;
65 	struct completion	comp;
66 	u32			port_num;
67 };
68 
69 struct mcast_device {
70 	struct ib_device	*device;
71 	struct ib_event_handler	event_handler;
72 	int			start_port;
73 	int			end_port;
74 	struct mcast_port	port[];
75 };
76 
77 enum mcast_state {
78 	MCAST_JOINING,
79 	MCAST_MEMBER,
80 	MCAST_ERROR,
81 };
82 
83 enum mcast_group_state {
84 	MCAST_IDLE,
85 	MCAST_BUSY,
86 	MCAST_GROUP_ERROR,
87 	MCAST_PKEY_EVENT
88 };
89 
90 enum {
91 	MCAST_INVALID_PKEY_INDEX = 0xFFFF
92 };
93 
94 struct mcast_member;
95 
96 struct mcast_group {
97 	struct ib_sa_mcmember_rec rec;
98 	struct rb_node		node;
99 	struct mcast_port	*port;
100 	spinlock_t		lock;
101 	struct work_struct	work;
102 	struct list_head	pending_list;
103 	struct list_head	active_list;
104 	struct mcast_member	*last_join;
105 	int			members[NUM_JOIN_MEMBERSHIP_TYPES];
106 	atomic_t		refcount;
107 	enum mcast_group_state	state;
108 	struct ib_sa_query	*query;
109 	u16			pkey_index;
110 	u8			leave_state;
111 	int			retries;
112 };
113 
114 struct mcast_member {
115 	struct ib_sa_multicast	multicast;
116 	struct ib_sa_client	*client;
117 	struct mcast_group	*group;
118 	struct list_head	list;
119 	enum mcast_state	state;
120 	refcount_t		refcount;
121 	struct completion	comp;
122 };
123 
124 static void join_handler(int status, struct ib_sa_mcmember_rec *rec,
125 			 void *context);
126 static void leave_handler(int status, struct ib_sa_mcmember_rec *rec,
127 			  void *context);
128 
129 static struct mcast_group *mcast_find(struct mcast_port *port,
130 				      union ib_gid *mgid)
131 {
132 	struct rb_node *node = port->table.rb_node;
133 	struct mcast_group *group;
134 	int ret;
135 
136 	while (node) {
137 		group = rb_entry(node, struct mcast_group, node);
138 		ret = memcmp(mgid->raw, group->rec.mgid.raw, sizeof *mgid);
139 		if (!ret)
140 			return group;
141 
142 		if (ret < 0)
143 			node = node->rb_left;
144 		else
145 			node = node->rb_right;
146 	}
147 	return NULL;
148 }
149 
150 static struct mcast_group *mcast_insert(struct mcast_port *port,
151 					struct mcast_group *group,
152 					int allow_duplicates)
153 {
154 	struct rb_node **link = &port->table.rb_node;
155 	struct rb_node *parent = NULL;
156 	struct mcast_group *cur_group;
157 	int ret;
158 
159 	while (*link) {
160 		parent = *link;
161 		cur_group = rb_entry(parent, struct mcast_group, node);
162 
163 		ret = memcmp(group->rec.mgid.raw, cur_group->rec.mgid.raw,
164 			     sizeof group->rec.mgid);
165 		if (ret < 0)
166 			link = &(*link)->rb_left;
167 		else if (ret > 0)
168 			link = &(*link)->rb_right;
169 		else if (allow_duplicates)
170 			link = &(*link)->rb_left;
171 		else
172 			return cur_group;
173 	}
174 	rb_link_node(&group->node, parent, link);
175 	rb_insert_color(&group->node, &port->table);
176 	return NULL;
177 }
178 
179 static void deref_port(struct mcast_port *port)
180 {
181 	if (refcount_dec_and_test(&port->refcount))
182 		complete(&port->comp);
183 }
184 
185 static void release_group(struct mcast_group *group)
186 {
187 	struct mcast_port *port = group->port;
188 	unsigned long flags;
189 
190 	spin_lock_irqsave(&port->lock, flags);
191 	if (atomic_dec_and_test(&group->refcount)) {
192 
193 		if (!RB_EMPTY_NODE(&group->node))
194 			rb_erase(&group->node, &port->table);
195 
196 		spin_unlock_irqrestore(&port->lock, flags);
197 		kfree(group);
198 		deref_port(port);
199 	} else
200 		spin_unlock_irqrestore(&port->lock, flags);
201 }
202 
203 static void deref_member(struct mcast_member *member)
204 {
205 	if (refcount_dec_and_test(&member->refcount))
206 		complete(&member->comp);
207 }
208 
209 static void queue_join(struct mcast_member *member)
210 {
211 	struct mcast_group *group = member->group;
212 	unsigned long flags;
213 
214 	spin_lock_irqsave(&group->lock, flags);
215 	list_add_tail(&member->list, &group->pending_list);
216 	if (group->state == MCAST_IDLE) {
217 		group->state = MCAST_BUSY;
218 		atomic_inc(&group->refcount);
219 		queue_work(mcast_wq, &group->work);
220 	}
221 	spin_unlock_irqrestore(&group->lock, flags);
222 }
223 
224 /*
225  * A multicast group has four types of members: full member, non member,
226  * sendonly non member and sendonly full member.
227  * We need to keep track of the number of members of each
228  * type based on their join state.  Adjust the number of members the belong to
229  * the specified join states.
230  */
231 static void adjust_membership(struct mcast_group *group, u8 join_state, int inc)
232 {
233 	int i;
234 
235 	for (i = 0; i < NUM_JOIN_MEMBERSHIP_TYPES; i++, join_state >>= 1)
236 		if (join_state & 0x1)
237 			group->members[i] += inc;
238 }
239 
240 /*
241  * If a multicast group has zero members left for a particular join state, but
242  * the group is still a member with the SA, we need to leave that join state.
243  * Determine which join states we still belong to, but that do not have any
244  * active members.
245  */
246 static u8 get_leave_state(struct mcast_group *group)
247 {
248 	u8 leave_state = 0;
249 	int i;
250 
251 	for (i = 0; i < NUM_JOIN_MEMBERSHIP_TYPES; i++)
252 		if (!group->members[i])
253 			leave_state |= (0x1 << i);
254 
255 	return leave_state & group->rec.join_state;
256 }
257 
258 static int check_selector(ib_sa_comp_mask comp_mask,
259 			  ib_sa_comp_mask selector_mask,
260 			  ib_sa_comp_mask value_mask,
261 			  u8 selector, u8 src_value, u8 dst_value)
262 {
263 	int err;
264 
265 	if (!(comp_mask & selector_mask) || !(comp_mask & value_mask))
266 		return 0;
267 
268 	switch (selector) {
269 	case IB_SA_GT:
270 		err = (src_value <= dst_value);
271 		break;
272 	case IB_SA_LT:
273 		err = (src_value >= dst_value);
274 		break;
275 	case IB_SA_EQ:
276 		err = (src_value != dst_value);
277 		break;
278 	default:
279 		err = 0;
280 		break;
281 	}
282 
283 	return err;
284 }
285 
286 static int cmp_rec(struct ib_sa_mcmember_rec *src,
287 		   struct ib_sa_mcmember_rec *dst, ib_sa_comp_mask comp_mask)
288 {
289 	/* MGID must already match */
290 
291 	if (comp_mask & IB_SA_MCMEMBER_REC_PORT_GID &&
292 	    memcmp(&src->port_gid, &dst->port_gid, sizeof src->port_gid))
293 		return -EINVAL;
294 	if (comp_mask & IB_SA_MCMEMBER_REC_QKEY && src->qkey != dst->qkey)
295 		return -EINVAL;
296 	if (comp_mask & IB_SA_MCMEMBER_REC_MLID && src->mlid != dst->mlid)
297 		return -EINVAL;
298 	if (check_selector(comp_mask, IB_SA_MCMEMBER_REC_MTU_SELECTOR,
299 			   IB_SA_MCMEMBER_REC_MTU, dst->mtu_selector,
300 			   src->mtu, dst->mtu))
301 		return -EINVAL;
302 	if (comp_mask & IB_SA_MCMEMBER_REC_TRAFFIC_CLASS &&
303 	    src->traffic_class != dst->traffic_class)
304 		return -EINVAL;
305 	if (comp_mask & IB_SA_MCMEMBER_REC_PKEY && src->pkey != dst->pkey)
306 		return -EINVAL;
307 	if (check_selector(comp_mask, IB_SA_MCMEMBER_REC_RATE_SELECTOR,
308 			   IB_SA_MCMEMBER_REC_RATE, dst->rate_selector,
309 			   src->rate, dst->rate))
310 		return -EINVAL;
311 	if (check_selector(comp_mask,
312 			   IB_SA_MCMEMBER_REC_PACKET_LIFE_TIME_SELECTOR,
313 			   IB_SA_MCMEMBER_REC_PACKET_LIFE_TIME,
314 			   dst->packet_life_time_selector,
315 			   src->packet_life_time, dst->packet_life_time))
316 		return -EINVAL;
317 	if (comp_mask & IB_SA_MCMEMBER_REC_SL && src->sl != dst->sl)
318 		return -EINVAL;
319 	if (comp_mask & IB_SA_MCMEMBER_REC_FLOW_LABEL &&
320 	    src->flow_label != dst->flow_label)
321 		return -EINVAL;
322 	if (comp_mask & IB_SA_MCMEMBER_REC_HOP_LIMIT &&
323 	    src->hop_limit != dst->hop_limit)
324 		return -EINVAL;
325 	if (comp_mask & IB_SA_MCMEMBER_REC_SCOPE && src->scope != dst->scope)
326 		return -EINVAL;
327 
328 	/* join_state checked separately, proxy_join ignored */
329 
330 	return 0;
331 }
332 
333 static int send_join(struct mcast_group *group, struct mcast_member *member)
334 {
335 	struct mcast_port *port = group->port;
336 	int ret;
337 
338 	group->last_join = member;
339 	ret = ib_sa_mcmember_rec_query(&sa_client, port->dev->device,
340 				       port->port_num, IB_MGMT_METHOD_SET,
341 				       &member->multicast.rec,
342 				       member->multicast.comp_mask,
343 				       3000, GFP_KERNEL, join_handler, group,
344 				       &group->query);
345 	return (ret > 0) ? 0 : ret;
346 }
347 
348 static int send_leave(struct mcast_group *group, u8 leave_state)
349 {
350 	struct mcast_port *port = group->port;
351 	struct ib_sa_mcmember_rec rec;
352 	int ret;
353 
354 	rec = group->rec;
355 	rec.join_state = leave_state;
356 	group->leave_state = leave_state;
357 
358 	ret = ib_sa_mcmember_rec_query(&sa_client, port->dev->device,
359 				       port->port_num, IB_SA_METHOD_DELETE, &rec,
360 				       IB_SA_MCMEMBER_REC_MGID     |
361 				       IB_SA_MCMEMBER_REC_PORT_GID |
362 				       IB_SA_MCMEMBER_REC_JOIN_STATE,
363 				       3000, GFP_KERNEL, leave_handler,
364 				       group, &group->query);
365 	return (ret > 0) ? 0 : ret;
366 }
367 
368 static void join_group(struct mcast_group *group, struct mcast_member *member,
369 		       u8 join_state)
370 {
371 	member->state = MCAST_MEMBER;
372 	adjust_membership(group, join_state, 1);
373 	group->rec.join_state |= join_state;
374 	member->multicast.rec = group->rec;
375 	member->multicast.rec.join_state = join_state;
376 	list_move(&member->list, &group->active_list);
377 }
378 
379 static int fail_join(struct mcast_group *group, struct mcast_member *member,
380 		     int status)
381 {
382 	spin_lock_irq(&group->lock);
383 	list_del_init(&member->list);
384 	spin_unlock_irq(&group->lock);
385 	return member->multicast.callback(status, &member->multicast);
386 }
387 
388 static void process_group_error(struct mcast_group *group)
389 {
390 	struct mcast_member *member;
391 	int ret = 0;
392 	u16 pkey_index;
393 
394 	if (group->state == MCAST_PKEY_EVENT)
395 		ret = ib_find_pkey(group->port->dev->device,
396 				   group->port->port_num,
397 				   be16_to_cpu(group->rec.pkey), &pkey_index);
398 
399 	spin_lock_irq(&group->lock);
400 	if (group->state == MCAST_PKEY_EVENT && !ret &&
401 	    group->pkey_index == pkey_index)
402 		goto out;
403 
404 	while (!list_empty(&group->active_list)) {
405 		member = list_entry(group->active_list.next,
406 				    struct mcast_member, list);
407 		refcount_inc(&member->refcount);
408 		list_del_init(&member->list);
409 		adjust_membership(group, member->multicast.rec.join_state, -1);
410 		member->state = MCAST_ERROR;
411 		spin_unlock_irq(&group->lock);
412 
413 		ret = member->multicast.callback(-ENETRESET,
414 						 &member->multicast);
415 		deref_member(member);
416 		if (ret)
417 			ib_sa_free_multicast(&member->multicast);
418 		spin_lock_irq(&group->lock);
419 	}
420 
421 	group->rec.join_state = 0;
422 out:
423 	group->state = MCAST_BUSY;
424 	spin_unlock_irq(&group->lock);
425 }
426 
427 static void mcast_work_handler(struct work_struct *work)
428 {
429 	struct mcast_group *group;
430 	struct mcast_member *member;
431 	struct ib_sa_multicast *multicast;
432 	int status, ret;
433 	u8 join_state;
434 
435 	group = container_of(work, typeof(*group), work);
436 retest:
437 	spin_lock_irq(&group->lock);
438 	while (!list_empty(&group->pending_list) ||
439 	       (group->state != MCAST_BUSY)) {
440 
441 		if (group->state != MCAST_BUSY) {
442 			spin_unlock_irq(&group->lock);
443 			process_group_error(group);
444 			goto retest;
445 		}
446 
447 		member = list_entry(group->pending_list.next,
448 				    struct mcast_member, list);
449 		multicast = &member->multicast;
450 		join_state = multicast->rec.join_state;
451 		refcount_inc(&member->refcount);
452 
453 		if (join_state == (group->rec.join_state & join_state)) {
454 			status = cmp_rec(&group->rec, &multicast->rec,
455 					 multicast->comp_mask);
456 			if (!status)
457 				join_group(group, member, join_state);
458 			else
459 				list_del_init(&member->list);
460 			spin_unlock_irq(&group->lock);
461 			ret = multicast->callback(status, multicast);
462 		} else {
463 			spin_unlock_irq(&group->lock);
464 			status = send_join(group, member);
465 			if (!status) {
466 				deref_member(member);
467 				return;
468 			}
469 			ret = fail_join(group, member, status);
470 		}
471 
472 		deref_member(member);
473 		if (ret)
474 			ib_sa_free_multicast(&member->multicast);
475 		spin_lock_irq(&group->lock);
476 	}
477 
478 	join_state = get_leave_state(group);
479 	if (join_state) {
480 		group->rec.join_state &= ~join_state;
481 		spin_unlock_irq(&group->lock);
482 		if (send_leave(group, join_state))
483 			goto retest;
484 	} else {
485 		group->state = MCAST_IDLE;
486 		spin_unlock_irq(&group->lock);
487 		release_group(group);
488 	}
489 }
490 
491 /*
492  * Fail a join request if it is still active - at the head of the pending queue.
493  */
494 static void process_join_error(struct mcast_group *group, int status)
495 {
496 	struct mcast_member *member;
497 	int ret;
498 
499 	spin_lock_irq(&group->lock);
500 	member = list_entry(group->pending_list.next,
501 			    struct mcast_member, list);
502 	if (group->last_join == member) {
503 		refcount_inc(&member->refcount);
504 		list_del_init(&member->list);
505 		spin_unlock_irq(&group->lock);
506 		ret = member->multicast.callback(status, &member->multicast);
507 		deref_member(member);
508 		if (ret)
509 			ib_sa_free_multicast(&member->multicast);
510 	} else
511 		spin_unlock_irq(&group->lock);
512 }
513 
514 static void join_handler(int status, struct ib_sa_mcmember_rec *rec,
515 			 void *context)
516 {
517 	struct mcast_group *group = context;
518 	u16 pkey_index = MCAST_INVALID_PKEY_INDEX;
519 
520 	if (status)
521 		process_join_error(group, status);
522 	else {
523 		int mgids_changed, is_mgid0;
524 
525 		if (ib_find_pkey(group->port->dev->device,
526 				 group->port->port_num, be16_to_cpu(rec->pkey),
527 				 &pkey_index))
528 			pkey_index = MCAST_INVALID_PKEY_INDEX;
529 
530 		spin_lock_irq(&group->port->lock);
531 		if (group->state == MCAST_BUSY &&
532 		    group->pkey_index == MCAST_INVALID_PKEY_INDEX)
533 			group->pkey_index = pkey_index;
534 		mgids_changed = memcmp(&rec->mgid, &group->rec.mgid,
535 				       sizeof(group->rec.mgid));
536 		group->rec = *rec;
537 		if (mgids_changed) {
538 			rb_erase(&group->node, &group->port->table);
539 
540 			RB_CLEAR_NODE(&group->node);
541 			is_mgid0 = !memcmp(&mgid0, &group->rec.mgid,
542 					   sizeof(mgid0));
543 			mcast_insert(group->port, group, is_mgid0);
544 		}
545 		spin_unlock_irq(&group->port->lock);
546 	}
547 	mcast_work_handler(&group->work);
548 }
549 
550 static void leave_handler(int status, struct ib_sa_mcmember_rec *rec,
551 			  void *context)
552 {
553 	struct mcast_group *group = context;
554 
555 	if (status && group->retries > 0 &&
556 	    !send_leave(group, group->leave_state))
557 		group->retries--;
558 	else
559 		mcast_work_handler(&group->work);
560 }
561 
562 static struct mcast_group *acquire_group(struct mcast_port *port,
563 					 union ib_gid *mgid, gfp_t gfp_mask)
564 {
565 	struct mcast_group *group, *cur_group;
566 	unsigned long flags;
567 	int is_mgid0;
568 
569 	is_mgid0 = !memcmp(&mgid0, mgid, sizeof mgid0);
570 	if (!is_mgid0) {
571 		spin_lock_irqsave(&port->lock, flags);
572 		group = mcast_find(port, mgid);
573 		if (group)
574 			goto found;
575 		spin_unlock_irqrestore(&port->lock, flags);
576 	}
577 
578 	group = kzalloc_obj(*group, gfp_mask);
579 	if (!group)
580 		return NULL;
581 
582 	group->retries = 3;
583 	group->port = port;
584 	group->rec.mgid = *mgid;
585 	group->pkey_index = MCAST_INVALID_PKEY_INDEX;
586 	INIT_LIST_HEAD(&group->pending_list);
587 	INIT_LIST_HEAD(&group->active_list);
588 	INIT_WORK(&group->work, mcast_work_handler);
589 	spin_lock_init(&group->lock);
590 
591 	spin_lock_irqsave(&port->lock, flags);
592 	cur_group = mcast_insert(port, group, is_mgid0);
593 	if (cur_group) {
594 		kfree(group);
595 		group = cur_group;
596 	} else
597 		refcount_inc(&port->refcount);
598 found:
599 	atomic_inc(&group->refcount);
600 	spin_unlock_irqrestore(&port->lock, flags);
601 	return group;
602 }
603 
604 /*
605  * We serialize all join requests to a single group to make our lives much
606  * easier.  Otherwise, two users could try to join the same group
607  * simultaneously, with different configurations, one could leave while the
608  * join is in progress, etc., which makes locking around error recovery
609  * difficult.
610  */
611 struct ib_sa_multicast *
612 ib_sa_join_multicast(struct ib_sa_client *client,
613 		     struct ib_device *device, u32 port_num,
614 		     struct ib_sa_mcmember_rec *rec,
615 		     ib_sa_comp_mask comp_mask, gfp_t gfp_mask,
616 		     int (*callback)(int status,
617 				     struct ib_sa_multicast *multicast),
618 		     void *context)
619 {
620 	struct mcast_device *dev;
621 	struct mcast_member *member;
622 	struct ib_sa_multicast *multicast;
623 	int ret;
624 
625 	dev = ib_get_client_data(device, &mcast_client);
626 	if (!dev)
627 		return ERR_PTR(-ENODEV);
628 
629 	member = kmalloc_obj(*member, gfp_mask);
630 	if (!member)
631 		return ERR_PTR(-ENOMEM);
632 
633 	ib_sa_client_get(client);
634 	member->client = client;
635 	member->multicast.rec = *rec;
636 	member->multicast.comp_mask = comp_mask;
637 	member->multicast.callback = callback;
638 	member->multicast.context = context;
639 	init_completion(&member->comp);
640 	refcount_set(&member->refcount, 1);
641 	member->state = MCAST_JOINING;
642 
643 	member->group = acquire_group(&dev->port[port_num - dev->start_port],
644 				      &rec->mgid, gfp_mask);
645 	if (!member->group) {
646 		ret = -ENOMEM;
647 		goto err;
648 	}
649 
650 	/*
651 	 * The user will get the multicast structure in their callback.  They
652 	 * could then free the multicast structure before we can return from
653 	 * this routine.  So we save the pointer to return before queuing
654 	 * any callback.
655 	 */
656 	multicast = &member->multicast;
657 	queue_join(member);
658 	return multicast;
659 
660 err:
661 	ib_sa_client_put(client);
662 	kfree(member);
663 	return ERR_PTR(ret);
664 }
665 EXPORT_SYMBOL(ib_sa_join_multicast);
666 
667 void ib_sa_free_multicast(struct ib_sa_multicast *multicast)
668 {
669 	struct mcast_member *member;
670 	struct mcast_group *group;
671 
672 	member = container_of(multicast, struct mcast_member, multicast);
673 	group = member->group;
674 
675 	spin_lock_irq(&group->lock);
676 	if (member->state == MCAST_MEMBER)
677 		adjust_membership(group, multicast->rec.join_state, -1);
678 
679 	list_del_init(&member->list);
680 
681 	if (group->state == MCAST_IDLE) {
682 		group->state = MCAST_BUSY;
683 		spin_unlock_irq(&group->lock);
684 		/* Continue to hold reference on group until callback */
685 		queue_work(mcast_wq, &group->work);
686 	} else {
687 		spin_unlock_irq(&group->lock);
688 		release_group(group);
689 	}
690 
691 	deref_member(member);
692 	wait_for_completion(&member->comp);
693 	ib_sa_client_put(member->client);
694 	kfree(member);
695 }
696 EXPORT_SYMBOL(ib_sa_free_multicast);
697 
698 int ib_sa_get_mcmember_rec(struct ib_device *device, u32 port_num,
699 			   union ib_gid *mgid, struct ib_sa_mcmember_rec *rec)
700 {
701 	struct mcast_device *dev;
702 	struct mcast_port *port;
703 	struct mcast_group *group;
704 	unsigned long flags;
705 	int ret = 0;
706 
707 	dev = ib_get_client_data(device, &mcast_client);
708 	if (!dev)
709 		return -ENODEV;
710 
711 	port = &dev->port[port_num - dev->start_port];
712 	spin_lock_irqsave(&port->lock, flags);
713 	group = mcast_find(port, mgid);
714 	if (group)
715 		*rec = group->rec;
716 	else
717 		ret = -EADDRNOTAVAIL;
718 	spin_unlock_irqrestore(&port->lock, flags);
719 
720 	return ret;
721 }
722 EXPORT_SYMBOL(ib_sa_get_mcmember_rec);
723 
724 /**
725  * ib_init_ah_from_mcmember - Initialize AH attribute from multicast
726  * member record and gid of the device.
727  * @device:	RDMA device
728  * @port_num:	Port of the rdma device to consider
729  * @rec:	Multicast member record to use
730  * @ndev:	Optional netdevice, applicable only for RoCE
731  * @gid_type:	GID type to consider
732  * @ah_attr:	AH attribute to fillup on successful completion
733  *
734  * ib_init_ah_from_mcmember() initializes AH attribute based on multicast
735  * member record and other device properties. On success the caller is
736  * responsible to call rdma_destroy_ah_attr on the ah_attr. Returns 0 on
737  * success or appropriate error code.
738  *
739  */
740 int ib_init_ah_from_mcmember(struct ib_device *device, u32 port_num,
741 			     struct ib_sa_mcmember_rec *rec,
742 			     struct net_device *ndev,
743 			     enum ib_gid_type gid_type,
744 			     struct rdma_ah_attr *ah_attr)
745 {
746 	const struct ib_gid_attr *sgid_attr;
747 
748 	/* GID table is not based on the netdevice for IB link layer,
749 	 * so ignore ndev during search.
750 	 */
751 	if (rdma_protocol_ib(device, port_num))
752 		ndev = NULL;
753 	else if (!rdma_protocol_roce(device, port_num))
754 		return -EINVAL;
755 
756 	sgid_attr = rdma_find_gid_by_port(device, &rec->port_gid,
757 					  gid_type, port_num, ndev);
758 	if (IS_ERR(sgid_attr))
759 		return PTR_ERR(sgid_attr);
760 
761 	memset(ah_attr, 0, sizeof(*ah_attr));
762 	ah_attr->type = rdma_ah_find_type(device, port_num);
763 
764 	rdma_ah_set_dlid(ah_attr, be16_to_cpu(rec->mlid));
765 	rdma_ah_set_sl(ah_attr, rec->sl);
766 	rdma_ah_set_port_num(ah_attr, port_num);
767 	rdma_ah_set_static_rate(ah_attr, rec->rate);
768 	rdma_move_grh_sgid_attr(ah_attr, &rec->mgid,
769 				be32_to_cpu(rec->flow_label),
770 				rec->hop_limit,	rec->traffic_class,
771 				sgid_attr);
772 	return 0;
773 }
774 EXPORT_SYMBOL(ib_init_ah_from_mcmember);
775 
776 static void mcast_groups_event(struct mcast_port *port,
777 			       enum mcast_group_state state)
778 {
779 	struct mcast_group *group;
780 	struct rb_node *node;
781 	unsigned long flags;
782 
783 	spin_lock_irqsave(&port->lock, flags);
784 	for (node = rb_first(&port->table); node; node = rb_next(node)) {
785 		group = rb_entry(node, struct mcast_group, node);
786 		spin_lock(&group->lock);
787 		if (group->state == MCAST_IDLE) {
788 			atomic_inc(&group->refcount);
789 			queue_work(mcast_wq, &group->work);
790 		}
791 		if (group->state != MCAST_GROUP_ERROR)
792 			group->state = state;
793 		spin_unlock(&group->lock);
794 	}
795 	spin_unlock_irqrestore(&port->lock, flags);
796 }
797 
798 static void mcast_event_handler(struct ib_event_handler *handler,
799 				struct ib_event *event)
800 {
801 	struct mcast_device *dev;
802 	int index;
803 
804 	dev = container_of(handler, struct mcast_device, event_handler);
805 	if (!rdma_cap_ib_mcast(dev->device, event->element.port_num))
806 		return;
807 
808 	index = event->element.port_num - dev->start_port;
809 
810 	switch (event->event) {
811 	case IB_EVENT_PORT_ERR:
812 	case IB_EVENT_LID_CHANGE:
813 	case IB_EVENT_CLIENT_REREGISTER:
814 		mcast_groups_event(&dev->port[index], MCAST_GROUP_ERROR);
815 		break;
816 	case IB_EVENT_PKEY_CHANGE:
817 		mcast_groups_event(&dev->port[index], MCAST_PKEY_EVENT);
818 		break;
819 	default:
820 		break;
821 	}
822 }
823 
824 static int mcast_add_one(struct ib_device *device)
825 {
826 	struct mcast_device *dev;
827 	struct mcast_port *port;
828 	int i;
829 	int count = 0;
830 
831 	dev = kmalloc_flex(*dev, port, device->phys_port_cnt);
832 	if (!dev)
833 		return -ENOMEM;
834 
835 	dev->start_port = rdma_start_port(device);
836 	dev->end_port = rdma_end_port(device);
837 
838 	for (i = 0; i <= dev->end_port - dev->start_port; i++) {
839 		if (!rdma_cap_ib_mcast(device, dev->start_port + i))
840 			continue;
841 		port = &dev->port[i];
842 		port->dev = dev;
843 		port->port_num = dev->start_port + i;
844 		spin_lock_init(&port->lock);
845 		port->table = RB_ROOT;
846 		init_completion(&port->comp);
847 		refcount_set(&port->refcount, 1);
848 		++count;
849 	}
850 
851 	if (!count) {
852 		kfree(dev);
853 		return -EOPNOTSUPP;
854 	}
855 
856 	dev->device = device;
857 	ib_set_client_data(device, &mcast_client, dev);
858 
859 	INIT_IB_EVENT_HANDLER(&dev->event_handler, device, mcast_event_handler);
860 	ib_register_event_handler(&dev->event_handler);
861 	return 0;
862 }
863 
864 static void mcast_remove_one(struct ib_device *device, void *client_data)
865 {
866 	struct mcast_device *dev = client_data;
867 	struct mcast_port *port;
868 	int i;
869 
870 	ib_unregister_event_handler(&dev->event_handler);
871 	flush_workqueue(mcast_wq);
872 
873 	for (i = 0; i <= dev->end_port - dev->start_port; i++) {
874 		if (rdma_cap_ib_mcast(device, dev->start_port + i)) {
875 			port = &dev->port[i];
876 			deref_port(port);
877 			wait_for_completion(&port->comp);
878 		}
879 	}
880 
881 	kfree(dev);
882 }
883 
884 int mcast_init(void)
885 {
886 	int ret;
887 
888 	mcast_wq = alloc_ordered_workqueue("ib_mcast", WQ_MEM_RECLAIM);
889 	if (!mcast_wq)
890 		return -ENOMEM;
891 
892 	ib_sa_register_client(&sa_client);
893 
894 	ret = ib_register_client(&mcast_client);
895 	if (ret)
896 		goto err;
897 	return 0;
898 
899 err:
900 	ib_sa_unregister_client(&sa_client);
901 	destroy_workqueue(mcast_wq);
902 	return ret;
903 }
904 
905 void mcast_cleanup(void)
906 {
907 	ib_unregister_client(&mcast_client);
908 	ib_sa_unregister_client(&sa_client);
909 	destroy_workqueue(mcast_wq);
910 }
911