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>
41 #include <rdma/rdma_cm.h>
42 #include <rdma/rdma_cm_ib.h>
43 #include <rdma/ib_cache.h>
44 #include <rdma/ib_cm.h>
45 #include <rdma/ib_sa.h>
47 MODULE_AUTHOR("Sean Hefty");
48 MODULE_DESCRIPTION("Generic RDMA CM Agent");
49 MODULE_LICENSE("Dual BSD/GPL");
51 #define CMA_CM_RESPONSE_TIMEOUT 20
52 #define CMA_MAX_CM_RETRIES 15
54 static void cma_add_one(struct ib_device *device);
55 static void cma_remove_one(struct ib_device *device);
57 static struct ib_client cma_client = {
60 .remove = cma_remove_one
63 static LIST_HEAD(dev_list);
64 static LIST_HEAD(listen_any_list);
65 static DEFINE_MUTEX(lock);
66 static struct workqueue_struct *cma_wq;
67 static DEFINE_IDR(sdp_ps);
68 static DEFINE_IDR(tcp_ps);
71 struct list_head list;
72 struct ib_device *device;
74 struct completion comp;
76 struct list_head id_list;
93 struct rdma_bind_list {
95 struct hlist_head owners;
100 * Device removal can occur at anytime, so we need extra handling to
101 * serialize notifying the user of device removal with other callbacks.
102 * We do this by disabling removal notification while a callback is in process,
103 * and reporting it after the callback completes.
105 struct rdma_id_private {
106 struct rdma_cm_id id;
108 struct rdma_bind_list *bind_list;
109 struct hlist_node node;
110 struct list_head list;
111 struct list_head listen_list;
112 struct cma_device *cma_dev;
114 enum cma_state state;
116 struct completion comp;
118 wait_queue_head_t wait_remove;
123 struct ib_sa_query *query;
131 enum ib_qp_type qp_type;
136 struct work_struct work;
137 struct rdma_id_private *id;
138 enum cma_state old_state;
139 enum cma_state new_state;
140 struct rdma_cm_event event;
153 u8 ip_version; /* IP version: 7:4 */
155 union cma_ip_addr src_addr;
156 union cma_ip_addr dst_addr;
161 u8 sdp_version; /* Major version: 7:4 */
162 u8 ip_version; /* IP version: 7:4 */
163 u8 sdp_specific1[10];
166 union cma_ip_addr src_addr;
167 union cma_ip_addr dst_addr;
175 #define CMA_VERSION 0x00
176 #define SDP_MAJ_VERSION 0x2
178 static int cma_comp(struct rdma_id_private *id_priv, enum cma_state comp)
183 spin_lock_irqsave(&id_priv->lock, flags);
184 ret = (id_priv->state == comp);
185 spin_unlock_irqrestore(&id_priv->lock, flags);
189 static int cma_comp_exch(struct rdma_id_private *id_priv,
190 enum cma_state comp, enum cma_state exch)
195 spin_lock_irqsave(&id_priv->lock, flags);
196 if ((ret = (id_priv->state == comp)))
197 id_priv->state = exch;
198 spin_unlock_irqrestore(&id_priv->lock, flags);
202 static enum cma_state cma_exch(struct rdma_id_private *id_priv,
208 spin_lock_irqsave(&id_priv->lock, flags);
209 old = id_priv->state;
210 id_priv->state = exch;
211 spin_unlock_irqrestore(&id_priv->lock, flags);
215 static inline u8 cma_get_ip_ver(struct cma_hdr *hdr)
217 return hdr->ip_version >> 4;
220 static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
222 hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
225 static inline u8 sdp_get_majv(u8 sdp_version)
227 return sdp_version >> 4;
230 static inline u8 sdp_get_ip_ver(struct sdp_hh *hh)
232 return hh->ip_version >> 4;
235 static inline void sdp_set_ip_ver(struct sdp_hh *hh, u8 ip_ver)
237 hh->ip_version = (ip_ver << 4) | (hh->ip_version & 0xF);
240 static void cma_attach_to_dev(struct rdma_id_private *id_priv,
241 struct cma_device *cma_dev)
243 atomic_inc(&cma_dev->refcount);
244 id_priv->cma_dev = cma_dev;
245 id_priv->id.device = cma_dev->device;
246 list_add_tail(&id_priv->list, &cma_dev->id_list);
249 static inline void cma_deref_dev(struct cma_device *cma_dev)
251 if (atomic_dec_and_test(&cma_dev->refcount))
252 complete(&cma_dev->comp);
255 static void cma_detach_from_dev(struct rdma_id_private *id_priv)
257 list_del(&id_priv->list);
258 cma_deref_dev(id_priv->cma_dev);
259 id_priv->cma_dev = NULL;
262 static int cma_acquire_ib_dev(struct rdma_id_private *id_priv)
264 struct cma_device *cma_dev;
268 ib_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid),
271 list_for_each_entry(cma_dev, &dev_list, list) {
272 ret = ib_find_cached_gid(cma_dev->device, &gid,
273 &id_priv->id.port_num, NULL);
275 cma_attach_to_dev(id_priv, cma_dev);
283 static int cma_acquire_dev(struct rdma_id_private *id_priv)
285 switch (id_priv->id.route.addr.dev_addr.dev_type) {
287 return cma_acquire_ib_dev(id_priv);
293 static void cma_deref_id(struct rdma_id_private *id_priv)
295 if (atomic_dec_and_test(&id_priv->refcount))
296 complete(&id_priv->comp);
299 static void cma_release_remove(struct rdma_id_private *id_priv)
301 if (atomic_dec_and_test(&id_priv->dev_remove))
302 wake_up(&id_priv->wait_remove);
305 struct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler,
306 void *context, enum rdma_port_space ps)
308 struct rdma_id_private *id_priv;
310 id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
312 return ERR_PTR(-ENOMEM);
314 id_priv->state = CMA_IDLE;
315 id_priv->id.context = context;
316 id_priv->id.event_handler = event_handler;
318 spin_lock_init(&id_priv->lock);
319 init_completion(&id_priv->comp);
320 atomic_set(&id_priv->refcount, 1);
321 init_waitqueue_head(&id_priv->wait_remove);
322 atomic_set(&id_priv->dev_remove, 0);
323 INIT_LIST_HEAD(&id_priv->listen_list);
324 get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
328 EXPORT_SYMBOL(rdma_create_id);
330 static int cma_init_ib_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
332 struct ib_qp_attr qp_attr;
333 struct rdma_dev_addr *dev_addr;
336 dev_addr = &id_priv->id.route.addr.dev_addr;
337 ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
338 ib_addr_get_pkey(dev_addr),
339 &qp_attr.pkey_index);
343 qp_attr.qp_state = IB_QPS_INIT;
344 qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE;
345 qp_attr.port_num = id_priv->id.port_num;
346 return ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_ACCESS_FLAGS |
347 IB_QP_PKEY_INDEX | IB_QP_PORT);
350 int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
351 struct ib_qp_init_attr *qp_init_attr)
353 struct rdma_id_private *id_priv;
357 id_priv = container_of(id, struct rdma_id_private, id);
358 if (id->device != pd->device)
361 qp = ib_create_qp(pd, qp_init_attr);
365 switch (id->device->node_type) {
367 ret = cma_init_ib_qp(id_priv, qp);
378 id_priv->qp_num = qp->qp_num;
379 id_priv->qp_type = qp->qp_type;
380 id_priv->srq = (qp->srq != NULL);
386 EXPORT_SYMBOL(rdma_create_qp);
388 void rdma_destroy_qp(struct rdma_cm_id *id)
390 ib_destroy_qp(id->qp);
392 EXPORT_SYMBOL(rdma_destroy_qp);
394 static int cma_modify_qp_rtr(struct rdma_cm_id *id)
396 struct ib_qp_attr qp_attr;
397 int qp_attr_mask, ret;
402 /* Need to update QP attributes from default values. */
403 qp_attr.qp_state = IB_QPS_INIT;
404 ret = rdma_init_qp_attr(id, &qp_attr, &qp_attr_mask);
408 ret = ib_modify_qp(id->qp, &qp_attr, qp_attr_mask);
412 qp_attr.qp_state = IB_QPS_RTR;
413 ret = rdma_init_qp_attr(id, &qp_attr, &qp_attr_mask);
417 return ib_modify_qp(id->qp, &qp_attr, qp_attr_mask);
420 static int cma_modify_qp_rts(struct rdma_cm_id *id)
422 struct ib_qp_attr qp_attr;
423 int qp_attr_mask, ret;
428 qp_attr.qp_state = IB_QPS_RTS;
429 ret = rdma_init_qp_attr(id, &qp_attr, &qp_attr_mask);
433 return ib_modify_qp(id->qp, &qp_attr, qp_attr_mask);
436 static int cma_modify_qp_err(struct rdma_cm_id *id)
438 struct ib_qp_attr qp_attr;
443 qp_attr.qp_state = IB_QPS_ERR;
444 return ib_modify_qp(id->qp, &qp_attr, IB_QP_STATE);
447 int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
450 struct rdma_id_private *id_priv;
453 id_priv = container_of(id, struct rdma_id_private, id);
454 switch (id_priv->id.device->node_type) {
456 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
458 if (qp_attr->qp_state == IB_QPS_RTR)
459 qp_attr->rq_psn = id_priv->seq_num;
468 EXPORT_SYMBOL(rdma_init_qp_attr);
470 static inline int cma_zero_addr(struct sockaddr *addr)
472 struct in6_addr *ip6;
474 if (addr->sa_family == AF_INET)
475 return ZERONET(((struct sockaddr_in *) addr)->sin_addr.s_addr);
477 ip6 = &((struct sockaddr_in6 *) addr)->sin6_addr;
478 return (ip6->s6_addr32[0] | ip6->s6_addr32[1] |
479 ip6->s6_addr32[2] | ip6->s6_addr32[3]) == 0;
483 static inline int cma_loopback_addr(struct sockaddr *addr)
485 return LOOPBACK(((struct sockaddr_in *) addr)->sin_addr.s_addr);
488 static inline int cma_any_addr(struct sockaddr *addr)
490 return cma_zero_addr(addr) || cma_loopback_addr(addr);
493 static inline int cma_any_port(struct sockaddr *addr)
495 return !((struct sockaddr_in *) addr)->sin_port;
498 static int cma_get_net_info(void *hdr, enum rdma_port_space ps,
499 u8 *ip_ver, __u16 *port,
500 union cma_ip_addr **src, union cma_ip_addr **dst)
504 if (sdp_get_majv(((struct sdp_hh *) hdr)->sdp_version) !=
508 *ip_ver = sdp_get_ip_ver(hdr);
509 *port = ((struct sdp_hh *) hdr)->port;
510 *src = &((struct sdp_hh *) hdr)->src_addr;
511 *dst = &((struct sdp_hh *) hdr)->dst_addr;
514 if (((struct cma_hdr *) hdr)->cma_version != CMA_VERSION)
517 *ip_ver = cma_get_ip_ver(hdr);
518 *port = ((struct cma_hdr *) hdr)->port;
519 *src = &((struct cma_hdr *) hdr)->src_addr;
520 *dst = &((struct cma_hdr *) hdr)->dst_addr;
524 if (*ip_ver != 4 && *ip_ver != 6)
529 static void cma_save_net_info(struct rdma_addr *addr,
530 struct rdma_addr *listen_addr,
531 u8 ip_ver, __u16 port,
532 union cma_ip_addr *src, union cma_ip_addr *dst)
534 struct sockaddr_in *listen4, *ip4;
535 struct sockaddr_in6 *listen6, *ip6;
539 listen4 = (struct sockaddr_in *) &listen_addr->src_addr;
540 ip4 = (struct sockaddr_in *) &addr->src_addr;
541 ip4->sin_family = listen4->sin_family;
542 ip4->sin_addr.s_addr = dst->ip4.addr;
543 ip4->sin_port = listen4->sin_port;
545 ip4 = (struct sockaddr_in *) &addr->dst_addr;
546 ip4->sin_family = listen4->sin_family;
547 ip4->sin_addr.s_addr = src->ip4.addr;
548 ip4->sin_port = port;
551 listen6 = (struct sockaddr_in6 *) &listen_addr->src_addr;
552 ip6 = (struct sockaddr_in6 *) &addr->src_addr;
553 ip6->sin6_family = listen6->sin6_family;
554 ip6->sin6_addr = dst->ip6;
555 ip6->sin6_port = listen6->sin6_port;
557 ip6 = (struct sockaddr_in6 *) &addr->dst_addr;
558 ip6->sin6_family = listen6->sin6_family;
559 ip6->sin6_addr = src->ip6;
560 ip6->sin6_port = port;
567 static inline int cma_user_data_offset(enum rdma_port_space ps)
573 return sizeof(struct cma_hdr);
577 static int cma_notify_user(struct rdma_id_private *id_priv,
578 enum rdma_cm_event_type type, int status,
579 void *data, u8 data_len)
581 struct rdma_cm_event event;
584 event.status = status;
585 event.private_data = data;
586 event.private_data_len = data_len;
588 return id_priv->id.event_handler(&id_priv->id, &event);
591 static void cma_cancel_route(struct rdma_id_private *id_priv)
593 switch (id_priv->id.device->node_type) {
596 ib_sa_cancel_query(id_priv->query_id, id_priv->query);
603 static inline int cma_internal_listen(struct rdma_id_private *id_priv)
605 return (id_priv->state == CMA_LISTEN) && id_priv->cma_dev &&
606 cma_any_addr(&id_priv->id.route.addr.src_addr);
609 static void cma_destroy_listen(struct rdma_id_private *id_priv)
611 cma_exch(id_priv, CMA_DESTROYING);
613 if (id_priv->cma_dev) {
614 switch (id_priv->id.device->node_type) {
616 if (id_priv->cm_id.ib && !IS_ERR(id_priv->cm_id.ib))
617 ib_destroy_cm_id(id_priv->cm_id.ib);
622 cma_detach_from_dev(id_priv);
624 list_del(&id_priv->listen_list);
626 cma_deref_id(id_priv);
627 wait_for_completion(&id_priv->comp);
632 static void cma_cancel_listens(struct rdma_id_private *id_priv)
634 struct rdma_id_private *dev_id_priv;
637 list_del(&id_priv->list);
639 while (!list_empty(&id_priv->listen_list)) {
640 dev_id_priv = list_entry(id_priv->listen_list.next,
641 struct rdma_id_private, listen_list);
642 cma_destroy_listen(dev_id_priv);
647 static void cma_cancel_operation(struct rdma_id_private *id_priv,
648 enum cma_state state)
652 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
654 case CMA_ROUTE_QUERY:
655 cma_cancel_route(id_priv);
658 if (cma_any_addr(&id_priv->id.route.addr.src_addr) &&
660 cma_cancel_listens(id_priv);
667 static void cma_release_port(struct rdma_id_private *id_priv)
669 struct rdma_bind_list *bind_list = id_priv->bind_list;
675 hlist_del(&id_priv->node);
676 if (hlist_empty(&bind_list->owners)) {
677 idr_remove(bind_list->ps, bind_list->port);
683 void rdma_destroy_id(struct rdma_cm_id *id)
685 struct rdma_id_private *id_priv;
686 enum cma_state state;
688 id_priv = container_of(id, struct rdma_id_private, id);
689 state = cma_exch(id_priv, CMA_DESTROYING);
690 cma_cancel_operation(id_priv, state);
692 if (id_priv->cma_dev) {
693 switch (id->device->node_type) {
695 if (id_priv->cm_id.ib && !IS_ERR(id_priv->cm_id.ib))
696 ib_destroy_cm_id(id_priv->cm_id.ib);
702 cma_detach_from_dev(id_priv);
706 cma_release_port(id_priv);
707 cma_deref_id(id_priv);
708 wait_for_completion(&id_priv->comp);
710 kfree(id_priv->id.route.path_rec);
713 EXPORT_SYMBOL(rdma_destroy_id);
715 static int cma_rep_recv(struct rdma_id_private *id_priv)
719 ret = cma_modify_qp_rtr(&id_priv->id);
723 ret = cma_modify_qp_rts(&id_priv->id);
727 ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
733 cma_modify_qp_err(&id_priv->id);
734 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
739 static int cma_verify_rep(struct rdma_id_private *id_priv, void *data)
741 if (id_priv->id.ps == RDMA_PS_SDP &&
742 sdp_get_majv(((struct sdp_hah *) data)->sdp_version) !=
749 static int cma_rtu_recv(struct rdma_id_private *id_priv)
753 ret = cma_modify_qp_rts(&id_priv->id);
759 cma_modify_qp_err(&id_priv->id);
760 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
765 static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
767 struct rdma_id_private *id_priv = cm_id->context;
768 enum rdma_cm_event_type event;
769 u8 private_data_len = 0;
770 int ret = 0, status = 0;
772 atomic_inc(&id_priv->dev_remove);
773 if (!cma_comp(id_priv, CMA_CONNECT))
776 switch (ib_event->event) {
777 case IB_CM_REQ_ERROR:
778 case IB_CM_REP_ERROR:
779 event = RDMA_CM_EVENT_UNREACHABLE;
782 case IB_CM_REP_RECEIVED:
783 status = cma_verify_rep(id_priv, ib_event->private_data);
785 event = RDMA_CM_EVENT_CONNECT_ERROR;
786 else if (id_priv->id.qp && id_priv->id.ps != RDMA_PS_SDP) {
787 status = cma_rep_recv(id_priv);
788 event = status ? RDMA_CM_EVENT_CONNECT_ERROR :
789 RDMA_CM_EVENT_ESTABLISHED;
791 event = RDMA_CM_EVENT_CONNECT_RESPONSE;
792 private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
794 case IB_CM_RTU_RECEIVED:
795 status = cma_rtu_recv(id_priv);
796 event = status ? RDMA_CM_EVENT_CONNECT_ERROR :
797 RDMA_CM_EVENT_ESTABLISHED;
799 case IB_CM_DREQ_ERROR:
800 status = -ETIMEDOUT; /* fall through */
801 case IB_CM_DREQ_RECEIVED:
802 case IB_CM_DREP_RECEIVED:
803 if (!cma_comp_exch(id_priv, CMA_CONNECT, CMA_DISCONNECT))
805 event = RDMA_CM_EVENT_DISCONNECTED;
807 case IB_CM_TIMEWAIT_EXIT:
808 case IB_CM_MRA_RECEIVED:
811 case IB_CM_REJ_RECEIVED:
812 cma_modify_qp_err(&id_priv->id);
813 status = ib_event->param.rej_rcvd.reason;
814 event = RDMA_CM_EVENT_REJECTED;
815 private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
818 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d",
823 ret = cma_notify_user(id_priv, event, status, ib_event->private_data,
826 /* Destroy the CM ID by returning a non-zero value. */
827 id_priv->cm_id.ib = NULL;
828 cma_exch(id_priv, CMA_DESTROYING);
829 cma_release_remove(id_priv);
830 rdma_destroy_id(&id_priv->id);
834 cma_release_remove(id_priv);
838 static struct rdma_id_private *cma_new_id(struct rdma_cm_id *listen_id,
839 struct ib_cm_event *ib_event)
841 struct rdma_id_private *id_priv;
842 struct rdma_cm_id *id;
843 struct rdma_route *rt;
844 union cma_ip_addr *src, *dst;
848 id = rdma_create_id(listen_id->event_handler, listen_id->context,
854 rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
855 rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths, GFP_KERNEL);
859 if (cma_get_net_info(ib_event->private_data, listen_id->ps,
860 &ip_ver, &port, &src, &dst))
863 cma_save_net_info(&id->route.addr, &listen_id->route.addr,
864 ip_ver, port, src, dst);
865 rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
866 if (rt->num_paths == 2)
867 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
869 ib_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
870 ib_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
871 ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
872 rt->addr.dev_addr.dev_type = IB_NODE_CA;
874 id_priv = container_of(id, struct rdma_id_private, id);
875 id_priv->state = CMA_CONNECT;
882 static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
884 struct rdma_id_private *listen_id, *conn_id;
887 listen_id = cm_id->context;
888 atomic_inc(&listen_id->dev_remove);
889 if (!cma_comp(listen_id, CMA_LISTEN)) {
894 conn_id = cma_new_id(&listen_id->id, ib_event);
900 atomic_inc(&conn_id->dev_remove);
901 ret = cma_acquire_ib_dev(conn_id);
904 cma_release_remove(conn_id);
905 rdma_destroy_id(&conn_id->id);
909 conn_id->cm_id.ib = cm_id;
910 cm_id->context = conn_id;
911 cm_id->cm_handler = cma_ib_handler;
913 offset = cma_user_data_offset(listen_id->id.ps);
914 ret = cma_notify_user(conn_id, RDMA_CM_EVENT_CONNECT_REQUEST, 0,
915 ib_event->private_data + offset,
916 IB_CM_REQ_PRIVATE_DATA_SIZE - offset);
918 /* Destroy the CM ID by returning a non-zero value. */
919 conn_id->cm_id.ib = NULL;
920 cma_exch(conn_id, CMA_DESTROYING);
921 cma_release_remove(conn_id);
922 rdma_destroy_id(&conn_id->id);
925 cma_release_remove(listen_id);
929 static __be64 cma_get_service_id(enum rdma_port_space ps, struct sockaddr *addr)
931 return cpu_to_be64(((u64)ps << 16) +
932 be16_to_cpu(((struct sockaddr_in *) addr)->sin_port));
935 static void cma_set_compare_data(enum rdma_port_space ps, struct sockaddr *addr,
936 struct ib_cm_compare_data *compare)
938 struct cma_hdr *cma_data, *cma_mask;
939 struct sdp_hh *sdp_data, *sdp_mask;
941 struct in6_addr ip6_addr;
943 memset(compare, 0, sizeof *compare);
944 cma_data = (void *) compare->data;
945 cma_mask = (void *) compare->mask;
946 sdp_data = (void *) compare->data;
947 sdp_mask = (void *) compare->mask;
949 switch (addr->sa_family) {
951 ip4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
952 if (ps == RDMA_PS_SDP) {
953 sdp_set_ip_ver(sdp_data, 4);
954 sdp_set_ip_ver(sdp_mask, 0xF);
955 sdp_data->dst_addr.ip4.addr = ip4_addr;
956 sdp_mask->dst_addr.ip4.addr = ~0;
958 cma_set_ip_ver(cma_data, 4);
959 cma_set_ip_ver(cma_mask, 0xF);
960 cma_data->dst_addr.ip4.addr = ip4_addr;
961 cma_mask->dst_addr.ip4.addr = ~0;
965 ip6_addr = ((struct sockaddr_in6 *) addr)->sin6_addr;
966 if (ps == RDMA_PS_SDP) {
967 sdp_set_ip_ver(sdp_data, 6);
968 sdp_set_ip_ver(sdp_mask, 0xF);
969 sdp_data->dst_addr.ip6 = ip6_addr;
970 memset(&sdp_mask->dst_addr.ip6, 0xFF,
971 sizeof sdp_mask->dst_addr.ip6);
973 cma_set_ip_ver(cma_data, 6);
974 cma_set_ip_ver(cma_mask, 0xF);
975 cma_data->dst_addr.ip6 = ip6_addr;
976 memset(&cma_mask->dst_addr.ip6, 0xFF,
977 sizeof cma_mask->dst_addr.ip6);
985 static int cma_ib_listen(struct rdma_id_private *id_priv)
987 struct ib_cm_compare_data compare_data;
988 struct sockaddr *addr;
992 id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_req_handler,
994 if (IS_ERR(id_priv->cm_id.ib))
995 return PTR_ERR(id_priv->cm_id.ib);
997 addr = &id_priv->id.route.addr.src_addr;
998 svc_id = cma_get_service_id(id_priv->id.ps, addr);
999 if (cma_any_addr(addr))
1000 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, NULL);
1002 cma_set_compare_data(id_priv->id.ps, addr, &compare_data);
1003 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, &compare_data);
1007 ib_destroy_cm_id(id_priv->cm_id.ib);
1008 id_priv->cm_id.ib = NULL;
1014 static int cma_listen_handler(struct rdma_cm_id *id,
1015 struct rdma_cm_event *event)
1017 struct rdma_id_private *id_priv = id->context;
1019 id->context = id_priv->id.context;
1020 id->event_handler = id_priv->id.event_handler;
1021 return id_priv->id.event_handler(id, event);
1024 static void cma_listen_on_dev(struct rdma_id_private *id_priv,
1025 struct cma_device *cma_dev)
1027 struct rdma_id_private *dev_id_priv;
1028 struct rdma_cm_id *id;
1031 id = rdma_create_id(cma_listen_handler, id_priv, id_priv->id.ps);
1035 dev_id_priv = container_of(id, struct rdma_id_private, id);
1037 dev_id_priv->state = CMA_ADDR_BOUND;
1038 memcpy(&id->route.addr.src_addr, &id_priv->id.route.addr.src_addr,
1039 ip_addr_size(&id_priv->id.route.addr.src_addr));
1041 cma_attach_to_dev(dev_id_priv, cma_dev);
1042 list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
1044 ret = rdma_listen(id, id_priv->backlog);
1050 cma_destroy_listen(dev_id_priv);
1053 static void cma_listen_on_all(struct rdma_id_private *id_priv)
1055 struct cma_device *cma_dev;
1058 list_add_tail(&id_priv->list, &listen_any_list);
1059 list_for_each_entry(cma_dev, &dev_list, list)
1060 cma_listen_on_dev(id_priv, cma_dev);
1061 mutex_unlock(&lock);
1064 static int cma_bind_any(struct rdma_cm_id *id, sa_family_t af)
1066 struct sockaddr_in addr_in;
1068 memset(&addr_in, 0, sizeof addr_in);
1069 addr_in.sin_family = af;
1070 return rdma_bind_addr(id, (struct sockaddr *) &addr_in);
1073 int rdma_listen(struct rdma_cm_id *id, int backlog)
1075 struct rdma_id_private *id_priv;
1078 id_priv = container_of(id, struct rdma_id_private, id);
1079 if (id_priv->state == CMA_IDLE) {
1080 ret = cma_bind_any(id, AF_INET);
1085 if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_LISTEN))
1088 id_priv->backlog = backlog;
1090 switch (id->device->node_type) {
1092 ret = cma_ib_listen(id_priv);
1101 cma_listen_on_all(id_priv);
1105 id_priv->backlog = 0;
1106 cma_comp_exch(id_priv, CMA_LISTEN, CMA_ADDR_BOUND);
1109 EXPORT_SYMBOL(rdma_listen);
1111 static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
1114 struct cma_work *work = context;
1115 struct rdma_route *route;
1117 route = &work->id->id.route;
1120 route->num_paths = 1;
1121 *route->path_rec = *path_rec;
1123 work->old_state = CMA_ROUTE_QUERY;
1124 work->new_state = CMA_ADDR_RESOLVED;
1125 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
1128 queue_work(cma_wq, &work->work);
1131 static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
1132 struct cma_work *work)
1134 struct rdma_dev_addr *addr = &id_priv->id.route.addr.dev_addr;
1135 struct ib_sa_path_rec path_rec;
1137 memset(&path_rec, 0, sizeof path_rec);
1138 ib_addr_get_sgid(addr, &path_rec.sgid);
1139 ib_addr_get_dgid(addr, &path_rec.dgid);
1140 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(addr));
1141 path_rec.numb_path = 1;
1143 id_priv->query_id = ib_sa_path_rec_get(id_priv->id.device,
1144 id_priv->id.port_num, &path_rec,
1145 IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
1146 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH,
1147 timeout_ms, GFP_KERNEL,
1148 cma_query_handler, work, &id_priv->query);
1150 return (id_priv->query_id < 0) ? id_priv->query_id : 0;
1153 static void cma_work_handler(void *data)
1155 struct cma_work *work = data;
1156 struct rdma_id_private *id_priv = work->id;
1159 atomic_inc(&id_priv->dev_remove);
1160 if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
1163 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
1164 cma_exch(id_priv, CMA_DESTROYING);
1168 cma_release_remove(id_priv);
1169 cma_deref_id(id_priv);
1171 rdma_destroy_id(&id_priv->id);
1175 static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
1177 struct rdma_route *route = &id_priv->id.route;
1178 struct cma_work *work;
1181 work = kzalloc(sizeof *work, GFP_KERNEL);
1186 INIT_WORK(&work->work, cma_work_handler, work);
1187 work->old_state = CMA_ROUTE_QUERY;
1188 work->new_state = CMA_ROUTE_RESOLVED;
1189 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1191 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
1192 if (!route->path_rec) {
1197 ret = cma_query_ib_route(id_priv, timeout_ms, work);
1203 kfree(route->path_rec);
1204 route->path_rec = NULL;
1210 int rdma_set_ib_paths(struct rdma_cm_id *id,
1211 struct ib_sa_path_rec *path_rec, int num_paths)
1213 struct rdma_id_private *id_priv;
1216 id_priv = container_of(id, struct rdma_id_private, id);
1217 if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ROUTE_RESOLVED))
1220 id->route.path_rec = kmalloc(sizeof *path_rec * num_paths, GFP_KERNEL);
1221 if (!id->route.path_rec) {
1226 memcpy(id->route.path_rec, path_rec, sizeof *path_rec * num_paths);
1229 cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_ADDR_RESOLVED);
1232 EXPORT_SYMBOL(rdma_set_ib_paths);
1234 int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
1236 struct rdma_id_private *id_priv;
1239 id_priv = container_of(id, struct rdma_id_private, id);
1240 if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ROUTE_QUERY))
1243 atomic_inc(&id_priv->refcount);
1244 switch (id->device->node_type) {
1246 ret = cma_resolve_ib_route(id_priv, timeout_ms);
1257 cma_comp_exch(id_priv, CMA_ROUTE_QUERY, CMA_ADDR_RESOLVED);
1258 cma_deref_id(id_priv);
1261 EXPORT_SYMBOL(rdma_resolve_route);
1263 static int cma_bind_loopback(struct rdma_id_private *id_priv)
1265 struct cma_device *cma_dev;
1266 struct ib_port_attr port_attr;
1273 list_for_each_entry(cma_dev, &dev_list, list)
1274 for (p = 1; p <= cma_dev->device->phys_port_cnt; ++p)
1275 if (!ib_query_port (cma_dev->device, p, &port_attr) &&
1276 port_attr.state == IB_PORT_ACTIVE)
1279 if (!list_empty(&dev_list)) {
1281 cma_dev = list_entry(dev_list.next, struct cma_device, list);
1288 ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid);
1292 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
1296 ib_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
1297 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
1298 id_priv->id.port_num = p;
1299 cma_attach_to_dev(id_priv, cma_dev);
1301 mutex_unlock(&lock);
1305 static void addr_handler(int status, struct sockaddr *src_addr,
1306 struct rdma_dev_addr *dev_addr, void *context)
1308 struct rdma_id_private *id_priv = context;
1309 enum rdma_cm_event_type event;
1311 atomic_inc(&id_priv->dev_remove);
1312 if (!id_priv->cma_dev && !status)
1313 status = cma_acquire_dev(id_priv);
1316 if (!cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_BOUND))
1318 event = RDMA_CM_EVENT_ADDR_ERROR;
1320 if (!cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_RESOLVED))
1322 memcpy(&id_priv->id.route.addr.src_addr, src_addr,
1323 ip_addr_size(src_addr));
1324 event = RDMA_CM_EVENT_ADDR_RESOLVED;
1327 if (cma_notify_user(id_priv, event, status, NULL, 0)) {
1328 cma_exch(id_priv, CMA_DESTROYING);
1329 cma_release_remove(id_priv);
1330 cma_deref_id(id_priv);
1331 rdma_destroy_id(&id_priv->id);
1335 cma_release_remove(id_priv);
1336 cma_deref_id(id_priv);
1339 static int cma_resolve_loopback(struct rdma_id_private *id_priv)
1341 struct cma_work *work;
1342 struct sockaddr_in *src_in, *dst_in;
1346 work = kzalloc(sizeof *work, GFP_KERNEL);
1350 if (!id_priv->cma_dev) {
1351 ret = cma_bind_loopback(id_priv);
1356 ib_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
1357 ib_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
1359 if (cma_zero_addr(&id_priv->id.route.addr.src_addr)) {
1360 src_in = (struct sockaddr_in *)&id_priv->id.route.addr.src_addr;
1361 dst_in = (struct sockaddr_in *)&id_priv->id.route.addr.dst_addr;
1362 src_in->sin_family = dst_in->sin_family;
1363 src_in->sin_addr.s_addr = dst_in->sin_addr.s_addr;
1367 INIT_WORK(&work->work, cma_work_handler, work);
1368 work->old_state = CMA_ADDR_QUERY;
1369 work->new_state = CMA_ADDR_RESOLVED;
1370 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
1371 queue_work(cma_wq, &work->work);
1378 static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
1379 struct sockaddr *dst_addr)
1381 if (src_addr && src_addr->sa_family)
1382 return rdma_bind_addr(id, src_addr);
1384 return cma_bind_any(id, dst_addr->sa_family);
1387 int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
1388 struct sockaddr *dst_addr, int timeout_ms)
1390 struct rdma_id_private *id_priv;
1393 id_priv = container_of(id, struct rdma_id_private, id);
1394 if (id_priv->state == CMA_IDLE) {
1395 ret = cma_bind_addr(id, src_addr, dst_addr);
1400 if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_ADDR_QUERY))
1403 atomic_inc(&id_priv->refcount);
1404 memcpy(&id->route.addr.dst_addr, dst_addr, ip_addr_size(dst_addr));
1405 if (cma_any_addr(dst_addr))
1406 ret = cma_resolve_loopback(id_priv);
1408 ret = rdma_resolve_ip(&id->route.addr.src_addr, dst_addr,
1409 &id->route.addr.dev_addr,
1410 timeout_ms, addr_handler, id_priv);
1416 cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_BOUND);
1417 cma_deref_id(id_priv);
1420 EXPORT_SYMBOL(rdma_resolve_addr);
1422 static void cma_bind_port(struct rdma_bind_list *bind_list,
1423 struct rdma_id_private *id_priv)
1425 struct sockaddr_in *sin;
1427 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1428 sin->sin_port = htons(bind_list->port);
1429 id_priv->bind_list = bind_list;
1430 hlist_add_head(&id_priv->node, &bind_list->owners);
1433 static int cma_alloc_port(struct idr *ps, struct rdma_id_private *id_priv,
1434 unsigned short snum)
1436 struct rdma_bind_list *bind_list;
1437 int port, start, ret;
1439 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
1443 start = snum ? snum : sysctl_local_port_range[0];
1446 ret = idr_get_new_above(ps, bind_list, start, &port);
1447 } while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL));
1452 if ((snum && port != snum) ||
1453 (!snum && port > sysctl_local_port_range[1])) {
1454 idr_remove(ps, port);
1455 ret = -EADDRNOTAVAIL;
1460 bind_list->port = (unsigned short) port;
1461 cma_bind_port(bind_list, id_priv);
1468 static int cma_use_port(struct idr *ps, struct rdma_id_private *id_priv)
1470 struct rdma_id_private *cur_id;
1471 struct sockaddr_in *sin, *cur_sin;
1472 struct rdma_bind_list *bind_list;
1473 struct hlist_node *node;
1474 unsigned short snum;
1476 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1477 snum = ntohs(sin->sin_port);
1478 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
1481 bind_list = idr_find(ps, snum);
1483 return cma_alloc_port(ps, id_priv, snum);
1486 * We don't support binding to any address if anyone is bound to
1487 * a specific address on the same port.
1489 if (cma_any_addr(&id_priv->id.route.addr.src_addr))
1490 return -EADDRNOTAVAIL;
1492 hlist_for_each_entry(cur_id, node, &bind_list->owners, node) {
1493 if (cma_any_addr(&cur_id->id.route.addr.src_addr))
1494 return -EADDRNOTAVAIL;
1496 cur_sin = (struct sockaddr_in *) &cur_id->id.route.addr.src_addr;
1497 if (sin->sin_addr.s_addr == cur_sin->sin_addr.s_addr)
1501 cma_bind_port(bind_list, id_priv);
1505 static int cma_get_port(struct rdma_id_private *id_priv)
1510 switch (id_priv->id.ps) {
1518 return -EPROTONOSUPPORT;
1522 if (cma_any_port(&id_priv->id.route.addr.src_addr))
1523 ret = cma_alloc_port(ps, id_priv, 0);
1525 ret = cma_use_port(ps, id_priv);
1526 mutex_unlock(&lock);
1531 int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
1533 struct rdma_id_private *id_priv;
1536 if (addr->sa_family != AF_INET)
1537 return -EAFNOSUPPORT;
1539 id_priv = container_of(id, struct rdma_id_private, id);
1540 if (!cma_comp_exch(id_priv, CMA_IDLE, CMA_ADDR_BOUND))
1543 if (!cma_any_addr(addr)) {
1544 ret = rdma_translate_ip(addr, &id->route.addr.dev_addr);
1546 ret = cma_acquire_dev(id_priv);
1551 memcpy(&id->route.addr.src_addr, addr, ip_addr_size(addr));
1552 ret = cma_get_port(id_priv);
1558 cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_IDLE);
1561 EXPORT_SYMBOL(rdma_bind_addr);
1563 static int cma_format_hdr(void *hdr, enum rdma_port_space ps,
1564 struct rdma_route *route)
1566 struct sockaddr_in *src4, *dst4;
1567 struct cma_hdr *cma_hdr;
1568 struct sdp_hh *sdp_hdr;
1570 src4 = (struct sockaddr_in *) &route->addr.src_addr;
1571 dst4 = (struct sockaddr_in *) &route->addr.dst_addr;
1576 if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION)
1578 sdp_set_ip_ver(sdp_hdr, 4);
1579 sdp_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
1580 sdp_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
1581 sdp_hdr->port = src4->sin_port;
1585 cma_hdr->cma_version = CMA_VERSION;
1586 cma_set_ip_ver(cma_hdr, 4);
1587 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
1588 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
1589 cma_hdr->port = src4->sin_port;
1595 static int cma_connect_ib(struct rdma_id_private *id_priv,
1596 struct rdma_conn_param *conn_param)
1598 struct ib_cm_req_param req;
1599 struct rdma_route *route;
1603 memset(&req, 0, sizeof req);
1604 offset = cma_user_data_offset(id_priv->id.ps);
1605 req.private_data_len = offset + conn_param->private_data_len;
1606 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
1610 if (conn_param->private_data && conn_param->private_data_len)
1611 memcpy(private_data + offset, conn_param->private_data,
1612 conn_param->private_data_len);
1614 id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_ib_handler,
1616 if (IS_ERR(id_priv->cm_id.ib)) {
1617 ret = PTR_ERR(id_priv->cm_id.ib);
1621 route = &id_priv->id.route;
1622 ret = cma_format_hdr(private_data, id_priv->id.ps, route);
1625 req.private_data = private_data;
1627 req.primary_path = &route->path_rec[0];
1628 if (route->num_paths == 2)
1629 req.alternate_path = &route->path_rec[1];
1631 req.service_id = cma_get_service_id(id_priv->id.ps,
1632 &route->addr.dst_addr);
1633 req.qp_num = id_priv->qp_num;
1634 req.qp_type = id_priv->qp_type;
1635 req.starting_psn = id_priv->seq_num;
1636 req.responder_resources = conn_param->responder_resources;
1637 req.initiator_depth = conn_param->initiator_depth;
1638 req.flow_control = conn_param->flow_control;
1639 req.retry_count = conn_param->retry_count;
1640 req.rnr_retry_count = conn_param->rnr_retry_count;
1641 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
1642 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
1643 req.max_cm_retries = CMA_MAX_CM_RETRIES;
1644 req.srq = id_priv->srq ? 1 : 0;
1646 ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
1648 kfree(private_data);
1652 int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
1654 struct rdma_id_private *id_priv;
1657 id_priv = container_of(id, struct rdma_id_private, id);
1658 if (!cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_CONNECT))
1662 id_priv->qp_num = conn_param->qp_num;
1663 id_priv->qp_type = conn_param->qp_type;
1664 id_priv->srq = conn_param->srq;
1667 switch (id->device->node_type) {
1669 ret = cma_connect_ib(id_priv, conn_param);
1680 cma_comp_exch(id_priv, CMA_CONNECT, CMA_ROUTE_RESOLVED);
1683 EXPORT_SYMBOL(rdma_connect);
1685 static int cma_accept_ib(struct rdma_id_private *id_priv,
1686 struct rdma_conn_param *conn_param)
1688 struct ib_cm_rep_param rep;
1691 ret = cma_modify_qp_rtr(&id_priv->id);
1695 memset(&rep, 0, sizeof rep);
1696 rep.qp_num = id_priv->qp_num;
1697 rep.starting_psn = id_priv->seq_num;
1698 rep.private_data = conn_param->private_data;
1699 rep.private_data_len = conn_param->private_data_len;
1700 rep.responder_resources = conn_param->responder_resources;
1701 rep.initiator_depth = conn_param->initiator_depth;
1702 rep.target_ack_delay = CMA_CM_RESPONSE_TIMEOUT;
1703 rep.failover_accepted = 0;
1704 rep.flow_control = conn_param->flow_control;
1705 rep.rnr_retry_count = conn_param->rnr_retry_count;
1706 rep.srq = id_priv->srq ? 1 : 0;
1708 return ib_send_cm_rep(id_priv->cm_id.ib, &rep);
1711 int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
1713 struct rdma_id_private *id_priv;
1716 id_priv = container_of(id, struct rdma_id_private, id);
1717 if (!cma_comp(id_priv, CMA_CONNECT))
1720 if (!id->qp && conn_param) {
1721 id_priv->qp_num = conn_param->qp_num;
1722 id_priv->qp_type = conn_param->qp_type;
1723 id_priv->srq = conn_param->srq;
1726 switch (id->device->node_type) {
1729 ret = cma_accept_ib(id_priv, conn_param);
1731 ret = cma_rep_recv(id_priv);
1743 cma_modify_qp_err(id);
1744 rdma_reject(id, NULL, 0);
1747 EXPORT_SYMBOL(rdma_accept);
1749 int rdma_reject(struct rdma_cm_id *id, const void *private_data,
1750 u8 private_data_len)
1752 struct rdma_id_private *id_priv;
1755 id_priv = container_of(id, struct rdma_id_private, id);
1756 if (!cma_comp(id_priv, CMA_CONNECT))
1759 switch (id->device->node_type) {
1761 ret = ib_send_cm_rej(id_priv->cm_id.ib,
1762 IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
1763 private_data, private_data_len);
1771 EXPORT_SYMBOL(rdma_reject);
1773 int rdma_disconnect(struct rdma_cm_id *id)
1775 struct rdma_id_private *id_priv;
1778 id_priv = container_of(id, struct rdma_id_private, id);
1779 if (!cma_comp(id_priv, CMA_CONNECT) &&
1780 !cma_comp(id_priv, CMA_DISCONNECT))
1783 ret = cma_modify_qp_err(id);
1787 switch (id->device->node_type) {
1789 /* Initiate or respond to a disconnect. */
1790 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
1791 ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
1799 EXPORT_SYMBOL(rdma_disconnect);
1801 static void cma_add_one(struct ib_device *device)
1803 struct cma_device *cma_dev;
1804 struct rdma_id_private *id_priv;
1806 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
1810 cma_dev->device = device;
1811 cma_dev->node_guid = device->node_guid;
1812 if (!cma_dev->node_guid)
1815 init_completion(&cma_dev->comp);
1816 atomic_set(&cma_dev->refcount, 1);
1817 INIT_LIST_HEAD(&cma_dev->id_list);
1818 ib_set_client_data(device, &cma_client, cma_dev);
1821 list_add_tail(&cma_dev->list, &dev_list);
1822 list_for_each_entry(id_priv, &listen_any_list, list)
1823 cma_listen_on_dev(id_priv, cma_dev);
1824 mutex_unlock(&lock);
1830 static int cma_remove_id_dev(struct rdma_id_private *id_priv)
1832 enum cma_state state;
1834 /* Record that we want to remove the device */
1835 state = cma_exch(id_priv, CMA_DEVICE_REMOVAL);
1836 if (state == CMA_DESTROYING)
1839 cma_cancel_operation(id_priv, state);
1840 wait_event(id_priv->wait_remove, !atomic_read(&id_priv->dev_remove));
1842 /* Check for destruction from another callback. */
1843 if (!cma_comp(id_priv, CMA_DEVICE_REMOVAL))
1846 return cma_notify_user(id_priv, RDMA_CM_EVENT_DEVICE_REMOVAL,
1850 static void cma_process_remove(struct cma_device *cma_dev)
1852 struct list_head remove_list;
1853 struct rdma_id_private *id_priv;
1856 INIT_LIST_HEAD(&remove_list);
1859 while (!list_empty(&cma_dev->id_list)) {
1860 id_priv = list_entry(cma_dev->id_list.next,
1861 struct rdma_id_private, list);
1863 if (cma_internal_listen(id_priv)) {
1864 cma_destroy_listen(id_priv);
1868 list_del(&id_priv->list);
1869 list_add_tail(&id_priv->list, &remove_list);
1870 atomic_inc(&id_priv->refcount);
1871 mutex_unlock(&lock);
1873 ret = cma_remove_id_dev(id_priv);
1874 cma_deref_id(id_priv);
1876 rdma_destroy_id(&id_priv->id);
1880 mutex_unlock(&lock);
1882 cma_deref_dev(cma_dev);
1883 wait_for_completion(&cma_dev->comp);
1886 static void cma_remove_one(struct ib_device *device)
1888 struct cma_device *cma_dev;
1890 cma_dev = ib_get_client_data(device, &cma_client);
1895 list_del(&cma_dev->list);
1896 mutex_unlock(&lock);
1898 cma_process_remove(cma_dev);
1902 static int cma_init(void)
1906 cma_wq = create_singlethread_workqueue("rdma_cm_wq");
1910 ret = ib_register_client(&cma_client);
1916 destroy_workqueue(cma_wq);
1920 static void cma_cleanup(void)
1922 ib_unregister_client(&cma_client);
1923 destroy_workqueue(cma_wq);
1924 idr_destroy(&sdp_ps);
1925 idr_destroy(&tcp_ps);
1928 module_init(cma_init);
1929 module_exit(cma_cleanup);