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 int cma_disable_remove(struct rdma_id_private *id_priv,
350 enum cma_state state)
355 spin_lock_irqsave(&id_priv->lock, flags);
356 if (id_priv->state == state) {
357 atomic_inc(&id_priv->dev_remove);
361 spin_unlock_irqrestore(&id_priv->lock, flags);
365 static void cma_enable_remove(struct rdma_id_private *id_priv)
367 if (atomic_dec_and_test(&id_priv->dev_remove))
368 wake_up(&id_priv->wait_remove);
371 static int cma_has_cm_dev(struct rdma_id_private *id_priv)
373 return (id_priv->id.device && id_priv->cm_id.ib);
376 struct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler,
377 void *context, enum rdma_port_space ps)
379 struct rdma_id_private *id_priv;
381 id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
383 return ERR_PTR(-ENOMEM);
385 id_priv->state = CMA_IDLE;
386 id_priv->id.context = context;
387 id_priv->id.event_handler = event_handler;
389 spin_lock_init(&id_priv->lock);
390 init_completion(&id_priv->comp);
391 atomic_set(&id_priv->refcount, 1);
392 init_waitqueue_head(&id_priv->wait_remove);
393 atomic_set(&id_priv->dev_remove, 0);
394 INIT_LIST_HEAD(&id_priv->listen_list);
395 INIT_LIST_HEAD(&id_priv->mc_list);
396 get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
400 EXPORT_SYMBOL(rdma_create_id);
402 static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
404 struct ib_qp_attr qp_attr;
405 int qp_attr_mask, ret;
407 qp_attr.qp_state = IB_QPS_INIT;
408 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
412 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
416 qp_attr.qp_state = IB_QPS_RTR;
417 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
421 qp_attr.qp_state = IB_QPS_RTS;
423 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
428 static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
430 struct ib_qp_attr qp_attr;
431 int qp_attr_mask, ret;
433 qp_attr.qp_state = IB_QPS_INIT;
434 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
438 return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
441 int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
442 struct ib_qp_init_attr *qp_init_attr)
444 struct rdma_id_private *id_priv;
448 id_priv = container_of(id, struct rdma_id_private, id);
449 if (id->device != pd->device)
452 qp = ib_create_qp(pd, qp_init_attr);
456 if (cma_is_ud_ps(id_priv->id.ps))
457 ret = cma_init_ud_qp(id_priv, qp);
459 ret = cma_init_conn_qp(id_priv, qp);
464 id_priv->qp_num = qp->qp_num;
465 id_priv->srq = (qp->srq != NULL);
471 EXPORT_SYMBOL(rdma_create_qp);
473 void rdma_destroy_qp(struct rdma_cm_id *id)
475 ib_destroy_qp(id->qp);
477 EXPORT_SYMBOL(rdma_destroy_qp);
479 static int cma_modify_qp_rtr(struct rdma_cm_id *id)
481 struct ib_qp_attr qp_attr;
482 int qp_attr_mask, ret;
487 /* Need to update QP attributes from default values. */
488 qp_attr.qp_state = IB_QPS_INIT;
489 ret = rdma_init_qp_attr(id, &qp_attr, &qp_attr_mask);
493 ret = ib_modify_qp(id->qp, &qp_attr, qp_attr_mask);
497 qp_attr.qp_state = IB_QPS_RTR;
498 ret = rdma_init_qp_attr(id, &qp_attr, &qp_attr_mask);
502 return ib_modify_qp(id->qp, &qp_attr, qp_attr_mask);
505 static int cma_modify_qp_rts(struct rdma_cm_id *id)
507 struct ib_qp_attr qp_attr;
508 int qp_attr_mask, ret;
513 qp_attr.qp_state = IB_QPS_RTS;
514 ret = rdma_init_qp_attr(id, &qp_attr, &qp_attr_mask);
518 return ib_modify_qp(id->qp, &qp_attr, qp_attr_mask);
521 static int cma_modify_qp_err(struct rdma_cm_id *id)
523 struct ib_qp_attr qp_attr;
528 qp_attr.qp_state = IB_QPS_ERR;
529 return ib_modify_qp(id->qp, &qp_attr, IB_QP_STATE);
532 static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
533 struct ib_qp_attr *qp_attr, int *qp_attr_mask)
535 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
538 ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
539 ib_addr_get_pkey(dev_addr),
540 &qp_attr->pkey_index);
544 qp_attr->port_num = id_priv->id.port_num;
545 *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
547 if (cma_is_ud_ps(id_priv->id.ps)) {
548 qp_attr->qkey = id_priv->qkey;
549 *qp_attr_mask |= IB_QP_QKEY;
551 qp_attr->qp_access_flags = 0;
552 *qp_attr_mask |= IB_QP_ACCESS_FLAGS;
557 int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
560 struct rdma_id_private *id_priv;
563 id_priv = container_of(id, struct rdma_id_private, id);
564 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
565 case RDMA_TRANSPORT_IB:
566 if (!id_priv->cm_id.ib || cma_is_ud_ps(id_priv->id.ps))
567 ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
569 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
571 if (qp_attr->qp_state == IB_QPS_RTR)
572 qp_attr->rq_psn = id_priv->seq_num;
574 case RDMA_TRANSPORT_IWARP:
575 if (!id_priv->cm_id.iw) {
576 qp_attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE;
577 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
579 ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
589 EXPORT_SYMBOL(rdma_init_qp_attr);
591 static inline int cma_zero_addr(struct sockaddr *addr)
593 struct in6_addr *ip6;
595 if (addr->sa_family == AF_INET)
596 return ZERONET(((struct sockaddr_in *) addr)->sin_addr.s_addr);
598 ip6 = &((struct sockaddr_in6 *) addr)->sin6_addr;
599 return (ip6->s6_addr32[0] | ip6->s6_addr32[1] |
600 ip6->s6_addr32[2] | ip6->s6_addr32[3]) == 0;
604 static inline int cma_loopback_addr(struct sockaddr *addr)
606 return LOOPBACK(((struct sockaddr_in *) addr)->sin_addr.s_addr);
609 static inline int cma_any_addr(struct sockaddr *addr)
611 return cma_zero_addr(addr) || cma_loopback_addr(addr);
614 static inline __be16 cma_port(struct sockaddr *addr)
616 if (addr->sa_family == AF_INET)
617 return ((struct sockaddr_in *) addr)->sin_port;
619 return ((struct sockaddr_in6 *) addr)->sin6_port;
622 static inline int cma_any_port(struct sockaddr *addr)
624 return !cma_port(addr);
627 static int cma_get_net_info(void *hdr, enum rdma_port_space ps,
628 u8 *ip_ver, __u16 *port,
629 union cma_ip_addr **src, union cma_ip_addr **dst)
633 if (sdp_get_majv(((struct sdp_hh *) hdr)->sdp_version) !=
637 *ip_ver = sdp_get_ip_ver(hdr);
638 *port = ((struct sdp_hh *) hdr)->port;
639 *src = &((struct sdp_hh *) hdr)->src_addr;
640 *dst = &((struct sdp_hh *) hdr)->dst_addr;
643 if (((struct cma_hdr *) hdr)->cma_version != CMA_VERSION)
646 *ip_ver = cma_get_ip_ver(hdr);
647 *port = ((struct cma_hdr *) hdr)->port;
648 *src = &((struct cma_hdr *) hdr)->src_addr;
649 *dst = &((struct cma_hdr *) hdr)->dst_addr;
653 if (*ip_ver != 4 && *ip_ver != 6)
658 static void cma_save_net_info(struct rdma_addr *addr,
659 struct rdma_addr *listen_addr,
660 u8 ip_ver, __u16 port,
661 union cma_ip_addr *src, union cma_ip_addr *dst)
663 struct sockaddr_in *listen4, *ip4;
664 struct sockaddr_in6 *listen6, *ip6;
668 listen4 = (struct sockaddr_in *) &listen_addr->src_addr;
669 ip4 = (struct sockaddr_in *) &addr->src_addr;
670 ip4->sin_family = listen4->sin_family;
671 ip4->sin_addr.s_addr = dst->ip4.addr;
672 ip4->sin_port = listen4->sin_port;
674 ip4 = (struct sockaddr_in *) &addr->dst_addr;
675 ip4->sin_family = listen4->sin_family;
676 ip4->sin_addr.s_addr = src->ip4.addr;
677 ip4->sin_port = port;
680 listen6 = (struct sockaddr_in6 *) &listen_addr->src_addr;
681 ip6 = (struct sockaddr_in6 *) &addr->src_addr;
682 ip6->sin6_family = listen6->sin6_family;
683 ip6->sin6_addr = dst->ip6;
684 ip6->sin6_port = listen6->sin6_port;
686 ip6 = (struct sockaddr_in6 *) &addr->dst_addr;
687 ip6->sin6_family = listen6->sin6_family;
688 ip6->sin6_addr = src->ip6;
689 ip6->sin6_port = port;
696 static inline int cma_user_data_offset(enum rdma_port_space ps)
702 return sizeof(struct cma_hdr);
706 static void cma_cancel_route(struct rdma_id_private *id_priv)
708 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
709 case RDMA_TRANSPORT_IB:
711 ib_sa_cancel_query(id_priv->query_id, id_priv->query);
718 static inline int cma_internal_listen(struct rdma_id_private *id_priv)
720 return (id_priv->state == CMA_LISTEN) && id_priv->cma_dev &&
721 cma_any_addr(&id_priv->id.route.addr.src_addr);
724 static void cma_destroy_listen(struct rdma_id_private *id_priv)
726 cma_exch(id_priv, CMA_DESTROYING);
728 if (id_priv->cma_dev) {
729 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
730 case RDMA_TRANSPORT_IB:
731 if (id_priv->cm_id.ib && !IS_ERR(id_priv->cm_id.ib))
732 ib_destroy_cm_id(id_priv->cm_id.ib);
734 case RDMA_TRANSPORT_IWARP:
735 if (id_priv->cm_id.iw && !IS_ERR(id_priv->cm_id.iw))
736 iw_destroy_cm_id(id_priv->cm_id.iw);
741 cma_detach_from_dev(id_priv);
743 list_del(&id_priv->listen_list);
745 cma_deref_id(id_priv);
746 wait_for_completion(&id_priv->comp);
751 static void cma_cancel_listens(struct rdma_id_private *id_priv)
753 struct rdma_id_private *dev_id_priv;
756 list_del(&id_priv->list);
758 while (!list_empty(&id_priv->listen_list)) {
759 dev_id_priv = list_entry(id_priv->listen_list.next,
760 struct rdma_id_private, listen_list);
761 cma_destroy_listen(dev_id_priv);
766 static void cma_cancel_operation(struct rdma_id_private *id_priv,
767 enum cma_state state)
771 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
773 case CMA_ROUTE_QUERY:
774 cma_cancel_route(id_priv);
777 if (cma_any_addr(&id_priv->id.route.addr.src_addr) &&
779 cma_cancel_listens(id_priv);
786 static void cma_release_port(struct rdma_id_private *id_priv)
788 struct rdma_bind_list *bind_list = id_priv->bind_list;
794 hlist_del(&id_priv->node);
795 if (hlist_empty(&bind_list->owners)) {
796 idr_remove(bind_list->ps, bind_list->port);
802 static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
804 struct cma_multicast *mc;
806 while (!list_empty(&id_priv->mc_list)) {
807 mc = container_of(id_priv->mc_list.next,
808 struct cma_multicast, list);
810 ib_sa_free_multicast(mc->multicast.ib);
815 void rdma_destroy_id(struct rdma_cm_id *id)
817 struct rdma_id_private *id_priv;
818 enum cma_state state;
820 id_priv = container_of(id, struct rdma_id_private, id);
821 state = cma_exch(id_priv, CMA_DESTROYING);
822 cma_cancel_operation(id_priv, state);
825 if (id_priv->cma_dev) {
827 switch (rdma_node_get_transport(id->device->node_type)) {
828 case RDMA_TRANSPORT_IB:
829 if (id_priv->cm_id.ib && !IS_ERR(id_priv->cm_id.ib))
830 ib_destroy_cm_id(id_priv->cm_id.ib);
832 case RDMA_TRANSPORT_IWARP:
833 if (id_priv->cm_id.iw && !IS_ERR(id_priv->cm_id.iw))
834 iw_destroy_cm_id(id_priv->cm_id.iw);
839 cma_leave_mc_groups(id_priv);
841 cma_detach_from_dev(id_priv);
845 cma_release_port(id_priv);
846 cma_deref_id(id_priv);
847 wait_for_completion(&id_priv->comp);
849 kfree(id_priv->id.route.path_rec);
852 EXPORT_SYMBOL(rdma_destroy_id);
854 static int cma_rep_recv(struct rdma_id_private *id_priv)
858 ret = cma_modify_qp_rtr(&id_priv->id);
862 ret = cma_modify_qp_rts(&id_priv->id);
866 ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
872 cma_modify_qp_err(&id_priv->id);
873 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
878 static int cma_verify_rep(struct rdma_id_private *id_priv, void *data)
880 if (id_priv->id.ps == RDMA_PS_SDP &&
881 sdp_get_majv(((struct sdp_hah *) data)->sdp_version) !=
888 static void cma_set_rep_event_data(struct rdma_cm_event *event,
889 struct ib_cm_rep_event_param *rep_data,
892 event->param.conn.private_data = private_data;
893 event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
894 event->param.conn.responder_resources = rep_data->responder_resources;
895 event->param.conn.initiator_depth = rep_data->initiator_depth;
896 event->param.conn.flow_control = rep_data->flow_control;
897 event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
898 event->param.conn.srq = rep_data->srq;
899 event->param.conn.qp_num = rep_data->remote_qpn;
902 static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
904 struct rdma_id_private *id_priv = cm_id->context;
905 struct rdma_cm_event event;
908 if (cma_disable_remove(id_priv, CMA_CONNECT))
911 memset(&event, 0, sizeof event);
912 switch (ib_event->event) {
913 case IB_CM_REQ_ERROR:
914 case IB_CM_REP_ERROR:
915 event.event = RDMA_CM_EVENT_UNREACHABLE;
916 event.status = -ETIMEDOUT;
918 case IB_CM_REP_RECEIVED:
919 event.status = cma_verify_rep(id_priv, ib_event->private_data);
921 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
922 else if (id_priv->id.qp && id_priv->id.ps != RDMA_PS_SDP) {
923 event.status = cma_rep_recv(id_priv);
924 event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
925 RDMA_CM_EVENT_ESTABLISHED;
927 event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
928 cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
929 ib_event->private_data);
931 case IB_CM_RTU_RECEIVED:
932 case IB_CM_USER_ESTABLISHED:
933 event.event = RDMA_CM_EVENT_ESTABLISHED;
935 case IB_CM_DREQ_ERROR:
936 event.status = -ETIMEDOUT; /* fall through */
937 case IB_CM_DREQ_RECEIVED:
938 case IB_CM_DREP_RECEIVED:
939 if (!cma_comp_exch(id_priv, CMA_CONNECT, CMA_DISCONNECT))
941 event.event = RDMA_CM_EVENT_DISCONNECTED;
943 case IB_CM_TIMEWAIT_EXIT:
944 case IB_CM_MRA_RECEIVED:
947 case IB_CM_REJ_RECEIVED:
948 cma_modify_qp_err(&id_priv->id);
949 event.status = ib_event->param.rej_rcvd.reason;
950 event.event = RDMA_CM_EVENT_REJECTED;
951 event.param.conn.private_data = ib_event->private_data;
952 event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
955 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d",
960 ret = id_priv->id.event_handler(&id_priv->id, &event);
962 /* Destroy the CM ID by returning a non-zero value. */
963 id_priv->cm_id.ib = NULL;
964 cma_exch(id_priv, CMA_DESTROYING);
965 cma_enable_remove(id_priv);
966 rdma_destroy_id(&id_priv->id);
970 cma_enable_remove(id_priv);
974 static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
975 struct ib_cm_event *ib_event)
977 struct rdma_id_private *id_priv;
978 struct rdma_cm_id *id;
979 struct rdma_route *rt;
980 union cma_ip_addr *src, *dst;
984 if (cma_get_net_info(ib_event->private_data, listen_id->ps,
985 &ip_ver, &port, &src, &dst))
988 id = rdma_create_id(listen_id->event_handler, listen_id->context,
993 cma_save_net_info(&id->route.addr, &listen_id->route.addr,
994 ip_ver, port, src, dst);
997 rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
998 rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
1003 rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
1004 if (rt->num_paths == 2)
1005 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
1007 ib_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
1008 ib_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
1009 ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
1010 rt->addr.dev_addr.dev_type = RDMA_NODE_IB_CA;
1012 id_priv = container_of(id, struct rdma_id_private, id);
1013 id_priv->state = CMA_CONNECT;
1017 rdma_destroy_id(id);
1022 static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
1023 struct ib_cm_event *ib_event)
1025 struct rdma_id_private *id_priv;
1026 struct rdma_cm_id *id;
1027 union cma_ip_addr *src, *dst;
1032 id = rdma_create_id(listen_id->event_handler, listen_id->context,
1038 if (cma_get_net_info(ib_event->private_data, listen_id->ps,
1039 &ip_ver, &port, &src, &dst))
1042 cma_save_net_info(&id->route.addr, &listen_id->route.addr,
1043 ip_ver, port, src, dst);
1045 ret = rdma_translate_ip(&id->route.addr.src_addr,
1046 &id->route.addr.dev_addr);
1050 id_priv = container_of(id, struct rdma_id_private, id);
1051 id_priv->state = CMA_CONNECT;
1054 rdma_destroy_id(id);
1058 static void cma_set_req_event_data(struct rdma_cm_event *event,
1059 struct ib_cm_req_event_param *req_data,
1060 void *private_data, int offset)
1062 event->param.conn.private_data = private_data + offset;
1063 event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
1064 event->param.conn.responder_resources = req_data->responder_resources;
1065 event->param.conn.initiator_depth = req_data->initiator_depth;
1066 event->param.conn.flow_control = req_data->flow_control;
1067 event->param.conn.retry_count = req_data->retry_count;
1068 event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
1069 event->param.conn.srq = req_data->srq;
1070 event->param.conn.qp_num = req_data->remote_qpn;
1073 static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1075 struct rdma_id_private *listen_id, *conn_id;
1076 struct rdma_cm_event event;
1079 listen_id = cm_id->context;
1080 if (cma_disable_remove(listen_id, CMA_LISTEN))
1081 return -ECONNABORTED;
1083 memset(&event, 0, sizeof event);
1084 offset = cma_user_data_offset(listen_id->id.ps);
1085 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1086 if (cma_is_ud_ps(listen_id->id.ps)) {
1087 conn_id = cma_new_udp_id(&listen_id->id, ib_event);
1088 event.param.ud.private_data = ib_event->private_data + offset;
1089 event.param.ud.private_data_len =
1090 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
1092 conn_id = cma_new_conn_id(&listen_id->id, ib_event);
1093 cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
1094 ib_event->private_data, offset);
1101 atomic_inc(&conn_id->dev_remove);
1103 ret = cma_acquire_dev(conn_id);
1104 mutex_unlock(&lock);
1106 goto release_conn_id;
1108 conn_id->cm_id.ib = cm_id;
1109 cm_id->context = conn_id;
1110 cm_id->cm_handler = cma_ib_handler;
1112 ret = conn_id->id.event_handler(&conn_id->id, &event);
1116 /* Destroy the CM ID by returning a non-zero value. */
1117 conn_id->cm_id.ib = NULL;
1120 cma_exch(conn_id, CMA_DESTROYING);
1121 cma_enable_remove(conn_id);
1122 rdma_destroy_id(&conn_id->id);
1125 cma_enable_remove(listen_id);
1129 static __be64 cma_get_service_id(enum rdma_port_space ps, struct sockaddr *addr)
1131 return cpu_to_be64(((u64)ps << 16) + be16_to_cpu(cma_port(addr)));
1134 static void cma_set_compare_data(enum rdma_port_space ps, struct sockaddr *addr,
1135 struct ib_cm_compare_data *compare)
1137 struct cma_hdr *cma_data, *cma_mask;
1138 struct sdp_hh *sdp_data, *sdp_mask;
1140 struct in6_addr ip6_addr;
1142 memset(compare, 0, sizeof *compare);
1143 cma_data = (void *) compare->data;
1144 cma_mask = (void *) compare->mask;
1145 sdp_data = (void *) compare->data;
1146 sdp_mask = (void *) compare->mask;
1148 switch (addr->sa_family) {
1150 ip4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1151 if (ps == RDMA_PS_SDP) {
1152 sdp_set_ip_ver(sdp_data, 4);
1153 sdp_set_ip_ver(sdp_mask, 0xF);
1154 sdp_data->dst_addr.ip4.addr = ip4_addr;
1155 sdp_mask->dst_addr.ip4.addr = ~0;
1157 cma_set_ip_ver(cma_data, 4);
1158 cma_set_ip_ver(cma_mask, 0xF);
1159 cma_data->dst_addr.ip4.addr = ip4_addr;
1160 cma_mask->dst_addr.ip4.addr = ~0;
1164 ip6_addr = ((struct sockaddr_in6 *) addr)->sin6_addr;
1165 if (ps == RDMA_PS_SDP) {
1166 sdp_set_ip_ver(sdp_data, 6);
1167 sdp_set_ip_ver(sdp_mask, 0xF);
1168 sdp_data->dst_addr.ip6 = ip6_addr;
1169 memset(&sdp_mask->dst_addr.ip6, 0xFF,
1170 sizeof sdp_mask->dst_addr.ip6);
1172 cma_set_ip_ver(cma_data, 6);
1173 cma_set_ip_ver(cma_mask, 0xF);
1174 cma_data->dst_addr.ip6 = ip6_addr;
1175 memset(&cma_mask->dst_addr.ip6, 0xFF,
1176 sizeof cma_mask->dst_addr.ip6);
1184 static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
1186 struct rdma_id_private *id_priv = iw_id->context;
1187 struct rdma_cm_event event;
1188 struct sockaddr_in *sin;
1191 if (cma_disable_remove(id_priv, CMA_CONNECT))
1194 memset(&event, 0, sizeof event);
1195 switch (iw_event->event) {
1196 case IW_CM_EVENT_CLOSE:
1197 event.event = RDMA_CM_EVENT_DISCONNECTED;
1199 case IW_CM_EVENT_CONNECT_REPLY:
1200 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1201 *sin = iw_event->local_addr;
1202 sin = (struct sockaddr_in *) &id_priv->id.route.addr.dst_addr;
1203 *sin = iw_event->remote_addr;
1204 switch (iw_event->status) {
1206 event.event = RDMA_CM_EVENT_ESTABLISHED;
1210 event.event = RDMA_CM_EVENT_REJECTED;
1213 event.event = RDMA_CM_EVENT_UNREACHABLE;
1216 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
1220 case IW_CM_EVENT_ESTABLISHED:
1221 event.event = RDMA_CM_EVENT_ESTABLISHED;
1227 event.status = iw_event->status;
1228 event.param.conn.private_data = iw_event->private_data;
1229 event.param.conn.private_data_len = iw_event->private_data_len;
1230 ret = id_priv->id.event_handler(&id_priv->id, &event);
1232 /* Destroy the CM ID by returning a non-zero value. */
1233 id_priv->cm_id.iw = NULL;
1234 cma_exch(id_priv, CMA_DESTROYING);
1235 cma_enable_remove(id_priv);
1236 rdma_destroy_id(&id_priv->id);
1240 cma_enable_remove(id_priv);
1244 static int iw_conn_req_handler(struct iw_cm_id *cm_id,
1245 struct iw_cm_event *iw_event)
1247 struct rdma_cm_id *new_cm_id;
1248 struct rdma_id_private *listen_id, *conn_id;
1249 struct sockaddr_in *sin;
1250 struct net_device *dev = NULL;
1251 struct rdma_cm_event event;
1254 listen_id = cm_id->context;
1255 if (cma_disable_remove(listen_id, CMA_LISTEN))
1256 return -ECONNABORTED;
1258 /* Create a new RDMA id for the new IW CM ID */
1259 new_cm_id = rdma_create_id(listen_id->id.event_handler,
1260 listen_id->id.context,
1266 conn_id = container_of(new_cm_id, struct rdma_id_private, id);
1267 atomic_inc(&conn_id->dev_remove);
1268 conn_id->state = CMA_CONNECT;
1270 dev = ip_dev_find(iw_event->local_addr.sin_addr.s_addr);
1272 ret = -EADDRNOTAVAIL;
1273 cma_enable_remove(conn_id);
1274 rdma_destroy_id(new_cm_id);
1277 ret = rdma_copy_addr(&conn_id->id.route.addr.dev_addr, dev, NULL);
1279 cma_enable_remove(conn_id);
1280 rdma_destroy_id(new_cm_id);
1285 ret = cma_acquire_dev(conn_id);
1286 mutex_unlock(&lock);
1288 cma_enable_remove(conn_id);
1289 rdma_destroy_id(new_cm_id);
1293 conn_id->cm_id.iw = cm_id;
1294 cm_id->context = conn_id;
1295 cm_id->cm_handler = cma_iw_handler;
1297 sin = (struct sockaddr_in *) &new_cm_id->route.addr.src_addr;
1298 *sin = iw_event->local_addr;
1299 sin = (struct sockaddr_in *) &new_cm_id->route.addr.dst_addr;
1300 *sin = iw_event->remote_addr;
1302 memset(&event, 0, sizeof event);
1303 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1304 event.param.conn.private_data = iw_event->private_data;
1305 event.param.conn.private_data_len = iw_event->private_data_len;
1306 ret = conn_id->id.event_handler(&conn_id->id, &event);
1308 /* User wants to destroy the CM ID */
1309 conn_id->cm_id.iw = NULL;
1310 cma_exch(conn_id, CMA_DESTROYING);
1311 cma_enable_remove(conn_id);
1312 rdma_destroy_id(&conn_id->id);
1318 cma_enable_remove(listen_id);
1322 static int cma_ib_listen(struct rdma_id_private *id_priv)
1324 struct ib_cm_compare_data compare_data;
1325 struct sockaddr *addr;
1329 id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_req_handler,
1331 if (IS_ERR(id_priv->cm_id.ib))
1332 return PTR_ERR(id_priv->cm_id.ib);
1334 addr = &id_priv->id.route.addr.src_addr;
1335 svc_id = cma_get_service_id(id_priv->id.ps, addr);
1336 if (cma_any_addr(addr))
1337 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, NULL);
1339 cma_set_compare_data(id_priv->id.ps, addr, &compare_data);
1340 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, &compare_data);
1344 ib_destroy_cm_id(id_priv->cm_id.ib);
1345 id_priv->cm_id.ib = NULL;
1351 static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
1354 struct sockaddr_in *sin;
1356 id_priv->cm_id.iw = iw_create_cm_id(id_priv->id.device,
1357 iw_conn_req_handler,
1359 if (IS_ERR(id_priv->cm_id.iw))
1360 return PTR_ERR(id_priv->cm_id.iw);
1362 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1363 id_priv->cm_id.iw->local_addr = *sin;
1365 ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
1368 iw_destroy_cm_id(id_priv->cm_id.iw);
1369 id_priv->cm_id.iw = NULL;
1375 static int cma_listen_handler(struct rdma_cm_id *id,
1376 struct rdma_cm_event *event)
1378 struct rdma_id_private *id_priv = id->context;
1380 id->context = id_priv->id.context;
1381 id->event_handler = id_priv->id.event_handler;
1382 return id_priv->id.event_handler(id, event);
1385 static void cma_listen_on_dev(struct rdma_id_private *id_priv,
1386 struct cma_device *cma_dev)
1388 struct rdma_id_private *dev_id_priv;
1389 struct rdma_cm_id *id;
1392 id = rdma_create_id(cma_listen_handler, id_priv, id_priv->id.ps);
1396 dev_id_priv = container_of(id, struct rdma_id_private, id);
1398 dev_id_priv->state = CMA_ADDR_BOUND;
1399 memcpy(&id->route.addr.src_addr, &id_priv->id.route.addr.src_addr,
1400 ip_addr_size(&id_priv->id.route.addr.src_addr));
1402 cma_attach_to_dev(dev_id_priv, cma_dev);
1403 list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
1405 ret = rdma_listen(id, id_priv->backlog);
1411 cma_destroy_listen(dev_id_priv);
1414 static void cma_listen_on_all(struct rdma_id_private *id_priv)
1416 struct cma_device *cma_dev;
1419 list_add_tail(&id_priv->list, &listen_any_list);
1420 list_for_each_entry(cma_dev, &dev_list, list)
1421 cma_listen_on_dev(id_priv, cma_dev);
1422 mutex_unlock(&lock);
1425 static int cma_bind_any(struct rdma_cm_id *id, sa_family_t af)
1427 struct sockaddr_in addr_in;
1429 memset(&addr_in, 0, sizeof addr_in);
1430 addr_in.sin_family = af;
1431 return rdma_bind_addr(id, (struct sockaddr *) &addr_in);
1434 int rdma_listen(struct rdma_cm_id *id, int backlog)
1436 struct rdma_id_private *id_priv;
1439 id_priv = container_of(id, struct rdma_id_private, id);
1440 if (id_priv->state == CMA_IDLE) {
1441 ret = cma_bind_any(id, AF_INET);
1446 if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_LISTEN))
1449 id_priv->backlog = backlog;
1451 switch (rdma_node_get_transport(id->device->node_type)) {
1452 case RDMA_TRANSPORT_IB:
1453 ret = cma_ib_listen(id_priv);
1457 case RDMA_TRANSPORT_IWARP:
1458 ret = cma_iw_listen(id_priv, backlog);
1467 cma_listen_on_all(id_priv);
1471 id_priv->backlog = 0;
1472 cma_comp_exch(id_priv, CMA_LISTEN, CMA_ADDR_BOUND);
1475 EXPORT_SYMBOL(rdma_listen);
1477 static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
1480 struct cma_work *work = context;
1481 struct rdma_route *route;
1483 route = &work->id->id.route;
1486 route->num_paths = 1;
1487 *route->path_rec = *path_rec;
1489 work->old_state = CMA_ROUTE_QUERY;
1490 work->new_state = CMA_ADDR_RESOLVED;
1491 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
1492 work->event.status = status;
1495 queue_work(cma_wq, &work->work);
1498 static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
1499 struct cma_work *work)
1501 struct rdma_dev_addr *addr = &id_priv->id.route.addr.dev_addr;
1502 struct ib_sa_path_rec path_rec;
1504 memset(&path_rec, 0, sizeof path_rec);
1505 ib_addr_get_sgid(addr, &path_rec.sgid);
1506 ib_addr_get_dgid(addr, &path_rec.dgid);
1507 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(addr));
1508 path_rec.numb_path = 1;
1509 path_rec.reversible = 1;
1511 id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
1512 id_priv->id.port_num, &path_rec,
1513 IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
1514 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
1515 IB_SA_PATH_REC_REVERSIBLE,
1516 timeout_ms, GFP_KERNEL,
1517 cma_query_handler, work, &id_priv->query);
1519 return (id_priv->query_id < 0) ? id_priv->query_id : 0;
1522 static void cma_work_handler(struct work_struct *_work)
1524 struct cma_work *work = container_of(_work, struct cma_work, work);
1525 struct rdma_id_private *id_priv = work->id;
1528 atomic_inc(&id_priv->dev_remove);
1529 if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
1532 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
1533 cma_exch(id_priv, CMA_DESTROYING);
1537 cma_enable_remove(id_priv);
1538 cma_deref_id(id_priv);
1540 rdma_destroy_id(&id_priv->id);
1544 static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
1546 struct rdma_route *route = &id_priv->id.route;
1547 struct cma_work *work;
1550 work = kzalloc(sizeof *work, GFP_KERNEL);
1555 INIT_WORK(&work->work, cma_work_handler);
1556 work->old_state = CMA_ROUTE_QUERY;
1557 work->new_state = CMA_ROUTE_RESOLVED;
1558 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1560 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
1561 if (!route->path_rec) {
1566 ret = cma_query_ib_route(id_priv, timeout_ms, work);
1572 kfree(route->path_rec);
1573 route->path_rec = NULL;
1579 int rdma_set_ib_paths(struct rdma_cm_id *id,
1580 struct ib_sa_path_rec *path_rec, int num_paths)
1582 struct rdma_id_private *id_priv;
1585 id_priv = container_of(id, struct rdma_id_private, id);
1586 if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ROUTE_RESOLVED))
1589 id->route.path_rec = kmalloc(sizeof *path_rec * num_paths, GFP_KERNEL);
1590 if (!id->route.path_rec) {
1595 memcpy(id->route.path_rec, path_rec, sizeof *path_rec * num_paths);
1598 cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_ADDR_RESOLVED);
1601 EXPORT_SYMBOL(rdma_set_ib_paths);
1603 static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
1605 struct cma_work *work;
1607 work = kzalloc(sizeof *work, GFP_KERNEL);
1612 INIT_WORK(&work->work, cma_work_handler);
1613 work->old_state = CMA_ROUTE_QUERY;
1614 work->new_state = CMA_ROUTE_RESOLVED;
1615 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1616 queue_work(cma_wq, &work->work);
1620 int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
1622 struct rdma_id_private *id_priv;
1625 id_priv = container_of(id, struct rdma_id_private, id);
1626 if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ROUTE_QUERY))
1629 atomic_inc(&id_priv->refcount);
1630 switch (rdma_node_get_transport(id->device->node_type)) {
1631 case RDMA_TRANSPORT_IB:
1632 ret = cma_resolve_ib_route(id_priv, timeout_ms);
1634 case RDMA_TRANSPORT_IWARP:
1635 ret = cma_resolve_iw_route(id_priv, timeout_ms);
1646 cma_comp_exch(id_priv, CMA_ROUTE_QUERY, CMA_ADDR_RESOLVED);
1647 cma_deref_id(id_priv);
1650 EXPORT_SYMBOL(rdma_resolve_route);
1652 static int cma_bind_loopback(struct rdma_id_private *id_priv)
1654 struct cma_device *cma_dev;
1655 struct ib_port_attr port_attr;
1662 if (list_empty(&dev_list)) {
1666 list_for_each_entry(cma_dev, &dev_list, list)
1667 for (p = 1; p <= cma_dev->device->phys_port_cnt; ++p)
1668 if (!ib_query_port(cma_dev->device, p, &port_attr) &&
1669 port_attr.state == IB_PORT_ACTIVE)
1673 cma_dev = list_entry(dev_list.next, struct cma_device, list);
1676 ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid);
1680 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
1684 ib_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
1685 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
1686 id_priv->id.port_num = p;
1687 cma_attach_to_dev(id_priv, cma_dev);
1689 mutex_unlock(&lock);
1693 static void addr_handler(int status, struct sockaddr *src_addr,
1694 struct rdma_dev_addr *dev_addr, void *context)
1696 struct rdma_id_private *id_priv = context;
1697 struct rdma_cm_event event;
1699 memset(&event, 0, sizeof event);
1700 atomic_inc(&id_priv->dev_remove);
1703 * Grab mutex to block rdma_destroy_id() from removing the device while
1704 * we're trying to acquire it.
1707 if (!cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_RESOLVED)) {
1708 mutex_unlock(&lock);
1712 if (!status && !id_priv->cma_dev)
1713 status = cma_acquire_dev(id_priv);
1714 mutex_unlock(&lock);
1717 if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ADDR_BOUND))
1719 event.event = RDMA_CM_EVENT_ADDR_ERROR;
1720 event.status = status;
1722 memcpy(&id_priv->id.route.addr.src_addr, src_addr,
1723 ip_addr_size(src_addr));
1724 event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
1727 if (id_priv->id.event_handler(&id_priv->id, &event)) {
1728 cma_exch(id_priv, CMA_DESTROYING);
1729 cma_enable_remove(id_priv);
1730 cma_deref_id(id_priv);
1731 rdma_destroy_id(&id_priv->id);
1735 cma_enable_remove(id_priv);
1736 cma_deref_id(id_priv);
1739 static int cma_resolve_loopback(struct rdma_id_private *id_priv)
1741 struct cma_work *work;
1742 struct sockaddr_in *src_in, *dst_in;
1746 work = kzalloc(sizeof *work, GFP_KERNEL);
1750 if (!id_priv->cma_dev) {
1751 ret = cma_bind_loopback(id_priv);
1756 ib_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
1757 ib_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
1759 if (cma_zero_addr(&id_priv->id.route.addr.src_addr)) {
1760 src_in = (struct sockaddr_in *)&id_priv->id.route.addr.src_addr;
1761 dst_in = (struct sockaddr_in *)&id_priv->id.route.addr.dst_addr;
1762 src_in->sin_family = dst_in->sin_family;
1763 src_in->sin_addr.s_addr = dst_in->sin_addr.s_addr;
1767 INIT_WORK(&work->work, cma_work_handler);
1768 work->old_state = CMA_ADDR_QUERY;
1769 work->new_state = CMA_ADDR_RESOLVED;
1770 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
1771 queue_work(cma_wq, &work->work);
1778 static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
1779 struct sockaddr *dst_addr)
1781 if (src_addr && src_addr->sa_family)
1782 return rdma_bind_addr(id, src_addr);
1784 return cma_bind_any(id, dst_addr->sa_family);
1787 int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
1788 struct sockaddr *dst_addr, int timeout_ms)
1790 struct rdma_id_private *id_priv;
1793 id_priv = container_of(id, struct rdma_id_private, id);
1794 if (id_priv->state == CMA_IDLE) {
1795 ret = cma_bind_addr(id, src_addr, dst_addr);
1800 if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_ADDR_QUERY))
1803 atomic_inc(&id_priv->refcount);
1804 memcpy(&id->route.addr.dst_addr, dst_addr, ip_addr_size(dst_addr));
1805 if (cma_any_addr(dst_addr))
1806 ret = cma_resolve_loopback(id_priv);
1808 ret = rdma_resolve_ip(&addr_client, &id->route.addr.src_addr,
1809 dst_addr, &id->route.addr.dev_addr,
1810 timeout_ms, addr_handler, id_priv);
1816 cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_BOUND);
1817 cma_deref_id(id_priv);
1820 EXPORT_SYMBOL(rdma_resolve_addr);
1822 static void cma_bind_port(struct rdma_bind_list *bind_list,
1823 struct rdma_id_private *id_priv)
1825 struct sockaddr_in *sin;
1827 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1828 sin->sin_port = htons(bind_list->port);
1829 id_priv->bind_list = bind_list;
1830 hlist_add_head(&id_priv->node, &bind_list->owners);
1833 static int cma_alloc_port(struct idr *ps, struct rdma_id_private *id_priv,
1834 unsigned short snum)
1836 struct rdma_bind_list *bind_list;
1839 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
1844 ret = idr_get_new_above(ps, bind_list, snum, &port);
1845 } while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL));
1851 ret = -EADDRNOTAVAIL;
1856 bind_list->port = (unsigned short) port;
1857 cma_bind_port(bind_list, id_priv);
1860 idr_remove(ps, port);
1866 static int cma_alloc_any_port(struct idr *ps, struct rdma_id_private *id_priv)
1868 struct rdma_bind_list *bind_list;
1871 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
1877 ret = idr_get_new_above(ps, bind_list, next_port, &port);
1878 } while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL));
1883 if (port > sysctl_local_port_range[1]) {
1884 if (next_port != sysctl_local_port_range[0]) {
1885 idr_remove(ps, port);
1886 next_port = sysctl_local_port_range[0];
1889 ret = -EADDRNOTAVAIL;
1893 if (port == sysctl_local_port_range[1])
1894 next_port = sysctl_local_port_range[0];
1896 next_port = port + 1;
1899 bind_list->port = (unsigned short) port;
1900 cma_bind_port(bind_list, id_priv);
1903 idr_remove(ps, port);
1909 static int cma_use_port(struct idr *ps, struct rdma_id_private *id_priv)
1911 struct rdma_id_private *cur_id;
1912 struct sockaddr_in *sin, *cur_sin;
1913 struct rdma_bind_list *bind_list;
1914 struct hlist_node *node;
1915 unsigned short snum;
1917 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1918 snum = ntohs(sin->sin_port);
1919 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
1922 bind_list = idr_find(ps, snum);
1924 return cma_alloc_port(ps, id_priv, snum);
1927 * We don't support binding to any address if anyone is bound to
1928 * a specific address on the same port.
1930 if (cma_any_addr(&id_priv->id.route.addr.src_addr))
1931 return -EADDRNOTAVAIL;
1933 hlist_for_each_entry(cur_id, node, &bind_list->owners, node) {
1934 if (cma_any_addr(&cur_id->id.route.addr.src_addr))
1935 return -EADDRNOTAVAIL;
1937 cur_sin = (struct sockaddr_in *) &cur_id->id.route.addr.src_addr;
1938 if (sin->sin_addr.s_addr == cur_sin->sin_addr.s_addr)
1942 cma_bind_port(bind_list, id_priv);
1946 static int cma_get_port(struct rdma_id_private *id_priv)
1951 switch (id_priv->id.ps) {
1965 return -EPROTONOSUPPORT;
1969 if (cma_any_port(&id_priv->id.route.addr.src_addr))
1970 ret = cma_alloc_any_port(ps, id_priv);
1972 ret = cma_use_port(ps, id_priv);
1973 mutex_unlock(&lock);
1978 int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
1980 struct rdma_id_private *id_priv;
1983 if (addr->sa_family != AF_INET)
1984 return -EAFNOSUPPORT;
1986 id_priv = container_of(id, struct rdma_id_private, id);
1987 if (!cma_comp_exch(id_priv, CMA_IDLE, CMA_ADDR_BOUND))
1990 if (!cma_any_addr(addr)) {
1991 ret = rdma_translate_ip(addr, &id->route.addr.dev_addr);
1996 ret = cma_acquire_dev(id_priv);
1997 mutex_unlock(&lock);
2002 memcpy(&id->route.addr.src_addr, addr, ip_addr_size(addr));
2003 ret = cma_get_port(id_priv);
2009 if (!cma_any_addr(addr)) {
2011 cma_detach_from_dev(id_priv);
2012 mutex_unlock(&lock);
2015 cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_IDLE);
2018 EXPORT_SYMBOL(rdma_bind_addr);
2020 static int cma_format_hdr(void *hdr, enum rdma_port_space ps,
2021 struct rdma_route *route)
2023 struct sockaddr_in *src4, *dst4;
2024 struct cma_hdr *cma_hdr;
2025 struct sdp_hh *sdp_hdr;
2027 src4 = (struct sockaddr_in *) &route->addr.src_addr;
2028 dst4 = (struct sockaddr_in *) &route->addr.dst_addr;
2033 if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION)
2035 sdp_set_ip_ver(sdp_hdr, 4);
2036 sdp_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
2037 sdp_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
2038 sdp_hdr->port = src4->sin_port;
2042 cma_hdr->cma_version = CMA_VERSION;
2043 cma_set_ip_ver(cma_hdr, 4);
2044 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
2045 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
2046 cma_hdr->port = src4->sin_port;
2052 static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
2053 struct ib_cm_event *ib_event)
2055 struct rdma_id_private *id_priv = cm_id->context;
2056 struct rdma_cm_event event;
2057 struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
2060 if (cma_disable_remove(id_priv, CMA_CONNECT))
2063 memset(&event, 0, sizeof event);
2064 switch (ib_event->event) {
2065 case IB_CM_SIDR_REQ_ERROR:
2066 event.event = RDMA_CM_EVENT_UNREACHABLE;
2067 event.status = -ETIMEDOUT;
2069 case IB_CM_SIDR_REP_RECEIVED:
2070 event.param.ud.private_data = ib_event->private_data;
2071 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
2072 if (rep->status != IB_SIDR_SUCCESS) {
2073 event.event = RDMA_CM_EVENT_UNREACHABLE;
2074 event.status = ib_event->param.sidr_rep_rcvd.status;
2077 if (id_priv->qkey != rep->qkey) {
2078 event.event = RDMA_CM_EVENT_UNREACHABLE;
2079 event.status = -EINVAL;
2082 ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
2083 id_priv->id.route.path_rec,
2084 &event.param.ud.ah_attr);
2085 event.param.ud.qp_num = rep->qpn;
2086 event.param.ud.qkey = rep->qkey;
2087 event.event = RDMA_CM_EVENT_ESTABLISHED;
2091 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d",
2096 ret = id_priv->id.event_handler(&id_priv->id, &event);
2098 /* Destroy the CM ID by returning a non-zero value. */
2099 id_priv->cm_id.ib = NULL;
2100 cma_exch(id_priv, CMA_DESTROYING);
2101 cma_enable_remove(id_priv);
2102 rdma_destroy_id(&id_priv->id);
2106 cma_enable_remove(id_priv);
2110 static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
2111 struct rdma_conn_param *conn_param)
2113 struct ib_cm_sidr_req_param req;
2114 struct rdma_route *route;
2117 req.private_data_len = sizeof(struct cma_hdr) +
2118 conn_param->private_data_len;
2119 req.private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2120 if (!req.private_data)
2123 if (conn_param->private_data && conn_param->private_data_len)
2124 memcpy((void *) req.private_data + sizeof(struct cma_hdr),
2125 conn_param->private_data, conn_param->private_data_len);
2127 route = &id_priv->id.route;
2128 ret = cma_format_hdr((void *) req.private_data, id_priv->id.ps, route);
2132 id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device,
2133 cma_sidr_rep_handler, id_priv);
2134 if (IS_ERR(id_priv->cm_id.ib)) {
2135 ret = PTR_ERR(id_priv->cm_id.ib);
2139 req.path = route->path_rec;
2140 req.service_id = cma_get_service_id(id_priv->id.ps,
2141 &route->addr.dst_addr);
2142 req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
2143 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2145 ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
2147 ib_destroy_cm_id(id_priv->cm_id.ib);
2148 id_priv->cm_id.ib = NULL;
2151 kfree(req.private_data);
2155 static int cma_connect_ib(struct rdma_id_private *id_priv,
2156 struct rdma_conn_param *conn_param)
2158 struct ib_cm_req_param req;
2159 struct rdma_route *route;
2163 memset(&req, 0, sizeof req);
2164 offset = cma_user_data_offset(id_priv->id.ps);
2165 req.private_data_len = offset + conn_param->private_data_len;
2166 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2170 if (conn_param->private_data && conn_param->private_data_len)
2171 memcpy(private_data + offset, conn_param->private_data,
2172 conn_param->private_data_len);
2174 id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_ib_handler,
2176 if (IS_ERR(id_priv->cm_id.ib)) {
2177 ret = PTR_ERR(id_priv->cm_id.ib);
2181 route = &id_priv->id.route;
2182 ret = cma_format_hdr(private_data, id_priv->id.ps, route);
2185 req.private_data = private_data;
2187 req.primary_path = &route->path_rec[0];
2188 if (route->num_paths == 2)
2189 req.alternate_path = &route->path_rec[1];
2191 req.service_id = cma_get_service_id(id_priv->id.ps,
2192 &route->addr.dst_addr);
2193 req.qp_num = id_priv->qp_num;
2194 req.qp_type = IB_QPT_RC;
2195 req.starting_psn = id_priv->seq_num;
2196 req.responder_resources = conn_param->responder_resources;
2197 req.initiator_depth = conn_param->initiator_depth;
2198 req.flow_control = conn_param->flow_control;
2199 req.retry_count = conn_param->retry_count;
2200 req.rnr_retry_count = conn_param->rnr_retry_count;
2201 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2202 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2203 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2204 req.srq = id_priv->srq ? 1 : 0;
2206 ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
2208 if (ret && !IS_ERR(id_priv->cm_id.ib)) {
2209 ib_destroy_cm_id(id_priv->cm_id.ib);
2210 id_priv->cm_id.ib = NULL;
2213 kfree(private_data);
2217 static int cma_connect_iw(struct rdma_id_private *id_priv,
2218 struct rdma_conn_param *conn_param)
2220 struct iw_cm_id *cm_id;
2221 struct sockaddr_in* sin;
2223 struct iw_cm_conn_param iw_param;
2225 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
2226 if (IS_ERR(cm_id)) {
2227 ret = PTR_ERR(cm_id);
2231 id_priv->cm_id.iw = cm_id;
2233 sin = (struct sockaddr_in*) &id_priv->id.route.addr.src_addr;
2234 cm_id->local_addr = *sin;
2236 sin = (struct sockaddr_in*) &id_priv->id.route.addr.dst_addr;
2237 cm_id->remote_addr = *sin;
2239 ret = cma_modify_qp_rtr(&id_priv->id);
2243 iw_param.ord = conn_param->initiator_depth;
2244 iw_param.ird = conn_param->responder_resources;
2245 iw_param.private_data = conn_param->private_data;
2246 iw_param.private_data_len = conn_param->private_data_len;
2248 iw_param.qpn = id_priv->qp_num;
2250 iw_param.qpn = conn_param->qp_num;
2251 ret = iw_cm_connect(cm_id, &iw_param);
2253 if (ret && !IS_ERR(cm_id)) {
2254 iw_destroy_cm_id(cm_id);
2255 id_priv->cm_id.iw = NULL;
2260 int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2262 struct rdma_id_private *id_priv;
2265 id_priv = container_of(id, struct rdma_id_private, id);
2266 if (!cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_CONNECT))
2270 id_priv->qp_num = conn_param->qp_num;
2271 id_priv->srq = conn_param->srq;
2274 switch (rdma_node_get_transport(id->device->node_type)) {
2275 case RDMA_TRANSPORT_IB:
2276 if (cma_is_ud_ps(id->ps))
2277 ret = cma_resolve_ib_udp(id_priv, conn_param);
2279 ret = cma_connect_ib(id_priv, conn_param);
2281 case RDMA_TRANSPORT_IWARP:
2282 ret = cma_connect_iw(id_priv, conn_param);
2293 cma_comp_exch(id_priv, CMA_CONNECT, CMA_ROUTE_RESOLVED);
2296 EXPORT_SYMBOL(rdma_connect);
2298 static int cma_accept_ib(struct rdma_id_private *id_priv,
2299 struct rdma_conn_param *conn_param)
2301 struct ib_cm_rep_param rep;
2302 struct ib_qp_attr qp_attr;
2303 int qp_attr_mask, ret;
2305 if (id_priv->id.qp) {
2306 ret = cma_modify_qp_rtr(&id_priv->id);
2310 qp_attr.qp_state = IB_QPS_RTS;
2311 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, &qp_attr,
2316 qp_attr.max_rd_atomic = conn_param->initiator_depth;
2317 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
2322 memset(&rep, 0, sizeof rep);
2323 rep.qp_num = id_priv->qp_num;
2324 rep.starting_psn = id_priv->seq_num;
2325 rep.private_data = conn_param->private_data;
2326 rep.private_data_len = conn_param->private_data_len;
2327 rep.responder_resources = conn_param->responder_resources;
2328 rep.initiator_depth = conn_param->initiator_depth;
2329 rep.failover_accepted = 0;
2330 rep.flow_control = conn_param->flow_control;
2331 rep.rnr_retry_count = conn_param->rnr_retry_count;
2332 rep.srq = id_priv->srq ? 1 : 0;
2334 ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
2339 static int cma_accept_iw(struct rdma_id_private *id_priv,
2340 struct rdma_conn_param *conn_param)
2342 struct iw_cm_conn_param iw_param;
2345 ret = cma_modify_qp_rtr(&id_priv->id);
2349 iw_param.ord = conn_param->initiator_depth;
2350 iw_param.ird = conn_param->responder_resources;
2351 iw_param.private_data = conn_param->private_data;
2352 iw_param.private_data_len = conn_param->private_data_len;
2353 if (id_priv->id.qp) {
2354 iw_param.qpn = id_priv->qp_num;
2356 iw_param.qpn = conn_param->qp_num;
2358 return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
2361 static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
2362 enum ib_cm_sidr_status status,
2363 const void *private_data, int private_data_len)
2365 struct ib_cm_sidr_rep_param rep;
2367 memset(&rep, 0, sizeof rep);
2368 rep.status = status;
2369 if (status == IB_SIDR_SUCCESS) {
2370 rep.qp_num = id_priv->qp_num;
2371 rep.qkey = id_priv->qkey;
2373 rep.private_data = private_data;
2374 rep.private_data_len = private_data_len;
2376 return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
2379 int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2381 struct rdma_id_private *id_priv;
2384 id_priv = container_of(id, struct rdma_id_private, id);
2385 if (!cma_comp(id_priv, CMA_CONNECT))
2388 if (!id->qp && conn_param) {
2389 id_priv->qp_num = conn_param->qp_num;
2390 id_priv->srq = conn_param->srq;
2393 switch (rdma_node_get_transport(id->device->node_type)) {
2394 case RDMA_TRANSPORT_IB:
2395 if (cma_is_ud_ps(id->ps))
2396 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
2397 conn_param->private_data,
2398 conn_param->private_data_len);
2399 else if (conn_param)
2400 ret = cma_accept_ib(id_priv, conn_param);
2402 ret = cma_rep_recv(id_priv);
2404 case RDMA_TRANSPORT_IWARP:
2405 ret = cma_accept_iw(id_priv, conn_param);
2417 cma_modify_qp_err(id);
2418 rdma_reject(id, NULL, 0);
2421 EXPORT_SYMBOL(rdma_accept);
2423 int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
2425 struct rdma_id_private *id_priv;
2428 id_priv = container_of(id, struct rdma_id_private, id);
2429 if (!cma_has_cm_dev(id_priv))
2432 switch (id->device->node_type) {
2433 case RDMA_NODE_IB_CA:
2434 ret = ib_cm_notify(id_priv->cm_id.ib, event);
2442 EXPORT_SYMBOL(rdma_notify);
2444 int rdma_reject(struct rdma_cm_id *id, const void *private_data,
2445 u8 private_data_len)
2447 struct rdma_id_private *id_priv;
2450 id_priv = container_of(id, struct rdma_id_private, id);
2451 if (!cma_has_cm_dev(id_priv))
2454 switch (rdma_node_get_transport(id->device->node_type)) {
2455 case RDMA_TRANSPORT_IB:
2456 if (cma_is_ud_ps(id->ps))
2457 ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT,
2458 private_data, private_data_len);
2460 ret = ib_send_cm_rej(id_priv->cm_id.ib,
2461 IB_CM_REJ_CONSUMER_DEFINED, NULL,
2462 0, private_data, private_data_len);
2464 case RDMA_TRANSPORT_IWARP:
2465 ret = iw_cm_reject(id_priv->cm_id.iw,
2466 private_data, private_data_len);
2474 EXPORT_SYMBOL(rdma_reject);
2476 int rdma_disconnect(struct rdma_cm_id *id)
2478 struct rdma_id_private *id_priv;
2481 id_priv = container_of(id, struct rdma_id_private, id);
2482 if (!cma_has_cm_dev(id_priv))
2485 switch (rdma_node_get_transport(id->device->node_type)) {
2486 case RDMA_TRANSPORT_IB:
2487 ret = cma_modify_qp_err(id);
2490 /* Initiate or respond to a disconnect. */
2491 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
2492 ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
2494 case RDMA_TRANSPORT_IWARP:
2495 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
2504 EXPORT_SYMBOL(rdma_disconnect);
2506 static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
2508 struct rdma_id_private *id_priv;
2509 struct cma_multicast *mc = multicast->context;
2510 struct rdma_cm_event event;
2513 id_priv = mc->id_priv;
2514 if (cma_disable_remove(id_priv, CMA_ADDR_BOUND) &&
2515 cma_disable_remove(id_priv, CMA_ADDR_RESOLVED))
2518 if (!status && id_priv->id.qp)
2519 status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
2520 multicast->rec.mlid);
2522 memset(&event, 0, sizeof event);
2523 event.status = status;
2524 event.param.ud.private_data = mc->context;
2526 event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
2527 ib_init_ah_from_mcmember(id_priv->id.device,
2528 id_priv->id.port_num, &multicast->rec,
2529 &event.param.ud.ah_attr);
2530 event.param.ud.qp_num = 0xFFFFFF;
2531 event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
2533 event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
2535 ret = id_priv->id.event_handler(&id_priv->id, &event);
2537 cma_exch(id_priv, CMA_DESTROYING);
2538 cma_enable_remove(id_priv);
2539 rdma_destroy_id(&id_priv->id);
2543 cma_enable_remove(id_priv);
2547 static void cma_set_mgid(struct rdma_id_private *id_priv,
2548 struct sockaddr *addr, union ib_gid *mgid)
2550 unsigned char mc_map[MAX_ADDR_LEN];
2551 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2552 struct sockaddr_in *sin = (struct sockaddr_in *) addr;
2553 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
2555 if (cma_any_addr(addr)) {
2556 memset(mgid, 0, sizeof *mgid);
2557 } else if ((addr->sa_family == AF_INET6) &&
2558 ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFF10A01B) ==
2560 /* IPv6 address is an SA assigned MGID. */
2561 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
2563 ip_ib_mc_map(sin->sin_addr.s_addr, mc_map);
2564 if (id_priv->id.ps == RDMA_PS_UDP)
2565 mc_map[7] = 0x01; /* Use RDMA CM signature */
2566 mc_map[8] = ib_addr_get_pkey(dev_addr) >> 8;
2567 mc_map[9] = (unsigned char) ib_addr_get_pkey(dev_addr);
2568 *mgid = *(union ib_gid *) (mc_map + 4);
2572 static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
2573 struct cma_multicast *mc)
2575 struct ib_sa_mcmember_rec rec;
2576 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2577 ib_sa_comp_mask comp_mask;
2580 ib_addr_get_mgid(dev_addr, &rec.mgid);
2581 ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
2586 cma_set_mgid(id_priv, &mc->addr, &rec.mgid);
2587 if (id_priv->id.ps == RDMA_PS_UDP)
2588 rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
2589 ib_addr_get_sgid(dev_addr, &rec.port_gid);
2590 rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
2593 comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
2594 IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
2595 IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
2596 IB_SA_MCMEMBER_REC_FLOW_LABEL |
2597 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
2599 mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
2600 id_priv->id.port_num, &rec,
2601 comp_mask, GFP_KERNEL,
2602 cma_ib_mc_handler, mc);
2603 if (IS_ERR(mc->multicast.ib))
2604 return PTR_ERR(mc->multicast.ib);
2609 int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
2612 struct rdma_id_private *id_priv;
2613 struct cma_multicast *mc;
2616 id_priv = container_of(id, struct rdma_id_private, id);
2617 if (!cma_comp(id_priv, CMA_ADDR_BOUND) &&
2618 !cma_comp(id_priv, CMA_ADDR_RESOLVED))
2621 mc = kmalloc(sizeof *mc, GFP_KERNEL);
2625 memcpy(&mc->addr, addr, ip_addr_size(addr));
2626 mc->context = context;
2627 mc->id_priv = id_priv;
2629 spin_lock(&id_priv->lock);
2630 list_add(&mc->list, &id_priv->mc_list);
2631 spin_unlock(&id_priv->lock);
2633 switch (rdma_node_get_transport(id->device->node_type)) {
2634 case RDMA_TRANSPORT_IB:
2635 ret = cma_join_ib_multicast(id_priv, mc);
2643 spin_lock_irq(&id_priv->lock);
2644 list_del(&mc->list);
2645 spin_unlock_irq(&id_priv->lock);
2650 EXPORT_SYMBOL(rdma_join_multicast);
2652 void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
2654 struct rdma_id_private *id_priv;
2655 struct cma_multicast *mc;
2657 id_priv = container_of(id, struct rdma_id_private, id);
2658 spin_lock_irq(&id_priv->lock);
2659 list_for_each_entry(mc, &id_priv->mc_list, list) {
2660 if (!memcmp(&mc->addr, addr, ip_addr_size(addr))) {
2661 list_del(&mc->list);
2662 spin_unlock_irq(&id_priv->lock);
2665 ib_detach_mcast(id->qp,
2666 &mc->multicast.ib->rec.mgid,
2667 mc->multicast.ib->rec.mlid);
2668 ib_sa_free_multicast(mc->multicast.ib);
2673 spin_unlock_irq(&id_priv->lock);
2675 EXPORT_SYMBOL(rdma_leave_multicast);
2677 static void cma_add_one(struct ib_device *device)
2679 struct cma_device *cma_dev;
2680 struct rdma_id_private *id_priv;
2682 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
2686 cma_dev->device = device;
2688 init_completion(&cma_dev->comp);
2689 atomic_set(&cma_dev->refcount, 1);
2690 INIT_LIST_HEAD(&cma_dev->id_list);
2691 ib_set_client_data(device, &cma_client, cma_dev);
2694 list_add_tail(&cma_dev->list, &dev_list);
2695 list_for_each_entry(id_priv, &listen_any_list, list)
2696 cma_listen_on_dev(id_priv, cma_dev);
2697 mutex_unlock(&lock);
2700 static int cma_remove_id_dev(struct rdma_id_private *id_priv)
2702 struct rdma_cm_event event;
2703 enum cma_state state;
2705 /* Record that we want to remove the device */
2706 state = cma_exch(id_priv, CMA_DEVICE_REMOVAL);
2707 if (state == CMA_DESTROYING)
2710 cma_cancel_operation(id_priv, state);
2711 wait_event(id_priv->wait_remove, !atomic_read(&id_priv->dev_remove));
2713 /* Check for destruction from another callback. */
2714 if (!cma_comp(id_priv, CMA_DEVICE_REMOVAL))
2717 memset(&event, 0, sizeof event);
2718 event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
2719 return id_priv->id.event_handler(&id_priv->id, &event);
2722 static void cma_process_remove(struct cma_device *cma_dev)
2724 struct rdma_id_private *id_priv;
2728 while (!list_empty(&cma_dev->id_list)) {
2729 id_priv = list_entry(cma_dev->id_list.next,
2730 struct rdma_id_private, list);
2732 if (cma_internal_listen(id_priv)) {
2733 cma_destroy_listen(id_priv);
2737 list_del_init(&id_priv->list);
2738 atomic_inc(&id_priv->refcount);
2739 mutex_unlock(&lock);
2741 ret = cma_remove_id_dev(id_priv);
2742 cma_deref_id(id_priv);
2744 rdma_destroy_id(&id_priv->id);
2748 mutex_unlock(&lock);
2750 cma_deref_dev(cma_dev);
2751 wait_for_completion(&cma_dev->comp);
2754 static void cma_remove_one(struct ib_device *device)
2756 struct cma_device *cma_dev;
2758 cma_dev = ib_get_client_data(device, &cma_client);
2763 list_del(&cma_dev->list);
2764 mutex_unlock(&lock);
2766 cma_process_remove(cma_dev);
2770 static int cma_init(void)
2774 get_random_bytes(&next_port, sizeof next_port);
2775 next_port = ((unsigned int) next_port %
2776 (sysctl_local_port_range[1] - sysctl_local_port_range[0])) +
2777 sysctl_local_port_range[0];
2778 cma_wq = create_singlethread_workqueue("rdma_cm");
2782 ib_sa_register_client(&sa_client);
2783 rdma_addr_register_client(&addr_client);
2785 ret = ib_register_client(&cma_client);
2791 rdma_addr_unregister_client(&addr_client);
2792 ib_sa_unregister_client(&sa_client);
2793 destroy_workqueue(cma_wq);
2797 static void cma_cleanup(void)
2799 ib_unregister_client(&cma_client);
2800 rdma_addr_unregister_client(&addr_client);
2801 ib_sa_unregister_client(&sa_client);
2802 destroy_workqueue(cma_wq);
2803 idr_destroy(&sdp_ps);
2804 idr_destroy(&tcp_ps);
2805 idr_destroy(&udp_ps);
2806 idr_destroy(&ipoib_ps);
2809 module_init(cma_init);
2810 module_exit(cma_cleanup);