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;
81 struct completion comp;
83 struct list_head id_list;
100 struct rdma_bind_list {
102 struct hlist_head owners;
107 * Device removal can occur at anytime, so we need extra handling to
108 * serialize notifying the user of device removal with other callbacks.
109 * We do this by disabling removal notification while a callback is in process,
110 * and reporting it after the callback completes.
112 struct rdma_id_private {
113 struct rdma_cm_id id;
115 struct rdma_bind_list *bind_list;
116 struct hlist_node node;
117 struct list_head list;
118 struct list_head listen_list;
119 struct cma_device *cma_dev;
120 struct list_head mc_list;
122 enum cma_state state;
124 struct completion comp;
126 wait_queue_head_t wait_remove;
131 struct ib_sa_query *query;
144 struct cma_multicast {
145 struct rdma_id_private *id_priv;
147 struct ib_sa_multicast *ib;
149 struct list_head list;
151 struct sockaddr addr;
152 u8 pad[sizeof(struct sockaddr_in6) -
153 sizeof(struct sockaddr)];
157 struct work_struct work;
158 struct rdma_id_private *id;
159 enum cma_state old_state;
160 enum cma_state new_state;
161 struct rdma_cm_event event;
174 u8 ip_version; /* IP version: 7:4 */
176 union cma_ip_addr src_addr;
177 union cma_ip_addr dst_addr;
182 u8 sdp_version; /* Major version: 7:4 */
183 u8 ip_version; /* IP version: 7:4 */
184 u8 sdp_specific1[10];
187 union cma_ip_addr src_addr;
188 union cma_ip_addr dst_addr;
196 #define CMA_VERSION 0x00
197 #define SDP_MAJ_VERSION 0x2
199 static int cma_comp(struct rdma_id_private *id_priv, enum cma_state comp)
204 spin_lock_irqsave(&id_priv->lock, flags);
205 ret = (id_priv->state == comp);
206 spin_unlock_irqrestore(&id_priv->lock, flags);
210 static int cma_comp_exch(struct rdma_id_private *id_priv,
211 enum cma_state comp, enum cma_state exch)
216 spin_lock_irqsave(&id_priv->lock, flags);
217 if ((ret = (id_priv->state == comp)))
218 id_priv->state = exch;
219 spin_unlock_irqrestore(&id_priv->lock, flags);
223 static enum cma_state cma_exch(struct rdma_id_private *id_priv,
229 spin_lock_irqsave(&id_priv->lock, flags);
230 old = id_priv->state;
231 id_priv->state = exch;
232 spin_unlock_irqrestore(&id_priv->lock, flags);
236 static inline u8 cma_get_ip_ver(struct cma_hdr *hdr)
238 return hdr->ip_version >> 4;
241 static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
243 hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
246 static inline u8 sdp_get_majv(u8 sdp_version)
248 return sdp_version >> 4;
251 static inline u8 sdp_get_ip_ver(struct sdp_hh *hh)
253 return hh->ip_version >> 4;
256 static inline void sdp_set_ip_ver(struct sdp_hh *hh, u8 ip_ver)
258 hh->ip_version = (ip_ver << 4) | (hh->ip_version & 0xF);
261 static inline int cma_is_ud_ps(enum rdma_port_space ps)
263 return (ps == RDMA_PS_UDP || ps == RDMA_PS_IPOIB);
266 static void cma_attach_to_dev(struct rdma_id_private *id_priv,
267 struct cma_device *cma_dev)
269 atomic_inc(&cma_dev->refcount);
270 id_priv->cma_dev = cma_dev;
271 id_priv->id.device = cma_dev->device;
272 list_add_tail(&id_priv->list, &cma_dev->id_list);
275 static inline void cma_deref_dev(struct cma_device *cma_dev)
277 if (atomic_dec_and_test(&cma_dev->refcount))
278 complete(&cma_dev->comp);
281 static void cma_detach_from_dev(struct rdma_id_private *id_priv)
283 list_del(&id_priv->list);
284 cma_deref_dev(id_priv->cma_dev);
285 id_priv->cma_dev = NULL;
288 static int cma_set_qkey(struct ib_device *device, u8 port_num,
289 enum rdma_port_space ps,
290 struct rdma_dev_addr *dev_addr, u32 *qkey)
292 struct ib_sa_mcmember_rec rec;
297 *qkey = RDMA_UDP_QKEY;
300 ib_addr_get_mgid(dev_addr, &rec.mgid);
301 ret = ib_sa_get_mcmember_rec(device, port_num, &rec.mgid, &rec);
302 *qkey = be32_to_cpu(rec.qkey);
310 static int cma_acquire_dev(struct rdma_id_private *id_priv)
312 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
313 struct cma_device *cma_dev;
317 switch (rdma_node_get_transport(dev_addr->dev_type)) {
318 case RDMA_TRANSPORT_IB:
319 ib_addr_get_sgid(dev_addr, &gid);
321 case RDMA_TRANSPORT_IWARP:
322 iw_addr_get_sgid(dev_addr, &gid);
328 list_for_each_entry(cma_dev, &dev_list, list) {
329 ret = ib_find_cached_gid(cma_dev->device, &gid,
330 &id_priv->id.port_num, NULL);
332 ret = cma_set_qkey(cma_dev->device,
333 id_priv->id.port_num,
334 id_priv->id.ps, dev_addr,
337 cma_attach_to_dev(id_priv, cma_dev);
344 static void cma_deref_id(struct rdma_id_private *id_priv)
346 if (atomic_dec_and_test(&id_priv->refcount))
347 complete(&id_priv->comp);
350 static void cma_release_remove(struct rdma_id_private *id_priv)
352 if (atomic_dec_and_test(&id_priv->dev_remove))
353 wake_up(&id_priv->wait_remove);
356 struct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler,
357 void *context, enum rdma_port_space ps)
359 struct rdma_id_private *id_priv;
361 id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
363 return ERR_PTR(-ENOMEM);
365 id_priv->state = CMA_IDLE;
366 id_priv->id.context = context;
367 id_priv->id.event_handler = event_handler;
369 spin_lock_init(&id_priv->lock);
370 init_completion(&id_priv->comp);
371 atomic_set(&id_priv->refcount, 1);
372 init_waitqueue_head(&id_priv->wait_remove);
373 atomic_set(&id_priv->dev_remove, 0);
374 INIT_LIST_HEAD(&id_priv->listen_list);
375 INIT_LIST_HEAD(&id_priv->mc_list);
376 get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
380 EXPORT_SYMBOL(rdma_create_id);
382 static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
384 struct ib_qp_attr qp_attr;
385 int qp_attr_mask, ret;
387 qp_attr.qp_state = IB_QPS_INIT;
388 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
392 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
396 qp_attr.qp_state = IB_QPS_RTR;
397 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
401 qp_attr.qp_state = IB_QPS_RTS;
403 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
408 static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
410 struct ib_qp_attr qp_attr;
411 int qp_attr_mask, ret;
413 qp_attr.qp_state = IB_QPS_INIT;
414 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
418 return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
421 int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
422 struct ib_qp_init_attr *qp_init_attr)
424 struct rdma_id_private *id_priv;
428 id_priv = container_of(id, struct rdma_id_private, id);
429 if (id->device != pd->device)
432 qp = ib_create_qp(pd, qp_init_attr);
436 if (cma_is_ud_ps(id_priv->id.ps))
437 ret = cma_init_ud_qp(id_priv, qp);
439 ret = cma_init_conn_qp(id_priv, qp);
444 id_priv->qp_num = qp->qp_num;
445 id_priv->srq = (qp->srq != NULL);
451 EXPORT_SYMBOL(rdma_create_qp);
453 void rdma_destroy_qp(struct rdma_cm_id *id)
455 ib_destroy_qp(id->qp);
457 EXPORT_SYMBOL(rdma_destroy_qp);
459 static int cma_modify_qp_rtr(struct rdma_cm_id *id)
461 struct ib_qp_attr qp_attr;
462 int qp_attr_mask, ret;
467 /* Need to update QP attributes from default values. */
468 qp_attr.qp_state = IB_QPS_INIT;
469 ret = rdma_init_qp_attr(id, &qp_attr, &qp_attr_mask);
473 ret = ib_modify_qp(id->qp, &qp_attr, qp_attr_mask);
477 qp_attr.qp_state = IB_QPS_RTR;
478 ret = rdma_init_qp_attr(id, &qp_attr, &qp_attr_mask);
482 return ib_modify_qp(id->qp, &qp_attr, qp_attr_mask);
485 static int cma_modify_qp_rts(struct rdma_cm_id *id)
487 struct ib_qp_attr qp_attr;
488 int qp_attr_mask, ret;
493 qp_attr.qp_state = IB_QPS_RTS;
494 ret = rdma_init_qp_attr(id, &qp_attr, &qp_attr_mask);
498 return ib_modify_qp(id->qp, &qp_attr, qp_attr_mask);
501 static int cma_modify_qp_err(struct rdma_cm_id *id)
503 struct ib_qp_attr qp_attr;
508 qp_attr.qp_state = IB_QPS_ERR;
509 return ib_modify_qp(id->qp, &qp_attr, IB_QP_STATE);
512 static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
513 struct ib_qp_attr *qp_attr, int *qp_attr_mask)
515 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
518 ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
519 ib_addr_get_pkey(dev_addr),
520 &qp_attr->pkey_index);
524 qp_attr->port_num = id_priv->id.port_num;
525 *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
527 if (cma_is_ud_ps(id_priv->id.ps)) {
528 qp_attr->qkey = id_priv->qkey;
529 *qp_attr_mask |= IB_QP_QKEY;
531 qp_attr->qp_access_flags = 0;
532 *qp_attr_mask |= IB_QP_ACCESS_FLAGS;
537 int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
540 struct rdma_id_private *id_priv;
543 id_priv = container_of(id, struct rdma_id_private, id);
544 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
545 case RDMA_TRANSPORT_IB:
546 if (!id_priv->cm_id.ib || cma_is_ud_ps(id_priv->id.ps))
547 ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
549 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
551 if (qp_attr->qp_state == IB_QPS_RTR)
552 qp_attr->rq_psn = id_priv->seq_num;
554 case RDMA_TRANSPORT_IWARP:
555 if (!id_priv->cm_id.iw) {
556 qp_attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE;
557 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
559 ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
569 EXPORT_SYMBOL(rdma_init_qp_attr);
571 static inline int cma_zero_addr(struct sockaddr *addr)
573 struct in6_addr *ip6;
575 if (addr->sa_family == AF_INET)
576 return ZERONET(((struct sockaddr_in *) addr)->sin_addr.s_addr);
578 ip6 = &((struct sockaddr_in6 *) addr)->sin6_addr;
579 return (ip6->s6_addr32[0] | ip6->s6_addr32[1] |
580 ip6->s6_addr32[2] | ip6->s6_addr32[3]) == 0;
584 static inline int cma_loopback_addr(struct sockaddr *addr)
586 return LOOPBACK(((struct sockaddr_in *) addr)->sin_addr.s_addr);
589 static inline int cma_any_addr(struct sockaddr *addr)
591 return cma_zero_addr(addr) || cma_loopback_addr(addr);
594 static inline __be16 cma_port(struct sockaddr *addr)
596 if (addr->sa_family == AF_INET)
597 return ((struct sockaddr_in *) addr)->sin_port;
599 return ((struct sockaddr_in6 *) addr)->sin6_port;
602 static inline int cma_any_port(struct sockaddr *addr)
604 return !cma_port(addr);
607 static int cma_get_net_info(void *hdr, enum rdma_port_space ps,
608 u8 *ip_ver, __u16 *port,
609 union cma_ip_addr **src, union cma_ip_addr **dst)
613 if (sdp_get_majv(((struct sdp_hh *) hdr)->sdp_version) !=
617 *ip_ver = sdp_get_ip_ver(hdr);
618 *port = ((struct sdp_hh *) hdr)->port;
619 *src = &((struct sdp_hh *) hdr)->src_addr;
620 *dst = &((struct sdp_hh *) hdr)->dst_addr;
623 if (((struct cma_hdr *) hdr)->cma_version != CMA_VERSION)
626 *ip_ver = cma_get_ip_ver(hdr);
627 *port = ((struct cma_hdr *) hdr)->port;
628 *src = &((struct cma_hdr *) hdr)->src_addr;
629 *dst = &((struct cma_hdr *) hdr)->dst_addr;
633 if (*ip_ver != 4 && *ip_ver != 6)
638 static void cma_save_net_info(struct rdma_addr *addr,
639 struct rdma_addr *listen_addr,
640 u8 ip_ver, __u16 port,
641 union cma_ip_addr *src, union cma_ip_addr *dst)
643 struct sockaddr_in *listen4, *ip4;
644 struct sockaddr_in6 *listen6, *ip6;
648 listen4 = (struct sockaddr_in *) &listen_addr->src_addr;
649 ip4 = (struct sockaddr_in *) &addr->src_addr;
650 ip4->sin_family = listen4->sin_family;
651 ip4->sin_addr.s_addr = dst->ip4.addr;
652 ip4->sin_port = listen4->sin_port;
654 ip4 = (struct sockaddr_in *) &addr->dst_addr;
655 ip4->sin_family = listen4->sin_family;
656 ip4->sin_addr.s_addr = src->ip4.addr;
657 ip4->sin_port = port;
660 listen6 = (struct sockaddr_in6 *) &listen_addr->src_addr;
661 ip6 = (struct sockaddr_in6 *) &addr->src_addr;
662 ip6->sin6_family = listen6->sin6_family;
663 ip6->sin6_addr = dst->ip6;
664 ip6->sin6_port = listen6->sin6_port;
666 ip6 = (struct sockaddr_in6 *) &addr->dst_addr;
667 ip6->sin6_family = listen6->sin6_family;
668 ip6->sin6_addr = src->ip6;
669 ip6->sin6_port = port;
676 static inline int cma_user_data_offset(enum rdma_port_space ps)
682 return sizeof(struct cma_hdr);
686 static void cma_cancel_route(struct rdma_id_private *id_priv)
688 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
689 case RDMA_TRANSPORT_IB:
691 ib_sa_cancel_query(id_priv->query_id, id_priv->query);
698 static inline int cma_internal_listen(struct rdma_id_private *id_priv)
700 return (id_priv->state == CMA_LISTEN) && id_priv->cma_dev &&
701 cma_any_addr(&id_priv->id.route.addr.src_addr);
704 static void cma_destroy_listen(struct rdma_id_private *id_priv)
706 cma_exch(id_priv, CMA_DESTROYING);
708 if (id_priv->cma_dev) {
709 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
710 case RDMA_TRANSPORT_IB:
711 if (id_priv->cm_id.ib && !IS_ERR(id_priv->cm_id.ib))
712 ib_destroy_cm_id(id_priv->cm_id.ib);
714 case RDMA_TRANSPORT_IWARP:
715 if (id_priv->cm_id.iw && !IS_ERR(id_priv->cm_id.iw))
716 iw_destroy_cm_id(id_priv->cm_id.iw);
721 cma_detach_from_dev(id_priv);
723 list_del(&id_priv->listen_list);
725 cma_deref_id(id_priv);
726 wait_for_completion(&id_priv->comp);
731 static void cma_cancel_listens(struct rdma_id_private *id_priv)
733 struct rdma_id_private *dev_id_priv;
736 list_del(&id_priv->list);
738 while (!list_empty(&id_priv->listen_list)) {
739 dev_id_priv = list_entry(id_priv->listen_list.next,
740 struct rdma_id_private, listen_list);
741 cma_destroy_listen(dev_id_priv);
746 static void cma_cancel_operation(struct rdma_id_private *id_priv,
747 enum cma_state state)
751 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
753 case CMA_ROUTE_QUERY:
754 cma_cancel_route(id_priv);
757 if (cma_any_addr(&id_priv->id.route.addr.src_addr) &&
759 cma_cancel_listens(id_priv);
766 static void cma_release_port(struct rdma_id_private *id_priv)
768 struct rdma_bind_list *bind_list = id_priv->bind_list;
774 hlist_del(&id_priv->node);
775 if (hlist_empty(&bind_list->owners)) {
776 idr_remove(bind_list->ps, bind_list->port);
782 static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
784 struct cma_multicast *mc;
786 while (!list_empty(&id_priv->mc_list)) {
787 mc = container_of(id_priv->mc_list.next,
788 struct cma_multicast, list);
790 ib_sa_free_multicast(mc->multicast.ib);
795 void rdma_destroy_id(struct rdma_cm_id *id)
797 struct rdma_id_private *id_priv;
798 enum cma_state state;
800 id_priv = container_of(id, struct rdma_id_private, id);
801 state = cma_exch(id_priv, CMA_DESTROYING);
802 cma_cancel_operation(id_priv, state);
805 if (id_priv->cma_dev) {
807 switch (rdma_node_get_transport(id->device->node_type)) {
808 case RDMA_TRANSPORT_IB:
809 if (id_priv->cm_id.ib && !IS_ERR(id_priv->cm_id.ib))
810 ib_destroy_cm_id(id_priv->cm_id.ib);
812 case RDMA_TRANSPORT_IWARP:
813 if (id_priv->cm_id.iw && !IS_ERR(id_priv->cm_id.iw))
814 iw_destroy_cm_id(id_priv->cm_id.iw);
819 cma_leave_mc_groups(id_priv);
821 cma_detach_from_dev(id_priv);
825 cma_release_port(id_priv);
826 cma_deref_id(id_priv);
827 wait_for_completion(&id_priv->comp);
829 kfree(id_priv->id.route.path_rec);
832 EXPORT_SYMBOL(rdma_destroy_id);
834 static int cma_rep_recv(struct rdma_id_private *id_priv)
838 ret = cma_modify_qp_rtr(&id_priv->id);
842 ret = cma_modify_qp_rts(&id_priv->id);
846 ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
852 cma_modify_qp_err(&id_priv->id);
853 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
858 static int cma_verify_rep(struct rdma_id_private *id_priv, void *data)
860 if (id_priv->id.ps == RDMA_PS_SDP &&
861 sdp_get_majv(((struct sdp_hah *) data)->sdp_version) !=
868 static void cma_set_rep_event_data(struct rdma_cm_event *event,
869 struct ib_cm_rep_event_param *rep_data,
872 event->param.conn.private_data = private_data;
873 event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
874 event->param.conn.responder_resources = rep_data->responder_resources;
875 event->param.conn.initiator_depth = rep_data->initiator_depth;
876 event->param.conn.flow_control = rep_data->flow_control;
877 event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
878 event->param.conn.srq = rep_data->srq;
879 event->param.conn.qp_num = rep_data->remote_qpn;
882 static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
884 struct rdma_id_private *id_priv = cm_id->context;
885 struct rdma_cm_event event;
888 atomic_inc(&id_priv->dev_remove);
889 if (!cma_comp(id_priv, CMA_CONNECT))
892 memset(&event, 0, sizeof event);
893 switch (ib_event->event) {
894 case IB_CM_REQ_ERROR:
895 case IB_CM_REP_ERROR:
896 event.event = RDMA_CM_EVENT_UNREACHABLE;
897 event.status = -ETIMEDOUT;
899 case IB_CM_REP_RECEIVED:
900 event.status = cma_verify_rep(id_priv, ib_event->private_data);
902 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
903 else if (id_priv->id.qp && id_priv->id.ps != RDMA_PS_SDP) {
904 event.status = cma_rep_recv(id_priv);
905 event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
906 RDMA_CM_EVENT_ESTABLISHED;
908 event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
909 cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
910 ib_event->private_data);
912 case IB_CM_RTU_RECEIVED:
913 case IB_CM_USER_ESTABLISHED:
914 event.event = RDMA_CM_EVENT_ESTABLISHED;
916 case IB_CM_DREQ_ERROR:
917 event.status = -ETIMEDOUT; /* fall through */
918 case IB_CM_DREQ_RECEIVED:
919 case IB_CM_DREP_RECEIVED:
920 if (!cma_comp_exch(id_priv, CMA_CONNECT, CMA_DISCONNECT))
922 event.event = RDMA_CM_EVENT_DISCONNECTED;
924 case IB_CM_TIMEWAIT_EXIT:
925 case IB_CM_MRA_RECEIVED:
928 case IB_CM_REJ_RECEIVED:
929 cma_modify_qp_err(&id_priv->id);
930 event.status = ib_event->param.rej_rcvd.reason;
931 event.event = RDMA_CM_EVENT_REJECTED;
932 event.param.conn.private_data = ib_event->private_data;
933 event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
936 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d",
941 ret = id_priv->id.event_handler(&id_priv->id, &event);
943 /* Destroy the CM ID by returning a non-zero value. */
944 id_priv->cm_id.ib = NULL;
945 cma_exch(id_priv, CMA_DESTROYING);
946 cma_release_remove(id_priv);
947 rdma_destroy_id(&id_priv->id);
951 cma_release_remove(id_priv);
955 static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
956 struct ib_cm_event *ib_event)
958 struct rdma_id_private *id_priv;
959 struct rdma_cm_id *id;
960 struct rdma_route *rt;
961 union cma_ip_addr *src, *dst;
965 if (cma_get_net_info(ib_event->private_data, listen_id->ps,
966 &ip_ver, &port, &src, &dst))
969 id = rdma_create_id(listen_id->event_handler, listen_id->context,
974 cma_save_net_info(&id->route.addr, &listen_id->route.addr,
975 ip_ver, port, src, dst);
978 rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
979 rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
984 rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
985 if (rt->num_paths == 2)
986 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
988 ib_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
989 ib_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
990 ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
991 rt->addr.dev_addr.dev_type = RDMA_NODE_IB_CA;
993 id_priv = container_of(id, struct rdma_id_private, id);
994 id_priv->state = CMA_CONNECT;
1003 static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
1004 struct ib_cm_event *ib_event)
1006 struct rdma_id_private *id_priv;
1007 struct rdma_cm_id *id;
1008 union cma_ip_addr *src, *dst;
1013 id = rdma_create_id(listen_id->event_handler, listen_id->context,
1019 if (cma_get_net_info(ib_event->private_data, listen_id->ps,
1020 &ip_ver, &port, &src, &dst))
1023 cma_save_net_info(&id->route.addr, &listen_id->route.addr,
1024 ip_ver, port, src, dst);
1026 ret = rdma_translate_ip(&id->route.addr.src_addr,
1027 &id->route.addr.dev_addr);
1031 id_priv = container_of(id, struct rdma_id_private, id);
1032 id_priv->state = CMA_CONNECT;
1035 rdma_destroy_id(id);
1039 static void cma_set_req_event_data(struct rdma_cm_event *event,
1040 struct ib_cm_req_event_param *req_data,
1041 void *private_data, int offset)
1043 event->param.conn.private_data = private_data + offset;
1044 event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
1045 event->param.conn.responder_resources = req_data->responder_resources;
1046 event->param.conn.initiator_depth = req_data->initiator_depth;
1047 event->param.conn.flow_control = req_data->flow_control;
1048 event->param.conn.retry_count = req_data->retry_count;
1049 event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
1050 event->param.conn.srq = req_data->srq;
1051 event->param.conn.qp_num = req_data->remote_qpn;
1054 static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1056 struct rdma_id_private *listen_id, *conn_id;
1057 struct rdma_cm_event event;
1060 listen_id = cm_id->context;
1061 atomic_inc(&listen_id->dev_remove);
1062 if (!cma_comp(listen_id, CMA_LISTEN)) {
1063 ret = -ECONNABORTED;
1067 memset(&event, 0, sizeof event);
1068 offset = cma_user_data_offset(listen_id->id.ps);
1069 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1070 if (cma_is_ud_ps(listen_id->id.ps)) {
1071 conn_id = cma_new_udp_id(&listen_id->id, ib_event);
1072 event.param.ud.private_data = ib_event->private_data + offset;
1073 event.param.ud.private_data_len =
1074 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
1076 conn_id = cma_new_conn_id(&listen_id->id, ib_event);
1077 cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
1078 ib_event->private_data, offset);
1085 atomic_inc(&conn_id->dev_remove);
1087 ret = cma_acquire_dev(conn_id);
1088 mutex_unlock(&lock);
1090 goto release_conn_id;
1092 conn_id->cm_id.ib = cm_id;
1093 cm_id->context = conn_id;
1094 cm_id->cm_handler = cma_ib_handler;
1096 ret = conn_id->id.event_handler(&conn_id->id, &event);
1100 /* Destroy the CM ID by returning a non-zero value. */
1101 conn_id->cm_id.ib = NULL;
1104 cma_exch(conn_id, CMA_DESTROYING);
1105 cma_release_remove(conn_id);
1106 rdma_destroy_id(&conn_id->id);
1109 cma_release_remove(listen_id);
1113 static __be64 cma_get_service_id(enum rdma_port_space ps, struct sockaddr *addr)
1115 return cpu_to_be64(((u64)ps << 16) + be16_to_cpu(cma_port(addr)));
1118 static void cma_set_compare_data(enum rdma_port_space ps, struct sockaddr *addr,
1119 struct ib_cm_compare_data *compare)
1121 struct cma_hdr *cma_data, *cma_mask;
1122 struct sdp_hh *sdp_data, *sdp_mask;
1124 struct in6_addr ip6_addr;
1126 memset(compare, 0, sizeof *compare);
1127 cma_data = (void *) compare->data;
1128 cma_mask = (void *) compare->mask;
1129 sdp_data = (void *) compare->data;
1130 sdp_mask = (void *) compare->mask;
1132 switch (addr->sa_family) {
1134 ip4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1135 if (ps == RDMA_PS_SDP) {
1136 sdp_set_ip_ver(sdp_data, 4);
1137 sdp_set_ip_ver(sdp_mask, 0xF);
1138 sdp_data->dst_addr.ip4.addr = ip4_addr;
1139 sdp_mask->dst_addr.ip4.addr = ~0;
1141 cma_set_ip_ver(cma_data, 4);
1142 cma_set_ip_ver(cma_mask, 0xF);
1143 cma_data->dst_addr.ip4.addr = ip4_addr;
1144 cma_mask->dst_addr.ip4.addr = ~0;
1148 ip6_addr = ((struct sockaddr_in6 *) addr)->sin6_addr;
1149 if (ps == RDMA_PS_SDP) {
1150 sdp_set_ip_ver(sdp_data, 6);
1151 sdp_set_ip_ver(sdp_mask, 0xF);
1152 sdp_data->dst_addr.ip6 = ip6_addr;
1153 memset(&sdp_mask->dst_addr.ip6, 0xFF,
1154 sizeof sdp_mask->dst_addr.ip6);
1156 cma_set_ip_ver(cma_data, 6);
1157 cma_set_ip_ver(cma_mask, 0xF);
1158 cma_data->dst_addr.ip6 = ip6_addr;
1159 memset(&cma_mask->dst_addr.ip6, 0xFF,
1160 sizeof cma_mask->dst_addr.ip6);
1168 static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
1170 struct rdma_id_private *id_priv = iw_id->context;
1171 struct rdma_cm_event event;
1172 struct sockaddr_in *sin;
1175 memset(&event, 0, sizeof event);
1176 atomic_inc(&id_priv->dev_remove);
1178 switch (iw_event->event) {
1179 case IW_CM_EVENT_CLOSE:
1180 event.event = RDMA_CM_EVENT_DISCONNECTED;
1182 case IW_CM_EVENT_CONNECT_REPLY:
1183 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1184 *sin = iw_event->local_addr;
1185 sin = (struct sockaddr_in *) &id_priv->id.route.addr.dst_addr;
1186 *sin = iw_event->remote_addr;
1187 switch (iw_event->status) {
1189 event.event = RDMA_CM_EVENT_ESTABLISHED;
1193 event.event = RDMA_CM_EVENT_REJECTED;
1196 event.event = RDMA_CM_EVENT_UNREACHABLE;
1199 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
1203 case IW_CM_EVENT_ESTABLISHED:
1204 event.event = RDMA_CM_EVENT_ESTABLISHED;
1210 event.status = iw_event->status;
1211 event.param.conn.private_data = iw_event->private_data;
1212 event.param.conn.private_data_len = iw_event->private_data_len;
1213 ret = id_priv->id.event_handler(&id_priv->id, &event);
1215 /* Destroy the CM ID by returning a non-zero value. */
1216 id_priv->cm_id.iw = NULL;
1217 cma_exch(id_priv, CMA_DESTROYING);
1218 cma_release_remove(id_priv);
1219 rdma_destroy_id(&id_priv->id);
1223 cma_release_remove(id_priv);
1227 static int iw_conn_req_handler(struct iw_cm_id *cm_id,
1228 struct iw_cm_event *iw_event)
1230 struct rdma_cm_id *new_cm_id;
1231 struct rdma_id_private *listen_id, *conn_id;
1232 struct sockaddr_in *sin;
1233 struct net_device *dev = NULL;
1234 struct rdma_cm_event event;
1237 listen_id = cm_id->context;
1238 atomic_inc(&listen_id->dev_remove);
1239 if (!cma_comp(listen_id, CMA_LISTEN)) {
1240 ret = -ECONNABORTED;
1244 /* Create a new RDMA id for the new IW CM ID */
1245 new_cm_id = rdma_create_id(listen_id->id.event_handler,
1246 listen_id->id.context,
1252 conn_id = container_of(new_cm_id, struct rdma_id_private, id);
1253 atomic_inc(&conn_id->dev_remove);
1254 conn_id->state = CMA_CONNECT;
1256 dev = ip_dev_find(iw_event->local_addr.sin_addr.s_addr);
1258 ret = -EADDRNOTAVAIL;
1259 cma_release_remove(conn_id);
1260 rdma_destroy_id(new_cm_id);
1263 ret = rdma_copy_addr(&conn_id->id.route.addr.dev_addr, dev, NULL);
1265 cma_release_remove(conn_id);
1266 rdma_destroy_id(new_cm_id);
1271 ret = cma_acquire_dev(conn_id);
1272 mutex_unlock(&lock);
1274 cma_release_remove(conn_id);
1275 rdma_destroy_id(new_cm_id);
1279 conn_id->cm_id.iw = cm_id;
1280 cm_id->context = conn_id;
1281 cm_id->cm_handler = cma_iw_handler;
1283 sin = (struct sockaddr_in *) &new_cm_id->route.addr.src_addr;
1284 *sin = iw_event->local_addr;
1285 sin = (struct sockaddr_in *) &new_cm_id->route.addr.dst_addr;
1286 *sin = iw_event->remote_addr;
1288 memset(&event, 0, sizeof event);
1289 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1290 event.param.conn.private_data = iw_event->private_data;
1291 event.param.conn.private_data_len = iw_event->private_data_len;
1292 ret = conn_id->id.event_handler(&conn_id->id, &event);
1294 /* User wants to destroy the CM ID */
1295 conn_id->cm_id.iw = NULL;
1296 cma_exch(conn_id, CMA_DESTROYING);
1297 cma_release_remove(conn_id);
1298 rdma_destroy_id(&conn_id->id);
1304 cma_release_remove(listen_id);
1308 static int cma_ib_listen(struct rdma_id_private *id_priv)
1310 struct ib_cm_compare_data compare_data;
1311 struct sockaddr *addr;
1315 id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_req_handler,
1317 if (IS_ERR(id_priv->cm_id.ib))
1318 return PTR_ERR(id_priv->cm_id.ib);
1320 addr = &id_priv->id.route.addr.src_addr;
1321 svc_id = cma_get_service_id(id_priv->id.ps, addr);
1322 if (cma_any_addr(addr))
1323 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, NULL);
1325 cma_set_compare_data(id_priv->id.ps, addr, &compare_data);
1326 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, &compare_data);
1330 ib_destroy_cm_id(id_priv->cm_id.ib);
1331 id_priv->cm_id.ib = NULL;
1337 static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
1340 struct sockaddr_in *sin;
1342 id_priv->cm_id.iw = iw_create_cm_id(id_priv->id.device,
1343 iw_conn_req_handler,
1345 if (IS_ERR(id_priv->cm_id.iw))
1346 return PTR_ERR(id_priv->cm_id.iw);
1348 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1349 id_priv->cm_id.iw->local_addr = *sin;
1351 ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
1354 iw_destroy_cm_id(id_priv->cm_id.iw);
1355 id_priv->cm_id.iw = NULL;
1361 static int cma_listen_handler(struct rdma_cm_id *id,
1362 struct rdma_cm_event *event)
1364 struct rdma_id_private *id_priv = id->context;
1366 id->context = id_priv->id.context;
1367 id->event_handler = id_priv->id.event_handler;
1368 return id_priv->id.event_handler(id, event);
1371 static void cma_listen_on_dev(struct rdma_id_private *id_priv,
1372 struct cma_device *cma_dev)
1374 struct rdma_id_private *dev_id_priv;
1375 struct rdma_cm_id *id;
1378 id = rdma_create_id(cma_listen_handler, id_priv, id_priv->id.ps);
1382 dev_id_priv = container_of(id, struct rdma_id_private, id);
1384 dev_id_priv->state = CMA_ADDR_BOUND;
1385 memcpy(&id->route.addr.src_addr, &id_priv->id.route.addr.src_addr,
1386 ip_addr_size(&id_priv->id.route.addr.src_addr));
1388 cma_attach_to_dev(dev_id_priv, cma_dev);
1389 list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
1391 ret = rdma_listen(id, id_priv->backlog);
1397 cma_destroy_listen(dev_id_priv);
1400 static void cma_listen_on_all(struct rdma_id_private *id_priv)
1402 struct cma_device *cma_dev;
1405 list_add_tail(&id_priv->list, &listen_any_list);
1406 list_for_each_entry(cma_dev, &dev_list, list)
1407 cma_listen_on_dev(id_priv, cma_dev);
1408 mutex_unlock(&lock);
1411 static int cma_bind_any(struct rdma_cm_id *id, sa_family_t af)
1413 struct sockaddr_in addr_in;
1415 memset(&addr_in, 0, sizeof addr_in);
1416 addr_in.sin_family = af;
1417 return rdma_bind_addr(id, (struct sockaddr *) &addr_in);
1420 int rdma_listen(struct rdma_cm_id *id, int backlog)
1422 struct rdma_id_private *id_priv;
1425 id_priv = container_of(id, struct rdma_id_private, id);
1426 if (id_priv->state == CMA_IDLE) {
1427 ret = cma_bind_any(id, AF_INET);
1432 if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_LISTEN))
1435 id_priv->backlog = backlog;
1437 switch (rdma_node_get_transport(id->device->node_type)) {
1438 case RDMA_TRANSPORT_IB:
1439 ret = cma_ib_listen(id_priv);
1443 case RDMA_TRANSPORT_IWARP:
1444 ret = cma_iw_listen(id_priv, backlog);
1453 cma_listen_on_all(id_priv);
1457 id_priv->backlog = 0;
1458 cma_comp_exch(id_priv, CMA_LISTEN, CMA_ADDR_BOUND);
1461 EXPORT_SYMBOL(rdma_listen);
1463 static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
1466 struct cma_work *work = context;
1467 struct rdma_route *route;
1469 route = &work->id->id.route;
1472 route->num_paths = 1;
1473 *route->path_rec = *path_rec;
1475 work->old_state = CMA_ROUTE_QUERY;
1476 work->new_state = CMA_ADDR_RESOLVED;
1477 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
1478 work->event.status = status;
1481 queue_work(cma_wq, &work->work);
1484 static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
1485 struct cma_work *work)
1487 struct rdma_dev_addr *addr = &id_priv->id.route.addr.dev_addr;
1488 struct ib_sa_path_rec path_rec;
1490 memset(&path_rec, 0, sizeof path_rec);
1491 ib_addr_get_sgid(addr, &path_rec.sgid);
1492 ib_addr_get_dgid(addr, &path_rec.dgid);
1493 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(addr));
1494 path_rec.numb_path = 1;
1496 id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
1497 id_priv->id.port_num, &path_rec,
1498 IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
1499 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH,
1500 timeout_ms, GFP_KERNEL,
1501 cma_query_handler, work, &id_priv->query);
1503 return (id_priv->query_id < 0) ? id_priv->query_id : 0;
1506 static void cma_work_handler(struct work_struct *_work)
1508 struct cma_work *work = container_of(_work, struct cma_work, work);
1509 struct rdma_id_private *id_priv = work->id;
1512 atomic_inc(&id_priv->dev_remove);
1513 if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
1516 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
1517 cma_exch(id_priv, CMA_DESTROYING);
1521 cma_release_remove(id_priv);
1522 cma_deref_id(id_priv);
1524 rdma_destroy_id(&id_priv->id);
1528 static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
1530 struct rdma_route *route = &id_priv->id.route;
1531 struct cma_work *work;
1534 work = kzalloc(sizeof *work, GFP_KERNEL);
1539 INIT_WORK(&work->work, cma_work_handler);
1540 work->old_state = CMA_ROUTE_QUERY;
1541 work->new_state = CMA_ROUTE_RESOLVED;
1542 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1544 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
1545 if (!route->path_rec) {
1550 ret = cma_query_ib_route(id_priv, timeout_ms, work);
1556 kfree(route->path_rec);
1557 route->path_rec = NULL;
1563 int rdma_set_ib_paths(struct rdma_cm_id *id,
1564 struct ib_sa_path_rec *path_rec, int num_paths)
1566 struct rdma_id_private *id_priv;
1569 id_priv = container_of(id, struct rdma_id_private, id);
1570 if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ROUTE_RESOLVED))
1573 id->route.path_rec = kmalloc(sizeof *path_rec * num_paths, GFP_KERNEL);
1574 if (!id->route.path_rec) {
1579 memcpy(id->route.path_rec, path_rec, sizeof *path_rec * num_paths);
1582 cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_ADDR_RESOLVED);
1585 EXPORT_SYMBOL(rdma_set_ib_paths);
1587 static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
1589 struct cma_work *work;
1591 work = kzalloc(sizeof *work, GFP_KERNEL);
1596 INIT_WORK(&work->work, cma_work_handler);
1597 work->old_state = CMA_ROUTE_QUERY;
1598 work->new_state = CMA_ROUTE_RESOLVED;
1599 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1600 queue_work(cma_wq, &work->work);
1604 int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
1606 struct rdma_id_private *id_priv;
1609 id_priv = container_of(id, struct rdma_id_private, id);
1610 if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ROUTE_QUERY))
1613 atomic_inc(&id_priv->refcount);
1614 switch (rdma_node_get_transport(id->device->node_type)) {
1615 case RDMA_TRANSPORT_IB:
1616 ret = cma_resolve_ib_route(id_priv, timeout_ms);
1618 case RDMA_TRANSPORT_IWARP:
1619 ret = cma_resolve_iw_route(id_priv, timeout_ms);
1630 cma_comp_exch(id_priv, CMA_ROUTE_QUERY, CMA_ADDR_RESOLVED);
1631 cma_deref_id(id_priv);
1634 EXPORT_SYMBOL(rdma_resolve_route);
1636 static int cma_bind_loopback(struct rdma_id_private *id_priv)
1638 struct cma_device *cma_dev;
1639 struct ib_port_attr port_attr;
1646 if (list_empty(&dev_list)) {
1650 list_for_each_entry(cma_dev, &dev_list, list)
1651 for (p = 1; p <= cma_dev->device->phys_port_cnt; ++p)
1652 if (!ib_query_port(cma_dev->device, p, &port_attr) &&
1653 port_attr.state == IB_PORT_ACTIVE)
1657 cma_dev = list_entry(dev_list.next, struct cma_device, list);
1660 ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid);
1664 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
1668 ib_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
1669 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
1670 id_priv->id.port_num = p;
1671 cma_attach_to_dev(id_priv, cma_dev);
1673 mutex_unlock(&lock);
1677 static void addr_handler(int status, struct sockaddr *src_addr,
1678 struct rdma_dev_addr *dev_addr, void *context)
1680 struct rdma_id_private *id_priv = context;
1681 struct rdma_cm_event event;
1683 memset(&event, 0, sizeof event);
1684 atomic_inc(&id_priv->dev_remove);
1687 * Grab mutex to block rdma_destroy_id() from removing the device while
1688 * we're trying to acquire it.
1691 if (!cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_RESOLVED)) {
1692 mutex_unlock(&lock);
1696 if (!status && !id_priv->cma_dev)
1697 status = cma_acquire_dev(id_priv);
1698 mutex_unlock(&lock);
1701 if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ADDR_BOUND))
1703 event.event = RDMA_CM_EVENT_ADDR_ERROR;
1704 event.status = status;
1706 memcpy(&id_priv->id.route.addr.src_addr, src_addr,
1707 ip_addr_size(src_addr));
1708 event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
1711 if (id_priv->id.event_handler(&id_priv->id, &event)) {
1712 cma_exch(id_priv, CMA_DESTROYING);
1713 cma_release_remove(id_priv);
1714 cma_deref_id(id_priv);
1715 rdma_destroy_id(&id_priv->id);
1719 cma_release_remove(id_priv);
1720 cma_deref_id(id_priv);
1723 static int cma_resolve_loopback(struct rdma_id_private *id_priv)
1725 struct cma_work *work;
1726 struct sockaddr_in *src_in, *dst_in;
1730 work = kzalloc(sizeof *work, GFP_KERNEL);
1734 if (!id_priv->cma_dev) {
1735 ret = cma_bind_loopback(id_priv);
1740 ib_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
1741 ib_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
1743 if (cma_zero_addr(&id_priv->id.route.addr.src_addr)) {
1744 src_in = (struct sockaddr_in *)&id_priv->id.route.addr.src_addr;
1745 dst_in = (struct sockaddr_in *)&id_priv->id.route.addr.dst_addr;
1746 src_in->sin_family = dst_in->sin_family;
1747 src_in->sin_addr.s_addr = dst_in->sin_addr.s_addr;
1751 INIT_WORK(&work->work, cma_work_handler);
1752 work->old_state = CMA_ADDR_QUERY;
1753 work->new_state = CMA_ADDR_RESOLVED;
1754 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
1755 queue_work(cma_wq, &work->work);
1762 static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
1763 struct sockaddr *dst_addr)
1765 if (src_addr && src_addr->sa_family)
1766 return rdma_bind_addr(id, src_addr);
1768 return cma_bind_any(id, dst_addr->sa_family);
1771 int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
1772 struct sockaddr *dst_addr, int timeout_ms)
1774 struct rdma_id_private *id_priv;
1777 id_priv = container_of(id, struct rdma_id_private, id);
1778 if (id_priv->state == CMA_IDLE) {
1779 ret = cma_bind_addr(id, src_addr, dst_addr);
1784 if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_ADDR_QUERY))
1787 atomic_inc(&id_priv->refcount);
1788 memcpy(&id->route.addr.dst_addr, dst_addr, ip_addr_size(dst_addr));
1789 if (cma_any_addr(dst_addr))
1790 ret = cma_resolve_loopback(id_priv);
1792 ret = rdma_resolve_ip(&addr_client, &id->route.addr.src_addr,
1793 dst_addr, &id->route.addr.dev_addr,
1794 timeout_ms, addr_handler, id_priv);
1800 cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_BOUND);
1801 cma_deref_id(id_priv);
1804 EXPORT_SYMBOL(rdma_resolve_addr);
1806 static void cma_bind_port(struct rdma_bind_list *bind_list,
1807 struct rdma_id_private *id_priv)
1809 struct sockaddr_in *sin;
1811 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1812 sin->sin_port = htons(bind_list->port);
1813 id_priv->bind_list = bind_list;
1814 hlist_add_head(&id_priv->node, &bind_list->owners);
1817 static int cma_alloc_port(struct idr *ps, struct rdma_id_private *id_priv,
1818 unsigned short snum)
1820 struct rdma_bind_list *bind_list;
1823 bind_list = kmalloc(sizeof *bind_list, GFP_KERNEL);
1828 ret = idr_get_new_above(ps, bind_list, snum, &port);
1829 } while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL));
1835 ret = -EADDRNOTAVAIL;
1840 bind_list->port = (unsigned short) port;
1841 cma_bind_port(bind_list, id_priv);
1844 idr_remove(ps, port);
1850 static int cma_alloc_any_port(struct idr *ps, struct rdma_id_private *id_priv)
1852 struct rdma_bind_list *bind_list;
1855 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
1861 ret = idr_get_new_above(ps, bind_list, next_port, &port);
1862 } while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL));
1867 if (port > sysctl_local_port_range[1]) {
1868 if (next_port != sysctl_local_port_range[0]) {
1869 idr_remove(ps, port);
1870 next_port = sysctl_local_port_range[0];
1873 ret = -EADDRNOTAVAIL;
1877 if (port == sysctl_local_port_range[1])
1878 next_port = sysctl_local_port_range[0];
1880 next_port = port + 1;
1883 bind_list->port = (unsigned short) port;
1884 cma_bind_port(bind_list, id_priv);
1887 idr_remove(ps, port);
1893 static int cma_use_port(struct idr *ps, struct rdma_id_private *id_priv)
1895 struct rdma_id_private *cur_id;
1896 struct sockaddr_in *sin, *cur_sin;
1897 struct rdma_bind_list *bind_list;
1898 struct hlist_node *node;
1899 unsigned short snum;
1901 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1902 snum = ntohs(sin->sin_port);
1903 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
1906 bind_list = idr_find(ps, snum);
1908 return cma_alloc_port(ps, id_priv, snum);
1911 * We don't support binding to any address if anyone is bound to
1912 * a specific address on the same port.
1914 if (cma_any_addr(&id_priv->id.route.addr.src_addr))
1915 return -EADDRNOTAVAIL;
1917 hlist_for_each_entry(cur_id, node, &bind_list->owners, node) {
1918 if (cma_any_addr(&cur_id->id.route.addr.src_addr))
1919 return -EADDRNOTAVAIL;
1921 cur_sin = (struct sockaddr_in *) &cur_id->id.route.addr.src_addr;
1922 if (sin->sin_addr.s_addr == cur_sin->sin_addr.s_addr)
1926 cma_bind_port(bind_list, id_priv);
1930 static int cma_get_port(struct rdma_id_private *id_priv)
1935 switch (id_priv->id.ps) {
1949 return -EPROTONOSUPPORT;
1953 if (cma_any_port(&id_priv->id.route.addr.src_addr))
1954 ret = cma_alloc_any_port(ps, id_priv);
1956 ret = cma_use_port(ps, id_priv);
1957 mutex_unlock(&lock);
1962 int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
1964 struct rdma_id_private *id_priv;
1967 if (addr->sa_family != AF_INET)
1968 return -EAFNOSUPPORT;
1970 id_priv = container_of(id, struct rdma_id_private, id);
1971 if (!cma_comp_exch(id_priv, CMA_IDLE, CMA_ADDR_BOUND))
1974 if (!cma_any_addr(addr)) {
1975 ret = rdma_translate_ip(addr, &id->route.addr.dev_addr);
1980 ret = cma_acquire_dev(id_priv);
1981 mutex_unlock(&lock);
1986 memcpy(&id->route.addr.src_addr, addr, ip_addr_size(addr));
1987 ret = cma_get_port(id_priv);
1993 if (!cma_any_addr(addr)) {
1995 cma_detach_from_dev(id_priv);
1996 mutex_unlock(&lock);
1999 cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_IDLE);
2002 EXPORT_SYMBOL(rdma_bind_addr);
2004 static int cma_format_hdr(void *hdr, enum rdma_port_space ps,
2005 struct rdma_route *route)
2007 struct sockaddr_in *src4, *dst4;
2008 struct cma_hdr *cma_hdr;
2009 struct sdp_hh *sdp_hdr;
2011 src4 = (struct sockaddr_in *) &route->addr.src_addr;
2012 dst4 = (struct sockaddr_in *) &route->addr.dst_addr;
2017 if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION)
2019 sdp_set_ip_ver(sdp_hdr, 4);
2020 sdp_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
2021 sdp_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
2022 sdp_hdr->port = src4->sin_port;
2026 cma_hdr->cma_version = CMA_VERSION;
2027 cma_set_ip_ver(cma_hdr, 4);
2028 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
2029 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
2030 cma_hdr->port = src4->sin_port;
2036 static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
2037 struct ib_cm_event *ib_event)
2039 struct rdma_id_private *id_priv = cm_id->context;
2040 struct rdma_cm_event event;
2041 struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
2044 memset(&event, 0, sizeof event);
2045 atomic_inc(&id_priv->dev_remove);
2046 if (!cma_comp(id_priv, CMA_CONNECT))
2049 switch (ib_event->event) {
2050 case IB_CM_SIDR_REQ_ERROR:
2051 event.event = RDMA_CM_EVENT_UNREACHABLE;
2052 event.status = -ETIMEDOUT;
2054 case IB_CM_SIDR_REP_RECEIVED:
2055 event.param.ud.private_data = ib_event->private_data;
2056 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
2057 if (rep->status != IB_SIDR_SUCCESS) {
2058 event.event = RDMA_CM_EVENT_UNREACHABLE;
2059 event.status = ib_event->param.sidr_rep_rcvd.status;
2062 if (id_priv->qkey != rep->qkey) {
2063 event.event = RDMA_CM_EVENT_UNREACHABLE;
2064 event.status = -EINVAL;
2067 ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
2068 id_priv->id.route.path_rec,
2069 &event.param.ud.ah_attr);
2070 event.param.ud.qp_num = rep->qpn;
2071 event.param.ud.qkey = rep->qkey;
2072 event.event = RDMA_CM_EVENT_ESTABLISHED;
2076 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d",
2081 ret = id_priv->id.event_handler(&id_priv->id, &event);
2083 /* Destroy the CM ID by returning a non-zero value. */
2084 id_priv->cm_id.ib = NULL;
2085 cma_exch(id_priv, CMA_DESTROYING);
2086 cma_release_remove(id_priv);
2087 rdma_destroy_id(&id_priv->id);
2091 cma_release_remove(id_priv);
2095 static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
2096 struct rdma_conn_param *conn_param)
2098 struct ib_cm_sidr_req_param req;
2099 struct rdma_route *route;
2102 req.private_data_len = sizeof(struct cma_hdr) +
2103 conn_param->private_data_len;
2104 req.private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2105 if (!req.private_data)
2108 if (conn_param->private_data && conn_param->private_data_len)
2109 memcpy((void *) req.private_data + sizeof(struct cma_hdr),
2110 conn_param->private_data, conn_param->private_data_len);
2112 route = &id_priv->id.route;
2113 ret = cma_format_hdr((void *) req.private_data, id_priv->id.ps, route);
2117 id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device,
2118 cma_sidr_rep_handler, id_priv);
2119 if (IS_ERR(id_priv->cm_id.ib)) {
2120 ret = PTR_ERR(id_priv->cm_id.ib);
2124 req.path = route->path_rec;
2125 req.service_id = cma_get_service_id(id_priv->id.ps,
2126 &route->addr.dst_addr);
2127 req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
2128 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2130 ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
2132 ib_destroy_cm_id(id_priv->cm_id.ib);
2133 id_priv->cm_id.ib = NULL;
2136 kfree(req.private_data);
2140 static int cma_connect_ib(struct rdma_id_private *id_priv,
2141 struct rdma_conn_param *conn_param)
2143 struct ib_cm_req_param req;
2144 struct rdma_route *route;
2148 memset(&req, 0, sizeof req);
2149 offset = cma_user_data_offset(id_priv->id.ps);
2150 req.private_data_len = offset + conn_param->private_data_len;
2151 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2155 if (conn_param->private_data && conn_param->private_data_len)
2156 memcpy(private_data + offset, conn_param->private_data,
2157 conn_param->private_data_len);
2159 id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_ib_handler,
2161 if (IS_ERR(id_priv->cm_id.ib)) {
2162 ret = PTR_ERR(id_priv->cm_id.ib);
2166 route = &id_priv->id.route;
2167 ret = cma_format_hdr(private_data, id_priv->id.ps, route);
2170 req.private_data = private_data;
2172 req.primary_path = &route->path_rec[0];
2173 if (route->num_paths == 2)
2174 req.alternate_path = &route->path_rec[1];
2176 req.service_id = cma_get_service_id(id_priv->id.ps,
2177 &route->addr.dst_addr);
2178 req.qp_num = id_priv->qp_num;
2179 req.qp_type = IB_QPT_RC;
2180 req.starting_psn = id_priv->seq_num;
2181 req.responder_resources = conn_param->responder_resources;
2182 req.initiator_depth = conn_param->initiator_depth;
2183 req.flow_control = conn_param->flow_control;
2184 req.retry_count = conn_param->retry_count;
2185 req.rnr_retry_count = conn_param->rnr_retry_count;
2186 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2187 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2188 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2189 req.srq = id_priv->srq ? 1 : 0;
2191 ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
2193 if (ret && !IS_ERR(id_priv->cm_id.ib)) {
2194 ib_destroy_cm_id(id_priv->cm_id.ib);
2195 id_priv->cm_id.ib = NULL;
2198 kfree(private_data);
2202 static int cma_connect_iw(struct rdma_id_private *id_priv,
2203 struct rdma_conn_param *conn_param)
2205 struct iw_cm_id *cm_id;
2206 struct sockaddr_in* sin;
2208 struct iw_cm_conn_param iw_param;
2210 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
2211 if (IS_ERR(cm_id)) {
2212 ret = PTR_ERR(cm_id);
2216 id_priv->cm_id.iw = cm_id;
2218 sin = (struct sockaddr_in*) &id_priv->id.route.addr.src_addr;
2219 cm_id->local_addr = *sin;
2221 sin = (struct sockaddr_in*) &id_priv->id.route.addr.dst_addr;
2222 cm_id->remote_addr = *sin;
2224 ret = cma_modify_qp_rtr(&id_priv->id);
2228 iw_param.ord = conn_param->initiator_depth;
2229 iw_param.ird = conn_param->responder_resources;
2230 iw_param.private_data = conn_param->private_data;
2231 iw_param.private_data_len = conn_param->private_data_len;
2233 iw_param.qpn = id_priv->qp_num;
2235 iw_param.qpn = conn_param->qp_num;
2236 ret = iw_cm_connect(cm_id, &iw_param);
2238 if (ret && !IS_ERR(cm_id)) {
2239 iw_destroy_cm_id(cm_id);
2240 id_priv->cm_id.iw = NULL;
2245 int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2247 struct rdma_id_private *id_priv;
2250 id_priv = container_of(id, struct rdma_id_private, id);
2251 if (!cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_CONNECT))
2255 id_priv->qp_num = conn_param->qp_num;
2256 id_priv->srq = conn_param->srq;
2259 switch (rdma_node_get_transport(id->device->node_type)) {
2260 case RDMA_TRANSPORT_IB:
2261 if (cma_is_ud_ps(id->ps))
2262 ret = cma_resolve_ib_udp(id_priv, conn_param);
2264 ret = cma_connect_ib(id_priv, conn_param);
2266 case RDMA_TRANSPORT_IWARP:
2267 ret = cma_connect_iw(id_priv, conn_param);
2278 cma_comp_exch(id_priv, CMA_CONNECT, CMA_ROUTE_RESOLVED);
2281 EXPORT_SYMBOL(rdma_connect);
2283 static int cma_accept_ib(struct rdma_id_private *id_priv,
2284 struct rdma_conn_param *conn_param)
2286 struct ib_cm_rep_param rep;
2287 struct ib_qp_attr qp_attr;
2288 int qp_attr_mask, ret;
2290 if (id_priv->id.qp) {
2291 ret = cma_modify_qp_rtr(&id_priv->id);
2295 qp_attr.qp_state = IB_QPS_RTS;
2296 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, &qp_attr,
2301 qp_attr.max_rd_atomic = conn_param->initiator_depth;
2302 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
2307 memset(&rep, 0, sizeof rep);
2308 rep.qp_num = id_priv->qp_num;
2309 rep.starting_psn = id_priv->seq_num;
2310 rep.private_data = conn_param->private_data;
2311 rep.private_data_len = conn_param->private_data_len;
2312 rep.responder_resources = conn_param->responder_resources;
2313 rep.initiator_depth = conn_param->initiator_depth;
2314 rep.target_ack_delay = CMA_CM_RESPONSE_TIMEOUT;
2315 rep.failover_accepted = 0;
2316 rep.flow_control = conn_param->flow_control;
2317 rep.rnr_retry_count = conn_param->rnr_retry_count;
2318 rep.srq = id_priv->srq ? 1 : 0;
2320 ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
2325 static int cma_accept_iw(struct rdma_id_private *id_priv,
2326 struct rdma_conn_param *conn_param)
2328 struct iw_cm_conn_param iw_param;
2331 ret = cma_modify_qp_rtr(&id_priv->id);
2335 iw_param.ord = conn_param->initiator_depth;
2336 iw_param.ird = conn_param->responder_resources;
2337 iw_param.private_data = conn_param->private_data;
2338 iw_param.private_data_len = conn_param->private_data_len;
2339 if (id_priv->id.qp) {
2340 iw_param.qpn = id_priv->qp_num;
2342 iw_param.qpn = conn_param->qp_num;
2344 return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
2347 static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
2348 enum ib_cm_sidr_status status,
2349 const void *private_data, int private_data_len)
2351 struct ib_cm_sidr_rep_param rep;
2353 memset(&rep, 0, sizeof rep);
2354 rep.status = status;
2355 if (status == IB_SIDR_SUCCESS) {
2356 rep.qp_num = id_priv->qp_num;
2357 rep.qkey = id_priv->qkey;
2359 rep.private_data = private_data;
2360 rep.private_data_len = private_data_len;
2362 return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
2365 int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2367 struct rdma_id_private *id_priv;
2370 id_priv = container_of(id, struct rdma_id_private, id);
2371 if (!cma_comp(id_priv, CMA_CONNECT))
2374 if (!id->qp && conn_param) {
2375 id_priv->qp_num = conn_param->qp_num;
2376 id_priv->srq = conn_param->srq;
2379 switch (rdma_node_get_transport(id->device->node_type)) {
2380 case RDMA_TRANSPORT_IB:
2381 if (cma_is_ud_ps(id->ps))
2382 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
2383 conn_param->private_data,
2384 conn_param->private_data_len);
2385 else if (conn_param)
2386 ret = cma_accept_ib(id_priv, conn_param);
2388 ret = cma_rep_recv(id_priv);
2390 case RDMA_TRANSPORT_IWARP:
2391 ret = cma_accept_iw(id_priv, conn_param);
2403 cma_modify_qp_err(id);
2404 rdma_reject(id, NULL, 0);
2407 EXPORT_SYMBOL(rdma_accept);
2409 int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
2411 struct rdma_id_private *id_priv;
2414 id_priv = container_of(id, struct rdma_id_private, id);
2415 if (!cma_comp(id_priv, CMA_CONNECT))
2418 switch (id->device->node_type) {
2419 case RDMA_NODE_IB_CA:
2420 ret = ib_cm_notify(id_priv->cm_id.ib, event);
2428 EXPORT_SYMBOL(rdma_notify);
2430 int rdma_reject(struct rdma_cm_id *id, const void *private_data,
2431 u8 private_data_len)
2433 struct rdma_id_private *id_priv;
2436 id_priv = container_of(id, struct rdma_id_private, id);
2437 if (!cma_comp(id_priv, CMA_CONNECT))
2440 switch (rdma_node_get_transport(id->device->node_type)) {
2441 case RDMA_TRANSPORT_IB:
2442 if (cma_is_ud_ps(id->ps))
2443 ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT,
2444 private_data, private_data_len);
2446 ret = ib_send_cm_rej(id_priv->cm_id.ib,
2447 IB_CM_REJ_CONSUMER_DEFINED, NULL,
2448 0, private_data, private_data_len);
2450 case RDMA_TRANSPORT_IWARP:
2451 ret = iw_cm_reject(id_priv->cm_id.iw,
2452 private_data, private_data_len);
2460 EXPORT_SYMBOL(rdma_reject);
2462 int rdma_disconnect(struct rdma_cm_id *id)
2464 struct rdma_id_private *id_priv;
2467 id_priv = container_of(id, struct rdma_id_private, id);
2468 if (!cma_comp(id_priv, CMA_CONNECT) &&
2469 !cma_comp(id_priv, CMA_DISCONNECT))
2472 switch (rdma_node_get_transport(id->device->node_type)) {
2473 case RDMA_TRANSPORT_IB:
2474 ret = cma_modify_qp_err(id);
2477 /* Initiate or respond to a disconnect. */
2478 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
2479 ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
2481 case RDMA_TRANSPORT_IWARP:
2482 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
2491 EXPORT_SYMBOL(rdma_disconnect);
2493 static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
2495 struct rdma_id_private *id_priv;
2496 struct cma_multicast *mc = multicast->context;
2497 struct rdma_cm_event event;
2500 id_priv = mc->id_priv;
2501 atomic_inc(&id_priv->dev_remove);
2502 if (!cma_comp(id_priv, CMA_ADDR_BOUND) &&
2503 !cma_comp(id_priv, CMA_ADDR_RESOLVED))
2506 if (!status && id_priv->id.qp)
2507 status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
2508 multicast->rec.mlid);
2510 memset(&event, 0, sizeof event);
2511 event.status = status;
2512 event.param.ud.private_data = mc->context;
2514 event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
2515 ib_init_ah_from_mcmember(id_priv->id.device,
2516 id_priv->id.port_num, &multicast->rec,
2517 &event.param.ud.ah_attr);
2518 event.param.ud.qp_num = 0xFFFFFF;
2519 event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
2521 event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
2523 ret = id_priv->id.event_handler(&id_priv->id, &event);
2525 cma_exch(id_priv, CMA_DESTROYING);
2526 cma_release_remove(id_priv);
2527 rdma_destroy_id(&id_priv->id);
2531 cma_release_remove(id_priv);
2535 static void cma_set_mgid(struct rdma_id_private *id_priv,
2536 struct sockaddr *addr, union ib_gid *mgid)
2538 unsigned char mc_map[MAX_ADDR_LEN];
2539 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2540 struct sockaddr_in *sin = (struct sockaddr_in *) addr;
2541 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
2543 if (cma_any_addr(addr)) {
2544 memset(mgid, 0, sizeof *mgid);
2545 } else if ((addr->sa_family == AF_INET6) &&
2546 ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFF10A01B) ==
2548 /* IPv6 address is an SA assigned MGID. */
2549 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
2551 ip_ib_mc_map(sin->sin_addr.s_addr, mc_map);
2552 if (id_priv->id.ps == RDMA_PS_UDP)
2553 mc_map[7] = 0x01; /* Use RDMA CM signature */
2554 mc_map[8] = ib_addr_get_pkey(dev_addr) >> 8;
2555 mc_map[9] = (unsigned char) ib_addr_get_pkey(dev_addr);
2556 *mgid = *(union ib_gid *) (mc_map + 4);
2560 static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
2561 struct cma_multicast *mc)
2563 struct ib_sa_mcmember_rec rec;
2564 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2565 ib_sa_comp_mask comp_mask;
2568 ib_addr_get_mgid(dev_addr, &rec.mgid);
2569 ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
2574 cma_set_mgid(id_priv, &mc->addr, &rec.mgid);
2575 if (id_priv->id.ps == RDMA_PS_UDP)
2576 rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
2577 ib_addr_get_sgid(dev_addr, &rec.port_gid);
2578 rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
2581 comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
2582 IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
2583 IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
2584 IB_SA_MCMEMBER_REC_FLOW_LABEL |
2585 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
2587 mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
2588 id_priv->id.port_num, &rec,
2589 comp_mask, GFP_KERNEL,
2590 cma_ib_mc_handler, mc);
2591 if (IS_ERR(mc->multicast.ib))
2592 return PTR_ERR(mc->multicast.ib);
2597 int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
2600 struct rdma_id_private *id_priv;
2601 struct cma_multicast *mc;
2604 id_priv = container_of(id, struct rdma_id_private, id);
2605 if (!cma_comp(id_priv, CMA_ADDR_BOUND) &&
2606 !cma_comp(id_priv, CMA_ADDR_RESOLVED))
2609 mc = kmalloc(sizeof *mc, GFP_KERNEL);
2613 memcpy(&mc->addr, addr, ip_addr_size(addr));
2614 mc->context = context;
2615 mc->id_priv = id_priv;
2617 spin_lock(&id_priv->lock);
2618 list_add(&mc->list, &id_priv->mc_list);
2619 spin_unlock(&id_priv->lock);
2621 switch (rdma_node_get_transport(id->device->node_type)) {
2622 case RDMA_TRANSPORT_IB:
2623 ret = cma_join_ib_multicast(id_priv, mc);
2631 spin_lock_irq(&id_priv->lock);
2632 list_del(&mc->list);
2633 spin_unlock_irq(&id_priv->lock);
2638 EXPORT_SYMBOL(rdma_join_multicast);
2640 void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
2642 struct rdma_id_private *id_priv;
2643 struct cma_multicast *mc;
2645 id_priv = container_of(id, struct rdma_id_private, id);
2646 spin_lock_irq(&id_priv->lock);
2647 list_for_each_entry(mc, &id_priv->mc_list, list) {
2648 if (!memcmp(&mc->addr, addr, ip_addr_size(addr))) {
2649 list_del(&mc->list);
2650 spin_unlock_irq(&id_priv->lock);
2653 ib_detach_mcast(id->qp,
2654 &mc->multicast.ib->rec.mgid,
2655 mc->multicast.ib->rec.mlid);
2656 ib_sa_free_multicast(mc->multicast.ib);
2661 spin_unlock_irq(&id_priv->lock);
2663 EXPORT_SYMBOL(rdma_leave_multicast);
2665 static void cma_add_one(struct ib_device *device)
2667 struct cma_device *cma_dev;
2668 struct rdma_id_private *id_priv;
2670 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
2674 cma_dev->device = device;
2675 cma_dev->node_guid = device->node_guid;
2677 init_completion(&cma_dev->comp);
2678 atomic_set(&cma_dev->refcount, 1);
2679 INIT_LIST_HEAD(&cma_dev->id_list);
2680 ib_set_client_data(device, &cma_client, cma_dev);
2683 list_add_tail(&cma_dev->list, &dev_list);
2684 list_for_each_entry(id_priv, &listen_any_list, list)
2685 cma_listen_on_dev(id_priv, cma_dev);
2686 mutex_unlock(&lock);
2689 static int cma_remove_id_dev(struct rdma_id_private *id_priv)
2691 struct rdma_cm_event event;
2692 enum cma_state state;
2694 /* Record that we want to remove the device */
2695 state = cma_exch(id_priv, CMA_DEVICE_REMOVAL);
2696 if (state == CMA_DESTROYING)
2699 cma_cancel_operation(id_priv, state);
2700 wait_event(id_priv->wait_remove, !atomic_read(&id_priv->dev_remove));
2702 /* Check for destruction from another callback. */
2703 if (!cma_comp(id_priv, CMA_DEVICE_REMOVAL))
2706 memset(&event, 0, sizeof event);
2707 event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
2708 return id_priv->id.event_handler(&id_priv->id, &event);
2711 static void cma_process_remove(struct cma_device *cma_dev)
2713 struct rdma_id_private *id_priv;
2717 while (!list_empty(&cma_dev->id_list)) {
2718 id_priv = list_entry(cma_dev->id_list.next,
2719 struct rdma_id_private, list);
2721 if (cma_internal_listen(id_priv)) {
2722 cma_destroy_listen(id_priv);
2726 list_del_init(&id_priv->list);
2727 atomic_inc(&id_priv->refcount);
2728 mutex_unlock(&lock);
2730 ret = cma_remove_id_dev(id_priv);
2731 cma_deref_id(id_priv);
2733 rdma_destroy_id(&id_priv->id);
2737 mutex_unlock(&lock);
2739 cma_deref_dev(cma_dev);
2740 wait_for_completion(&cma_dev->comp);
2743 static void cma_remove_one(struct ib_device *device)
2745 struct cma_device *cma_dev;
2747 cma_dev = ib_get_client_data(device, &cma_client);
2752 list_del(&cma_dev->list);
2753 mutex_unlock(&lock);
2755 cma_process_remove(cma_dev);
2759 static int cma_init(void)
2763 get_random_bytes(&next_port, sizeof next_port);
2764 next_port = (next_port % (sysctl_local_port_range[1] -
2765 sysctl_local_port_range[0])) +
2766 sysctl_local_port_range[0];
2767 cma_wq = create_singlethread_workqueue("rdma_cm");
2771 ib_sa_register_client(&sa_client);
2772 rdma_addr_register_client(&addr_client);
2774 ret = ib_register_client(&cma_client);
2780 rdma_addr_unregister_client(&addr_client);
2781 ib_sa_unregister_client(&sa_client);
2782 destroy_workqueue(cma_wq);
2786 static void cma_cleanup(void)
2788 ib_unregister_client(&cma_client);
2789 rdma_addr_unregister_client(&addr_client);
2790 ib_sa_unregister_client(&sa_client);
2791 destroy_workqueue(cma_wq);
2792 idr_destroy(&sdp_ps);
2793 idr_destroy(&tcp_ps);
2794 idr_destroy(&udp_ps);
2795 idr_destroy(&ipoib_ps);
2798 module_init(cma_init);
2799 module_exit(cma_cleanup);