2 * Copyright (c) 2005 Voltaire Inc. All rights reserved.
3 * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
4 * Copyright (c) 1999-2005, Mellanox Technologies, Inc. All rights reserved.
5 * Copyright (c) 2005-2006 Intel Corporation. All rights reserved.
7 * This Software is licensed under one of the following licenses:
9 * 1) under the terms of the "Common Public License 1.0" a copy of which is
10 * available from the Open Source Initiative, see
11 * http://www.opensource.org/licenses/cpl.php.
13 * 2) under the terms of the "The BSD License" a copy of which is
14 * available from the Open Source Initiative, see
15 * http://www.opensource.org/licenses/bsd-license.php.
17 * 3) under the terms of the "GNU General Public License (GPL) Version 2" a
18 * copy of which is available from the Open Source Initiative, see
19 * http://www.opensource.org/licenses/gpl-license.php.
21 * Licensee has the right to choose one of the above licenses.
23 * Redistributions of source code must retain the above copyright
24 * notice and one of the license notices.
26 * Redistributions in binary form must reproduce both the above copyright
27 * notice, one of the license notices in the documentation
28 * and/or other materials provided with the distribution.
32 #include <linux/completion.h>
34 #include <linux/in6.h>
35 #include <linux/mutex.h>
36 #include <linux/random.h>
37 #include <linux/idr.h>
38 #include <linux/inetdevice.h>
42 #include <rdma/rdma_cm.h>
43 #include <rdma/rdma_cm_ib.h>
44 #include <rdma/ib_cache.h>
45 #include <rdma/ib_cm.h>
46 #include <rdma/ib_sa.h>
47 #include <rdma/iw_cm.h>
49 MODULE_AUTHOR("Sean Hefty");
50 MODULE_DESCRIPTION("Generic RDMA CM Agent");
51 MODULE_LICENSE("Dual BSD/GPL");
53 #define CMA_CM_RESPONSE_TIMEOUT 20
54 #define CMA_MAX_CM_RETRIES 15
56 static void cma_add_one(struct ib_device *device);
57 static void cma_remove_one(struct ib_device *device);
59 static struct ib_client cma_client = {
62 .remove = cma_remove_one
65 static struct ib_sa_client sa_client;
66 static struct rdma_addr_client addr_client;
67 static LIST_HEAD(dev_list);
68 static LIST_HEAD(listen_any_list);
69 static DEFINE_MUTEX(lock);
70 static struct workqueue_struct *cma_wq;
71 static DEFINE_IDR(sdp_ps);
72 static DEFINE_IDR(tcp_ps);
73 static DEFINE_IDR(udp_ps);
74 static DEFINE_IDR(ipoib_ps);
78 struct list_head list;
79 struct ib_device *device;
80 struct completion comp;
82 struct list_head id_list;
99 struct rdma_bind_list {
101 struct hlist_head owners;
106 * Device removal can occur at anytime, so we need extra handling to
107 * serialize notifying the user of device removal with other callbacks.
108 * We do this by disabling removal notification while a callback is in process,
109 * and reporting it after the callback completes.
111 struct rdma_id_private {
112 struct rdma_cm_id id;
114 struct rdma_bind_list *bind_list;
115 struct hlist_node node;
116 struct list_head list;
117 struct list_head listen_list;
118 struct cma_device *cma_dev;
119 struct list_head mc_list;
121 enum cma_state state;
123 struct completion comp;
125 wait_queue_head_t wait_remove;
130 struct ib_sa_query *query;
143 struct cma_multicast {
144 struct rdma_id_private *id_priv;
146 struct ib_sa_multicast *ib;
148 struct list_head list;
150 struct sockaddr addr;
151 u8 pad[sizeof(struct sockaddr_in6) -
152 sizeof(struct sockaddr)];
156 struct work_struct work;
157 struct rdma_id_private *id;
158 enum cma_state old_state;
159 enum cma_state new_state;
160 struct rdma_cm_event event;
173 u8 ip_version; /* IP version: 7:4 */
175 union cma_ip_addr src_addr;
176 union cma_ip_addr dst_addr;
181 u8 sdp_version; /* Major version: 7:4 */
182 u8 ip_version; /* IP version: 7:4 */
183 u8 sdp_specific1[10];
186 union cma_ip_addr src_addr;
187 union cma_ip_addr dst_addr;
195 #define CMA_VERSION 0x00
196 #define SDP_MAJ_VERSION 0x2
198 static int cma_comp(struct rdma_id_private *id_priv, enum cma_state comp)
203 spin_lock_irqsave(&id_priv->lock, flags);
204 ret = (id_priv->state == comp);
205 spin_unlock_irqrestore(&id_priv->lock, flags);
209 static int cma_comp_exch(struct rdma_id_private *id_priv,
210 enum cma_state comp, enum cma_state exch)
215 spin_lock_irqsave(&id_priv->lock, flags);
216 if ((ret = (id_priv->state == comp)))
217 id_priv->state = exch;
218 spin_unlock_irqrestore(&id_priv->lock, flags);
222 static enum cma_state cma_exch(struct rdma_id_private *id_priv,
228 spin_lock_irqsave(&id_priv->lock, flags);
229 old = id_priv->state;
230 id_priv->state = exch;
231 spin_unlock_irqrestore(&id_priv->lock, flags);
235 static inline u8 cma_get_ip_ver(struct cma_hdr *hdr)
237 return hdr->ip_version >> 4;
240 static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
242 hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
245 static inline u8 sdp_get_majv(u8 sdp_version)
247 return sdp_version >> 4;
250 static inline u8 sdp_get_ip_ver(struct sdp_hh *hh)
252 return hh->ip_version >> 4;
255 static inline void sdp_set_ip_ver(struct sdp_hh *hh, u8 ip_ver)
257 hh->ip_version = (ip_ver << 4) | (hh->ip_version & 0xF);
260 static inline int cma_is_ud_ps(enum rdma_port_space ps)
262 return (ps == RDMA_PS_UDP || ps == RDMA_PS_IPOIB);
265 static void cma_attach_to_dev(struct rdma_id_private *id_priv,
266 struct cma_device *cma_dev)
268 atomic_inc(&cma_dev->refcount);
269 id_priv->cma_dev = cma_dev;
270 id_priv->id.device = cma_dev->device;
271 list_add_tail(&id_priv->list, &cma_dev->id_list);
274 static inline void cma_deref_dev(struct cma_device *cma_dev)
276 if (atomic_dec_and_test(&cma_dev->refcount))
277 complete(&cma_dev->comp);
280 static void cma_detach_from_dev(struct rdma_id_private *id_priv)
282 list_del(&id_priv->list);
283 cma_deref_dev(id_priv->cma_dev);
284 id_priv->cma_dev = NULL;
287 static int cma_set_qkey(struct ib_device *device, u8 port_num,
288 enum rdma_port_space ps,
289 struct rdma_dev_addr *dev_addr, u32 *qkey)
291 struct ib_sa_mcmember_rec rec;
296 *qkey = RDMA_UDP_QKEY;
299 ib_addr_get_mgid(dev_addr, &rec.mgid);
300 ret = ib_sa_get_mcmember_rec(device, port_num, &rec.mgid, &rec);
301 *qkey = be32_to_cpu(rec.qkey);
309 static int cma_acquire_dev(struct rdma_id_private *id_priv)
311 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
312 struct cma_device *cma_dev;
316 switch (rdma_node_get_transport(dev_addr->dev_type)) {
317 case RDMA_TRANSPORT_IB:
318 ib_addr_get_sgid(dev_addr, &gid);
320 case RDMA_TRANSPORT_IWARP:
321 iw_addr_get_sgid(dev_addr, &gid);
327 list_for_each_entry(cma_dev, &dev_list, list) {
328 ret = ib_find_cached_gid(cma_dev->device, &gid,
329 &id_priv->id.port_num, NULL);
331 ret = cma_set_qkey(cma_dev->device,
332 id_priv->id.port_num,
333 id_priv->id.ps, dev_addr,
336 cma_attach_to_dev(id_priv, cma_dev);
343 static void cma_deref_id(struct rdma_id_private *id_priv)
345 if (atomic_dec_and_test(&id_priv->refcount))
346 complete(&id_priv->comp);
349 static void cma_release_remove(struct rdma_id_private *id_priv)
351 if (atomic_dec_and_test(&id_priv->dev_remove))
352 wake_up(&id_priv->wait_remove);
355 struct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler,
356 void *context, enum rdma_port_space ps)
358 struct rdma_id_private *id_priv;
360 id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
362 return ERR_PTR(-ENOMEM);
364 id_priv->state = CMA_IDLE;
365 id_priv->id.context = context;
366 id_priv->id.event_handler = event_handler;
368 spin_lock_init(&id_priv->lock);
369 init_completion(&id_priv->comp);
370 atomic_set(&id_priv->refcount, 1);
371 init_waitqueue_head(&id_priv->wait_remove);
372 atomic_set(&id_priv->dev_remove, 0);
373 INIT_LIST_HEAD(&id_priv->listen_list);
374 INIT_LIST_HEAD(&id_priv->mc_list);
375 get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
379 EXPORT_SYMBOL(rdma_create_id);
381 static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
383 struct ib_qp_attr qp_attr;
384 int qp_attr_mask, ret;
386 qp_attr.qp_state = IB_QPS_INIT;
387 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
391 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
395 qp_attr.qp_state = IB_QPS_RTR;
396 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
400 qp_attr.qp_state = IB_QPS_RTS;
402 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
407 static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
409 struct ib_qp_attr qp_attr;
410 int qp_attr_mask, ret;
412 qp_attr.qp_state = IB_QPS_INIT;
413 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
417 return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
420 int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
421 struct ib_qp_init_attr *qp_init_attr)
423 struct rdma_id_private *id_priv;
427 id_priv = container_of(id, struct rdma_id_private, id);
428 if (id->device != pd->device)
431 qp = ib_create_qp(pd, qp_init_attr);
435 if (cma_is_ud_ps(id_priv->id.ps))
436 ret = cma_init_ud_qp(id_priv, qp);
438 ret = cma_init_conn_qp(id_priv, qp);
443 id_priv->qp_num = qp->qp_num;
444 id_priv->srq = (qp->srq != NULL);
450 EXPORT_SYMBOL(rdma_create_qp);
452 void rdma_destroy_qp(struct rdma_cm_id *id)
454 ib_destroy_qp(id->qp);
456 EXPORT_SYMBOL(rdma_destroy_qp);
458 static int cma_modify_qp_rtr(struct rdma_cm_id *id)
460 struct ib_qp_attr qp_attr;
461 int qp_attr_mask, ret;
466 /* Need to update QP attributes from default values. */
467 qp_attr.qp_state = IB_QPS_INIT;
468 ret = rdma_init_qp_attr(id, &qp_attr, &qp_attr_mask);
472 ret = ib_modify_qp(id->qp, &qp_attr, qp_attr_mask);
476 qp_attr.qp_state = IB_QPS_RTR;
477 ret = rdma_init_qp_attr(id, &qp_attr, &qp_attr_mask);
481 return ib_modify_qp(id->qp, &qp_attr, qp_attr_mask);
484 static int cma_modify_qp_rts(struct rdma_cm_id *id)
486 struct ib_qp_attr qp_attr;
487 int qp_attr_mask, ret;
492 qp_attr.qp_state = IB_QPS_RTS;
493 ret = rdma_init_qp_attr(id, &qp_attr, &qp_attr_mask);
497 return ib_modify_qp(id->qp, &qp_attr, qp_attr_mask);
500 static int cma_modify_qp_err(struct rdma_cm_id *id)
502 struct ib_qp_attr qp_attr;
507 qp_attr.qp_state = IB_QPS_ERR;
508 return ib_modify_qp(id->qp, &qp_attr, IB_QP_STATE);
511 static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
512 struct ib_qp_attr *qp_attr, int *qp_attr_mask)
514 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
517 ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
518 ib_addr_get_pkey(dev_addr),
519 &qp_attr->pkey_index);
523 qp_attr->port_num = id_priv->id.port_num;
524 *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
526 if (cma_is_ud_ps(id_priv->id.ps)) {
527 qp_attr->qkey = id_priv->qkey;
528 *qp_attr_mask |= IB_QP_QKEY;
530 qp_attr->qp_access_flags = 0;
531 *qp_attr_mask |= IB_QP_ACCESS_FLAGS;
536 int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
539 struct rdma_id_private *id_priv;
542 id_priv = container_of(id, struct rdma_id_private, id);
543 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
544 case RDMA_TRANSPORT_IB:
545 if (!id_priv->cm_id.ib || cma_is_ud_ps(id_priv->id.ps))
546 ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
548 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
550 if (qp_attr->qp_state == IB_QPS_RTR)
551 qp_attr->rq_psn = id_priv->seq_num;
553 case RDMA_TRANSPORT_IWARP:
554 if (!id_priv->cm_id.iw) {
555 qp_attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE;
556 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
558 ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
568 EXPORT_SYMBOL(rdma_init_qp_attr);
570 static inline int cma_zero_addr(struct sockaddr *addr)
572 struct in6_addr *ip6;
574 if (addr->sa_family == AF_INET)
575 return ZERONET(((struct sockaddr_in *) addr)->sin_addr.s_addr);
577 ip6 = &((struct sockaddr_in6 *) addr)->sin6_addr;
578 return (ip6->s6_addr32[0] | ip6->s6_addr32[1] |
579 ip6->s6_addr32[2] | ip6->s6_addr32[3]) == 0;
583 static inline int cma_loopback_addr(struct sockaddr *addr)
585 return LOOPBACK(((struct sockaddr_in *) addr)->sin_addr.s_addr);
588 static inline int cma_any_addr(struct sockaddr *addr)
590 return cma_zero_addr(addr) || cma_loopback_addr(addr);
593 static inline __be16 cma_port(struct sockaddr *addr)
595 if (addr->sa_family == AF_INET)
596 return ((struct sockaddr_in *) addr)->sin_port;
598 return ((struct sockaddr_in6 *) addr)->sin6_port;
601 static inline int cma_any_port(struct sockaddr *addr)
603 return !cma_port(addr);
606 static int cma_get_net_info(void *hdr, enum rdma_port_space ps,
607 u8 *ip_ver, __u16 *port,
608 union cma_ip_addr **src, union cma_ip_addr **dst)
612 if (sdp_get_majv(((struct sdp_hh *) hdr)->sdp_version) !=
616 *ip_ver = sdp_get_ip_ver(hdr);
617 *port = ((struct sdp_hh *) hdr)->port;
618 *src = &((struct sdp_hh *) hdr)->src_addr;
619 *dst = &((struct sdp_hh *) hdr)->dst_addr;
622 if (((struct cma_hdr *) hdr)->cma_version != CMA_VERSION)
625 *ip_ver = cma_get_ip_ver(hdr);
626 *port = ((struct cma_hdr *) hdr)->port;
627 *src = &((struct cma_hdr *) hdr)->src_addr;
628 *dst = &((struct cma_hdr *) hdr)->dst_addr;
632 if (*ip_ver != 4 && *ip_ver != 6)
637 static void cma_save_net_info(struct rdma_addr *addr,
638 struct rdma_addr *listen_addr,
639 u8 ip_ver, __u16 port,
640 union cma_ip_addr *src, union cma_ip_addr *dst)
642 struct sockaddr_in *listen4, *ip4;
643 struct sockaddr_in6 *listen6, *ip6;
647 listen4 = (struct sockaddr_in *) &listen_addr->src_addr;
648 ip4 = (struct sockaddr_in *) &addr->src_addr;
649 ip4->sin_family = listen4->sin_family;
650 ip4->sin_addr.s_addr = dst->ip4.addr;
651 ip4->sin_port = listen4->sin_port;
653 ip4 = (struct sockaddr_in *) &addr->dst_addr;
654 ip4->sin_family = listen4->sin_family;
655 ip4->sin_addr.s_addr = src->ip4.addr;
656 ip4->sin_port = port;
659 listen6 = (struct sockaddr_in6 *) &listen_addr->src_addr;
660 ip6 = (struct sockaddr_in6 *) &addr->src_addr;
661 ip6->sin6_family = listen6->sin6_family;
662 ip6->sin6_addr = dst->ip6;
663 ip6->sin6_port = listen6->sin6_port;
665 ip6 = (struct sockaddr_in6 *) &addr->dst_addr;
666 ip6->sin6_family = listen6->sin6_family;
667 ip6->sin6_addr = src->ip6;
668 ip6->sin6_port = port;
675 static inline int cma_user_data_offset(enum rdma_port_space ps)
681 return sizeof(struct cma_hdr);
685 static void cma_cancel_route(struct rdma_id_private *id_priv)
687 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
688 case RDMA_TRANSPORT_IB:
690 ib_sa_cancel_query(id_priv->query_id, id_priv->query);
697 static inline int cma_internal_listen(struct rdma_id_private *id_priv)
699 return (id_priv->state == CMA_LISTEN) && id_priv->cma_dev &&
700 cma_any_addr(&id_priv->id.route.addr.src_addr);
703 static void cma_destroy_listen(struct rdma_id_private *id_priv)
705 cma_exch(id_priv, CMA_DESTROYING);
707 if (id_priv->cma_dev) {
708 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
709 case RDMA_TRANSPORT_IB:
710 if (id_priv->cm_id.ib && !IS_ERR(id_priv->cm_id.ib))
711 ib_destroy_cm_id(id_priv->cm_id.ib);
713 case RDMA_TRANSPORT_IWARP:
714 if (id_priv->cm_id.iw && !IS_ERR(id_priv->cm_id.iw))
715 iw_destroy_cm_id(id_priv->cm_id.iw);
720 cma_detach_from_dev(id_priv);
722 list_del(&id_priv->listen_list);
724 cma_deref_id(id_priv);
725 wait_for_completion(&id_priv->comp);
730 static void cma_cancel_listens(struct rdma_id_private *id_priv)
732 struct rdma_id_private *dev_id_priv;
735 list_del(&id_priv->list);
737 while (!list_empty(&id_priv->listen_list)) {
738 dev_id_priv = list_entry(id_priv->listen_list.next,
739 struct rdma_id_private, listen_list);
740 cma_destroy_listen(dev_id_priv);
745 static void cma_cancel_operation(struct rdma_id_private *id_priv,
746 enum cma_state state)
750 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
752 case CMA_ROUTE_QUERY:
753 cma_cancel_route(id_priv);
756 if (cma_any_addr(&id_priv->id.route.addr.src_addr) &&
758 cma_cancel_listens(id_priv);
765 static void cma_release_port(struct rdma_id_private *id_priv)
767 struct rdma_bind_list *bind_list = id_priv->bind_list;
773 hlist_del(&id_priv->node);
774 if (hlist_empty(&bind_list->owners)) {
775 idr_remove(bind_list->ps, bind_list->port);
781 static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
783 struct cma_multicast *mc;
785 while (!list_empty(&id_priv->mc_list)) {
786 mc = container_of(id_priv->mc_list.next,
787 struct cma_multicast, list);
789 ib_sa_free_multicast(mc->multicast.ib);
794 void rdma_destroy_id(struct rdma_cm_id *id)
796 struct rdma_id_private *id_priv;
797 enum cma_state state;
799 id_priv = container_of(id, struct rdma_id_private, id);
800 state = cma_exch(id_priv, CMA_DESTROYING);
801 cma_cancel_operation(id_priv, state);
804 if (id_priv->cma_dev) {
806 switch (rdma_node_get_transport(id->device->node_type)) {
807 case RDMA_TRANSPORT_IB:
808 if (id_priv->cm_id.ib && !IS_ERR(id_priv->cm_id.ib))
809 ib_destroy_cm_id(id_priv->cm_id.ib);
811 case RDMA_TRANSPORT_IWARP:
812 if (id_priv->cm_id.iw && !IS_ERR(id_priv->cm_id.iw))
813 iw_destroy_cm_id(id_priv->cm_id.iw);
818 cma_leave_mc_groups(id_priv);
820 cma_detach_from_dev(id_priv);
824 cma_release_port(id_priv);
825 cma_deref_id(id_priv);
826 wait_for_completion(&id_priv->comp);
828 kfree(id_priv->id.route.path_rec);
831 EXPORT_SYMBOL(rdma_destroy_id);
833 static int cma_rep_recv(struct rdma_id_private *id_priv)
837 ret = cma_modify_qp_rtr(&id_priv->id);
841 ret = cma_modify_qp_rts(&id_priv->id);
845 ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
851 cma_modify_qp_err(&id_priv->id);
852 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
857 static int cma_verify_rep(struct rdma_id_private *id_priv, void *data)
859 if (id_priv->id.ps == RDMA_PS_SDP &&
860 sdp_get_majv(((struct sdp_hah *) data)->sdp_version) !=
867 static void cma_set_rep_event_data(struct rdma_cm_event *event,
868 struct ib_cm_rep_event_param *rep_data,
871 event->param.conn.private_data = private_data;
872 event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
873 event->param.conn.responder_resources = rep_data->responder_resources;
874 event->param.conn.initiator_depth = rep_data->initiator_depth;
875 event->param.conn.flow_control = rep_data->flow_control;
876 event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
877 event->param.conn.srq = rep_data->srq;
878 event->param.conn.qp_num = rep_data->remote_qpn;
881 static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
883 struct rdma_id_private *id_priv = cm_id->context;
884 struct rdma_cm_event event;
887 atomic_inc(&id_priv->dev_remove);
888 if (!cma_comp(id_priv, CMA_CONNECT))
891 memset(&event, 0, sizeof event);
892 switch (ib_event->event) {
893 case IB_CM_REQ_ERROR:
894 case IB_CM_REP_ERROR:
895 event.event = RDMA_CM_EVENT_UNREACHABLE;
896 event.status = -ETIMEDOUT;
898 case IB_CM_REP_RECEIVED:
899 event.status = cma_verify_rep(id_priv, ib_event->private_data);
901 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
902 else if (id_priv->id.qp && id_priv->id.ps != RDMA_PS_SDP) {
903 event.status = cma_rep_recv(id_priv);
904 event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
905 RDMA_CM_EVENT_ESTABLISHED;
907 event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
908 cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
909 ib_event->private_data);
911 case IB_CM_RTU_RECEIVED:
912 case IB_CM_USER_ESTABLISHED:
913 event.event = RDMA_CM_EVENT_ESTABLISHED;
915 case IB_CM_DREQ_ERROR:
916 event.status = -ETIMEDOUT; /* fall through */
917 case IB_CM_DREQ_RECEIVED:
918 case IB_CM_DREP_RECEIVED:
919 if (!cma_comp_exch(id_priv, CMA_CONNECT, CMA_DISCONNECT))
921 event.event = RDMA_CM_EVENT_DISCONNECTED;
923 case IB_CM_TIMEWAIT_EXIT:
924 case IB_CM_MRA_RECEIVED:
927 case IB_CM_REJ_RECEIVED:
928 cma_modify_qp_err(&id_priv->id);
929 event.status = ib_event->param.rej_rcvd.reason;
930 event.event = RDMA_CM_EVENT_REJECTED;
931 event.param.conn.private_data = ib_event->private_data;
932 event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
935 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d",
940 ret = id_priv->id.event_handler(&id_priv->id, &event);
942 /* Destroy the CM ID by returning a non-zero value. */
943 id_priv->cm_id.ib = NULL;
944 cma_exch(id_priv, CMA_DESTROYING);
945 cma_release_remove(id_priv);
946 rdma_destroy_id(&id_priv->id);
950 cma_release_remove(id_priv);
954 static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
955 struct ib_cm_event *ib_event)
957 struct rdma_id_private *id_priv;
958 struct rdma_cm_id *id;
959 struct rdma_route *rt;
960 union cma_ip_addr *src, *dst;
964 if (cma_get_net_info(ib_event->private_data, listen_id->ps,
965 &ip_ver, &port, &src, &dst))
968 id = rdma_create_id(listen_id->event_handler, listen_id->context,
973 cma_save_net_info(&id->route.addr, &listen_id->route.addr,
974 ip_ver, port, src, dst);
977 rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
978 rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
983 rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
984 if (rt->num_paths == 2)
985 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
987 ib_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
988 ib_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
989 ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
990 rt->addr.dev_addr.dev_type = RDMA_NODE_IB_CA;
992 id_priv = container_of(id, struct rdma_id_private, id);
993 id_priv->state = CMA_CONNECT;
1002 static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
1003 struct ib_cm_event *ib_event)
1005 struct rdma_id_private *id_priv;
1006 struct rdma_cm_id *id;
1007 union cma_ip_addr *src, *dst;
1012 id = rdma_create_id(listen_id->event_handler, listen_id->context,
1018 if (cma_get_net_info(ib_event->private_data, listen_id->ps,
1019 &ip_ver, &port, &src, &dst))
1022 cma_save_net_info(&id->route.addr, &listen_id->route.addr,
1023 ip_ver, port, src, dst);
1025 ret = rdma_translate_ip(&id->route.addr.src_addr,
1026 &id->route.addr.dev_addr);
1030 id_priv = container_of(id, struct rdma_id_private, id);
1031 id_priv->state = CMA_CONNECT;
1034 rdma_destroy_id(id);
1038 static void cma_set_req_event_data(struct rdma_cm_event *event,
1039 struct ib_cm_req_event_param *req_data,
1040 void *private_data, int offset)
1042 event->param.conn.private_data = private_data + offset;
1043 event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
1044 event->param.conn.responder_resources = req_data->responder_resources;
1045 event->param.conn.initiator_depth = req_data->initiator_depth;
1046 event->param.conn.flow_control = req_data->flow_control;
1047 event->param.conn.retry_count = req_data->retry_count;
1048 event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
1049 event->param.conn.srq = req_data->srq;
1050 event->param.conn.qp_num = req_data->remote_qpn;
1053 static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1055 struct rdma_id_private *listen_id, *conn_id;
1056 struct rdma_cm_event event;
1059 listen_id = cm_id->context;
1060 atomic_inc(&listen_id->dev_remove);
1061 if (!cma_comp(listen_id, CMA_LISTEN)) {
1062 ret = -ECONNABORTED;
1066 memset(&event, 0, sizeof event);
1067 offset = cma_user_data_offset(listen_id->id.ps);
1068 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1069 if (cma_is_ud_ps(listen_id->id.ps)) {
1070 conn_id = cma_new_udp_id(&listen_id->id, ib_event);
1071 event.param.ud.private_data = ib_event->private_data + offset;
1072 event.param.ud.private_data_len =
1073 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
1075 conn_id = cma_new_conn_id(&listen_id->id, ib_event);
1076 cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
1077 ib_event->private_data, offset);
1084 atomic_inc(&conn_id->dev_remove);
1086 ret = cma_acquire_dev(conn_id);
1087 mutex_unlock(&lock);
1089 goto release_conn_id;
1091 conn_id->cm_id.ib = cm_id;
1092 cm_id->context = conn_id;
1093 cm_id->cm_handler = cma_ib_handler;
1095 ret = conn_id->id.event_handler(&conn_id->id, &event);
1099 /* Destroy the CM ID by returning a non-zero value. */
1100 conn_id->cm_id.ib = NULL;
1103 cma_exch(conn_id, CMA_DESTROYING);
1104 cma_release_remove(conn_id);
1105 rdma_destroy_id(&conn_id->id);
1108 cma_release_remove(listen_id);
1112 static __be64 cma_get_service_id(enum rdma_port_space ps, struct sockaddr *addr)
1114 return cpu_to_be64(((u64)ps << 16) + be16_to_cpu(cma_port(addr)));
1117 static void cma_set_compare_data(enum rdma_port_space ps, struct sockaddr *addr,
1118 struct ib_cm_compare_data *compare)
1120 struct cma_hdr *cma_data, *cma_mask;
1121 struct sdp_hh *sdp_data, *sdp_mask;
1123 struct in6_addr ip6_addr;
1125 memset(compare, 0, sizeof *compare);
1126 cma_data = (void *) compare->data;
1127 cma_mask = (void *) compare->mask;
1128 sdp_data = (void *) compare->data;
1129 sdp_mask = (void *) compare->mask;
1131 switch (addr->sa_family) {
1133 ip4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1134 if (ps == RDMA_PS_SDP) {
1135 sdp_set_ip_ver(sdp_data, 4);
1136 sdp_set_ip_ver(sdp_mask, 0xF);
1137 sdp_data->dst_addr.ip4.addr = ip4_addr;
1138 sdp_mask->dst_addr.ip4.addr = ~0;
1140 cma_set_ip_ver(cma_data, 4);
1141 cma_set_ip_ver(cma_mask, 0xF);
1142 cma_data->dst_addr.ip4.addr = ip4_addr;
1143 cma_mask->dst_addr.ip4.addr = ~0;
1147 ip6_addr = ((struct sockaddr_in6 *) addr)->sin6_addr;
1148 if (ps == RDMA_PS_SDP) {
1149 sdp_set_ip_ver(sdp_data, 6);
1150 sdp_set_ip_ver(sdp_mask, 0xF);
1151 sdp_data->dst_addr.ip6 = ip6_addr;
1152 memset(&sdp_mask->dst_addr.ip6, 0xFF,
1153 sizeof sdp_mask->dst_addr.ip6);
1155 cma_set_ip_ver(cma_data, 6);
1156 cma_set_ip_ver(cma_mask, 0xF);
1157 cma_data->dst_addr.ip6 = ip6_addr;
1158 memset(&cma_mask->dst_addr.ip6, 0xFF,
1159 sizeof cma_mask->dst_addr.ip6);
1167 static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
1169 struct rdma_id_private *id_priv = iw_id->context;
1170 struct rdma_cm_event event;
1171 struct sockaddr_in *sin;
1174 memset(&event, 0, sizeof event);
1175 atomic_inc(&id_priv->dev_remove);
1177 switch (iw_event->event) {
1178 case IW_CM_EVENT_CLOSE:
1179 event.event = RDMA_CM_EVENT_DISCONNECTED;
1181 case IW_CM_EVENT_CONNECT_REPLY:
1182 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1183 *sin = iw_event->local_addr;
1184 sin = (struct sockaddr_in *) &id_priv->id.route.addr.dst_addr;
1185 *sin = iw_event->remote_addr;
1186 switch (iw_event->status) {
1188 event.event = RDMA_CM_EVENT_ESTABLISHED;
1192 event.event = RDMA_CM_EVENT_REJECTED;
1195 event.event = RDMA_CM_EVENT_UNREACHABLE;
1198 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
1202 case IW_CM_EVENT_ESTABLISHED:
1203 event.event = RDMA_CM_EVENT_ESTABLISHED;
1209 event.status = iw_event->status;
1210 event.param.conn.private_data = iw_event->private_data;
1211 event.param.conn.private_data_len = iw_event->private_data_len;
1212 ret = id_priv->id.event_handler(&id_priv->id, &event);
1214 /* Destroy the CM ID by returning a non-zero value. */
1215 id_priv->cm_id.iw = NULL;
1216 cma_exch(id_priv, CMA_DESTROYING);
1217 cma_release_remove(id_priv);
1218 rdma_destroy_id(&id_priv->id);
1222 cma_release_remove(id_priv);
1226 static int iw_conn_req_handler(struct iw_cm_id *cm_id,
1227 struct iw_cm_event *iw_event)
1229 struct rdma_cm_id *new_cm_id;
1230 struct rdma_id_private *listen_id, *conn_id;
1231 struct sockaddr_in *sin;
1232 struct net_device *dev = NULL;
1233 struct rdma_cm_event event;
1236 listen_id = cm_id->context;
1237 atomic_inc(&listen_id->dev_remove);
1238 if (!cma_comp(listen_id, CMA_LISTEN)) {
1239 ret = -ECONNABORTED;
1243 /* Create a new RDMA id for the new IW CM ID */
1244 new_cm_id = rdma_create_id(listen_id->id.event_handler,
1245 listen_id->id.context,
1251 conn_id = container_of(new_cm_id, struct rdma_id_private, id);
1252 atomic_inc(&conn_id->dev_remove);
1253 conn_id->state = CMA_CONNECT;
1255 dev = ip_dev_find(iw_event->local_addr.sin_addr.s_addr);
1257 ret = -EADDRNOTAVAIL;
1258 cma_release_remove(conn_id);
1259 rdma_destroy_id(new_cm_id);
1262 ret = rdma_copy_addr(&conn_id->id.route.addr.dev_addr, dev, NULL);
1264 cma_release_remove(conn_id);
1265 rdma_destroy_id(new_cm_id);
1270 ret = cma_acquire_dev(conn_id);
1271 mutex_unlock(&lock);
1273 cma_release_remove(conn_id);
1274 rdma_destroy_id(new_cm_id);
1278 conn_id->cm_id.iw = cm_id;
1279 cm_id->context = conn_id;
1280 cm_id->cm_handler = cma_iw_handler;
1282 sin = (struct sockaddr_in *) &new_cm_id->route.addr.src_addr;
1283 *sin = iw_event->local_addr;
1284 sin = (struct sockaddr_in *) &new_cm_id->route.addr.dst_addr;
1285 *sin = iw_event->remote_addr;
1287 memset(&event, 0, sizeof event);
1288 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1289 event.param.conn.private_data = iw_event->private_data;
1290 event.param.conn.private_data_len = iw_event->private_data_len;
1291 ret = conn_id->id.event_handler(&conn_id->id, &event);
1293 /* User wants to destroy the CM ID */
1294 conn_id->cm_id.iw = NULL;
1295 cma_exch(conn_id, CMA_DESTROYING);
1296 cma_release_remove(conn_id);
1297 rdma_destroy_id(&conn_id->id);
1303 cma_release_remove(listen_id);
1307 static int cma_ib_listen(struct rdma_id_private *id_priv)
1309 struct ib_cm_compare_data compare_data;
1310 struct sockaddr *addr;
1314 id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_req_handler,
1316 if (IS_ERR(id_priv->cm_id.ib))
1317 return PTR_ERR(id_priv->cm_id.ib);
1319 addr = &id_priv->id.route.addr.src_addr;
1320 svc_id = cma_get_service_id(id_priv->id.ps, addr);
1321 if (cma_any_addr(addr))
1322 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, NULL);
1324 cma_set_compare_data(id_priv->id.ps, addr, &compare_data);
1325 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, &compare_data);
1329 ib_destroy_cm_id(id_priv->cm_id.ib);
1330 id_priv->cm_id.ib = NULL;
1336 static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
1339 struct sockaddr_in *sin;
1341 id_priv->cm_id.iw = iw_create_cm_id(id_priv->id.device,
1342 iw_conn_req_handler,
1344 if (IS_ERR(id_priv->cm_id.iw))
1345 return PTR_ERR(id_priv->cm_id.iw);
1347 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1348 id_priv->cm_id.iw->local_addr = *sin;
1350 ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
1353 iw_destroy_cm_id(id_priv->cm_id.iw);
1354 id_priv->cm_id.iw = NULL;
1360 static int cma_listen_handler(struct rdma_cm_id *id,
1361 struct rdma_cm_event *event)
1363 struct rdma_id_private *id_priv = id->context;
1365 id->context = id_priv->id.context;
1366 id->event_handler = id_priv->id.event_handler;
1367 return id_priv->id.event_handler(id, event);
1370 static void cma_listen_on_dev(struct rdma_id_private *id_priv,
1371 struct cma_device *cma_dev)
1373 struct rdma_id_private *dev_id_priv;
1374 struct rdma_cm_id *id;
1377 id = rdma_create_id(cma_listen_handler, id_priv, id_priv->id.ps);
1381 dev_id_priv = container_of(id, struct rdma_id_private, id);
1383 dev_id_priv->state = CMA_ADDR_BOUND;
1384 memcpy(&id->route.addr.src_addr, &id_priv->id.route.addr.src_addr,
1385 ip_addr_size(&id_priv->id.route.addr.src_addr));
1387 cma_attach_to_dev(dev_id_priv, cma_dev);
1388 list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
1390 ret = rdma_listen(id, id_priv->backlog);
1396 cma_destroy_listen(dev_id_priv);
1399 static void cma_listen_on_all(struct rdma_id_private *id_priv)
1401 struct cma_device *cma_dev;
1404 list_add_tail(&id_priv->list, &listen_any_list);
1405 list_for_each_entry(cma_dev, &dev_list, list)
1406 cma_listen_on_dev(id_priv, cma_dev);
1407 mutex_unlock(&lock);
1410 static int cma_bind_any(struct rdma_cm_id *id, sa_family_t af)
1412 struct sockaddr_in addr_in;
1414 memset(&addr_in, 0, sizeof addr_in);
1415 addr_in.sin_family = af;
1416 return rdma_bind_addr(id, (struct sockaddr *) &addr_in);
1419 int rdma_listen(struct rdma_cm_id *id, int backlog)
1421 struct rdma_id_private *id_priv;
1424 id_priv = container_of(id, struct rdma_id_private, id);
1425 if (id_priv->state == CMA_IDLE) {
1426 ret = cma_bind_any(id, AF_INET);
1431 if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_LISTEN))
1434 id_priv->backlog = backlog;
1436 switch (rdma_node_get_transport(id->device->node_type)) {
1437 case RDMA_TRANSPORT_IB:
1438 ret = cma_ib_listen(id_priv);
1442 case RDMA_TRANSPORT_IWARP:
1443 ret = cma_iw_listen(id_priv, backlog);
1452 cma_listen_on_all(id_priv);
1456 id_priv->backlog = 0;
1457 cma_comp_exch(id_priv, CMA_LISTEN, CMA_ADDR_BOUND);
1460 EXPORT_SYMBOL(rdma_listen);
1462 static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
1465 struct cma_work *work = context;
1466 struct rdma_route *route;
1468 route = &work->id->id.route;
1471 route->num_paths = 1;
1472 *route->path_rec = *path_rec;
1474 work->old_state = CMA_ROUTE_QUERY;
1475 work->new_state = CMA_ADDR_RESOLVED;
1476 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
1477 work->event.status = status;
1480 queue_work(cma_wq, &work->work);
1483 static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
1484 struct cma_work *work)
1486 struct rdma_dev_addr *addr = &id_priv->id.route.addr.dev_addr;
1487 struct ib_sa_path_rec path_rec;
1489 memset(&path_rec, 0, sizeof path_rec);
1490 ib_addr_get_sgid(addr, &path_rec.sgid);
1491 ib_addr_get_dgid(addr, &path_rec.dgid);
1492 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(addr));
1493 path_rec.numb_path = 1;
1494 path_rec.reversible = 1;
1496 id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
1497 id_priv->id.port_num, &path_rec,
1498 IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
1499 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
1500 IB_SA_PATH_REC_REVERSIBLE,
1501 timeout_ms, GFP_KERNEL,
1502 cma_query_handler, work, &id_priv->query);
1504 return (id_priv->query_id < 0) ? id_priv->query_id : 0;
1507 static void cma_work_handler(struct work_struct *_work)
1509 struct cma_work *work = container_of(_work, struct cma_work, work);
1510 struct rdma_id_private *id_priv = work->id;
1513 atomic_inc(&id_priv->dev_remove);
1514 if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
1517 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
1518 cma_exch(id_priv, CMA_DESTROYING);
1522 cma_release_remove(id_priv);
1523 cma_deref_id(id_priv);
1525 rdma_destroy_id(&id_priv->id);
1529 static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
1531 struct rdma_route *route = &id_priv->id.route;
1532 struct cma_work *work;
1535 work = kzalloc(sizeof *work, GFP_KERNEL);
1540 INIT_WORK(&work->work, cma_work_handler);
1541 work->old_state = CMA_ROUTE_QUERY;
1542 work->new_state = CMA_ROUTE_RESOLVED;
1543 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1545 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
1546 if (!route->path_rec) {
1551 ret = cma_query_ib_route(id_priv, timeout_ms, work);
1557 kfree(route->path_rec);
1558 route->path_rec = NULL;
1564 int rdma_set_ib_paths(struct rdma_cm_id *id,
1565 struct ib_sa_path_rec *path_rec, int num_paths)
1567 struct rdma_id_private *id_priv;
1570 id_priv = container_of(id, struct rdma_id_private, id);
1571 if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ROUTE_RESOLVED))
1574 id->route.path_rec = kmalloc(sizeof *path_rec * num_paths, GFP_KERNEL);
1575 if (!id->route.path_rec) {
1580 memcpy(id->route.path_rec, path_rec, sizeof *path_rec * num_paths);
1583 cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_ADDR_RESOLVED);
1586 EXPORT_SYMBOL(rdma_set_ib_paths);
1588 static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
1590 struct cma_work *work;
1592 work = kzalloc(sizeof *work, GFP_KERNEL);
1597 INIT_WORK(&work->work, cma_work_handler);
1598 work->old_state = CMA_ROUTE_QUERY;
1599 work->new_state = CMA_ROUTE_RESOLVED;
1600 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1601 queue_work(cma_wq, &work->work);
1605 int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
1607 struct rdma_id_private *id_priv;
1610 id_priv = container_of(id, struct rdma_id_private, id);
1611 if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ROUTE_QUERY))
1614 atomic_inc(&id_priv->refcount);
1615 switch (rdma_node_get_transport(id->device->node_type)) {
1616 case RDMA_TRANSPORT_IB:
1617 ret = cma_resolve_ib_route(id_priv, timeout_ms);
1619 case RDMA_TRANSPORT_IWARP:
1620 ret = cma_resolve_iw_route(id_priv, timeout_ms);
1631 cma_comp_exch(id_priv, CMA_ROUTE_QUERY, CMA_ADDR_RESOLVED);
1632 cma_deref_id(id_priv);
1635 EXPORT_SYMBOL(rdma_resolve_route);
1637 static int cma_bind_loopback(struct rdma_id_private *id_priv)
1639 struct cma_device *cma_dev;
1640 struct ib_port_attr port_attr;
1647 if (list_empty(&dev_list)) {
1651 list_for_each_entry(cma_dev, &dev_list, list)
1652 for (p = 1; p <= cma_dev->device->phys_port_cnt; ++p)
1653 if (!ib_query_port(cma_dev->device, p, &port_attr) &&
1654 port_attr.state == IB_PORT_ACTIVE)
1658 cma_dev = list_entry(dev_list.next, struct cma_device, list);
1661 ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid);
1665 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
1669 ib_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
1670 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
1671 id_priv->id.port_num = p;
1672 cma_attach_to_dev(id_priv, cma_dev);
1674 mutex_unlock(&lock);
1678 static void addr_handler(int status, struct sockaddr *src_addr,
1679 struct rdma_dev_addr *dev_addr, void *context)
1681 struct rdma_id_private *id_priv = context;
1682 struct rdma_cm_event event;
1684 memset(&event, 0, sizeof event);
1685 atomic_inc(&id_priv->dev_remove);
1688 * Grab mutex to block rdma_destroy_id() from removing the device while
1689 * we're trying to acquire it.
1692 if (!cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_RESOLVED)) {
1693 mutex_unlock(&lock);
1697 if (!status && !id_priv->cma_dev)
1698 status = cma_acquire_dev(id_priv);
1699 mutex_unlock(&lock);
1702 if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ADDR_BOUND))
1704 event.event = RDMA_CM_EVENT_ADDR_ERROR;
1705 event.status = status;
1707 memcpy(&id_priv->id.route.addr.src_addr, src_addr,
1708 ip_addr_size(src_addr));
1709 event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
1712 if (id_priv->id.event_handler(&id_priv->id, &event)) {
1713 cma_exch(id_priv, CMA_DESTROYING);
1714 cma_release_remove(id_priv);
1715 cma_deref_id(id_priv);
1716 rdma_destroy_id(&id_priv->id);
1720 cma_release_remove(id_priv);
1721 cma_deref_id(id_priv);
1724 static int cma_resolve_loopback(struct rdma_id_private *id_priv)
1726 struct cma_work *work;
1727 struct sockaddr_in *src_in, *dst_in;
1731 work = kzalloc(sizeof *work, GFP_KERNEL);
1735 if (!id_priv->cma_dev) {
1736 ret = cma_bind_loopback(id_priv);
1741 ib_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
1742 ib_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
1744 if (cma_zero_addr(&id_priv->id.route.addr.src_addr)) {
1745 src_in = (struct sockaddr_in *)&id_priv->id.route.addr.src_addr;
1746 dst_in = (struct sockaddr_in *)&id_priv->id.route.addr.dst_addr;
1747 src_in->sin_family = dst_in->sin_family;
1748 src_in->sin_addr.s_addr = dst_in->sin_addr.s_addr;
1752 INIT_WORK(&work->work, cma_work_handler);
1753 work->old_state = CMA_ADDR_QUERY;
1754 work->new_state = CMA_ADDR_RESOLVED;
1755 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
1756 queue_work(cma_wq, &work->work);
1763 static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
1764 struct sockaddr *dst_addr)
1766 if (src_addr && src_addr->sa_family)
1767 return rdma_bind_addr(id, src_addr);
1769 return cma_bind_any(id, dst_addr->sa_family);
1772 int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
1773 struct sockaddr *dst_addr, int timeout_ms)
1775 struct rdma_id_private *id_priv;
1778 id_priv = container_of(id, struct rdma_id_private, id);
1779 if (id_priv->state == CMA_IDLE) {
1780 ret = cma_bind_addr(id, src_addr, dst_addr);
1785 if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_ADDR_QUERY))
1788 atomic_inc(&id_priv->refcount);
1789 memcpy(&id->route.addr.dst_addr, dst_addr, ip_addr_size(dst_addr));
1790 if (cma_any_addr(dst_addr))
1791 ret = cma_resolve_loopback(id_priv);
1793 ret = rdma_resolve_ip(&addr_client, &id->route.addr.src_addr,
1794 dst_addr, &id->route.addr.dev_addr,
1795 timeout_ms, addr_handler, id_priv);
1801 cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_BOUND);
1802 cma_deref_id(id_priv);
1805 EXPORT_SYMBOL(rdma_resolve_addr);
1807 static void cma_bind_port(struct rdma_bind_list *bind_list,
1808 struct rdma_id_private *id_priv)
1810 struct sockaddr_in *sin;
1812 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1813 sin->sin_port = htons(bind_list->port);
1814 id_priv->bind_list = bind_list;
1815 hlist_add_head(&id_priv->node, &bind_list->owners);
1818 static int cma_alloc_port(struct idr *ps, struct rdma_id_private *id_priv,
1819 unsigned short snum)
1821 struct rdma_bind_list *bind_list;
1824 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
1829 ret = idr_get_new_above(ps, bind_list, snum, &port);
1830 } while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL));
1836 ret = -EADDRNOTAVAIL;
1841 bind_list->port = (unsigned short) port;
1842 cma_bind_port(bind_list, id_priv);
1845 idr_remove(ps, port);
1851 static int cma_alloc_any_port(struct idr *ps, struct rdma_id_private *id_priv)
1853 struct rdma_bind_list *bind_list;
1856 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
1862 ret = idr_get_new_above(ps, bind_list, next_port, &port);
1863 } while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL));
1868 if (port > sysctl_local_port_range[1]) {
1869 if (next_port != sysctl_local_port_range[0]) {
1870 idr_remove(ps, port);
1871 next_port = sysctl_local_port_range[0];
1874 ret = -EADDRNOTAVAIL;
1878 if (port == sysctl_local_port_range[1])
1879 next_port = sysctl_local_port_range[0];
1881 next_port = port + 1;
1884 bind_list->port = (unsigned short) port;
1885 cma_bind_port(bind_list, id_priv);
1888 idr_remove(ps, port);
1894 static int cma_use_port(struct idr *ps, struct rdma_id_private *id_priv)
1896 struct rdma_id_private *cur_id;
1897 struct sockaddr_in *sin, *cur_sin;
1898 struct rdma_bind_list *bind_list;
1899 struct hlist_node *node;
1900 unsigned short snum;
1902 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1903 snum = ntohs(sin->sin_port);
1904 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
1907 bind_list = idr_find(ps, snum);
1909 return cma_alloc_port(ps, id_priv, snum);
1912 * We don't support binding to any address if anyone is bound to
1913 * a specific address on the same port.
1915 if (cma_any_addr(&id_priv->id.route.addr.src_addr))
1916 return -EADDRNOTAVAIL;
1918 hlist_for_each_entry(cur_id, node, &bind_list->owners, node) {
1919 if (cma_any_addr(&cur_id->id.route.addr.src_addr))
1920 return -EADDRNOTAVAIL;
1922 cur_sin = (struct sockaddr_in *) &cur_id->id.route.addr.src_addr;
1923 if (sin->sin_addr.s_addr == cur_sin->sin_addr.s_addr)
1927 cma_bind_port(bind_list, id_priv);
1931 static int cma_get_port(struct rdma_id_private *id_priv)
1936 switch (id_priv->id.ps) {
1950 return -EPROTONOSUPPORT;
1954 if (cma_any_port(&id_priv->id.route.addr.src_addr))
1955 ret = cma_alloc_any_port(ps, id_priv);
1957 ret = cma_use_port(ps, id_priv);
1958 mutex_unlock(&lock);
1963 int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
1965 struct rdma_id_private *id_priv;
1968 if (addr->sa_family != AF_INET)
1969 return -EAFNOSUPPORT;
1971 id_priv = container_of(id, struct rdma_id_private, id);
1972 if (!cma_comp_exch(id_priv, CMA_IDLE, CMA_ADDR_BOUND))
1975 if (!cma_any_addr(addr)) {
1976 ret = rdma_translate_ip(addr, &id->route.addr.dev_addr);
1981 ret = cma_acquire_dev(id_priv);
1982 mutex_unlock(&lock);
1987 memcpy(&id->route.addr.src_addr, addr, ip_addr_size(addr));
1988 ret = cma_get_port(id_priv);
1994 if (!cma_any_addr(addr)) {
1996 cma_detach_from_dev(id_priv);
1997 mutex_unlock(&lock);
2000 cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_IDLE);
2003 EXPORT_SYMBOL(rdma_bind_addr);
2005 static int cma_format_hdr(void *hdr, enum rdma_port_space ps,
2006 struct rdma_route *route)
2008 struct sockaddr_in *src4, *dst4;
2009 struct cma_hdr *cma_hdr;
2010 struct sdp_hh *sdp_hdr;
2012 src4 = (struct sockaddr_in *) &route->addr.src_addr;
2013 dst4 = (struct sockaddr_in *) &route->addr.dst_addr;
2018 if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION)
2020 sdp_set_ip_ver(sdp_hdr, 4);
2021 sdp_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
2022 sdp_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
2023 sdp_hdr->port = src4->sin_port;
2027 cma_hdr->cma_version = CMA_VERSION;
2028 cma_set_ip_ver(cma_hdr, 4);
2029 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
2030 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
2031 cma_hdr->port = src4->sin_port;
2037 static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
2038 struct ib_cm_event *ib_event)
2040 struct rdma_id_private *id_priv = cm_id->context;
2041 struct rdma_cm_event event;
2042 struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
2045 memset(&event, 0, sizeof event);
2046 atomic_inc(&id_priv->dev_remove);
2047 if (!cma_comp(id_priv, CMA_CONNECT))
2050 switch (ib_event->event) {
2051 case IB_CM_SIDR_REQ_ERROR:
2052 event.event = RDMA_CM_EVENT_UNREACHABLE;
2053 event.status = -ETIMEDOUT;
2055 case IB_CM_SIDR_REP_RECEIVED:
2056 event.param.ud.private_data = ib_event->private_data;
2057 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
2058 if (rep->status != IB_SIDR_SUCCESS) {
2059 event.event = RDMA_CM_EVENT_UNREACHABLE;
2060 event.status = ib_event->param.sidr_rep_rcvd.status;
2063 if (id_priv->qkey != rep->qkey) {
2064 event.event = RDMA_CM_EVENT_UNREACHABLE;
2065 event.status = -EINVAL;
2068 ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
2069 id_priv->id.route.path_rec,
2070 &event.param.ud.ah_attr);
2071 event.param.ud.qp_num = rep->qpn;
2072 event.param.ud.qkey = rep->qkey;
2073 event.event = RDMA_CM_EVENT_ESTABLISHED;
2077 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d",
2082 ret = id_priv->id.event_handler(&id_priv->id, &event);
2084 /* Destroy the CM ID by returning a non-zero value. */
2085 id_priv->cm_id.ib = NULL;
2086 cma_exch(id_priv, CMA_DESTROYING);
2087 cma_release_remove(id_priv);
2088 rdma_destroy_id(&id_priv->id);
2092 cma_release_remove(id_priv);
2096 static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
2097 struct rdma_conn_param *conn_param)
2099 struct ib_cm_sidr_req_param req;
2100 struct rdma_route *route;
2103 req.private_data_len = sizeof(struct cma_hdr) +
2104 conn_param->private_data_len;
2105 req.private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2106 if (!req.private_data)
2109 if (conn_param->private_data && conn_param->private_data_len)
2110 memcpy((void *) req.private_data + sizeof(struct cma_hdr),
2111 conn_param->private_data, conn_param->private_data_len);
2113 route = &id_priv->id.route;
2114 ret = cma_format_hdr((void *) req.private_data, id_priv->id.ps, route);
2118 id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device,
2119 cma_sidr_rep_handler, id_priv);
2120 if (IS_ERR(id_priv->cm_id.ib)) {
2121 ret = PTR_ERR(id_priv->cm_id.ib);
2125 req.path = route->path_rec;
2126 req.service_id = cma_get_service_id(id_priv->id.ps,
2127 &route->addr.dst_addr);
2128 req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
2129 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2131 ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
2133 ib_destroy_cm_id(id_priv->cm_id.ib);
2134 id_priv->cm_id.ib = NULL;
2137 kfree(req.private_data);
2141 static int cma_connect_ib(struct rdma_id_private *id_priv,
2142 struct rdma_conn_param *conn_param)
2144 struct ib_cm_req_param req;
2145 struct rdma_route *route;
2149 memset(&req, 0, sizeof req);
2150 offset = cma_user_data_offset(id_priv->id.ps);
2151 req.private_data_len = offset + conn_param->private_data_len;
2152 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2156 if (conn_param->private_data && conn_param->private_data_len)
2157 memcpy(private_data + offset, conn_param->private_data,
2158 conn_param->private_data_len);
2160 id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_ib_handler,
2162 if (IS_ERR(id_priv->cm_id.ib)) {
2163 ret = PTR_ERR(id_priv->cm_id.ib);
2167 route = &id_priv->id.route;
2168 ret = cma_format_hdr(private_data, id_priv->id.ps, route);
2171 req.private_data = private_data;
2173 req.primary_path = &route->path_rec[0];
2174 if (route->num_paths == 2)
2175 req.alternate_path = &route->path_rec[1];
2177 req.service_id = cma_get_service_id(id_priv->id.ps,
2178 &route->addr.dst_addr);
2179 req.qp_num = id_priv->qp_num;
2180 req.qp_type = IB_QPT_RC;
2181 req.starting_psn = id_priv->seq_num;
2182 req.responder_resources = conn_param->responder_resources;
2183 req.initiator_depth = conn_param->initiator_depth;
2184 req.flow_control = conn_param->flow_control;
2185 req.retry_count = conn_param->retry_count;
2186 req.rnr_retry_count = conn_param->rnr_retry_count;
2187 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2188 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2189 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2190 req.srq = id_priv->srq ? 1 : 0;
2192 ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
2194 if (ret && !IS_ERR(id_priv->cm_id.ib)) {
2195 ib_destroy_cm_id(id_priv->cm_id.ib);
2196 id_priv->cm_id.ib = NULL;
2199 kfree(private_data);
2203 static int cma_connect_iw(struct rdma_id_private *id_priv,
2204 struct rdma_conn_param *conn_param)
2206 struct iw_cm_id *cm_id;
2207 struct sockaddr_in* sin;
2209 struct iw_cm_conn_param iw_param;
2211 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
2212 if (IS_ERR(cm_id)) {
2213 ret = PTR_ERR(cm_id);
2217 id_priv->cm_id.iw = cm_id;
2219 sin = (struct sockaddr_in*) &id_priv->id.route.addr.src_addr;
2220 cm_id->local_addr = *sin;
2222 sin = (struct sockaddr_in*) &id_priv->id.route.addr.dst_addr;
2223 cm_id->remote_addr = *sin;
2225 ret = cma_modify_qp_rtr(&id_priv->id);
2229 iw_param.ord = conn_param->initiator_depth;
2230 iw_param.ird = conn_param->responder_resources;
2231 iw_param.private_data = conn_param->private_data;
2232 iw_param.private_data_len = conn_param->private_data_len;
2234 iw_param.qpn = id_priv->qp_num;
2236 iw_param.qpn = conn_param->qp_num;
2237 ret = iw_cm_connect(cm_id, &iw_param);
2239 if (ret && !IS_ERR(cm_id)) {
2240 iw_destroy_cm_id(cm_id);
2241 id_priv->cm_id.iw = NULL;
2246 int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2248 struct rdma_id_private *id_priv;
2251 id_priv = container_of(id, struct rdma_id_private, id);
2252 if (!cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_CONNECT))
2256 id_priv->qp_num = conn_param->qp_num;
2257 id_priv->srq = conn_param->srq;
2260 switch (rdma_node_get_transport(id->device->node_type)) {
2261 case RDMA_TRANSPORT_IB:
2262 if (cma_is_ud_ps(id->ps))
2263 ret = cma_resolve_ib_udp(id_priv, conn_param);
2265 ret = cma_connect_ib(id_priv, conn_param);
2267 case RDMA_TRANSPORT_IWARP:
2268 ret = cma_connect_iw(id_priv, conn_param);
2279 cma_comp_exch(id_priv, CMA_CONNECT, CMA_ROUTE_RESOLVED);
2282 EXPORT_SYMBOL(rdma_connect);
2284 static int cma_accept_ib(struct rdma_id_private *id_priv,
2285 struct rdma_conn_param *conn_param)
2287 struct ib_cm_rep_param rep;
2288 struct ib_qp_attr qp_attr;
2289 int qp_attr_mask, ret;
2291 if (id_priv->id.qp) {
2292 ret = cma_modify_qp_rtr(&id_priv->id);
2296 qp_attr.qp_state = IB_QPS_RTS;
2297 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, &qp_attr,
2302 qp_attr.max_rd_atomic = conn_param->initiator_depth;
2303 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
2308 memset(&rep, 0, sizeof rep);
2309 rep.qp_num = id_priv->qp_num;
2310 rep.starting_psn = id_priv->seq_num;
2311 rep.private_data = conn_param->private_data;
2312 rep.private_data_len = conn_param->private_data_len;
2313 rep.responder_resources = conn_param->responder_resources;
2314 rep.initiator_depth = conn_param->initiator_depth;
2315 rep.target_ack_delay = CMA_CM_RESPONSE_TIMEOUT;
2316 rep.failover_accepted = 0;
2317 rep.flow_control = conn_param->flow_control;
2318 rep.rnr_retry_count = conn_param->rnr_retry_count;
2319 rep.srq = id_priv->srq ? 1 : 0;
2321 ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
2326 static int cma_accept_iw(struct rdma_id_private *id_priv,
2327 struct rdma_conn_param *conn_param)
2329 struct iw_cm_conn_param iw_param;
2332 ret = cma_modify_qp_rtr(&id_priv->id);
2336 iw_param.ord = conn_param->initiator_depth;
2337 iw_param.ird = conn_param->responder_resources;
2338 iw_param.private_data = conn_param->private_data;
2339 iw_param.private_data_len = conn_param->private_data_len;
2340 if (id_priv->id.qp) {
2341 iw_param.qpn = id_priv->qp_num;
2343 iw_param.qpn = conn_param->qp_num;
2345 return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
2348 static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
2349 enum ib_cm_sidr_status status,
2350 const void *private_data, int private_data_len)
2352 struct ib_cm_sidr_rep_param rep;
2354 memset(&rep, 0, sizeof rep);
2355 rep.status = status;
2356 if (status == IB_SIDR_SUCCESS) {
2357 rep.qp_num = id_priv->qp_num;
2358 rep.qkey = id_priv->qkey;
2360 rep.private_data = private_data;
2361 rep.private_data_len = private_data_len;
2363 return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
2366 int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2368 struct rdma_id_private *id_priv;
2371 id_priv = container_of(id, struct rdma_id_private, id);
2372 if (!cma_comp(id_priv, CMA_CONNECT))
2375 if (!id->qp && conn_param) {
2376 id_priv->qp_num = conn_param->qp_num;
2377 id_priv->srq = conn_param->srq;
2380 switch (rdma_node_get_transport(id->device->node_type)) {
2381 case RDMA_TRANSPORT_IB:
2382 if (cma_is_ud_ps(id->ps))
2383 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
2384 conn_param->private_data,
2385 conn_param->private_data_len);
2386 else if (conn_param)
2387 ret = cma_accept_ib(id_priv, conn_param);
2389 ret = cma_rep_recv(id_priv);
2391 case RDMA_TRANSPORT_IWARP:
2392 ret = cma_accept_iw(id_priv, conn_param);
2404 cma_modify_qp_err(id);
2405 rdma_reject(id, NULL, 0);
2408 EXPORT_SYMBOL(rdma_accept);
2410 int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
2412 struct rdma_id_private *id_priv;
2415 id_priv = container_of(id, struct rdma_id_private, id);
2416 if (!cma_comp(id_priv, CMA_CONNECT))
2419 switch (id->device->node_type) {
2420 case RDMA_NODE_IB_CA:
2421 ret = ib_cm_notify(id_priv->cm_id.ib, event);
2429 EXPORT_SYMBOL(rdma_notify);
2431 int rdma_reject(struct rdma_cm_id *id, const void *private_data,
2432 u8 private_data_len)
2434 struct rdma_id_private *id_priv;
2437 id_priv = container_of(id, struct rdma_id_private, id);
2438 if (!cma_comp(id_priv, CMA_CONNECT))
2441 switch (rdma_node_get_transport(id->device->node_type)) {
2442 case RDMA_TRANSPORT_IB:
2443 if (cma_is_ud_ps(id->ps))
2444 ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT,
2445 private_data, private_data_len);
2447 ret = ib_send_cm_rej(id_priv->cm_id.ib,
2448 IB_CM_REJ_CONSUMER_DEFINED, NULL,
2449 0, private_data, private_data_len);
2451 case RDMA_TRANSPORT_IWARP:
2452 ret = iw_cm_reject(id_priv->cm_id.iw,
2453 private_data, private_data_len);
2461 EXPORT_SYMBOL(rdma_reject);
2463 int rdma_disconnect(struct rdma_cm_id *id)
2465 struct rdma_id_private *id_priv;
2468 id_priv = container_of(id, struct rdma_id_private, id);
2469 if (!cma_comp(id_priv, CMA_CONNECT) &&
2470 !cma_comp(id_priv, CMA_DISCONNECT))
2473 switch (rdma_node_get_transport(id->device->node_type)) {
2474 case RDMA_TRANSPORT_IB:
2475 ret = cma_modify_qp_err(id);
2478 /* Initiate or respond to a disconnect. */
2479 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
2480 ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
2482 case RDMA_TRANSPORT_IWARP:
2483 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
2492 EXPORT_SYMBOL(rdma_disconnect);
2494 static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
2496 struct rdma_id_private *id_priv;
2497 struct cma_multicast *mc = multicast->context;
2498 struct rdma_cm_event event;
2501 id_priv = mc->id_priv;
2502 atomic_inc(&id_priv->dev_remove);
2503 if (!cma_comp(id_priv, CMA_ADDR_BOUND) &&
2504 !cma_comp(id_priv, CMA_ADDR_RESOLVED))
2507 if (!status && id_priv->id.qp)
2508 status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
2509 multicast->rec.mlid);
2511 memset(&event, 0, sizeof event);
2512 event.status = status;
2513 event.param.ud.private_data = mc->context;
2515 event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
2516 ib_init_ah_from_mcmember(id_priv->id.device,
2517 id_priv->id.port_num, &multicast->rec,
2518 &event.param.ud.ah_attr);
2519 event.param.ud.qp_num = 0xFFFFFF;
2520 event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
2522 event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
2524 ret = id_priv->id.event_handler(&id_priv->id, &event);
2526 cma_exch(id_priv, CMA_DESTROYING);
2527 cma_release_remove(id_priv);
2528 rdma_destroy_id(&id_priv->id);
2532 cma_release_remove(id_priv);
2536 static void cma_set_mgid(struct rdma_id_private *id_priv,
2537 struct sockaddr *addr, union ib_gid *mgid)
2539 unsigned char mc_map[MAX_ADDR_LEN];
2540 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2541 struct sockaddr_in *sin = (struct sockaddr_in *) addr;
2542 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
2544 if (cma_any_addr(addr)) {
2545 memset(mgid, 0, sizeof *mgid);
2546 } else if ((addr->sa_family == AF_INET6) &&
2547 ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFF10A01B) ==
2549 /* IPv6 address is an SA assigned MGID. */
2550 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
2552 ip_ib_mc_map(sin->sin_addr.s_addr, mc_map);
2553 if (id_priv->id.ps == RDMA_PS_UDP)
2554 mc_map[7] = 0x01; /* Use RDMA CM signature */
2555 mc_map[8] = ib_addr_get_pkey(dev_addr) >> 8;
2556 mc_map[9] = (unsigned char) ib_addr_get_pkey(dev_addr);
2557 *mgid = *(union ib_gid *) (mc_map + 4);
2561 static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
2562 struct cma_multicast *mc)
2564 struct ib_sa_mcmember_rec rec;
2565 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2566 ib_sa_comp_mask comp_mask;
2569 ib_addr_get_mgid(dev_addr, &rec.mgid);
2570 ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
2575 cma_set_mgid(id_priv, &mc->addr, &rec.mgid);
2576 if (id_priv->id.ps == RDMA_PS_UDP)
2577 rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
2578 ib_addr_get_sgid(dev_addr, &rec.port_gid);
2579 rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
2582 comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
2583 IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
2584 IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
2585 IB_SA_MCMEMBER_REC_FLOW_LABEL |
2586 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
2588 mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
2589 id_priv->id.port_num, &rec,
2590 comp_mask, GFP_KERNEL,
2591 cma_ib_mc_handler, mc);
2592 if (IS_ERR(mc->multicast.ib))
2593 return PTR_ERR(mc->multicast.ib);
2598 int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
2601 struct rdma_id_private *id_priv;
2602 struct cma_multicast *mc;
2605 id_priv = container_of(id, struct rdma_id_private, id);
2606 if (!cma_comp(id_priv, CMA_ADDR_BOUND) &&
2607 !cma_comp(id_priv, CMA_ADDR_RESOLVED))
2610 mc = kmalloc(sizeof *mc, GFP_KERNEL);
2614 memcpy(&mc->addr, addr, ip_addr_size(addr));
2615 mc->context = context;
2616 mc->id_priv = id_priv;
2618 spin_lock(&id_priv->lock);
2619 list_add(&mc->list, &id_priv->mc_list);
2620 spin_unlock(&id_priv->lock);
2622 switch (rdma_node_get_transport(id->device->node_type)) {
2623 case RDMA_TRANSPORT_IB:
2624 ret = cma_join_ib_multicast(id_priv, mc);
2632 spin_lock_irq(&id_priv->lock);
2633 list_del(&mc->list);
2634 spin_unlock_irq(&id_priv->lock);
2639 EXPORT_SYMBOL(rdma_join_multicast);
2641 void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
2643 struct rdma_id_private *id_priv;
2644 struct cma_multicast *mc;
2646 id_priv = container_of(id, struct rdma_id_private, id);
2647 spin_lock_irq(&id_priv->lock);
2648 list_for_each_entry(mc, &id_priv->mc_list, list) {
2649 if (!memcmp(&mc->addr, addr, ip_addr_size(addr))) {
2650 list_del(&mc->list);
2651 spin_unlock_irq(&id_priv->lock);
2654 ib_detach_mcast(id->qp,
2655 &mc->multicast.ib->rec.mgid,
2656 mc->multicast.ib->rec.mlid);
2657 ib_sa_free_multicast(mc->multicast.ib);
2662 spin_unlock_irq(&id_priv->lock);
2664 EXPORT_SYMBOL(rdma_leave_multicast);
2666 static void cma_add_one(struct ib_device *device)
2668 struct cma_device *cma_dev;
2669 struct rdma_id_private *id_priv;
2671 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
2675 cma_dev->device = device;
2677 init_completion(&cma_dev->comp);
2678 atomic_set(&cma_dev->refcount, 1);
2679 INIT_LIST_HEAD(&cma_dev->id_list);
2680 ib_set_client_data(device, &cma_client, cma_dev);
2683 list_add_tail(&cma_dev->list, &dev_list);
2684 list_for_each_entry(id_priv, &listen_any_list, list)
2685 cma_listen_on_dev(id_priv, cma_dev);
2686 mutex_unlock(&lock);
2689 static int cma_remove_id_dev(struct rdma_id_private *id_priv)
2691 struct rdma_cm_event event;
2692 enum cma_state state;
2694 /* Record that we want to remove the device */
2695 state = cma_exch(id_priv, CMA_DEVICE_REMOVAL);
2696 if (state == CMA_DESTROYING)
2699 cma_cancel_operation(id_priv, state);
2700 wait_event(id_priv->wait_remove, !atomic_read(&id_priv->dev_remove));
2702 /* Check for destruction from another callback. */
2703 if (!cma_comp(id_priv, CMA_DEVICE_REMOVAL))
2706 memset(&event, 0, sizeof event);
2707 event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
2708 return id_priv->id.event_handler(&id_priv->id, &event);
2711 static void cma_process_remove(struct cma_device *cma_dev)
2713 struct rdma_id_private *id_priv;
2717 while (!list_empty(&cma_dev->id_list)) {
2718 id_priv = list_entry(cma_dev->id_list.next,
2719 struct rdma_id_private, list);
2721 if (cma_internal_listen(id_priv)) {
2722 cma_destroy_listen(id_priv);
2726 list_del_init(&id_priv->list);
2727 atomic_inc(&id_priv->refcount);
2728 mutex_unlock(&lock);
2730 ret = cma_remove_id_dev(id_priv);
2731 cma_deref_id(id_priv);
2733 rdma_destroy_id(&id_priv->id);
2737 mutex_unlock(&lock);
2739 cma_deref_dev(cma_dev);
2740 wait_for_completion(&cma_dev->comp);
2743 static void cma_remove_one(struct ib_device *device)
2745 struct cma_device *cma_dev;
2747 cma_dev = ib_get_client_data(device, &cma_client);
2752 list_del(&cma_dev->list);
2753 mutex_unlock(&lock);
2755 cma_process_remove(cma_dev);
2759 static int cma_init(void)
2763 get_random_bytes(&next_port, sizeof next_port);
2764 next_port = (next_port % (sysctl_local_port_range[1] -
2765 sysctl_local_port_range[0])) +
2766 sysctl_local_port_range[0];
2767 cma_wq = create_singlethread_workqueue("rdma_cm");
2771 ib_sa_register_client(&sa_client);
2772 rdma_addr_register_client(&addr_client);
2774 ret = ib_register_client(&cma_client);
2780 rdma_addr_unregister_client(&addr_client);
2781 ib_sa_unregister_client(&sa_client);
2782 destroy_workqueue(cma_wq);
2786 static void cma_cleanup(void)
2788 ib_unregister_client(&cma_client);
2789 rdma_addr_unregister_client(&addr_client);
2790 ib_sa_unregister_client(&sa_client);
2791 destroy_workqueue(cma_wq);
2792 idr_destroy(&sdp_ps);
2793 idr_destroy(&tcp_ps);
2794 idr_destroy(&udp_ps);
2795 idr_destroy(&ipoib_ps);
2798 module_init(cma_init);
2799 module_exit(cma_cleanup);