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 #define LINUXKPI_PARAM_PREFIX ibcore_
35 #include <linux/completion.h>
36 #include <linux/dma-mapping.h>
37 #include <linux/err.h>
38 #include <linux/interrupt.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);
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;
115 struct mcast_member {
116 struct ib_sa_multicast multicast;
117 struct ib_sa_client *client;
118 struct mcast_group *group;
119 struct list_head list;
120 enum mcast_state state;
122 struct completion comp;
125 static void join_handler(int status, struct ib_sa_mcmember_rec *rec,
127 static void leave_handler(int status, struct ib_sa_mcmember_rec *rec,
130 static struct mcast_group *mcast_find(struct mcast_port *port,
133 struct rb_node *node = port->table.rb_node;
134 struct mcast_group *group;
138 group = rb_entry(node, struct mcast_group, node);
139 ret = memcmp(mgid->raw, group->rec.mgid.raw, sizeof *mgid);
144 node = node->rb_left;
146 node = node->rb_right;
151 static struct mcast_group *mcast_insert(struct mcast_port *port,
152 struct mcast_group *group,
153 int allow_duplicates)
155 struct rb_node **link = &port->table.rb_node;
156 struct rb_node *parent = NULL;
157 struct mcast_group *cur_group;
162 cur_group = rb_entry(parent, struct mcast_group, node);
164 ret = memcmp(group->rec.mgid.raw, cur_group->rec.mgid.raw,
165 sizeof group->rec.mgid);
167 link = &(*link)->rb_left;
169 link = &(*link)->rb_right;
170 else if (allow_duplicates)
171 link = &(*link)->rb_left;
175 rb_link_node(&group->node, parent, link);
176 rb_insert_color(&group->node, &port->table);
180 static void deref_port(struct mcast_port *port)
182 if (atomic_dec_and_test(&port->refcount))
183 complete(&port->comp);
186 static void release_group(struct mcast_group *group)
188 struct mcast_port *port = group->port;
191 spin_lock_irqsave(&port->lock, flags);
192 if (atomic_dec_and_test(&group->refcount)) {
193 rb_erase(&group->node, &port->table);
194 spin_unlock_irqrestore(&port->lock, flags);
198 spin_unlock_irqrestore(&port->lock, flags);
201 static void deref_member(struct mcast_member *member)
203 if (atomic_dec_and_test(&member->refcount))
204 complete(&member->comp);
207 static void queue_join(struct mcast_member *member)
209 struct mcast_group *group = member->group;
212 spin_lock_irqsave(&group->lock, flags);
213 list_add_tail(&member->list, &group->pending_list);
214 if (group->state == MCAST_IDLE) {
215 group->state = MCAST_BUSY;
216 atomic_inc(&group->refcount);
217 queue_work(mcast_wq, &group->work);
219 spin_unlock_irqrestore(&group->lock, flags);
223 * A multicast group has three types of members: full member, non member, and
224 * send only member. We need to keep track of the number of members of each
225 * type based on their join state. Adjust the number of members the belong to
226 * the specified join states.
228 static void adjust_membership(struct mcast_group *group, u8 join_state, int inc)
232 for (i = 0; i < 3; i++, join_state >>= 1)
233 if (join_state & 0x1)
234 group->members[i] += inc;
238 * If a multicast group has zero members left for a particular join state, but
239 * the group is still a member with the SA, we need to leave that join state.
240 * Determine which join states we still belong to, but that do not have any
243 static u8 get_leave_state(struct mcast_group *group)
248 for (i = 0; i < 3; i++)
249 if (!group->members[i])
250 leave_state |= (0x1 << i);
252 return leave_state & group->rec.join_state;
255 static int cmp_rec(struct ib_sa_mcmember_rec *src,
256 struct ib_sa_mcmember_rec *dst, ib_sa_comp_mask comp_mask)
258 /* MGID must already match */
260 if (comp_mask & IB_SA_MCMEMBER_REC_PORT_GID &&
261 memcmp(&src->port_gid, &dst->port_gid, sizeof src->port_gid))
263 if (comp_mask & IB_SA_MCMEMBER_REC_QKEY && src->qkey != dst->qkey)
265 if (comp_mask & IB_SA_MCMEMBER_REC_MLID && src->mlid != dst->mlid)
267 if (ib_sa_check_selector(comp_mask, IB_SA_MCMEMBER_REC_MTU_SELECTOR,
268 IB_SA_MCMEMBER_REC_MTU, dst->mtu_selector,
271 if (comp_mask & IB_SA_MCMEMBER_REC_TRAFFIC_CLASS &&
272 src->traffic_class != dst->traffic_class)
274 if (comp_mask & IB_SA_MCMEMBER_REC_PKEY && src->pkey != dst->pkey)
276 if (ib_sa_check_selector(comp_mask, IB_SA_MCMEMBER_REC_RATE_SELECTOR,
277 IB_SA_MCMEMBER_REC_RATE, dst->rate_selector,
278 src->rate, dst->rate))
280 if (ib_sa_check_selector(comp_mask,
281 IB_SA_MCMEMBER_REC_PACKET_LIFE_TIME_SELECTOR,
282 IB_SA_MCMEMBER_REC_PACKET_LIFE_TIME,
283 dst->packet_life_time_selector,
284 src->packet_life_time, dst->packet_life_time))
286 if (comp_mask & IB_SA_MCMEMBER_REC_SL && src->sl != dst->sl)
288 if (comp_mask & IB_SA_MCMEMBER_REC_FLOW_LABEL &&
289 src->flow_label != dst->flow_label)
291 if (comp_mask & IB_SA_MCMEMBER_REC_HOP_LIMIT &&
292 src->hop_limit != dst->hop_limit)
294 if (comp_mask & IB_SA_MCMEMBER_REC_SCOPE && src->scope != dst->scope)
297 /* join_state checked separately, proxy_join ignored */
302 static int send_join(struct mcast_group *group, struct mcast_member *member)
304 struct mcast_port *port = group->port;
307 group->last_join = member;
308 ret = ib_sa_mcmember_rec_query(&sa_client, port->dev->device,
309 port->port_num, IB_MGMT_METHOD_SET,
310 &member->multicast.rec,
311 member->multicast.comp_mask,
312 3000, GFP_KERNEL, join_handler, group,
315 group->query_id = ret;
321 static int send_leave(struct mcast_group *group, u8 leave_state)
323 struct mcast_port *port = group->port;
324 struct ib_sa_mcmember_rec rec;
328 rec.join_state = leave_state;
329 group->leave_state = leave_state;
331 ret = ib_sa_mcmember_rec_query(&sa_client, port->dev->device,
332 port->port_num, IB_SA_METHOD_DELETE, &rec,
333 IB_SA_MCMEMBER_REC_MGID |
334 IB_SA_MCMEMBER_REC_PORT_GID |
335 IB_SA_MCMEMBER_REC_JOIN_STATE,
336 3000, GFP_KERNEL, leave_handler,
337 group, &group->query);
339 group->query_id = ret;
345 static void join_group(struct mcast_group *group, struct mcast_member *member,
348 member->state = MCAST_MEMBER;
349 adjust_membership(group, join_state, 1);
350 group->rec.join_state |= join_state;
351 member->multicast.rec = group->rec;
352 member->multicast.rec.join_state = join_state;
353 list_move(&member->list, &group->active_list);
356 static int fail_join(struct mcast_group *group, struct mcast_member *member,
359 spin_lock_irq(&group->lock);
360 list_del_init(&member->list);
361 spin_unlock_irq(&group->lock);
362 return member->multicast.callback(status, &member->multicast);
365 static void process_group_error(struct mcast_group *group)
367 struct mcast_member *member;
371 if (group->state == MCAST_PKEY_EVENT)
372 ret = ib_find_pkey(group->port->dev->device,
373 group->port->port_num,
374 be16_to_cpu(group->rec.pkey), &pkey_index);
376 spin_lock_irq(&group->lock);
377 if (group->state == MCAST_PKEY_EVENT && !ret &&
378 group->pkey_index == pkey_index)
381 while (!list_empty(&group->active_list)) {
382 member = list_entry(group->active_list.next,
383 struct mcast_member, list);
384 atomic_inc(&member->refcount);
385 list_del_init(&member->list);
386 adjust_membership(group, member->multicast.rec.join_state, -1);
387 member->state = MCAST_ERROR;
388 spin_unlock_irq(&group->lock);
390 ret = member->multicast.callback(-ENETRESET,
392 deref_member(member);
394 ib_sa_free_multicast(&member->multicast);
395 spin_lock_irq(&group->lock);
398 group->rec.join_state = 0;
400 group->state = MCAST_BUSY;
401 spin_unlock_irq(&group->lock);
404 static void mcast_work_handler(struct work_struct *work)
406 struct mcast_group *group;
407 struct mcast_member *member;
408 struct ib_sa_multicast *multicast;
412 group = container_of(work, typeof(*group), work);
414 spin_lock_irq(&group->lock);
415 while (!list_empty(&group->pending_list) ||
416 (group->state != MCAST_BUSY)) {
418 if (group->state != MCAST_BUSY) {
419 spin_unlock_irq(&group->lock);
420 process_group_error(group);
424 member = list_entry(group->pending_list.next,
425 struct mcast_member, list);
426 multicast = &member->multicast;
427 join_state = multicast->rec.join_state;
428 atomic_inc(&member->refcount);
430 if (join_state == (group->rec.join_state & join_state)) {
431 status = cmp_rec(&group->rec, &multicast->rec,
432 multicast->comp_mask);
434 join_group(group, member, join_state);
436 list_del_init(&member->list);
437 spin_unlock_irq(&group->lock);
438 ret = multicast->callback(status, multicast);
440 spin_unlock_irq(&group->lock);
441 status = send_join(group, member);
443 deref_member(member);
446 ret = fail_join(group, member, status);
449 deref_member(member);
451 ib_sa_free_multicast(&member->multicast);
452 spin_lock_irq(&group->lock);
455 join_state = get_leave_state(group);
457 group->rec.join_state &= ~join_state;
458 spin_unlock_irq(&group->lock);
459 if (send_leave(group, join_state))
462 group->state = MCAST_IDLE;
463 spin_unlock_irq(&group->lock);
464 release_group(group);
469 * Fail a join request if it is still active - at the head of the pending queue.
471 static void process_join_error(struct mcast_group *group, int status)
473 struct mcast_member *member;
476 spin_lock_irq(&group->lock);
477 member = list_entry(group->pending_list.next,
478 struct mcast_member, list);
479 if (group->last_join == member) {
480 atomic_inc(&member->refcount);
481 list_del_init(&member->list);
482 spin_unlock_irq(&group->lock);
483 ret = member->multicast.callback(status, &member->multicast);
484 deref_member(member);
486 ib_sa_free_multicast(&member->multicast);
488 spin_unlock_irq(&group->lock);
491 static void join_handler(int status, struct ib_sa_mcmember_rec *rec,
494 struct mcast_group *group = context;
495 u16 pkey_index = MCAST_INVALID_PKEY_INDEX;
498 process_join_error(group, status);
500 ib_find_pkey(group->port->dev->device, group->port->port_num,
501 be16_to_cpu(rec->pkey), &pkey_index);
503 spin_lock_irq(&group->port->lock);
505 if (group->state == MCAST_BUSY &&
506 group->pkey_index == MCAST_INVALID_PKEY_INDEX)
507 group->pkey_index = pkey_index;
508 if (!memcmp(&mgid0, &group->rec.mgid, sizeof mgid0)) {
509 rb_erase(&group->node, &group->port->table);
510 mcast_insert(group->port, group, 1);
512 spin_unlock_irq(&group->port->lock);
514 mcast_work_handler(&group->work);
517 static void leave_handler(int status, struct ib_sa_mcmember_rec *rec,
520 struct mcast_group *group = context;
522 if (status && (group->retries > 0) &&
523 !send_leave(group, group->leave_state))
526 mcast_work_handler(&group->work);
529 static struct mcast_group *acquire_group(struct mcast_port *port,
530 union ib_gid *mgid, gfp_t gfp_mask)
532 struct mcast_group *group, *cur_group;
536 is_mgid0 = !memcmp(&mgid0, mgid, sizeof mgid0);
538 spin_lock_irqsave(&port->lock, flags);
539 group = mcast_find(port, mgid);
542 spin_unlock_irqrestore(&port->lock, flags);
545 group = kzalloc(sizeof *group, gfp_mask);
551 group->rec.mgid = *mgid;
552 group->pkey_index = MCAST_INVALID_PKEY_INDEX;
553 INIT_LIST_HEAD(&group->pending_list);
554 INIT_LIST_HEAD(&group->active_list);
555 INIT_WORK(&group->work, mcast_work_handler);
556 spin_lock_init(&group->lock);
558 spin_lock_irqsave(&port->lock, flags);
559 cur_group = mcast_insert(port, group, is_mgid0);
564 atomic_inc(&port->refcount);
566 atomic_inc(&group->refcount);
567 spin_unlock_irqrestore(&port->lock, flags);
572 * We serialize all join requests to a single group to make our lives much
573 * easier. Otherwise, two users could try to join the same group
574 * simultaneously, with different configurations, one could leave while the
575 * join is in progress, etc., which makes locking around error recovery
578 struct ib_sa_multicast *
579 ib_sa_join_multicast(struct ib_sa_client *client,
580 struct ib_device *device, u8 port_num,
581 struct ib_sa_mcmember_rec *rec,
582 ib_sa_comp_mask comp_mask, gfp_t gfp_mask,
583 int (*callback)(int status,
584 struct ib_sa_multicast *multicast),
587 struct mcast_device *dev;
588 struct mcast_member *member;
589 struct ib_sa_multicast *multicast;
592 dev = ib_get_client_data(device, &mcast_client);
594 return ERR_PTR(-ENODEV);
596 member = kmalloc(sizeof *member, gfp_mask);
598 return ERR_PTR(-ENOMEM);
600 ib_sa_client_get(client);
601 member->client = client;
602 member->multicast.rec = *rec;
603 member->multicast.comp_mask = comp_mask;
604 member->multicast.callback = callback;
605 member->multicast.context = context;
606 init_completion(&member->comp);
607 atomic_set(&member->refcount, 1);
608 member->state = MCAST_JOINING;
610 member->group = acquire_group(&dev->port[port_num - dev->start_port],
611 &rec->mgid, gfp_mask);
612 if (!member->group) {
618 * The user will get the multicast structure in their callback. They
619 * could then free the multicast structure before we can return from
620 * this routine. So we save the pointer to return before queuing
623 multicast = &member->multicast;
628 ib_sa_client_put(client);
632 EXPORT_SYMBOL(ib_sa_join_multicast);
634 void ib_sa_free_multicast(struct ib_sa_multicast *multicast)
636 struct mcast_member *member;
637 struct mcast_group *group;
639 member = container_of(multicast, struct mcast_member, multicast);
640 group = member->group;
642 spin_lock_irq(&group->lock);
643 if (member->state == MCAST_MEMBER)
644 adjust_membership(group, multicast->rec.join_state, -1);
646 list_del_init(&member->list);
648 if (group->state == MCAST_IDLE) {
649 group->state = MCAST_BUSY;
650 spin_unlock_irq(&group->lock);
651 /* Continue to hold reference on group until callback */
652 queue_work(mcast_wq, &group->work);
654 spin_unlock_irq(&group->lock);
655 release_group(group);
658 deref_member(member);
659 wait_for_completion(&member->comp);
660 ib_sa_client_put(member->client);
663 EXPORT_SYMBOL(ib_sa_free_multicast);
665 int ib_sa_get_mcmember_rec(struct ib_device *device, u8 port_num,
666 union ib_gid *mgid, struct ib_sa_mcmember_rec *rec)
668 struct mcast_device *dev;
669 struct mcast_port *port;
670 struct mcast_group *group;
674 dev = ib_get_client_data(device, &mcast_client);
678 port = &dev->port[port_num - dev->start_port];
679 spin_lock_irqsave(&port->lock, flags);
680 group = mcast_find(port, mgid);
684 ret = -EADDRNOTAVAIL;
685 spin_unlock_irqrestore(&port->lock, flags);
689 EXPORT_SYMBOL(ib_sa_get_mcmember_rec);
691 int ib_init_ah_from_mcmember(struct ib_device *device, u8 port_num,
692 struct ib_sa_mcmember_rec *rec,
693 struct ib_ah_attr *ah_attr)
699 ret = ib_find_cached_gid(device, &rec->port_gid, &p, &gid_index);
703 memset(ah_attr, 0, sizeof *ah_attr);
704 ah_attr->dlid = be16_to_cpu(rec->mlid);
705 ah_attr->sl = rec->sl;
706 ah_attr->port_num = port_num;
707 ah_attr->static_rate = rec->rate;
709 ah_attr->ah_flags = IB_AH_GRH;
710 ah_attr->grh.dgid = rec->mgid;
712 ah_attr->grh.sgid_index = (u8) gid_index;
713 ah_attr->grh.flow_label = be32_to_cpu(rec->flow_label);
714 ah_attr->grh.hop_limit = rec->hop_limit;
715 ah_attr->grh.traffic_class = rec->traffic_class;
719 EXPORT_SYMBOL(ib_init_ah_from_mcmember);
721 static void mcast_groups_event(struct mcast_port *port,
722 enum mcast_group_state state)
724 struct mcast_group *group;
725 struct rb_node *node;
728 spin_lock_irqsave(&port->lock, flags);
729 for (node = rb_first(&port->table); node; node = rb_next(node)) {
730 group = rb_entry(node, struct mcast_group, node);
731 spin_lock(&group->lock);
732 if (group->state == MCAST_IDLE) {
733 atomic_inc(&group->refcount);
734 queue_work(mcast_wq, &group->work);
736 if (group->state != MCAST_GROUP_ERROR)
737 group->state = state;
738 spin_unlock(&group->lock);
740 spin_unlock_irqrestore(&port->lock, flags);
743 static void mcast_event_handler(struct ib_event_handler *handler,
744 struct ib_event *event)
746 struct mcast_device *dev;
749 dev = container_of(handler, struct mcast_device, event_handler);
750 if (rdma_port_get_link_layer(dev->device, event->element.port_num) !=
751 IB_LINK_LAYER_INFINIBAND)
754 index = event->element.port_num - dev->start_port;
756 switch (event->event) {
757 case IB_EVENT_PORT_ERR:
758 case IB_EVENT_LID_CHANGE:
759 case IB_EVENT_SM_CHANGE:
760 case IB_EVENT_CLIENT_REREGISTER:
761 mcast_groups_event(&dev->port[index], MCAST_GROUP_ERROR);
763 case IB_EVENT_PKEY_CHANGE:
764 mcast_groups_event(&dev->port[index], MCAST_PKEY_EVENT);
771 static void mcast_add_one(struct ib_device *device)
773 struct mcast_device *dev;
774 struct mcast_port *port;
778 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
781 dev = kmalloc(sizeof *dev + device->phys_port_cnt * sizeof *port,
786 if (device->node_type == RDMA_NODE_IB_SWITCH)
787 dev->start_port = dev->end_port = 0;
790 dev->end_port = device->phys_port_cnt;
793 for (i = 0; i <= dev->end_port - dev->start_port; i++) {
794 if (rdma_port_get_link_layer(device, dev->start_port + i) !=
795 IB_LINK_LAYER_INFINIBAND)
797 port = &dev->port[i];
799 port->port_num = dev->start_port + i;
800 spin_lock_init(&port->lock);
801 port->table = RB_ROOT;
802 init_completion(&port->comp);
803 atomic_set(&port->refcount, 1);
812 dev->device = device;
813 ib_set_client_data(device, &mcast_client, dev);
815 INIT_IB_EVENT_HANDLER(&dev->event_handler, device, mcast_event_handler);
816 ib_register_event_handler(&dev->event_handler);
819 static void mcast_remove_one(struct ib_device *device)
821 struct mcast_device *dev;
822 struct mcast_port *port;
825 dev = ib_get_client_data(device, &mcast_client);
829 ib_unregister_event_handler(&dev->event_handler);
830 flush_workqueue(mcast_wq);
832 for (i = 0; i <= dev->end_port - dev->start_port; i++) {
833 if (rdma_port_get_link_layer(device, dev->start_port + i) ==
834 IB_LINK_LAYER_INFINIBAND) {
835 port = &dev->port[i];
837 wait_for_completion(&port->comp);
848 mcast_wq = create_singlethread_workqueue("ib_mcast");
852 ib_sa_register_client(&sa_client);
854 ret = ib_register_client(&mcast_client);
860 ib_sa_unregister_client(&sa_client);
861 destroy_workqueue(mcast_wq);
865 void mcast_cleanup(void)
867 ib_unregister_client(&mcast_client);
868 ib_sa_unregister_client(&sa_client);
869 destroy_workqueue(mcast_wq);