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 = 0;
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;
1869 int port, ret, low, high;
1871 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
1876 /* FIXME: add proper port randomization per like inet_csk_get_port */
1878 ret = idr_get_new_above(ps, bind_list, next_port, &port);
1879 } while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL));
1884 inet_get_local_port_range(&low, &high);
1886 if (next_port != low) {
1887 idr_remove(ps, port);
1891 ret = -EADDRNOTAVAIL;
1898 next_port = port + 1;
1901 bind_list->port = (unsigned short) port;
1902 cma_bind_port(bind_list, id_priv);
1905 idr_remove(ps, port);
1911 static int cma_use_port(struct idr *ps, struct rdma_id_private *id_priv)
1913 struct rdma_id_private *cur_id;
1914 struct sockaddr_in *sin, *cur_sin;
1915 struct rdma_bind_list *bind_list;
1916 struct hlist_node *node;
1917 unsigned short snum;
1919 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1920 snum = ntohs(sin->sin_port);
1921 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
1924 bind_list = idr_find(ps, snum);
1926 return cma_alloc_port(ps, id_priv, snum);
1929 * We don't support binding to any address if anyone is bound to
1930 * a specific address on the same port.
1932 if (cma_any_addr(&id_priv->id.route.addr.src_addr))
1933 return -EADDRNOTAVAIL;
1935 hlist_for_each_entry(cur_id, node, &bind_list->owners, node) {
1936 if (cma_any_addr(&cur_id->id.route.addr.src_addr))
1937 return -EADDRNOTAVAIL;
1939 cur_sin = (struct sockaddr_in *) &cur_id->id.route.addr.src_addr;
1940 if (sin->sin_addr.s_addr == cur_sin->sin_addr.s_addr)
1944 cma_bind_port(bind_list, id_priv);
1948 static int cma_get_port(struct rdma_id_private *id_priv)
1953 switch (id_priv->id.ps) {
1967 return -EPROTONOSUPPORT;
1971 if (cma_any_port(&id_priv->id.route.addr.src_addr))
1972 ret = cma_alloc_any_port(ps, id_priv);
1974 ret = cma_use_port(ps, id_priv);
1975 mutex_unlock(&lock);
1980 int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
1982 struct rdma_id_private *id_priv;
1985 if (addr->sa_family != AF_INET)
1986 return -EAFNOSUPPORT;
1988 id_priv = container_of(id, struct rdma_id_private, id);
1989 if (!cma_comp_exch(id_priv, CMA_IDLE, CMA_ADDR_BOUND))
1992 if (!cma_any_addr(addr)) {
1993 ret = rdma_translate_ip(addr, &id->route.addr.dev_addr);
1998 ret = cma_acquire_dev(id_priv);
1999 mutex_unlock(&lock);
2004 memcpy(&id->route.addr.src_addr, addr, ip_addr_size(addr));
2005 ret = cma_get_port(id_priv);
2011 if (!cma_any_addr(addr)) {
2013 cma_detach_from_dev(id_priv);
2014 mutex_unlock(&lock);
2017 cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_IDLE);
2020 EXPORT_SYMBOL(rdma_bind_addr);
2022 static int cma_format_hdr(void *hdr, enum rdma_port_space ps,
2023 struct rdma_route *route)
2025 struct sockaddr_in *src4, *dst4;
2026 struct cma_hdr *cma_hdr;
2027 struct sdp_hh *sdp_hdr;
2029 src4 = (struct sockaddr_in *) &route->addr.src_addr;
2030 dst4 = (struct sockaddr_in *) &route->addr.dst_addr;
2035 if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION)
2037 sdp_set_ip_ver(sdp_hdr, 4);
2038 sdp_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
2039 sdp_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
2040 sdp_hdr->port = src4->sin_port;
2044 cma_hdr->cma_version = CMA_VERSION;
2045 cma_set_ip_ver(cma_hdr, 4);
2046 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
2047 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
2048 cma_hdr->port = src4->sin_port;
2054 static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
2055 struct ib_cm_event *ib_event)
2057 struct rdma_id_private *id_priv = cm_id->context;
2058 struct rdma_cm_event event;
2059 struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
2062 if (cma_disable_remove(id_priv, CMA_CONNECT))
2065 memset(&event, 0, sizeof event);
2066 switch (ib_event->event) {
2067 case IB_CM_SIDR_REQ_ERROR:
2068 event.event = RDMA_CM_EVENT_UNREACHABLE;
2069 event.status = -ETIMEDOUT;
2071 case IB_CM_SIDR_REP_RECEIVED:
2072 event.param.ud.private_data = ib_event->private_data;
2073 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
2074 if (rep->status != IB_SIDR_SUCCESS) {
2075 event.event = RDMA_CM_EVENT_UNREACHABLE;
2076 event.status = ib_event->param.sidr_rep_rcvd.status;
2079 if (id_priv->qkey != rep->qkey) {
2080 event.event = RDMA_CM_EVENT_UNREACHABLE;
2081 event.status = -EINVAL;
2084 ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
2085 id_priv->id.route.path_rec,
2086 &event.param.ud.ah_attr);
2087 event.param.ud.qp_num = rep->qpn;
2088 event.param.ud.qkey = rep->qkey;
2089 event.event = RDMA_CM_EVENT_ESTABLISHED;
2093 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d",
2098 ret = id_priv->id.event_handler(&id_priv->id, &event);
2100 /* Destroy the CM ID by returning a non-zero value. */
2101 id_priv->cm_id.ib = NULL;
2102 cma_exch(id_priv, CMA_DESTROYING);
2103 cma_enable_remove(id_priv);
2104 rdma_destroy_id(&id_priv->id);
2108 cma_enable_remove(id_priv);
2112 static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
2113 struct rdma_conn_param *conn_param)
2115 struct ib_cm_sidr_req_param req;
2116 struct rdma_route *route;
2119 req.private_data_len = sizeof(struct cma_hdr) +
2120 conn_param->private_data_len;
2121 req.private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2122 if (!req.private_data)
2125 if (conn_param->private_data && conn_param->private_data_len)
2126 memcpy((void *) req.private_data + sizeof(struct cma_hdr),
2127 conn_param->private_data, conn_param->private_data_len);
2129 route = &id_priv->id.route;
2130 ret = cma_format_hdr((void *) req.private_data, id_priv->id.ps, route);
2134 id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device,
2135 cma_sidr_rep_handler, id_priv);
2136 if (IS_ERR(id_priv->cm_id.ib)) {
2137 ret = PTR_ERR(id_priv->cm_id.ib);
2141 req.path = route->path_rec;
2142 req.service_id = cma_get_service_id(id_priv->id.ps,
2143 &route->addr.dst_addr);
2144 req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
2145 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2147 ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
2149 ib_destroy_cm_id(id_priv->cm_id.ib);
2150 id_priv->cm_id.ib = NULL;
2153 kfree(req.private_data);
2157 static int cma_connect_ib(struct rdma_id_private *id_priv,
2158 struct rdma_conn_param *conn_param)
2160 struct ib_cm_req_param req;
2161 struct rdma_route *route;
2165 memset(&req, 0, sizeof req);
2166 offset = cma_user_data_offset(id_priv->id.ps);
2167 req.private_data_len = offset + conn_param->private_data_len;
2168 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2172 if (conn_param->private_data && conn_param->private_data_len)
2173 memcpy(private_data + offset, conn_param->private_data,
2174 conn_param->private_data_len);
2176 id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_ib_handler,
2178 if (IS_ERR(id_priv->cm_id.ib)) {
2179 ret = PTR_ERR(id_priv->cm_id.ib);
2183 route = &id_priv->id.route;
2184 ret = cma_format_hdr(private_data, id_priv->id.ps, route);
2187 req.private_data = private_data;
2189 req.primary_path = &route->path_rec[0];
2190 if (route->num_paths == 2)
2191 req.alternate_path = &route->path_rec[1];
2193 req.service_id = cma_get_service_id(id_priv->id.ps,
2194 &route->addr.dst_addr);
2195 req.qp_num = id_priv->qp_num;
2196 req.qp_type = IB_QPT_RC;
2197 req.starting_psn = id_priv->seq_num;
2198 req.responder_resources = conn_param->responder_resources;
2199 req.initiator_depth = conn_param->initiator_depth;
2200 req.flow_control = conn_param->flow_control;
2201 req.retry_count = conn_param->retry_count;
2202 req.rnr_retry_count = conn_param->rnr_retry_count;
2203 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2204 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2205 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2206 req.srq = id_priv->srq ? 1 : 0;
2208 ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
2210 if (ret && !IS_ERR(id_priv->cm_id.ib)) {
2211 ib_destroy_cm_id(id_priv->cm_id.ib);
2212 id_priv->cm_id.ib = NULL;
2215 kfree(private_data);
2219 static int cma_connect_iw(struct rdma_id_private *id_priv,
2220 struct rdma_conn_param *conn_param)
2222 struct iw_cm_id *cm_id;
2223 struct sockaddr_in* sin;
2225 struct iw_cm_conn_param iw_param;
2227 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
2228 if (IS_ERR(cm_id)) {
2229 ret = PTR_ERR(cm_id);
2233 id_priv->cm_id.iw = cm_id;
2235 sin = (struct sockaddr_in*) &id_priv->id.route.addr.src_addr;
2236 cm_id->local_addr = *sin;
2238 sin = (struct sockaddr_in*) &id_priv->id.route.addr.dst_addr;
2239 cm_id->remote_addr = *sin;
2241 ret = cma_modify_qp_rtr(&id_priv->id);
2245 iw_param.ord = conn_param->initiator_depth;
2246 iw_param.ird = conn_param->responder_resources;
2247 iw_param.private_data = conn_param->private_data;
2248 iw_param.private_data_len = conn_param->private_data_len;
2250 iw_param.qpn = id_priv->qp_num;
2252 iw_param.qpn = conn_param->qp_num;
2253 ret = iw_cm_connect(cm_id, &iw_param);
2255 if (ret && !IS_ERR(cm_id)) {
2256 iw_destroy_cm_id(cm_id);
2257 id_priv->cm_id.iw = NULL;
2262 int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2264 struct rdma_id_private *id_priv;
2267 id_priv = container_of(id, struct rdma_id_private, id);
2268 if (!cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_CONNECT))
2272 id_priv->qp_num = conn_param->qp_num;
2273 id_priv->srq = conn_param->srq;
2276 switch (rdma_node_get_transport(id->device->node_type)) {
2277 case RDMA_TRANSPORT_IB:
2278 if (cma_is_ud_ps(id->ps))
2279 ret = cma_resolve_ib_udp(id_priv, conn_param);
2281 ret = cma_connect_ib(id_priv, conn_param);
2283 case RDMA_TRANSPORT_IWARP:
2284 ret = cma_connect_iw(id_priv, conn_param);
2295 cma_comp_exch(id_priv, CMA_CONNECT, CMA_ROUTE_RESOLVED);
2298 EXPORT_SYMBOL(rdma_connect);
2300 static int cma_accept_ib(struct rdma_id_private *id_priv,
2301 struct rdma_conn_param *conn_param)
2303 struct ib_cm_rep_param rep;
2304 struct ib_qp_attr qp_attr;
2305 int qp_attr_mask, ret;
2307 if (id_priv->id.qp) {
2308 ret = cma_modify_qp_rtr(&id_priv->id);
2312 qp_attr.qp_state = IB_QPS_RTS;
2313 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, &qp_attr,
2318 qp_attr.max_rd_atomic = conn_param->initiator_depth;
2319 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
2324 memset(&rep, 0, sizeof rep);
2325 rep.qp_num = id_priv->qp_num;
2326 rep.starting_psn = id_priv->seq_num;
2327 rep.private_data = conn_param->private_data;
2328 rep.private_data_len = conn_param->private_data_len;
2329 rep.responder_resources = conn_param->responder_resources;
2330 rep.initiator_depth = conn_param->initiator_depth;
2331 rep.failover_accepted = 0;
2332 rep.flow_control = conn_param->flow_control;
2333 rep.rnr_retry_count = conn_param->rnr_retry_count;
2334 rep.srq = id_priv->srq ? 1 : 0;
2336 ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
2341 static int cma_accept_iw(struct rdma_id_private *id_priv,
2342 struct rdma_conn_param *conn_param)
2344 struct iw_cm_conn_param iw_param;
2347 ret = cma_modify_qp_rtr(&id_priv->id);
2351 iw_param.ord = conn_param->initiator_depth;
2352 iw_param.ird = conn_param->responder_resources;
2353 iw_param.private_data = conn_param->private_data;
2354 iw_param.private_data_len = conn_param->private_data_len;
2355 if (id_priv->id.qp) {
2356 iw_param.qpn = id_priv->qp_num;
2358 iw_param.qpn = conn_param->qp_num;
2360 return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
2363 static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
2364 enum ib_cm_sidr_status status,
2365 const void *private_data, int private_data_len)
2367 struct ib_cm_sidr_rep_param rep;
2369 memset(&rep, 0, sizeof rep);
2370 rep.status = status;
2371 if (status == IB_SIDR_SUCCESS) {
2372 rep.qp_num = id_priv->qp_num;
2373 rep.qkey = id_priv->qkey;
2375 rep.private_data = private_data;
2376 rep.private_data_len = private_data_len;
2378 return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
2381 int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2383 struct rdma_id_private *id_priv;
2386 id_priv = container_of(id, struct rdma_id_private, id);
2387 if (!cma_comp(id_priv, CMA_CONNECT))
2390 if (!id->qp && conn_param) {
2391 id_priv->qp_num = conn_param->qp_num;
2392 id_priv->srq = conn_param->srq;
2395 switch (rdma_node_get_transport(id->device->node_type)) {
2396 case RDMA_TRANSPORT_IB:
2397 if (cma_is_ud_ps(id->ps))
2398 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
2399 conn_param->private_data,
2400 conn_param->private_data_len);
2401 else if (conn_param)
2402 ret = cma_accept_ib(id_priv, conn_param);
2404 ret = cma_rep_recv(id_priv);
2406 case RDMA_TRANSPORT_IWARP:
2407 ret = cma_accept_iw(id_priv, conn_param);
2419 cma_modify_qp_err(id);
2420 rdma_reject(id, NULL, 0);
2423 EXPORT_SYMBOL(rdma_accept);
2425 int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
2427 struct rdma_id_private *id_priv;
2430 id_priv = container_of(id, struct rdma_id_private, id);
2431 if (!cma_has_cm_dev(id_priv))
2434 switch (id->device->node_type) {
2435 case RDMA_NODE_IB_CA:
2436 ret = ib_cm_notify(id_priv->cm_id.ib, event);
2444 EXPORT_SYMBOL(rdma_notify);
2446 int rdma_reject(struct rdma_cm_id *id, const void *private_data,
2447 u8 private_data_len)
2449 struct rdma_id_private *id_priv;
2452 id_priv = container_of(id, struct rdma_id_private, id);
2453 if (!cma_has_cm_dev(id_priv))
2456 switch (rdma_node_get_transport(id->device->node_type)) {
2457 case RDMA_TRANSPORT_IB:
2458 if (cma_is_ud_ps(id->ps))
2459 ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT,
2460 private_data, private_data_len);
2462 ret = ib_send_cm_rej(id_priv->cm_id.ib,
2463 IB_CM_REJ_CONSUMER_DEFINED, NULL,
2464 0, private_data, private_data_len);
2466 case RDMA_TRANSPORT_IWARP:
2467 ret = iw_cm_reject(id_priv->cm_id.iw,
2468 private_data, private_data_len);
2476 EXPORT_SYMBOL(rdma_reject);
2478 int rdma_disconnect(struct rdma_cm_id *id)
2480 struct rdma_id_private *id_priv;
2483 id_priv = container_of(id, struct rdma_id_private, id);
2484 if (!cma_has_cm_dev(id_priv))
2487 switch (rdma_node_get_transport(id->device->node_type)) {
2488 case RDMA_TRANSPORT_IB:
2489 ret = cma_modify_qp_err(id);
2492 /* Initiate or respond to a disconnect. */
2493 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
2494 ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
2496 case RDMA_TRANSPORT_IWARP:
2497 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
2506 EXPORT_SYMBOL(rdma_disconnect);
2508 static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
2510 struct rdma_id_private *id_priv;
2511 struct cma_multicast *mc = multicast->context;
2512 struct rdma_cm_event event;
2515 id_priv = mc->id_priv;
2516 if (cma_disable_remove(id_priv, CMA_ADDR_BOUND) &&
2517 cma_disable_remove(id_priv, CMA_ADDR_RESOLVED))
2520 if (!status && id_priv->id.qp)
2521 status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
2522 multicast->rec.mlid);
2524 memset(&event, 0, sizeof event);
2525 event.status = status;
2526 event.param.ud.private_data = mc->context;
2528 event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
2529 ib_init_ah_from_mcmember(id_priv->id.device,
2530 id_priv->id.port_num, &multicast->rec,
2531 &event.param.ud.ah_attr);
2532 event.param.ud.qp_num = 0xFFFFFF;
2533 event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
2535 event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
2537 ret = id_priv->id.event_handler(&id_priv->id, &event);
2539 cma_exch(id_priv, CMA_DESTROYING);
2540 cma_enable_remove(id_priv);
2541 rdma_destroy_id(&id_priv->id);
2545 cma_enable_remove(id_priv);
2549 static void cma_set_mgid(struct rdma_id_private *id_priv,
2550 struct sockaddr *addr, union ib_gid *mgid)
2552 unsigned char mc_map[MAX_ADDR_LEN];
2553 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2554 struct sockaddr_in *sin = (struct sockaddr_in *) addr;
2555 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
2557 if (cma_any_addr(addr)) {
2558 memset(mgid, 0, sizeof *mgid);
2559 } else if ((addr->sa_family == AF_INET6) &&
2560 ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFF10A01B) ==
2562 /* IPv6 address is an SA assigned MGID. */
2563 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
2565 ip_ib_mc_map(sin->sin_addr.s_addr, mc_map);
2566 if (id_priv->id.ps == RDMA_PS_UDP)
2567 mc_map[7] = 0x01; /* Use RDMA CM signature */
2568 mc_map[8] = ib_addr_get_pkey(dev_addr) >> 8;
2569 mc_map[9] = (unsigned char) ib_addr_get_pkey(dev_addr);
2570 *mgid = *(union ib_gid *) (mc_map + 4);
2574 static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
2575 struct cma_multicast *mc)
2577 struct ib_sa_mcmember_rec rec;
2578 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2579 ib_sa_comp_mask comp_mask;
2582 ib_addr_get_mgid(dev_addr, &rec.mgid);
2583 ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
2588 cma_set_mgid(id_priv, &mc->addr, &rec.mgid);
2589 if (id_priv->id.ps == RDMA_PS_UDP)
2590 rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
2591 ib_addr_get_sgid(dev_addr, &rec.port_gid);
2592 rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
2595 comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
2596 IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
2597 IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
2598 IB_SA_MCMEMBER_REC_FLOW_LABEL |
2599 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
2601 mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
2602 id_priv->id.port_num, &rec,
2603 comp_mask, GFP_KERNEL,
2604 cma_ib_mc_handler, mc);
2605 if (IS_ERR(mc->multicast.ib))
2606 return PTR_ERR(mc->multicast.ib);
2611 int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
2614 struct rdma_id_private *id_priv;
2615 struct cma_multicast *mc;
2618 id_priv = container_of(id, struct rdma_id_private, id);
2619 if (!cma_comp(id_priv, CMA_ADDR_BOUND) &&
2620 !cma_comp(id_priv, CMA_ADDR_RESOLVED))
2623 mc = kmalloc(sizeof *mc, GFP_KERNEL);
2627 memcpy(&mc->addr, addr, ip_addr_size(addr));
2628 mc->context = context;
2629 mc->id_priv = id_priv;
2631 spin_lock(&id_priv->lock);
2632 list_add(&mc->list, &id_priv->mc_list);
2633 spin_unlock(&id_priv->lock);
2635 switch (rdma_node_get_transport(id->device->node_type)) {
2636 case RDMA_TRANSPORT_IB:
2637 ret = cma_join_ib_multicast(id_priv, mc);
2645 spin_lock_irq(&id_priv->lock);
2646 list_del(&mc->list);
2647 spin_unlock_irq(&id_priv->lock);
2652 EXPORT_SYMBOL(rdma_join_multicast);
2654 void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
2656 struct rdma_id_private *id_priv;
2657 struct cma_multicast *mc;
2659 id_priv = container_of(id, struct rdma_id_private, id);
2660 spin_lock_irq(&id_priv->lock);
2661 list_for_each_entry(mc, &id_priv->mc_list, list) {
2662 if (!memcmp(&mc->addr, addr, ip_addr_size(addr))) {
2663 list_del(&mc->list);
2664 spin_unlock_irq(&id_priv->lock);
2667 ib_detach_mcast(id->qp,
2668 &mc->multicast.ib->rec.mgid,
2669 mc->multicast.ib->rec.mlid);
2670 ib_sa_free_multicast(mc->multicast.ib);
2675 spin_unlock_irq(&id_priv->lock);
2677 EXPORT_SYMBOL(rdma_leave_multicast);
2679 static void cma_add_one(struct ib_device *device)
2681 struct cma_device *cma_dev;
2682 struct rdma_id_private *id_priv;
2684 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
2688 cma_dev->device = device;
2690 init_completion(&cma_dev->comp);
2691 atomic_set(&cma_dev->refcount, 1);
2692 INIT_LIST_HEAD(&cma_dev->id_list);
2693 ib_set_client_data(device, &cma_client, cma_dev);
2696 list_add_tail(&cma_dev->list, &dev_list);
2697 list_for_each_entry(id_priv, &listen_any_list, list)
2698 cma_listen_on_dev(id_priv, cma_dev);
2699 mutex_unlock(&lock);
2702 static int cma_remove_id_dev(struct rdma_id_private *id_priv)
2704 struct rdma_cm_event event;
2705 enum cma_state state;
2707 /* Record that we want to remove the device */
2708 state = cma_exch(id_priv, CMA_DEVICE_REMOVAL);
2709 if (state == CMA_DESTROYING)
2712 cma_cancel_operation(id_priv, state);
2713 wait_event(id_priv->wait_remove, !atomic_read(&id_priv->dev_remove));
2715 /* Check for destruction from another callback. */
2716 if (!cma_comp(id_priv, CMA_DEVICE_REMOVAL))
2719 memset(&event, 0, sizeof event);
2720 event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
2721 return id_priv->id.event_handler(&id_priv->id, &event);
2724 static void cma_process_remove(struct cma_device *cma_dev)
2726 struct rdma_id_private *id_priv;
2730 while (!list_empty(&cma_dev->id_list)) {
2731 id_priv = list_entry(cma_dev->id_list.next,
2732 struct rdma_id_private, list);
2734 if (cma_internal_listen(id_priv)) {
2735 cma_destroy_listen(id_priv);
2739 list_del_init(&id_priv->list);
2740 atomic_inc(&id_priv->refcount);
2741 mutex_unlock(&lock);
2743 ret = cma_remove_id_dev(id_priv);
2744 cma_deref_id(id_priv);
2746 rdma_destroy_id(&id_priv->id);
2750 mutex_unlock(&lock);
2752 cma_deref_dev(cma_dev);
2753 wait_for_completion(&cma_dev->comp);
2756 static void cma_remove_one(struct ib_device *device)
2758 struct cma_device *cma_dev;
2760 cma_dev = ib_get_client_data(device, &cma_client);
2765 list_del(&cma_dev->list);
2766 mutex_unlock(&lock);
2768 cma_process_remove(cma_dev);
2772 static int cma_init(void)
2776 get_random_bytes(&next_port, sizeof next_port);
2777 inet_get_local_port_range(&low, &high);
2778 next_port = ((unsigned int) next_port % (high - low)) + low;
2780 cma_wq = create_singlethread_workqueue("rdma_cm");
2784 ib_sa_register_client(&sa_client);
2785 rdma_addr_register_client(&addr_client);
2787 ret = ib_register_client(&cma_client);
2793 rdma_addr_unregister_client(&addr_client);
2794 ib_sa_unregister_client(&sa_client);
2795 destroy_workqueue(cma_wq);
2799 static void cma_cleanup(void)
2801 ib_unregister_client(&cma_client);
2802 rdma_addr_unregister_client(&addr_client);
2803 ib_sa_unregister_client(&sa_client);
2804 destroy_workqueue(cma_wq);
2805 idr_destroy(&sdp_ps);
2806 idr_destroy(&tcp_ps);
2807 idr_destroy(&udp_ps);
2808 idr_destroy(&ipoib_ps);
2811 module_init(cma_init);
2812 module_exit(cma_cleanup);