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);
75 struct list_head list;
76 struct ib_device *device;
78 struct completion comp;
80 struct list_head id_list;
97 struct rdma_bind_list {
99 struct hlist_head owners;
104 * Device removal can occur at anytime, so we need extra handling to
105 * serialize notifying the user of device removal with other callbacks.
106 * We do this by disabling removal notification while a callback is in process,
107 * and reporting it after the callback completes.
109 struct rdma_id_private {
110 struct rdma_cm_id id;
112 struct rdma_bind_list *bind_list;
113 struct hlist_node node;
114 struct list_head list;
115 struct list_head listen_list;
116 struct cma_device *cma_dev;
118 enum cma_state state;
120 struct completion comp;
122 wait_queue_head_t wait_remove;
127 struct ib_sa_query *query;
136 enum ib_qp_type qp_type;
141 struct work_struct work;
142 struct rdma_id_private *id;
143 enum cma_state old_state;
144 enum cma_state new_state;
145 struct rdma_cm_event event;
158 u8 ip_version; /* IP version: 7:4 */
160 union cma_ip_addr src_addr;
161 union cma_ip_addr dst_addr;
166 u8 sdp_version; /* Major version: 7:4 */
167 u8 ip_version; /* IP version: 7:4 */
168 u8 sdp_specific1[10];
171 union cma_ip_addr src_addr;
172 union cma_ip_addr dst_addr;
180 #define CMA_VERSION 0x00
181 #define SDP_MAJ_VERSION 0x2
183 static int cma_comp(struct rdma_id_private *id_priv, enum cma_state comp)
188 spin_lock_irqsave(&id_priv->lock, flags);
189 ret = (id_priv->state == comp);
190 spin_unlock_irqrestore(&id_priv->lock, flags);
194 static int cma_comp_exch(struct rdma_id_private *id_priv,
195 enum cma_state comp, enum cma_state exch)
200 spin_lock_irqsave(&id_priv->lock, flags);
201 if ((ret = (id_priv->state == comp)))
202 id_priv->state = exch;
203 spin_unlock_irqrestore(&id_priv->lock, flags);
207 static enum cma_state cma_exch(struct rdma_id_private *id_priv,
213 spin_lock_irqsave(&id_priv->lock, flags);
214 old = id_priv->state;
215 id_priv->state = exch;
216 spin_unlock_irqrestore(&id_priv->lock, flags);
220 static inline u8 cma_get_ip_ver(struct cma_hdr *hdr)
222 return hdr->ip_version >> 4;
225 static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
227 hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
230 static inline u8 sdp_get_majv(u8 sdp_version)
232 return sdp_version >> 4;
235 static inline u8 sdp_get_ip_ver(struct sdp_hh *hh)
237 return hh->ip_version >> 4;
240 static inline void sdp_set_ip_ver(struct sdp_hh *hh, u8 ip_ver)
242 hh->ip_version = (ip_ver << 4) | (hh->ip_version & 0xF);
245 static void cma_attach_to_dev(struct rdma_id_private *id_priv,
246 struct cma_device *cma_dev)
248 atomic_inc(&cma_dev->refcount);
249 id_priv->cma_dev = cma_dev;
250 id_priv->id.device = cma_dev->device;
251 list_add_tail(&id_priv->list, &cma_dev->id_list);
254 static inline void cma_deref_dev(struct cma_device *cma_dev)
256 if (atomic_dec_and_test(&cma_dev->refcount))
257 complete(&cma_dev->comp);
260 static void cma_detach_from_dev(struct rdma_id_private *id_priv)
262 list_del(&id_priv->list);
263 cma_deref_dev(id_priv->cma_dev);
264 id_priv->cma_dev = NULL;
267 static int cma_acquire_dev(struct rdma_id_private *id_priv)
269 enum rdma_node_type dev_type = id_priv->id.route.addr.dev_addr.dev_type;
270 struct cma_device *cma_dev;
274 switch (rdma_node_get_transport(dev_type)) {
275 case RDMA_TRANSPORT_IB:
276 ib_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
278 case RDMA_TRANSPORT_IWARP:
279 iw_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
285 list_for_each_entry(cma_dev, &dev_list, list) {
286 ret = ib_find_cached_gid(cma_dev->device, &gid,
287 &id_priv->id.port_num, NULL);
289 cma_attach_to_dev(id_priv, cma_dev);
296 static void cma_deref_id(struct rdma_id_private *id_priv)
298 if (atomic_dec_and_test(&id_priv->refcount))
299 complete(&id_priv->comp);
302 static void cma_release_remove(struct rdma_id_private *id_priv)
304 if (atomic_dec_and_test(&id_priv->dev_remove))
305 wake_up(&id_priv->wait_remove);
308 struct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler,
309 void *context, enum rdma_port_space ps)
311 struct rdma_id_private *id_priv;
313 id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
315 return ERR_PTR(-ENOMEM);
317 id_priv->state = CMA_IDLE;
318 id_priv->id.context = context;
319 id_priv->id.event_handler = event_handler;
321 spin_lock_init(&id_priv->lock);
322 init_completion(&id_priv->comp);
323 atomic_set(&id_priv->refcount, 1);
324 init_waitqueue_head(&id_priv->wait_remove);
325 atomic_set(&id_priv->dev_remove, 0);
326 INIT_LIST_HEAD(&id_priv->listen_list);
327 get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
331 EXPORT_SYMBOL(rdma_create_id);
333 static int cma_init_ib_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
335 struct ib_qp_attr qp_attr;
336 struct rdma_dev_addr *dev_addr;
339 dev_addr = &id_priv->id.route.addr.dev_addr;
340 ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
341 ib_addr_get_pkey(dev_addr),
342 &qp_attr.pkey_index);
346 qp_attr.qp_state = IB_QPS_INIT;
347 qp_attr.qp_access_flags = 0;
348 qp_attr.port_num = id_priv->id.port_num;
349 return ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_ACCESS_FLAGS |
350 IB_QP_PKEY_INDEX | IB_QP_PORT);
353 static int cma_init_iw_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
355 struct ib_qp_attr qp_attr;
357 qp_attr.qp_state = IB_QPS_INIT;
358 qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE;
360 return ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_ACCESS_FLAGS);
363 int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
364 struct ib_qp_init_attr *qp_init_attr)
366 struct rdma_id_private *id_priv;
370 id_priv = container_of(id, struct rdma_id_private, id);
371 if (id->device != pd->device)
374 qp = ib_create_qp(pd, qp_init_attr);
378 switch (rdma_node_get_transport(id->device->node_type)) {
379 case RDMA_TRANSPORT_IB:
380 ret = cma_init_ib_qp(id_priv, qp);
382 case RDMA_TRANSPORT_IWARP:
383 ret = cma_init_iw_qp(id_priv, qp);
394 id_priv->qp_num = qp->qp_num;
395 id_priv->qp_type = qp->qp_type;
396 id_priv->srq = (qp->srq != NULL);
402 EXPORT_SYMBOL(rdma_create_qp);
404 void rdma_destroy_qp(struct rdma_cm_id *id)
406 ib_destroy_qp(id->qp);
408 EXPORT_SYMBOL(rdma_destroy_qp);
410 static int cma_modify_qp_rtr(struct rdma_cm_id *id)
412 struct ib_qp_attr qp_attr;
413 int qp_attr_mask, ret;
418 /* Need to update QP attributes from default values. */
419 qp_attr.qp_state = IB_QPS_INIT;
420 ret = rdma_init_qp_attr(id, &qp_attr, &qp_attr_mask);
424 ret = ib_modify_qp(id->qp, &qp_attr, qp_attr_mask);
428 qp_attr.qp_state = IB_QPS_RTR;
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_rts(struct rdma_cm_id *id)
438 struct ib_qp_attr qp_attr;
439 int qp_attr_mask, ret;
444 qp_attr.qp_state = IB_QPS_RTS;
445 ret = rdma_init_qp_attr(id, &qp_attr, &qp_attr_mask);
449 return ib_modify_qp(id->qp, &qp_attr, qp_attr_mask);
452 static int cma_modify_qp_err(struct rdma_cm_id *id)
454 struct ib_qp_attr qp_attr;
459 qp_attr.qp_state = IB_QPS_ERR;
460 return ib_modify_qp(id->qp, &qp_attr, IB_QP_STATE);
463 int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
466 struct rdma_id_private *id_priv;
469 id_priv = container_of(id, struct rdma_id_private, id);
470 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
471 case RDMA_TRANSPORT_IB:
472 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
474 if (qp_attr->qp_state == IB_QPS_RTR)
475 qp_attr->rq_psn = id_priv->seq_num;
477 case RDMA_TRANSPORT_IWARP:
478 ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
488 EXPORT_SYMBOL(rdma_init_qp_attr);
490 static inline int cma_zero_addr(struct sockaddr *addr)
492 struct in6_addr *ip6;
494 if (addr->sa_family == AF_INET)
495 return ZERONET(((struct sockaddr_in *) addr)->sin_addr.s_addr);
497 ip6 = &((struct sockaddr_in6 *) addr)->sin6_addr;
498 return (ip6->s6_addr32[0] | ip6->s6_addr32[1] |
499 ip6->s6_addr32[2] | ip6->s6_addr32[3]) == 0;
503 static inline int cma_loopback_addr(struct sockaddr *addr)
505 return LOOPBACK(((struct sockaddr_in *) addr)->sin_addr.s_addr);
508 static inline int cma_any_addr(struct sockaddr *addr)
510 return cma_zero_addr(addr) || cma_loopback_addr(addr);
513 static inline int cma_any_port(struct sockaddr *addr)
515 return !((struct sockaddr_in *) addr)->sin_port;
518 static int cma_get_net_info(void *hdr, enum rdma_port_space ps,
519 u8 *ip_ver, __u16 *port,
520 union cma_ip_addr **src, union cma_ip_addr **dst)
524 if (sdp_get_majv(((struct sdp_hh *) hdr)->sdp_version) !=
528 *ip_ver = sdp_get_ip_ver(hdr);
529 *port = ((struct sdp_hh *) hdr)->port;
530 *src = &((struct sdp_hh *) hdr)->src_addr;
531 *dst = &((struct sdp_hh *) hdr)->dst_addr;
534 if (((struct cma_hdr *) hdr)->cma_version != CMA_VERSION)
537 *ip_ver = cma_get_ip_ver(hdr);
538 *port = ((struct cma_hdr *) hdr)->port;
539 *src = &((struct cma_hdr *) hdr)->src_addr;
540 *dst = &((struct cma_hdr *) hdr)->dst_addr;
544 if (*ip_ver != 4 && *ip_ver != 6)
549 static void cma_save_net_info(struct rdma_addr *addr,
550 struct rdma_addr *listen_addr,
551 u8 ip_ver, __u16 port,
552 union cma_ip_addr *src, union cma_ip_addr *dst)
554 struct sockaddr_in *listen4, *ip4;
555 struct sockaddr_in6 *listen6, *ip6;
559 listen4 = (struct sockaddr_in *) &listen_addr->src_addr;
560 ip4 = (struct sockaddr_in *) &addr->src_addr;
561 ip4->sin_family = listen4->sin_family;
562 ip4->sin_addr.s_addr = dst->ip4.addr;
563 ip4->sin_port = listen4->sin_port;
565 ip4 = (struct sockaddr_in *) &addr->dst_addr;
566 ip4->sin_family = listen4->sin_family;
567 ip4->sin_addr.s_addr = src->ip4.addr;
568 ip4->sin_port = port;
571 listen6 = (struct sockaddr_in6 *) &listen_addr->src_addr;
572 ip6 = (struct sockaddr_in6 *) &addr->src_addr;
573 ip6->sin6_family = listen6->sin6_family;
574 ip6->sin6_addr = dst->ip6;
575 ip6->sin6_port = listen6->sin6_port;
577 ip6 = (struct sockaddr_in6 *) &addr->dst_addr;
578 ip6->sin6_family = listen6->sin6_family;
579 ip6->sin6_addr = src->ip6;
580 ip6->sin6_port = port;
587 static inline int cma_user_data_offset(enum rdma_port_space ps)
593 return sizeof(struct cma_hdr);
597 static int cma_notify_user(struct rdma_id_private *id_priv,
598 enum rdma_cm_event_type type, int status,
599 void *data, u8 data_len)
601 struct rdma_cm_event event;
604 event.status = status;
605 event.private_data = data;
606 event.private_data_len = data_len;
608 return id_priv->id.event_handler(&id_priv->id, &event);
611 static void cma_cancel_route(struct rdma_id_private *id_priv)
613 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
614 case RDMA_TRANSPORT_IB:
616 ib_sa_cancel_query(id_priv->query_id, id_priv->query);
623 static inline int cma_internal_listen(struct rdma_id_private *id_priv)
625 return (id_priv->state == CMA_LISTEN) && id_priv->cma_dev &&
626 cma_any_addr(&id_priv->id.route.addr.src_addr);
629 static void cma_destroy_listen(struct rdma_id_private *id_priv)
631 cma_exch(id_priv, CMA_DESTROYING);
633 if (id_priv->cma_dev) {
634 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
635 case RDMA_TRANSPORT_IB:
636 if (id_priv->cm_id.ib && !IS_ERR(id_priv->cm_id.ib))
637 ib_destroy_cm_id(id_priv->cm_id.ib);
639 case RDMA_TRANSPORT_IWARP:
640 if (id_priv->cm_id.iw && !IS_ERR(id_priv->cm_id.iw))
641 iw_destroy_cm_id(id_priv->cm_id.iw);
646 cma_detach_from_dev(id_priv);
648 list_del(&id_priv->listen_list);
650 cma_deref_id(id_priv);
651 wait_for_completion(&id_priv->comp);
656 static void cma_cancel_listens(struct rdma_id_private *id_priv)
658 struct rdma_id_private *dev_id_priv;
661 list_del(&id_priv->list);
663 while (!list_empty(&id_priv->listen_list)) {
664 dev_id_priv = list_entry(id_priv->listen_list.next,
665 struct rdma_id_private, listen_list);
666 cma_destroy_listen(dev_id_priv);
671 static void cma_cancel_operation(struct rdma_id_private *id_priv,
672 enum cma_state state)
676 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
678 case CMA_ROUTE_QUERY:
679 cma_cancel_route(id_priv);
682 if (cma_any_addr(&id_priv->id.route.addr.src_addr) &&
684 cma_cancel_listens(id_priv);
691 static void cma_release_port(struct rdma_id_private *id_priv)
693 struct rdma_bind_list *bind_list = id_priv->bind_list;
699 hlist_del(&id_priv->node);
700 if (hlist_empty(&bind_list->owners)) {
701 idr_remove(bind_list->ps, bind_list->port);
707 void rdma_destroy_id(struct rdma_cm_id *id)
709 struct rdma_id_private *id_priv;
710 enum cma_state state;
712 id_priv = container_of(id, struct rdma_id_private, id);
713 state = cma_exch(id_priv, CMA_DESTROYING);
714 cma_cancel_operation(id_priv, state);
717 if (id_priv->cma_dev) {
719 switch (rdma_node_get_transport(id->device->node_type)) {
720 case RDMA_TRANSPORT_IB:
721 if (id_priv->cm_id.ib && !IS_ERR(id_priv->cm_id.ib))
722 ib_destroy_cm_id(id_priv->cm_id.ib);
724 case RDMA_TRANSPORT_IWARP:
725 if (id_priv->cm_id.iw && !IS_ERR(id_priv->cm_id.iw))
726 iw_destroy_cm_id(id_priv->cm_id.iw);
732 cma_detach_from_dev(id_priv);
736 cma_release_port(id_priv);
737 cma_deref_id(id_priv);
738 wait_for_completion(&id_priv->comp);
740 kfree(id_priv->id.route.path_rec);
743 EXPORT_SYMBOL(rdma_destroy_id);
745 static int cma_rep_recv(struct rdma_id_private *id_priv)
749 ret = cma_modify_qp_rtr(&id_priv->id);
753 ret = cma_modify_qp_rts(&id_priv->id);
757 ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
763 cma_modify_qp_err(&id_priv->id);
764 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
769 static int cma_verify_rep(struct rdma_id_private *id_priv, void *data)
771 if (id_priv->id.ps == RDMA_PS_SDP &&
772 sdp_get_majv(((struct sdp_hah *) data)->sdp_version) !=
779 static int cma_rtu_recv(struct rdma_id_private *id_priv)
783 ret = cma_modify_qp_rts(&id_priv->id);
789 cma_modify_qp_err(&id_priv->id);
790 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
795 static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
797 struct rdma_id_private *id_priv = cm_id->context;
798 enum rdma_cm_event_type event;
799 u8 private_data_len = 0;
800 int ret = 0, status = 0;
802 atomic_inc(&id_priv->dev_remove);
803 if (!cma_comp(id_priv, CMA_CONNECT))
806 switch (ib_event->event) {
807 case IB_CM_REQ_ERROR:
808 case IB_CM_REP_ERROR:
809 event = RDMA_CM_EVENT_UNREACHABLE;
812 case IB_CM_REP_RECEIVED:
813 status = cma_verify_rep(id_priv, ib_event->private_data);
815 event = RDMA_CM_EVENT_CONNECT_ERROR;
816 else if (id_priv->id.qp && id_priv->id.ps != RDMA_PS_SDP) {
817 status = cma_rep_recv(id_priv);
818 event = status ? RDMA_CM_EVENT_CONNECT_ERROR :
819 RDMA_CM_EVENT_ESTABLISHED;
821 event = RDMA_CM_EVENT_CONNECT_RESPONSE;
822 private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
824 case IB_CM_RTU_RECEIVED:
825 status = cma_rtu_recv(id_priv);
826 event = status ? RDMA_CM_EVENT_CONNECT_ERROR :
827 RDMA_CM_EVENT_ESTABLISHED;
829 case IB_CM_DREQ_ERROR:
830 status = -ETIMEDOUT; /* fall through */
831 case IB_CM_DREQ_RECEIVED:
832 case IB_CM_DREP_RECEIVED:
833 if (!cma_comp_exch(id_priv, CMA_CONNECT, CMA_DISCONNECT))
835 event = RDMA_CM_EVENT_DISCONNECTED;
837 case IB_CM_TIMEWAIT_EXIT:
838 case IB_CM_MRA_RECEIVED:
841 case IB_CM_REJ_RECEIVED:
842 cma_modify_qp_err(&id_priv->id);
843 status = ib_event->param.rej_rcvd.reason;
844 event = RDMA_CM_EVENT_REJECTED;
845 private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
848 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d",
853 ret = cma_notify_user(id_priv, event, status, ib_event->private_data,
856 /* Destroy the CM ID by returning a non-zero value. */
857 id_priv->cm_id.ib = NULL;
858 cma_exch(id_priv, CMA_DESTROYING);
859 cma_release_remove(id_priv);
860 rdma_destroy_id(&id_priv->id);
864 cma_release_remove(id_priv);
868 static struct rdma_id_private *cma_new_id(struct rdma_cm_id *listen_id,
869 struct ib_cm_event *ib_event)
871 struct rdma_id_private *id_priv;
872 struct rdma_cm_id *id;
873 struct rdma_route *rt;
874 union cma_ip_addr *src, *dst;
878 if (cma_get_net_info(ib_event->private_data, listen_id->ps,
879 &ip_ver, &port, &src, &dst))
882 id = rdma_create_id(listen_id->event_handler, listen_id->context,
887 cma_save_net_info(&id->route.addr, &listen_id->route.addr,
888 ip_ver, port, src, dst);
891 rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
892 rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
897 rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
898 if (rt->num_paths == 2)
899 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
901 ib_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
902 ib_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
903 ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
904 rt->addr.dev_addr.dev_type = RDMA_NODE_IB_CA;
906 id_priv = container_of(id, struct rdma_id_private, id);
907 id_priv->state = CMA_CONNECT;
916 static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
918 struct rdma_id_private *listen_id, *conn_id;
921 listen_id = cm_id->context;
922 atomic_inc(&listen_id->dev_remove);
923 if (!cma_comp(listen_id, CMA_LISTEN)) {
928 conn_id = cma_new_id(&listen_id->id, ib_event);
934 atomic_inc(&conn_id->dev_remove);
936 ret = cma_acquire_dev(conn_id);
939 goto release_conn_id;
941 conn_id->cm_id.ib = cm_id;
942 cm_id->context = conn_id;
943 cm_id->cm_handler = cma_ib_handler;
945 offset = cma_user_data_offset(listen_id->id.ps);
946 ret = cma_notify_user(conn_id, RDMA_CM_EVENT_CONNECT_REQUEST, 0,
947 ib_event->private_data + offset,
948 IB_CM_REQ_PRIVATE_DATA_SIZE - offset);
952 /* Destroy the CM ID by returning a non-zero value. */
953 conn_id->cm_id.ib = NULL;
956 cma_exch(conn_id, CMA_DESTROYING);
957 cma_release_remove(conn_id);
958 rdma_destroy_id(&conn_id->id);
961 cma_release_remove(listen_id);
965 static __be64 cma_get_service_id(enum rdma_port_space ps, struct sockaddr *addr)
967 return cpu_to_be64(((u64)ps << 16) +
968 be16_to_cpu(((struct sockaddr_in *) addr)->sin_port));
971 static void cma_set_compare_data(enum rdma_port_space ps, struct sockaddr *addr,
972 struct ib_cm_compare_data *compare)
974 struct cma_hdr *cma_data, *cma_mask;
975 struct sdp_hh *sdp_data, *sdp_mask;
977 struct in6_addr ip6_addr;
979 memset(compare, 0, sizeof *compare);
980 cma_data = (void *) compare->data;
981 cma_mask = (void *) compare->mask;
982 sdp_data = (void *) compare->data;
983 sdp_mask = (void *) compare->mask;
985 switch (addr->sa_family) {
987 ip4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
988 if (ps == RDMA_PS_SDP) {
989 sdp_set_ip_ver(sdp_data, 4);
990 sdp_set_ip_ver(sdp_mask, 0xF);
991 sdp_data->dst_addr.ip4.addr = ip4_addr;
992 sdp_mask->dst_addr.ip4.addr = ~0;
994 cma_set_ip_ver(cma_data, 4);
995 cma_set_ip_ver(cma_mask, 0xF);
996 cma_data->dst_addr.ip4.addr = ip4_addr;
997 cma_mask->dst_addr.ip4.addr = ~0;
1001 ip6_addr = ((struct sockaddr_in6 *) addr)->sin6_addr;
1002 if (ps == RDMA_PS_SDP) {
1003 sdp_set_ip_ver(sdp_data, 6);
1004 sdp_set_ip_ver(sdp_mask, 0xF);
1005 sdp_data->dst_addr.ip6 = ip6_addr;
1006 memset(&sdp_mask->dst_addr.ip6, 0xFF,
1007 sizeof sdp_mask->dst_addr.ip6);
1009 cma_set_ip_ver(cma_data, 6);
1010 cma_set_ip_ver(cma_mask, 0xF);
1011 cma_data->dst_addr.ip6 = ip6_addr;
1012 memset(&cma_mask->dst_addr.ip6, 0xFF,
1013 sizeof cma_mask->dst_addr.ip6);
1021 static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
1023 struct rdma_id_private *id_priv = iw_id->context;
1024 enum rdma_cm_event_type event = 0;
1025 struct sockaddr_in *sin;
1028 atomic_inc(&id_priv->dev_remove);
1030 switch (iw_event->event) {
1031 case IW_CM_EVENT_CLOSE:
1032 event = RDMA_CM_EVENT_DISCONNECTED;
1034 case IW_CM_EVENT_CONNECT_REPLY:
1035 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1036 *sin = iw_event->local_addr;
1037 sin = (struct sockaddr_in *) &id_priv->id.route.addr.dst_addr;
1038 *sin = iw_event->remote_addr;
1039 if (iw_event->status)
1040 event = RDMA_CM_EVENT_REJECTED;
1042 event = RDMA_CM_EVENT_ESTABLISHED;
1044 case IW_CM_EVENT_ESTABLISHED:
1045 event = RDMA_CM_EVENT_ESTABLISHED;
1051 ret = cma_notify_user(id_priv, event, iw_event->status,
1052 iw_event->private_data,
1053 iw_event->private_data_len);
1055 /* Destroy the CM ID by returning a non-zero value. */
1056 id_priv->cm_id.iw = NULL;
1057 cma_exch(id_priv, CMA_DESTROYING);
1058 cma_release_remove(id_priv);
1059 rdma_destroy_id(&id_priv->id);
1063 cma_release_remove(id_priv);
1067 static int iw_conn_req_handler(struct iw_cm_id *cm_id,
1068 struct iw_cm_event *iw_event)
1070 struct rdma_cm_id *new_cm_id;
1071 struct rdma_id_private *listen_id, *conn_id;
1072 struct sockaddr_in *sin;
1073 struct net_device *dev = NULL;
1076 listen_id = cm_id->context;
1077 atomic_inc(&listen_id->dev_remove);
1078 if (!cma_comp(listen_id, CMA_LISTEN)) {
1079 ret = -ECONNABORTED;
1083 /* Create a new RDMA id for the new IW CM ID */
1084 new_cm_id = rdma_create_id(listen_id->id.event_handler,
1085 listen_id->id.context,
1091 conn_id = container_of(new_cm_id, struct rdma_id_private, id);
1092 atomic_inc(&conn_id->dev_remove);
1093 conn_id->state = CMA_CONNECT;
1095 dev = ip_dev_find(iw_event->local_addr.sin_addr.s_addr);
1097 ret = -EADDRNOTAVAIL;
1098 cma_release_remove(conn_id);
1099 rdma_destroy_id(new_cm_id);
1102 ret = rdma_copy_addr(&conn_id->id.route.addr.dev_addr, dev, NULL);
1104 cma_release_remove(conn_id);
1105 rdma_destroy_id(new_cm_id);
1110 ret = cma_acquire_dev(conn_id);
1111 mutex_unlock(&lock);
1113 cma_release_remove(conn_id);
1114 rdma_destroy_id(new_cm_id);
1118 conn_id->cm_id.iw = cm_id;
1119 cm_id->context = conn_id;
1120 cm_id->cm_handler = cma_iw_handler;
1122 sin = (struct sockaddr_in *) &new_cm_id->route.addr.src_addr;
1123 *sin = iw_event->local_addr;
1124 sin = (struct sockaddr_in *) &new_cm_id->route.addr.dst_addr;
1125 *sin = iw_event->remote_addr;
1127 ret = cma_notify_user(conn_id, RDMA_CM_EVENT_CONNECT_REQUEST, 0,
1128 iw_event->private_data,
1129 iw_event->private_data_len);
1131 /* User wants to destroy the CM ID */
1132 conn_id->cm_id.iw = NULL;
1133 cma_exch(conn_id, CMA_DESTROYING);
1134 cma_release_remove(conn_id);
1135 rdma_destroy_id(&conn_id->id);
1141 cma_release_remove(listen_id);
1145 static int cma_ib_listen(struct rdma_id_private *id_priv)
1147 struct ib_cm_compare_data compare_data;
1148 struct sockaddr *addr;
1152 id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_req_handler,
1154 if (IS_ERR(id_priv->cm_id.ib))
1155 return PTR_ERR(id_priv->cm_id.ib);
1157 addr = &id_priv->id.route.addr.src_addr;
1158 svc_id = cma_get_service_id(id_priv->id.ps, addr);
1159 if (cma_any_addr(addr))
1160 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, NULL);
1162 cma_set_compare_data(id_priv->id.ps, addr, &compare_data);
1163 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, &compare_data);
1167 ib_destroy_cm_id(id_priv->cm_id.ib);
1168 id_priv->cm_id.ib = NULL;
1174 static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
1177 struct sockaddr_in *sin;
1179 id_priv->cm_id.iw = iw_create_cm_id(id_priv->id.device,
1180 iw_conn_req_handler,
1182 if (IS_ERR(id_priv->cm_id.iw))
1183 return PTR_ERR(id_priv->cm_id.iw);
1185 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1186 id_priv->cm_id.iw->local_addr = *sin;
1188 ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
1191 iw_destroy_cm_id(id_priv->cm_id.iw);
1192 id_priv->cm_id.iw = NULL;
1198 static int cma_listen_handler(struct rdma_cm_id *id,
1199 struct rdma_cm_event *event)
1201 struct rdma_id_private *id_priv = id->context;
1203 id->context = id_priv->id.context;
1204 id->event_handler = id_priv->id.event_handler;
1205 return id_priv->id.event_handler(id, event);
1208 static void cma_listen_on_dev(struct rdma_id_private *id_priv,
1209 struct cma_device *cma_dev)
1211 struct rdma_id_private *dev_id_priv;
1212 struct rdma_cm_id *id;
1215 id = rdma_create_id(cma_listen_handler, id_priv, id_priv->id.ps);
1219 dev_id_priv = container_of(id, struct rdma_id_private, id);
1221 dev_id_priv->state = CMA_ADDR_BOUND;
1222 memcpy(&id->route.addr.src_addr, &id_priv->id.route.addr.src_addr,
1223 ip_addr_size(&id_priv->id.route.addr.src_addr));
1225 cma_attach_to_dev(dev_id_priv, cma_dev);
1226 list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
1228 ret = rdma_listen(id, id_priv->backlog);
1234 cma_destroy_listen(dev_id_priv);
1237 static void cma_listen_on_all(struct rdma_id_private *id_priv)
1239 struct cma_device *cma_dev;
1242 list_add_tail(&id_priv->list, &listen_any_list);
1243 list_for_each_entry(cma_dev, &dev_list, list)
1244 cma_listen_on_dev(id_priv, cma_dev);
1245 mutex_unlock(&lock);
1248 static int cma_bind_any(struct rdma_cm_id *id, sa_family_t af)
1250 struct sockaddr_in addr_in;
1252 memset(&addr_in, 0, sizeof addr_in);
1253 addr_in.sin_family = af;
1254 return rdma_bind_addr(id, (struct sockaddr *) &addr_in);
1257 int rdma_listen(struct rdma_cm_id *id, int backlog)
1259 struct rdma_id_private *id_priv;
1262 id_priv = container_of(id, struct rdma_id_private, id);
1263 if (id_priv->state == CMA_IDLE) {
1264 ret = cma_bind_any(id, AF_INET);
1269 if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_LISTEN))
1272 id_priv->backlog = backlog;
1274 switch (rdma_node_get_transport(id->device->node_type)) {
1275 case RDMA_TRANSPORT_IB:
1276 ret = cma_ib_listen(id_priv);
1280 case RDMA_TRANSPORT_IWARP:
1281 ret = cma_iw_listen(id_priv, backlog);
1290 cma_listen_on_all(id_priv);
1294 id_priv->backlog = 0;
1295 cma_comp_exch(id_priv, CMA_LISTEN, CMA_ADDR_BOUND);
1298 EXPORT_SYMBOL(rdma_listen);
1300 static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
1303 struct cma_work *work = context;
1304 struct rdma_route *route;
1306 route = &work->id->id.route;
1309 route->num_paths = 1;
1310 *route->path_rec = *path_rec;
1312 work->old_state = CMA_ROUTE_QUERY;
1313 work->new_state = CMA_ADDR_RESOLVED;
1314 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
1315 work->event.status = status;
1318 queue_work(cma_wq, &work->work);
1321 static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
1322 struct cma_work *work)
1324 struct rdma_dev_addr *addr = &id_priv->id.route.addr.dev_addr;
1325 struct ib_sa_path_rec path_rec;
1327 memset(&path_rec, 0, sizeof path_rec);
1328 ib_addr_get_sgid(addr, &path_rec.sgid);
1329 ib_addr_get_dgid(addr, &path_rec.dgid);
1330 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(addr));
1331 path_rec.numb_path = 1;
1333 id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
1334 id_priv->id.port_num, &path_rec,
1335 IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
1336 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH,
1337 timeout_ms, GFP_KERNEL,
1338 cma_query_handler, work, &id_priv->query);
1340 return (id_priv->query_id < 0) ? id_priv->query_id : 0;
1343 static void cma_work_handler(struct work_struct *_work)
1345 struct cma_work *work = container_of(_work, struct cma_work, work);
1346 struct rdma_id_private *id_priv = work->id;
1349 atomic_inc(&id_priv->dev_remove);
1350 if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
1353 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
1354 cma_exch(id_priv, CMA_DESTROYING);
1358 cma_release_remove(id_priv);
1359 cma_deref_id(id_priv);
1361 rdma_destroy_id(&id_priv->id);
1365 static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
1367 struct rdma_route *route = &id_priv->id.route;
1368 struct cma_work *work;
1371 work = kzalloc(sizeof *work, GFP_KERNEL);
1376 INIT_WORK(&work->work, cma_work_handler);
1377 work->old_state = CMA_ROUTE_QUERY;
1378 work->new_state = CMA_ROUTE_RESOLVED;
1379 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1381 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
1382 if (!route->path_rec) {
1387 ret = cma_query_ib_route(id_priv, timeout_ms, work);
1393 kfree(route->path_rec);
1394 route->path_rec = NULL;
1400 int rdma_set_ib_paths(struct rdma_cm_id *id,
1401 struct ib_sa_path_rec *path_rec, int num_paths)
1403 struct rdma_id_private *id_priv;
1406 id_priv = container_of(id, struct rdma_id_private, id);
1407 if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ROUTE_RESOLVED))
1410 id->route.path_rec = kmalloc(sizeof *path_rec * num_paths, GFP_KERNEL);
1411 if (!id->route.path_rec) {
1416 memcpy(id->route.path_rec, path_rec, sizeof *path_rec * num_paths);
1419 cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_ADDR_RESOLVED);
1422 EXPORT_SYMBOL(rdma_set_ib_paths);
1424 static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
1426 struct cma_work *work;
1428 work = kzalloc(sizeof *work, GFP_KERNEL);
1433 INIT_WORK(&work->work, cma_work_handler);
1434 work->old_state = CMA_ROUTE_QUERY;
1435 work->new_state = CMA_ROUTE_RESOLVED;
1436 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1437 queue_work(cma_wq, &work->work);
1441 int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
1443 struct rdma_id_private *id_priv;
1446 id_priv = container_of(id, struct rdma_id_private, id);
1447 if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ROUTE_QUERY))
1450 atomic_inc(&id_priv->refcount);
1451 switch (rdma_node_get_transport(id->device->node_type)) {
1452 case RDMA_TRANSPORT_IB:
1453 ret = cma_resolve_ib_route(id_priv, timeout_ms);
1455 case RDMA_TRANSPORT_IWARP:
1456 ret = cma_resolve_iw_route(id_priv, timeout_ms);
1467 cma_comp_exch(id_priv, CMA_ROUTE_QUERY, CMA_ADDR_RESOLVED);
1468 cma_deref_id(id_priv);
1471 EXPORT_SYMBOL(rdma_resolve_route);
1473 static int cma_bind_loopback(struct rdma_id_private *id_priv)
1475 struct cma_device *cma_dev;
1476 struct ib_port_attr port_attr;
1483 if (list_empty(&dev_list)) {
1487 list_for_each_entry(cma_dev, &dev_list, list)
1488 for (p = 1; p <= cma_dev->device->phys_port_cnt; ++p)
1489 if (!ib_query_port(cma_dev->device, p, &port_attr) &&
1490 port_attr.state == IB_PORT_ACTIVE)
1494 cma_dev = list_entry(dev_list.next, struct cma_device, list);
1497 ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid);
1501 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
1505 ib_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
1506 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
1507 id_priv->id.port_num = p;
1508 cma_attach_to_dev(id_priv, cma_dev);
1510 mutex_unlock(&lock);
1514 static void addr_handler(int status, struct sockaddr *src_addr,
1515 struct rdma_dev_addr *dev_addr, void *context)
1517 struct rdma_id_private *id_priv = context;
1518 enum rdma_cm_event_type event;
1520 atomic_inc(&id_priv->dev_remove);
1523 * Grab mutex to block rdma_destroy_id() from removing the device while
1524 * we're trying to acquire it.
1527 if (!cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_RESOLVED)) {
1528 mutex_unlock(&lock);
1532 if (!status && !id_priv->cma_dev)
1533 status = cma_acquire_dev(id_priv);
1534 mutex_unlock(&lock);
1537 if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ADDR_BOUND))
1539 event = RDMA_CM_EVENT_ADDR_ERROR;
1541 memcpy(&id_priv->id.route.addr.src_addr, src_addr,
1542 ip_addr_size(src_addr));
1543 event = RDMA_CM_EVENT_ADDR_RESOLVED;
1546 if (cma_notify_user(id_priv, event, status, NULL, 0)) {
1547 cma_exch(id_priv, CMA_DESTROYING);
1548 cma_release_remove(id_priv);
1549 cma_deref_id(id_priv);
1550 rdma_destroy_id(&id_priv->id);
1554 cma_release_remove(id_priv);
1555 cma_deref_id(id_priv);
1558 static int cma_resolve_loopback(struct rdma_id_private *id_priv)
1560 struct cma_work *work;
1561 struct sockaddr_in *src_in, *dst_in;
1565 work = kzalloc(sizeof *work, GFP_KERNEL);
1569 if (!id_priv->cma_dev) {
1570 ret = cma_bind_loopback(id_priv);
1575 ib_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
1576 ib_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
1578 if (cma_zero_addr(&id_priv->id.route.addr.src_addr)) {
1579 src_in = (struct sockaddr_in *)&id_priv->id.route.addr.src_addr;
1580 dst_in = (struct sockaddr_in *)&id_priv->id.route.addr.dst_addr;
1581 src_in->sin_family = dst_in->sin_family;
1582 src_in->sin_addr.s_addr = dst_in->sin_addr.s_addr;
1586 INIT_WORK(&work->work, cma_work_handler);
1587 work->old_state = CMA_ADDR_QUERY;
1588 work->new_state = CMA_ADDR_RESOLVED;
1589 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
1590 queue_work(cma_wq, &work->work);
1597 static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
1598 struct sockaddr *dst_addr)
1600 if (src_addr && src_addr->sa_family)
1601 return rdma_bind_addr(id, src_addr);
1603 return cma_bind_any(id, dst_addr->sa_family);
1606 int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
1607 struct sockaddr *dst_addr, int timeout_ms)
1609 struct rdma_id_private *id_priv;
1612 id_priv = container_of(id, struct rdma_id_private, id);
1613 if (id_priv->state == CMA_IDLE) {
1614 ret = cma_bind_addr(id, src_addr, dst_addr);
1619 if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_ADDR_QUERY))
1622 atomic_inc(&id_priv->refcount);
1623 memcpy(&id->route.addr.dst_addr, dst_addr, ip_addr_size(dst_addr));
1624 if (cma_any_addr(dst_addr))
1625 ret = cma_resolve_loopback(id_priv);
1627 ret = rdma_resolve_ip(&addr_client, &id->route.addr.src_addr,
1628 dst_addr, &id->route.addr.dev_addr,
1629 timeout_ms, addr_handler, id_priv);
1635 cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_BOUND);
1636 cma_deref_id(id_priv);
1639 EXPORT_SYMBOL(rdma_resolve_addr);
1641 static void cma_bind_port(struct rdma_bind_list *bind_list,
1642 struct rdma_id_private *id_priv)
1644 struct sockaddr_in *sin;
1646 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1647 sin->sin_port = htons(bind_list->port);
1648 id_priv->bind_list = bind_list;
1649 hlist_add_head(&id_priv->node, &bind_list->owners);
1652 static int cma_alloc_port(struct idr *ps, struct rdma_id_private *id_priv,
1653 unsigned short snum)
1655 struct rdma_bind_list *bind_list;
1656 int port, start, ret;
1658 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
1662 start = snum ? snum : sysctl_local_port_range[0];
1665 ret = idr_get_new_above(ps, bind_list, start, &port);
1666 } while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL));
1671 if ((snum && port != snum) ||
1672 (!snum && port > sysctl_local_port_range[1])) {
1673 idr_remove(ps, port);
1674 ret = -EADDRNOTAVAIL;
1679 bind_list->port = (unsigned short) port;
1680 cma_bind_port(bind_list, id_priv);
1687 static int cma_use_port(struct idr *ps, struct rdma_id_private *id_priv)
1689 struct rdma_id_private *cur_id;
1690 struct sockaddr_in *sin, *cur_sin;
1691 struct rdma_bind_list *bind_list;
1692 struct hlist_node *node;
1693 unsigned short snum;
1695 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1696 snum = ntohs(sin->sin_port);
1697 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
1700 bind_list = idr_find(ps, snum);
1702 return cma_alloc_port(ps, id_priv, snum);
1705 * We don't support binding to any address if anyone is bound to
1706 * a specific address on the same port.
1708 if (cma_any_addr(&id_priv->id.route.addr.src_addr))
1709 return -EADDRNOTAVAIL;
1711 hlist_for_each_entry(cur_id, node, &bind_list->owners, node) {
1712 if (cma_any_addr(&cur_id->id.route.addr.src_addr))
1713 return -EADDRNOTAVAIL;
1715 cur_sin = (struct sockaddr_in *) &cur_id->id.route.addr.src_addr;
1716 if (sin->sin_addr.s_addr == cur_sin->sin_addr.s_addr)
1720 cma_bind_port(bind_list, id_priv);
1724 static int cma_get_port(struct rdma_id_private *id_priv)
1729 switch (id_priv->id.ps) {
1737 return -EPROTONOSUPPORT;
1741 if (cma_any_port(&id_priv->id.route.addr.src_addr))
1742 ret = cma_alloc_port(ps, id_priv, 0);
1744 ret = cma_use_port(ps, id_priv);
1745 mutex_unlock(&lock);
1750 int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
1752 struct rdma_id_private *id_priv;
1755 if (addr->sa_family != AF_INET)
1756 return -EAFNOSUPPORT;
1758 id_priv = container_of(id, struct rdma_id_private, id);
1759 if (!cma_comp_exch(id_priv, CMA_IDLE, CMA_ADDR_BOUND))
1762 if (!cma_any_addr(addr)) {
1763 ret = rdma_translate_ip(addr, &id->route.addr.dev_addr);
1768 ret = cma_acquire_dev(id_priv);
1769 mutex_unlock(&lock);
1774 memcpy(&id->route.addr.src_addr, addr, ip_addr_size(addr));
1775 ret = cma_get_port(id_priv);
1781 if (!cma_any_addr(addr)) {
1783 cma_detach_from_dev(id_priv);
1784 mutex_unlock(&lock);
1787 cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_IDLE);
1790 EXPORT_SYMBOL(rdma_bind_addr);
1792 static int cma_format_hdr(void *hdr, enum rdma_port_space ps,
1793 struct rdma_route *route)
1795 struct sockaddr_in *src4, *dst4;
1796 struct cma_hdr *cma_hdr;
1797 struct sdp_hh *sdp_hdr;
1799 src4 = (struct sockaddr_in *) &route->addr.src_addr;
1800 dst4 = (struct sockaddr_in *) &route->addr.dst_addr;
1805 if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION)
1807 sdp_set_ip_ver(sdp_hdr, 4);
1808 sdp_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
1809 sdp_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
1810 sdp_hdr->port = src4->sin_port;
1814 cma_hdr->cma_version = CMA_VERSION;
1815 cma_set_ip_ver(cma_hdr, 4);
1816 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
1817 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
1818 cma_hdr->port = src4->sin_port;
1824 static int cma_connect_ib(struct rdma_id_private *id_priv,
1825 struct rdma_conn_param *conn_param)
1827 struct ib_cm_req_param req;
1828 struct rdma_route *route;
1832 memset(&req, 0, sizeof req);
1833 offset = cma_user_data_offset(id_priv->id.ps);
1834 req.private_data_len = offset + conn_param->private_data_len;
1835 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
1839 if (conn_param->private_data && conn_param->private_data_len)
1840 memcpy(private_data + offset, conn_param->private_data,
1841 conn_param->private_data_len);
1843 id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_ib_handler,
1845 if (IS_ERR(id_priv->cm_id.ib)) {
1846 ret = PTR_ERR(id_priv->cm_id.ib);
1850 route = &id_priv->id.route;
1851 ret = cma_format_hdr(private_data, id_priv->id.ps, route);
1854 req.private_data = private_data;
1856 req.primary_path = &route->path_rec[0];
1857 if (route->num_paths == 2)
1858 req.alternate_path = &route->path_rec[1];
1860 req.service_id = cma_get_service_id(id_priv->id.ps,
1861 &route->addr.dst_addr);
1862 req.qp_num = id_priv->qp_num;
1863 req.qp_type = id_priv->qp_type;
1864 req.starting_psn = id_priv->seq_num;
1865 req.responder_resources = conn_param->responder_resources;
1866 req.initiator_depth = conn_param->initiator_depth;
1867 req.flow_control = conn_param->flow_control;
1868 req.retry_count = conn_param->retry_count;
1869 req.rnr_retry_count = conn_param->rnr_retry_count;
1870 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
1871 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
1872 req.max_cm_retries = CMA_MAX_CM_RETRIES;
1873 req.srq = id_priv->srq ? 1 : 0;
1875 ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
1877 if (ret && !IS_ERR(id_priv->cm_id.ib)) {
1878 ib_destroy_cm_id(id_priv->cm_id.ib);
1879 id_priv->cm_id.ib = NULL;
1882 kfree(private_data);
1886 static int cma_connect_iw(struct rdma_id_private *id_priv,
1887 struct rdma_conn_param *conn_param)
1889 struct iw_cm_id *cm_id;
1890 struct sockaddr_in* sin;
1892 struct iw_cm_conn_param iw_param;
1894 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
1895 if (IS_ERR(cm_id)) {
1896 ret = PTR_ERR(cm_id);
1900 id_priv->cm_id.iw = cm_id;
1902 sin = (struct sockaddr_in*) &id_priv->id.route.addr.src_addr;
1903 cm_id->local_addr = *sin;
1905 sin = (struct sockaddr_in*) &id_priv->id.route.addr.dst_addr;
1906 cm_id->remote_addr = *sin;
1908 ret = cma_modify_qp_rtr(&id_priv->id);
1912 iw_param.ord = conn_param->initiator_depth;
1913 iw_param.ird = conn_param->responder_resources;
1914 iw_param.private_data = conn_param->private_data;
1915 iw_param.private_data_len = conn_param->private_data_len;
1917 iw_param.qpn = id_priv->qp_num;
1919 iw_param.qpn = conn_param->qp_num;
1920 ret = iw_cm_connect(cm_id, &iw_param);
1922 if (ret && !IS_ERR(cm_id)) {
1923 iw_destroy_cm_id(cm_id);
1924 id_priv->cm_id.iw = NULL;
1929 int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
1931 struct rdma_id_private *id_priv;
1934 id_priv = container_of(id, struct rdma_id_private, id);
1935 if (!cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_CONNECT))
1939 id_priv->qp_num = conn_param->qp_num;
1940 id_priv->qp_type = conn_param->qp_type;
1941 id_priv->srq = conn_param->srq;
1944 switch (rdma_node_get_transport(id->device->node_type)) {
1945 case RDMA_TRANSPORT_IB:
1946 ret = cma_connect_ib(id_priv, conn_param);
1948 case RDMA_TRANSPORT_IWARP:
1949 ret = cma_connect_iw(id_priv, conn_param);
1960 cma_comp_exch(id_priv, CMA_CONNECT, CMA_ROUTE_RESOLVED);
1963 EXPORT_SYMBOL(rdma_connect);
1965 static int cma_accept_ib(struct rdma_id_private *id_priv,
1966 struct rdma_conn_param *conn_param)
1968 struct ib_cm_rep_param rep;
1971 ret = cma_modify_qp_rtr(&id_priv->id);
1975 memset(&rep, 0, sizeof rep);
1976 rep.qp_num = id_priv->qp_num;
1977 rep.starting_psn = id_priv->seq_num;
1978 rep.private_data = conn_param->private_data;
1979 rep.private_data_len = conn_param->private_data_len;
1980 rep.responder_resources = conn_param->responder_resources;
1981 rep.initiator_depth = conn_param->initiator_depth;
1982 rep.target_ack_delay = CMA_CM_RESPONSE_TIMEOUT;
1983 rep.failover_accepted = 0;
1984 rep.flow_control = conn_param->flow_control;
1985 rep.rnr_retry_count = conn_param->rnr_retry_count;
1986 rep.srq = id_priv->srq ? 1 : 0;
1988 return ib_send_cm_rep(id_priv->cm_id.ib, &rep);
1991 static int cma_accept_iw(struct rdma_id_private *id_priv,
1992 struct rdma_conn_param *conn_param)
1994 struct iw_cm_conn_param iw_param;
1997 ret = cma_modify_qp_rtr(&id_priv->id);
2001 iw_param.ord = conn_param->initiator_depth;
2002 iw_param.ird = conn_param->responder_resources;
2003 iw_param.private_data = conn_param->private_data;
2004 iw_param.private_data_len = conn_param->private_data_len;
2005 if (id_priv->id.qp) {
2006 iw_param.qpn = id_priv->qp_num;
2008 iw_param.qpn = conn_param->qp_num;
2010 return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
2013 int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2015 struct rdma_id_private *id_priv;
2018 id_priv = container_of(id, struct rdma_id_private, id);
2019 if (!cma_comp(id_priv, CMA_CONNECT))
2022 if (!id->qp && conn_param) {
2023 id_priv->qp_num = conn_param->qp_num;
2024 id_priv->qp_type = conn_param->qp_type;
2025 id_priv->srq = conn_param->srq;
2028 switch (rdma_node_get_transport(id->device->node_type)) {
2029 case RDMA_TRANSPORT_IB:
2031 ret = cma_accept_ib(id_priv, conn_param);
2033 ret = cma_rep_recv(id_priv);
2035 case RDMA_TRANSPORT_IWARP:
2036 ret = cma_accept_iw(id_priv, conn_param);
2048 cma_modify_qp_err(id);
2049 rdma_reject(id, NULL, 0);
2052 EXPORT_SYMBOL(rdma_accept);
2054 int rdma_reject(struct rdma_cm_id *id, const void *private_data,
2055 u8 private_data_len)
2057 struct rdma_id_private *id_priv;
2060 id_priv = container_of(id, struct rdma_id_private, id);
2061 if (!cma_comp(id_priv, CMA_CONNECT))
2064 switch (rdma_node_get_transport(id->device->node_type)) {
2065 case RDMA_TRANSPORT_IB:
2066 ret = ib_send_cm_rej(id_priv->cm_id.ib,
2067 IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
2068 private_data, private_data_len);
2070 case RDMA_TRANSPORT_IWARP:
2071 ret = iw_cm_reject(id_priv->cm_id.iw,
2072 private_data, private_data_len);
2080 EXPORT_SYMBOL(rdma_reject);
2082 int rdma_disconnect(struct rdma_cm_id *id)
2084 struct rdma_id_private *id_priv;
2087 id_priv = container_of(id, struct rdma_id_private, id);
2088 if (!cma_comp(id_priv, CMA_CONNECT) &&
2089 !cma_comp(id_priv, CMA_DISCONNECT))
2092 switch (rdma_node_get_transport(id->device->node_type)) {
2093 case RDMA_TRANSPORT_IB:
2094 ret = cma_modify_qp_err(id);
2097 /* Initiate or respond to a disconnect. */
2098 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
2099 ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
2101 case RDMA_TRANSPORT_IWARP:
2102 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
2111 EXPORT_SYMBOL(rdma_disconnect);
2113 static void cma_add_one(struct ib_device *device)
2115 struct cma_device *cma_dev;
2116 struct rdma_id_private *id_priv;
2118 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
2122 cma_dev->device = device;
2123 cma_dev->node_guid = device->node_guid;
2125 init_completion(&cma_dev->comp);
2126 atomic_set(&cma_dev->refcount, 1);
2127 INIT_LIST_HEAD(&cma_dev->id_list);
2128 ib_set_client_data(device, &cma_client, cma_dev);
2131 list_add_tail(&cma_dev->list, &dev_list);
2132 list_for_each_entry(id_priv, &listen_any_list, list)
2133 cma_listen_on_dev(id_priv, cma_dev);
2134 mutex_unlock(&lock);
2137 static int cma_remove_id_dev(struct rdma_id_private *id_priv)
2139 enum cma_state state;
2141 /* Record that we want to remove the device */
2142 state = cma_exch(id_priv, CMA_DEVICE_REMOVAL);
2143 if (state == CMA_DESTROYING)
2146 cma_cancel_operation(id_priv, state);
2147 wait_event(id_priv->wait_remove, !atomic_read(&id_priv->dev_remove));
2149 /* Check for destruction from another callback. */
2150 if (!cma_comp(id_priv, CMA_DEVICE_REMOVAL))
2153 return cma_notify_user(id_priv, RDMA_CM_EVENT_DEVICE_REMOVAL,
2157 static void cma_process_remove(struct cma_device *cma_dev)
2159 struct rdma_id_private *id_priv;
2163 while (!list_empty(&cma_dev->id_list)) {
2164 id_priv = list_entry(cma_dev->id_list.next,
2165 struct rdma_id_private, list);
2167 if (cma_internal_listen(id_priv)) {
2168 cma_destroy_listen(id_priv);
2172 list_del_init(&id_priv->list);
2173 atomic_inc(&id_priv->refcount);
2174 mutex_unlock(&lock);
2176 ret = cma_remove_id_dev(id_priv);
2177 cma_deref_id(id_priv);
2179 rdma_destroy_id(&id_priv->id);
2183 mutex_unlock(&lock);
2185 cma_deref_dev(cma_dev);
2186 wait_for_completion(&cma_dev->comp);
2189 static void cma_remove_one(struct ib_device *device)
2191 struct cma_device *cma_dev;
2193 cma_dev = ib_get_client_data(device, &cma_client);
2198 list_del(&cma_dev->list);
2199 mutex_unlock(&lock);
2201 cma_process_remove(cma_dev);
2205 static int cma_init(void)
2209 cma_wq = create_singlethread_workqueue("rdma_cm_wq");
2213 ib_sa_register_client(&sa_client);
2214 rdma_addr_register_client(&addr_client);
2216 ret = ib_register_client(&cma_client);
2222 rdma_addr_unregister_client(&addr_client);
2223 ib_sa_unregister_client(&sa_client);
2224 destroy_workqueue(cma_wq);
2228 static void cma_cleanup(void)
2230 ib_unregister_client(&cma_client);
2231 rdma_addr_unregister_client(&addr_client);
2232 ib_sa_unregister_client(&sa_client);
2233 destroy_workqueue(cma_wq);
2234 idr_destroy(&sdp_ps);
2235 idr_destroy(&tcp_ps);
2238 module_init(cma_init);
2239 module_exit(cma_cleanup);