2 * Copyright (c) 2006 Intel Corporation. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
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>
42 #include <rdma/ib_cache.h>
45 static void mcast_add_one(struct ib_device *device);
46 static void mcast_remove_one(struct ib_device *device, void *client_data);
48 static struct ib_client mcast_client = {
49 .name = "ib_multicast",
51 .remove = mcast_remove_one
54 static struct ib_sa_client sa_client;
55 static struct workqueue_struct *mcast_wq;
56 static union ib_gid mgid0;
61 struct mcast_device *dev;
65 struct completion comp;
70 struct ib_device *device;
71 struct ib_event_handler event_handler;
74 struct mcast_port port[0];
83 enum mcast_group_state {
91 MCAST_INVALID_PKEY_INDEX = 0xFFFF
97 struct ib_sa_mcmember_rec rec;
99 struct mcast_port *port;
101 struct work_struct work;
102 struct list_head pending_list;
103 struct list_head active_list;
104 struct mcast_member *last_join;
107 enum mcast_group_state state;
108 struct ib_sa_query *query;
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;
121 struct completion comp;
124 static void join_handler(int status, struct ib_sa_mcmember_rec *rec,
126 static void leave_handler(int status, struct ib_sa_mcmember_rec *rec,
129 static struct mcast_group *mcast_find(struct mcast_port *port,
132 struct rb_node *node = port->table.rb_node;
133 struct mcast_group *group;
137 group = rb_entry(node, struct mcast_group, node);
138 ret = memcmp(mgid->raw, group->rec.mgid.raw, sizeof *mgid);
143 node = node->rb_left;
145 node = node->rb_right;
150 static struct mcast_group *mcast_insert(struct mcast_port *port,
151 struct mcast_group *group,
152 int allow_duplicates)
154 struct rb_node **link = &port->table.rb_node;
155 struct rb_node *parent = NULL;
156 struct mcast_group *cur_group;
161 cur_group = rb_entry(parent, struct mcast_group, node);
163 ret = memcmp(group->rec.mgid.raw, cur_group->rec.mgid.raw,
164 sizeof group->rec.mgid);
166 link = &(*link)->rb_left;
168 link = &(*link)->rb_right;
169 else if (allow_duplicates)
170 link = &(*link)->rb_left;
174 rb_link_node(&group->node, parent, link);
175 rb_insert_color(&group->node, &port->table);
179 static void deref_port(struct mcast_port *port)
181 if (atomic_dec_and_test(&port->refcount))
182 complete(&port->comp);
185 static void release_group(struct mcast_group *group)
187 struct mcast_port *port = group->port;
190 spin_lock_irqsave(&port->lock, flags);
191 if (atomic_dec_and_test(&group->refcount)) {
192 rb_erase(&group->node, &port->table);
193 spin_unlock_irqrestore(&port->lock, flags);
197 spin_unlock_irqrestore(&port->lock, flags);
200 static void deref_member(struct mcast_member *member)
202 if (atomic_dec_and_test(&member->refcount))
203 complete(&member->comp);
206 static void queue_join(struct mcast_member *member)
208 struct mcast_group *group = member->group;
211 spin_lock_irqsave(&group->lock, flags);
212 list_add_tail(&member->list, &group->pending_list);
213 if (group->state == MCAST_IDLE) {
214 group->state = MCAST_BUSY;
215 atomic_inc(&group->refcount);
216 queue_work(mcast_wq, &group->work);
218 spin_unlock_irqrestore(&group->lock, flags);
222 * A multicast group has three types of members: full member, non member, and
223 * send only member. We need to keep track of the number of members of each
224 * type based on their join state. Adjust the number of members the belong to
225 * the specified join states.
227 static void adjust_membership(struct mcast_group *group, u8 join_state, int inc)
231 for (i = 0; i < 3; i++, join_state >>= 1)
232 if (join_state & 0x1)
233 group->members[i] += inc;
237 * If a multicast group has zero members left for a particular join state, but
238 * the group is still a member with the SA, we need to leave that join state.
239 * Determine which join states we still belong to, but that do not have any
242 static u8 get_leave_state(struct mcast_group *group)
247 for (i = 0; i < 3; i++)
248 if (!group->members[i])
249 leave_state |= (0x1 << i);
251 return leave_state & group->rec.join_state;
254 static int check_selector(ib_sa_comp_mask comp_mask,
255 ib_sa_comp_mask selector_mask,
256 ib_sa_comp_mask value_mask,
257 u8 selector, u8 src_value, u8 dst_value)
261 if (!(comp_mask & selector_mask) || !(comp_mask & value_mask))
266 err = (src_value <= dst_value);
269 err = (src_value >= dst_value);
272 err = (src_value != dst_value);
282 static int cmp_rec(struct ib_sa_mcmember_rec *src,
283 struct ib_sa_mcmember_rec *dst, ib_sa_comp_mask comp_mask)
285 /* MGID must already match */
287 if (comp_mask & IB_SA_MCMEMBER_REC_PORT_GID &&
288 memcmp(&src->port_gid, &dst->port_gid, sizeof src->port_gid))
290 if (comp_mask & IB_SA_MCMEMBER_REC_QKEY && src->qkey != dst->qkey)
292 if (comp_mask & IB_SA_MCMEMBER_REC_MLID && src->mlid != dst->mlid)
294 if (check_selector(comp_mask, IB_SA_MCMEMBER_REC_MTU_SELECTOR,
295 IB_SA_MCMEMBER_REC_MTU, dst->mtu_selector,
298 if (comp_mask & IB_SA_MCMEMBER_REC_TRAFFIC_CLASS &&
299 src->traffic_class != dst->traffic_class)
301 if (comp_mask & IB_SA_MCMEMBER_REC_PKEY && src->pkey != dst->pkey)
303 if (check_selector(comp_mask, IB_SA_MCMEMBER_REC_RATE_SELECTOR,
304 IB_SA_MCMEMBER_REC_RATE, dst->rate_selector,
305 src->rate, dst->rate))
307 if (check_selector(comp_mask,
308 IB_SA_MCMEMBER_REC_PACKET_LIFE_TIME_SELECTOR,
309 IB_SA_MCMEMBER_REC_PACKET_LIFE_TIME,
310 dst->packet_life_time_selector,
311 src->packet_life_time, dst->packet_life_time))
313 if (comp_mask & IB_SA_MCMEMBER_REC_SL && src->sl != dst->sl)
315 if (comp_mask & IB_SA_MCMEMBER_REC_FLOW_LABEL &&
316 src->flow_label != dst->flow_label)
318 if (comp_mask & IB_SA_MCMEMBER_REC_HOP_LIMIT &&
319 src->hop_limit != dst->hop_limit)
321 if (comp_mask & IB_SA_MCMEMBER_REC_SCOPE && src->scope != dst->scope)
324 /* join_state checked separately, proxy_join ignored */
329 static int send_join(struct mcast_group *group, struct mcast_member *member)
331 struct mcast_port *port = group->port;
334 group->last_join = member;
335 ret = ib_sa_mcmember_rec_query(&sa_client, port->dev->device,
336 port->port_num, IB_MGMT_METHOD_SET,
337 &member->multicast.rec,
338 member->multicast.comp_mask,
339 3000, GFP_KERNEL, join_handler, group,
341 return (ret > 0) ? 0 : ret;
344 static int send_leave(struct mcast_group *group, u8 leave_state)
346 struct mcast_port *port = group->port;
347 struct ib_sa_mcmember_rec rec;
351 rec.join_state = leave_state;
352 group->leave_state = leave_state;
354 ret = ib_sa_mcmember_rec_query(&sa_client, port->dev->device,
355 port->port_num, IB_SA_METHOD_DELETE, &rec,
356 IB_SA_MCMEMBER_REC_MGID |
357 IB_SA_MCMEMBER_REC_PORT_GID |
358 IB_SA_MCMEMBER_REC_JOIN_STATE,
359 3000, GFP_KERNEL, leave_handler,
360 group, &group->query);
361 return (ret > 0) ? 0 : ret;
364 static void join_group(struct mcast_group *group, struct mcast_member *member,
367 member->state = MCAST_MEMBER;
368 adjust_membership(group, join_state, 1);
369 group->rec.join_state |= join_state;
370 member->multicast.rec = group->rec;
371 member->multicast.rec.join_state = join_state;
372 list_move(&member->list, &group->active_list);
375 static int fail_join(struct mcast_group *group, struct mcast_member *member,
378 spin_lock_irq(&group->lock);
379 list_del_init(&member->list);
380 spin_unlock_irq(&group->lock);
381 return member->multicast.callback(status, &member->multicast);
384 static void process_group_error(struct mcast_group *group)
386 struct mcast_member *member;
390 if (group->state == MCAST_PKEY_EVENT)
391 ret = ib_find_pkey(group->port->dev->device,
392 group->port->port_num,
393 be16_to_cpu(group->rec.pkey), &pkey_index);
395 spin_lock_irq(&group->lock);
396 if (group->state == MCAST_PKEY_EVENT && !ret &&
397 group->pkey_index == pkey_index)
400 while (!list_empty(&group->active_list)) {
401 member = list_entry(group->active_list.next,
402 struct mcast_member, list);
403 atomic_inc(&member->refcount);
404 list_del_init(&member->list);
405 adjust_membership(group, member->multicast.rec.join_state, -1);
406 member->state = MCAST_ERROR;
407 spin_unlock_irq(&group->lock);
409 ret = member->multicast.callback(-ENETRESET,
411 deref_member(member);
413 ib_sa_free_multicast(&member->multicast);
414 spin_lock_irq(&group->lock);
417 group->rec.join_state = 0;
419 group->state = MCAST_BUSY;
420 spin_unlock_irq(&group->lock);
423 static void mcast_work_handler(struct work_struct *work)
425 struct mcast_group *group;
426 struct mcast_member *member;
427 struct ib_sa_multicast *multicast;
431 group = container_of(work, typeof(*group), work);
433 spin_lock_irq(&group->lock);
434 while (!list_empty(&group->pending_list) ||
435 (group->state != MCAST_BUSY)) {
437 if (group->state != MCAST_BUSY) {
438 spin_unlock_irq(&group->lock);
439 process_group_error(group);
443 member = list_entry(group->pending_list.next,
444 struct mcast_member, list);
445 multicast = &member->multicast;
446 join_state = multicast->rec.join_state;
447 atomic_inc(&member->refcount);
449 if (join_state == (group->rec.join_state & join_state)) {
450 status = cmp_rec(&group->rec, &multicast->rec,
451 multicast->comp_mask);
453 join_group(group, member, join_state);
455 list_del_init(&member->list);
456 spin_unlock_irq(&group->lock);
457 ret = multicast->callback(status, multicast);
459 spin_unlock_irq(&group->lock);
460 status = send_join(group, member);
462 deref_member(member);
465 ret = fail_join(group, member, status);
468 deref_member(member);
470 ib_sa_free_multicast(&member->multicast);
471 spin_lock_irq(&group->lock);
474 join_state = get_leave_state(group);
476 group->rec.join_state &= ~join_state;
477 spin_unlock_irq(&group->lock);
478 if (send_leave(group, join_state))
481 group->state = MCAST_IDLE;
482 spin_unlock_irq(&group->lock);
483 release_group(group);
488 * Fail a join request if it is still active - at the head of the pending queue.
490 static void process_join_error(struct mcast_group *group, int status)
492 struct mcast_member *member;
495 spin_lock_irq(&group->lock);
496 member = list_entry(group->pending_list.next,
497 struct mcast_member, list);
498 if (group->last_join == member) {
499 atomic_inc(&member->refcount);
500 list_del_init(&member->list);
501 spin_unlock_irq(&group->lock);
502 ret = member->multicast.callback(status, &member->multicast);
503 deref_member(member);
505 ib_sa_free_multicast(&member->multicast);
507 spin_unlock_irq(&group->lock);
510 static void join_handler(int status, struct ib_sa_mcmember_rec *rec,
513 struct mcast_group *group = context;
514 u16 pkey_index = MCAST_INVALID_PKEY_INDEX;
517 process_join_error(group, status);
519 int mgids_changed, is_mgid0;
520 ib_find_pkey(group->port->dev->device, group->port->port_num,
521 be16_to_cpu(rec->pkey), &pkey_index);
523 spin_lock_irq(&group->port->lock);
524 if (group->state == MCAST_BUSY &&
525 group->pkey_index == MCAST_INVALID_PKEY_INDEX)
526 group->pkey_index = pkey_index;
527 mgids_changed = memcmp(&rec->mgid, &group->rec.mgid,
528 sizeof(group->rec.mgid));
531 rb_erase(&group->node, &group->port->table);
532 is_mgid0 = !memcmp(&mgid0, &group->rec.mgid,
534 mcast_insert(group->port, group, is_mgid0);
536 spin_unlock_irq(&group->port->lock);
538 mcast_work_handler(&group->work);
541 static void leave_handler(int status, struct ib_sa_mcmember_rec *rec,
544 struct mcast_group *group = context;
546 if (status && group->retries > 0 &&
547 !send_leave(group, group->leave_state))
550 mcast_work_handler(&group->work);
553 static struct mcast_group *acquire_group(struct mcast_port *port,
554 union ib_gid *mgid, gfp_t gfp_mask)
556 struct mcast_group *group, *cur_group;
560 is_mgid0 = !memcmp(&mgid0, mgid, sizeof mgid0);
562 spin_lock_irqsave(&port->lock, flags);
563 group = mcast_find(port, mgid);
566 spin_unlock_irqrestore(&port->lock, flags);
569 group = kzalloc(sizeof *group, gfp_mask);
575 group->rec.mgid = *mgid;
576 group->pkey_index = MCAST_INVALID_PKEY_INDEX;
577 INIT_LIST_HEAD(&group->pending_list);
578 INIT_LIST_HEAD(&group->active_list);
579 INIT_WORK(&group->work, mcast_work_handler);
580 spin_lock_init(&group->lock);
582 spin_lock_irqsave(&port->lock, flags);
583 cur_group = mcast_insert(port, group, is_mgid0);
588 atomic_inc(&port->refcount);
590 atomic_inc(&group->refcount);
591 spin_unlock_irqrestore(&port->lock, flags);
596 * We serialize all join requests to a single group to make our lives much
597 * easier. Otherwise, two users could try to join the same group
598 * simultaneously, with different configurations, one could leave while the
599 * join is in progress, etc., which makes locking around error recovery
602 struct ib_sa_multicast *
603 ib_sa_join_multicast(struct ib_sa_client *client,
604 struct ib_device *device, u8 port_num,
605 struct ib_sa_mcmember_rec *rec,
606 ib_sa_comp_mask comp_mask, gfp_t gfp_mask,
607 int (*callback)(int status,
608 struct ib_sa_multicast *multicast),
611 struct mcast_device *dev;
612 struct mcast_member *member;
613 struct ib_sa_multicast *multicast;
616 dev = ib_get_client_data(device, &mcast_client);
618 return ERR_PTR(-ENODEV);
620 member = kmalloc(sizeof *member, gfp_mask);
622 return ERR_PTR(-ENOMEM);
624 ib_sa_client_get(client);
625 member->client = client;
626 member->multicast.rec = *rec;
627 member->multicast.comp_mask = comp_mask;
628 member->multicast.callback = callback;
629 member->multicast.context = context;
630 init_completion(&member->comp);
631 atomic_set(&member->refcount, 1);
632 member->state = MCAST_JOINING;
634 member->group = acquire_group(&dev->port[port_num - dev->start_port],
635 &rec->mgid, gfp_mask);
636 if (!member->group) {
642 * The user will get the multicast structure in their callback. They
643 * could then free the multicast structure before we can return from
644 * this routine. So we save the pointer to return before queuing
647 multicast = &member->multicast;
652 ib_sa_client_put(client);
656 EXPORT_SYMBOL(ib_sa_join_multicast);
658 void ib_sa_free_multicast(struct ib_sa_multicast *multicast)
660 struct mcast_member *member;
661 struct mcast_group *group;
663 member = container_of(multicast, struct mcast_member, multicast);
664 group = member->group;
666 spin_lock_irq(&group->lock);
667 if (member->state == MCAST_MEMBER)
668 adjust_membership(group, multicast->rec.join_state, -1);
670 list_del_init(&member->list);
672 if (group->state == MCAST_IDLE) {
673 group->state = MCAST_BUSY;
674 spin_unlock_irq(&group->lock);
675 /* Continue to hold reference on group until callback */
676 queue_work(mcast_wq, &group->work);
678 spin_unlock_irq(&group->lock);
679 release_group(group);
682 deref_member(member);
683 wait_for_completion(&member->comp);
684 ib_sa_client_put(member->client);
687 EXPORT_SYMBOL(ib_sa_free_multicast);
689 int ib_sa_get_mcmember_rec(struct ib_device *device, u8 port_num,
690 union ib_gid *mgid, struct ib_sa_mcmember_rec *rec)
692 struct mcast_device *dev;
693 struct mcast_port *port;
694 struct mcast_group *group;
698 dev = ib_get_client_data(device, &mcast_client);
702 port = &dev->port[port_num - dev->start_port];
703 spin_lock_irqsave(&port->lock, flags);
704 group = mcast_find(port, mgid);
708 ret = -EADDRNOTAVAIL;
709 spin_unlock_irqrestore(&port->lock, flags);
713 EXPORT_SYMBOL(ib_sa_get_mcmember_rec);
715 int ib_init_ah_from_mcmember(struct ib_device *device, u8 port_num,
716 struct ib_sa_mcmember_rec *rec,
717 struct ib_ah_attr *ah_attr)
723 ret = ib_find_cached_gid(device, &rec->port_gid,
724 NULL, &p, &gid_index);
728 memset(ah_attr, 0, sizeof *ah_attr);
729 ah_attr->dlid = be16_to_cpu(rec->mlid);
730 ah_attr->sl = rec->sl;
731 ah_attr->port_num = port_num;
732 ah_attr->static_rate = rec->rate;
734 ah_attr->ah_flags = IB_AH_GRH;
735 ah_attr->grh.dgid = rec->mgid;
737 ah_attr->grh.sgid_index = (u8) gid_index;
738 ah_attr->grh.flow_label = be32_to_cpu(rec->flow_label);
739 ah_attr->grh.hop_limit = rec->hop_limit;
740 ah_attr->grh.traffic_class = rec->traffic_class;
744 EXPORT_SYMBOL(ib_init_ah_from_mcmember);
746 static void mcast_groups_event(struct mcast_port *port,
747 enum mcast_group_state state)
749 struct mcast_group *group;
750 struct rb_node *node;
753 spin_lock_irqsave(&port->lock, flags);
754 for (node = rb_first(&port->table); node; node = rb_next(node)) {
755 group = rb_entry(node, struct mcast_group, node);
756 spin_lock(&group->lock);
757 if (group->state == MCAST_IDLE) {
758 atomic_inc(&group->refcount);
759 queue_work(mcast_wq, &group->work);
761 if (group->state != MCAST_GROUP_ERROR)
762 group->state = state;
763 spin_unlock(&group->lock);
765 spin_unlock_irqrestore(&port->lock, flags);
768 static void mcast_event_handler(struct ib_event_handler *handler,
769 struct ib_event *event)
771 struct mcast_device *dev;
774 dev = container_of(handler, struct mcast_device, event_handler);
775 if (!rdma_cap_ib_mcast(dev->device, event->element.port_num))
778 index = event->element.port_num - dev->start_port;
780 switch (event->event) {
781 case IB_EVENT_PORT_ERR:
782 case IB_EVENT_LID_CHANGE:
783 case IB_EVENT_SM_CHANGE:
784 case IB_EVENT_CLIENT_REREGISTER:
785 mcast_groups_event(&dev->port[index], MCAST_GROUP_ERROR);
787 case IB_EVENT_PKEY_CHANGE:
788 mcast_groups_event(&dev->port[index], MCAST_PKEY_EVENT);
795 static void mcast_add_one(struct ib_device *device)
797 struct mcast_device *dev;
798 struct mcast_port *port;
802 dev = kmalloc(sizeof *dev + device->phys_port_cnt * sizeof *port,
807 dev->start_port = rdma_start_port(device);
808 dev->end_port = rdma_end_port(device);
810 for (i = 0; i <= dev->end_port - dev->start_port; i++) {
811 if (!rdma_cap_ib_mcast(device, dev->start_port + i))
813 port = &dev->port[i];
815 port->port_num = dev->start_port + i;
816 spin_lock_init(&port->lock);
817 port->table = RB_ROOT;
818 init_completion(&port->comp);
819 atomic_set(&port->refcount, 1);
828 dev->device = device;
829 ib_set_client_data(device, &mcast_client, dev);
831 INIT_IB_EVENT_HANDLER(&dev->event_handler, device, mcast_event_handler);
832 ib_register_event_handler(&dev->event_handler);
835 static void mcast_remove_one(struct ib_device *device, void *client_data)
837 struct mcast_device *dev = client_data;
838 struct mcast_port *port;
844 ib_unregister_event_handler(&dev->event_handler);
845 flush_workqueue(mcast_wq);
847 for (i = 0; i <= dev->end_port - dev->start_port; i++) {
848 if (rdma_cap_ib_mcast(device, dev->start_port + i)) {
849 port = &dev->port[i];
851 wait_for_completion(&port->comp);
862 mcast_wq = create_singlethread_workqueue("ib_mcast");
866 ib_sa_register_client(&sa_client);
868 ret = ib_register_client(&mcast_client);
874 ib_sa_unregister_client(&sa_client);
875 destroy_workqueue(mcast_wq);
879 void mcast_cleanup(void)
881 ib_unregister_client(&mcast_client);
882 ib_sa_unregister_client(&sa_client);
883 destroy_workqueue(mcast_wq);