Merge remote-tracking branch 'pm/linux-next'
[linux-2.6/next.git] / drivers / infiniband / core / cma.c
blobca4c5dcd7133365f09dc60d83f623d4b5412ec4d
1 /*
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 available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer.
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * SOFTWARE.
36 #include <linux/completion.h>
37 #include <linux/in.h>
38 #include <linux/in6.h>
39 #include <linux/mutex.h>
40 #include <linux/random.h>
41 #include <linux/idr.h>
42 #include <linux/inetdevice.h>
43 #include <linux/slab.h>
45 #include <net/tcp.h>
46 #include <net/ipv6.h>
48 #include <rdma/rdma_cm.h>
49 #include <rdma/rdma_cm_ib.h>
50 #include <rdma/rdma_netlink.h>
51 #include <rdma/ib_cache.h>
52 #include <rdma/ib_cm.h>
53 #include <rdma/ib_sa.h>
54 #include <rdma/iw_cm.h>
56 MODULE_AUTHOR("Sean Hefty");
57 MODULE_DESCRIPTION("Generic RDMA CM Agent");
58 MODULE_LICENSE("Dual BSD/GPL");
60 #define CMA_CM_RESPONSE_TIMEOUT 20
61 #define CMA_MAX_CM_RETRIES 15
62 #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
63 #define CMA_IBOE_PACKET_LIFETIME 18
65 static void cma_add_one(struct ib_device *device);
66 static void cma_remove_one(struct ib_device *device);
68 static struct ib_client cma_client = {
69 .name = "cma",
70 .add = cma_add_one,
71 .remove = cma_remove_one
74 static struct ib_sa_client sa_client;
75 static struct rdma_addr_client addr_client;
76 static LIST_HEAD(dev_list);
77 static LIST_HEAD(listen_any_list);
78 static DEFINE_MUTEX(lock);
79 static struct workqueue_struct *cma_wq;
80 static DEFINE_IDR(sdp_ps);
81 static DEFINE_IDR(tcp_ps);
82 static DEFINE_IDR(udp_ps);
83 static DEFINE_IDR(ipoib_ps);
85 struct cma_device {
86 struct list_head list;
87 struct ib_device *device;
88 struct completion comp;
89 atomic_t refcount;
90 struct list_head id_list;
93 struct rdma_bind_list {
94 struct idr *ps;
95 struct hlist_head owners;
96 unsigned short port;
100 * Device removal can occur at anytime, so we need extra handling to
101 * serialize notifying the user of device removal with other callbacks.
102 * We do this by disabling removal notification while a callback is in process,
103 * and reporting it after the callback completes.
105 struct rdma_id_private {
106 struct rdma_cm_id id;
108 struct rdma_bind_list *bind_list;
109 struct hlist_node node;
110 struct list_head list; /* listen_any_list or cma_device.list */
111 struct list_head listen_list; /* per device listens */
112 struct cma_device *cma_dev;
113 struct list_head mc_list;
115 int internal_id;
116 enum rdma_cm_state state;
117 spinlock_t lock;
118 struct mutex qp_mutex;
120 struct completion comp;
121 atomic_t refcount;
122 struct mutex handler_mutex;
124 int backlog;
125 int timeout_ms;
126 struct ib_sa_query *query;
127 int query_id;
128 union {
129 struct ib_cm_id *ib;
130 struct iw_cm_id *iw;
131 } cm_id;
133 u32 seq_num;
134 u32 qkey;
135 u32 qp_num;
136 pid_t owner;
137 u8 srq;
138 u8 tos;
139 u8 reuseaddr;
142 struct cma_multicast {
143 struct rdma_id_private *id_priv;
144 union {
145 struct ib_sa_multicast *ib;
146 } multicast;
147 struct list_head list;
148 void *context;
149 struct sockaddr_storage addr;
150 struct kref mcref;
153 struct cma_work {
154 struct work_struct work;
155 struct rdma_id_private *id;
156 enum rdma_cm_state old_state;
157 enum rdma_cm_state new_state;
158 struct rdma_cm_event event;
161 struct cma_ndev_work {
162 struct work_struct work;
163 struct rdma_id_private *id;
164 struct rdma_cm_event event;
167 struct iboe_mcast_work {
168 struct work_struct work;
169 struct rdma_id_private *id;
170 struct cma_multicast *mc;
173 union cma_ip_addr {
174 struct in6_addr ip6;
175 struct {
176 __be32 pad[3];
177 __be32 addr;
178 } ip4;
181 struct cma_hdr {
182 u8 cma_version;
183 u8 ip_version; /* IP version: 7:4 */
184 __be16 port;
185 union cma_ip_addr src_addr;
186 union cma_ip_addr dst_addr;
189 struct sdp_hh {
190 u8 bsdh[16];
191 u8 sdp_version; /* Major version: 7:4 */
192 u8 ip_version; /* IP version: 7:4 */
193 u8 sdp_specific1[10];
194 __be16 port;
195 __be16 sdp_specific2;
196 union cma_ip_addr src_addr;
197 union cma_ip_addr dst_addr;
200 struct sdp_hah {
201 u8 bsdh[16];
202 u8 sdp_version;
205 #define CMA_VERSION 0x00
206 #define SDP_MAJ_VERSION 0x2
208 static int cma_comp(struct rdma_id_private *id_priv, enum rdma_cm_state comp)
210 unsigned long flags;
211 int ret;
213 spin_lock_irqsave(&id_priv->lock, flags);
214 ret = (id_priv->state == comp);
215 spin_unlock_irqrestore(&id_priv->lock, flags);
216 return ret;
219 static int cma_comp_exch(struct rdma_id_private *id_priv,
220 enum rdma_cm_state comp, enum rdma_cm_state exch)
222 unsigned long flags;
223 int ret;
225 spin_lock_irqsave(&id_priv->lock, flags);
226 if ((ret = (id_priv->state == comp)))
227 id_priv->state = exch;
228 spin_unlock_irqrestore(&id_priv->lock, flags);
229 return ret;
232 static enum rdma_cm_state cma_exch(struct rdma_id_private *id_priv,
233 enum rdma_cm_state exch)
235 unsigned long flags;
236 enum rdma_cm_state old;
238 spin_lock_irqsave(&id_priv->lock, flags);
239 old = id_priv->state;
240 id_priv->state = exch;
241 spin_unlock_irqrestore(&id_priv->lock, flags);
242 return old;
245 static inline u8 cma_get_ip_ver(struct cma_hdr *hdr)
247 return hdr->ip_version >> 4;
250 static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
252 hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
255 static inline u8 sdp_get_majv(u8 sdp_version)
257 return sdp_version >> 4;
260 static inline u8 sdp_get_ip_ver(struct sdp_hh *hh)
262 return hh->ip_version >> 4;
265 static inline void sdp_set_ip_ver(struct sdp_hh *hh, u8 ip_ver)
267 hh->ip_version = (ip_ver << 4) | (hh->ip_version & 0xF);
270 static void cma_attach_to_dev(struct rdma_id_private *id_priv,
271 struct cma_device *cma_dev)
273 atomic_inc(&cma_dev->refcount);
274 id_priv->cma_dev = cma_dev;
275 id_priv->id.device = cma_dev->device;
276 id_priv->id.route.addr.dev_addr.transport =
277 rdma_node_get_transport(cma_dev->device->node_type);
278 list_add_tail(&id_priv->list, &cma_dev->id_list);
281 static inline void cma_deref_dev(struct cma_device *cma_dev)
283 if (atomic_dec_and_test(&cma_dev->refcount))
284 complete(&cma_dev->comp);
287 static inline void release_mc(struct kref *kref)
289 struct cma_multicast *mc = container_of(kref, struct cma_multicast, mcref);
291 kfree(mc->multicast.ib);
292 kfree(mc);
295 static void cma_release_dev(struct rdma_id_private *id_priv)
297 mutex_lock(&lock);
298 list_del(&id_priv->list);
299 cma_deref_dev(id_priv->cma_dev);
300 id_priv->cma_dev = NULL;
301 mutex_unlock(&lock);
304 static int cma_set_qkey(struct rdma_id_private *id_priv)
306 struct ib_sa_mcmember_rec rec;
307 int ret = 0;
309 if (id_priv->qkey)
310 return 0;
312 switch (id_priv->id.ps) {
313 case RDMA_PS_UDP:
314 id_priv->qkey = RDMA_UDP_QKEY;
315 break;
316 case RDMA_PS_IPOIB:
317 ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid);
318 ret = ib_sa_get_mcmember_rec(id_priv->id.device,
319 id_priv->id.port_num, &rec.mgid,
320 &rec);
321 if (!ret)
322 id_priv->qkey = be32_to_cpu(rec.qkey);
323 break;
324 default:
325 break;
327 return ret;
330 static int find_gid_port(struct ib_device *device, union ib_gid *gid, u8 port_num)
332 int i;
333 int err;
334 struct ib_port_attr props;
335 union ib_gid tmp;
337 err = ib_query_port(device, port_num, &props);
338 if (err)
339 return 1;
341 for (i = 0; i < props.gid_tbl_len; ++i) {
342 err = ib_query_gid(device, port_num, i, &tmp);
343 if (err)
344 return 1;
345 if (!memcmp(&tmp, gid, sizeof tmp))
346 return 0;
349 return -EAGAIN;
352 static int cma_acquire_dev(struct rdma_id_private *id_priv)
354 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
355 struct cma_device *cma_dev;
356 union ib_gid gid, iboe_gid;
357 int ret = -ENODEV;
358 u8 port;
359 enum rdma_link_layer dev_ll = dev_addr->dev_type == ARPHRD_INFINIBAND ?
360 IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET;
362 if (dev_ll != IB_LINK_LAYER_INFINIBAND &&
363 id_priv->id.ps == RDMA_PS_IPOIB)
364 return -EINVAL;
366 mutex_lock(&lock);
367 iboe_addr_get_sgid(dev_addr, &iboe_gid);
368 memcpy(&gid, dev_addr->src_dev_addr +
369 rdma_addr_gid_offset(dev_addr), sizeof gid);
370 list_for_each_entry(cma_dev, &dev_list, list) {
371 for (port = 1; port <= cma_dev->device->phys_port_cnt; ++port) {
372 if (rdma_port_get_link_layer(cma_dev->device, port) == dev_ll) {
373 if (rdma_node_get_transport(cma_dev->device->node_type) == RDMA_TRANSPORT_IB &&
374 rdma_port_get_link_layer(cma_dev->device, port) == IB_LINK_LAYER_ETHERNET)
375 ret = find_gid_port(cma_dev->device, &iboe_gid, port);
376 else
377 ret = find_gid_port(cma_dev->device, &gid, port);
379 if (!ret) {
380 id_priv->id.port_num = port;
381 goto out;
382 } else if (ret == 1)
383 break;
388 out:
389 if (!ret)
390 cma_attach_to_dev(id_priv, cma_dev);
392 mutex_unlock(&lock);
393 return ret;
396 static void cma_deref_id(struct rdma_id_private *id_priv)
398 if (atomic_dec_and_test(&id_priv->refcount))
399 complete(&id_priv->comp);
402 static int cma_disable_callback(struct rdma_id_private *id_priv,
403 enum rdma_cm_state state)
405 mutex_lock(&id_priv->handler_mutex);
406 if (id_priv->state != state) {
407 mutex_unlock(&id_priv->handler_mutex);
408 return -EINVAL;
410 return 0;
413 struct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler,
414 void *context, enum rdma_port_space ps,
415 enum ib_qp_type qp_type)
417 struct rdma_id_private *id_priv;
419 id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
420 if (!id_priv)
421 return ERR_PTR(-ENOMEM);
423 id_priv->owner = task_pid_nr(current);
424 id_priv->state = RDMA_CM_IDLE;
425 id_priv->id.context = context;
426 id_priv->id.event_handler = event_handler;
427 id_priv->id.ps = ps;
428 id_priv->id.qp_type = qp_type;
429 spin_lock_init(&id_priv->lock);
430 mutex_init(&id_priv->qp_mutex);
431 init_completion(&id_priv->comp);
432 atomic_set(&id_priv->refcount, 1);
433 mutex_init(&id_priv->handler_mutex);
434 INIT_LIST_HEAD(&id_priv->listen_list);
435 INIT_LIST_HEAD(&id_priv->mc_list);
436 get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
438 return &id_priv->id;
440 EXPORT_SYMBOL(rdma_create_id);
442 static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
444 struct ib_qp_attr qp_attr;
445 int qp_attr_mask, ret;
447 qp_attr.qp_state = IB_QPS_INIT;
448 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
449 if (ret)
450 return ret;
452 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
453 if (ret)
454 return ret;
456 qp_attr.qp_state = IB_QPS_RTR;
457 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
458 if (ret)
459 return ret;
461 qp_attr.qp_state = IB_QPS_RTS;
462 qp_attr.sq_psn = 0;
463 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
465 return ret;
468 static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
470 struct ib_qp_attr qp_attr;
471 int qp_attr_mask, ret;
473 qp_attr.qp_state = IB_QPS_INIT;
474 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
475 if (ret)
476 return ret;
478 return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
481 int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
482 struct ib_qp_init_attr *qp_init_attr)
484 struct rdma_id_private *id_priv;
485 struct ib_qp *qp;
486 int ret;
488 id_priv = container_of(id, struct rdma_id_private, id);
489 if (id->device != pd->device)
490 return -EINVAL;
492 qp = ib_create_qp(pd, qp_init_attr);
493 if (IS_ERR(qp))
494 return PTR_ERR(qp);
496 if (id->qp_type == IB_QPT_UD)
497 ret = cma_init_ud_qp(id_priv, qp);
498 else
499 ret = cma_init_conn_qp(id_priv, qp);
500 if (ret)
501 goto err;
503 id->qp = qp;
504 id_priv->qp_num = qp->qp_num;
505 id_priv->srq = (qp->srq != NULL);
506 return 0;
507 err:
508 ib_destroy_qp(qp);
509 return ret;
511 EXPORT_SYMBOL(rdma_create_qp);
513 void rdma_destroy_qp(struct rdma_cm_id *id)
515 struct rdma_id_private *id_priv;
517 id_priv = container_of(id, struct rdma_id_private, id);
518 mutex_lock(&id_priv->qp_mutex);
519 ib_destroy_qp(id_priv->id.qp);
520 id_priv->id.qp = NULL;
521 mutex_unlock(&id_priv->qp_mutex);
523 EXPORT_SYMBOL(rdma_destroy_qp);
525 static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
526 struct rdma_conn_param *conn_param)
528 struct ib_qp_attr qp_attr;
529 int qp_attr_mask, ret;
531 mutex_lock(&id_priv->qp_mutex);
532 if (!id_priv->id.qp) {
533 ret = 0;
534 goto out;
537 /* Need to update QP attributes from default values. */
538 qp_attr.qp_state = IB_QPS_INIT;
539 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
540 if (ret)
541 goto out;
543 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
544 if (ret)
545 goto out;
547 qp_attr.qp_state = IB_QPS_RTR;
548 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
549 if (ret)
550 goto out;
552 if (conn_param)
553 qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
554 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
555 out:
556 mutex_unlock(&id_priv->qp_mutex);
557 return ret;
560 static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
561 struct rdma_conn_param *conn_param)
563 struct ib_qp_attr qp_attr;
564 int qp_attr_mask, ret;
566 mutex_lock(&id_priv->qp_mutex);
567 if (!id_priv->id.qp) {
568 ret = 0;
569 goto out;
572 qp_attr.qp_state = IB_QPS_RTS;
573 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
574 if (ret)
575 goto out;
577 if (conn_param)
578 qp_attr.max_rd_atomic = conn_param->initiator_depth;
579 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
580 out:
581 mutex_unlock(&id_priv->qp_mutex);
582 return ret;
585 static int cma_modify_qp_err(struct rdma_id_private *id_priv)
587 struct ib_qp_attr qp_attr;
588 int ret;
590 mutex_lock(&id_priv->qp_mutex);
591 if (!id_priv->id.qp) {
592 ret = 0;
593 goto out;
596 qp_attr.qp_state = IB_QPS_ERR;
597 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
598 out:
599 mutex_unlock(&id_priv->qp_mutex);
600 return ret;
603 static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
604 struct ib_qp_attr *qp_attr, int *qp_attr_mask)
606 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
607 int ret;
608 u16 pkey;
610 if (rdma_port_get_link_layer(id_priv->id.device, id_priv->id.port_num) ==
611 IB_LINK_LAYER_INFINIBAND)
612 pkey = ib_addr_get_pkey(dev_addr);
613 else
614 pkey = 0xffff;
616 ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
617 pkey, &qp_attr->pkey_index);
618 if (ret)
619 return ret;
621 qp_attr->port_num = id_priv->id.port_num;
622 *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
624 if (id_priv->id.qp_type == IB_QPT_UD) {
625 ret = cma_set_qkey(id_priv);
626 if (ret)
627 return ret;
629 qp_attr->qkey = id_priv->qkey;
630 *qp_attr_mask |= IB_QP_QKEY;
631 } else {
632 qp_attr->qp_access_flags = 0;
633 *qp_attr_mask |= IB_QP_ACCESS_FLAGS;
635 return 0;
638 int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
639 int *qp_attr_mask)
641 struct rdma_id_private *id_priv;
642 int ret = 0;
644 id_priv = container_of(id, struct rdma_id_private, id);
645 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
646 case RDMA_TRANSPORT_IB:
647 if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD))
648 ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
649 else
650 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
651 qp_attr_mask);
652 if (qp_attr->qp_state == IB_QPS_RTR)
653 qp_attr->rq_psn = id_priv->seq_num;
654 break;
655 case RDMA_TRANSPORT_IWARP:
656 if (!id_priv->cm_id.iw) {
657 qp_attr->qp_access_flags = 0;
658 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
659 } else
660 ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
661 qp_attr_mask);
662 break;
663 default:
664 ret = -ENOSYS;
665 break;
668 return ret;
670 EXPORT_SYMBOL(rdma_init_qp_attr);
672 static inline int cma_zero_addr(struct sockaddr *addr)
674 struct in6_addr *ip6;
676 if (addr->sa_family == AF_INET)
677 return ipv4_is_zeronet(
678 ((struct sockaddr_in *)addr)->sin_addr.s_addr);
679 else {
680 ip6 = &((struct sockaddr_in6 *) addr)->sin6_addr;
681 return (ip6->s6_addr32[0] | ip6->s6_addr32[1] |
682 ip6->s6_addr32[2] | ip6->s6_addr32[3]) == 0;
686 static inline int cma_loopback_addr(struct sockaddr *addr)
688 if (addr->sa_family == AF_INET)
689 return ipv4_is_loopback(
690 ((struct sockaddr_in *) addr)->sin_addr.s_addr);
691 else
692 return ipv6_addr_loopback(
693 &((struct sockaddr_in6 *) addr)->sin6_addr);
696 static inline int cma_any_addr(struct sockaddr *addr)
698 return cma_zero_addr(addr) || cma_loopback_addr(addr);
701 static int cma_addr_cmp(struct sockaddr *src, struct sockaddr *dst)
703 if (src->sa_family != dst->sa_family)
704 return -1;
706 switch (src->sa_family) {
707 case AF_INET:
708 return ((struct sockaddr_in *) src)->sin_addr.s_addr !=
709 ((struct sockaddr_in *) dst)->sin_addr.s_addr;
710 default:
711 return ipv6_addr_cmp(&((struct sockaddr_in6 *) src)->sin6_addr,
712 &((struct sockaddr_in6 *) dst)->sin6_addr);
716 static inline __be16 cma_port(struct sockaddr *addr)
718 if (addr->sa_family == AF_INET)
719 return ((struct sockaddr_in *) addr)->sin_port;
720 else
721 return ((struct sockaddr_in6 *) addr)->sin6_port;
724 static inline int cma_any_port(struct sockaddr *addr)
726 return !cma_port(addr);
729 static int cma_get_net_info(void *hdr, enum rdma_port_space ps,
730 u8 *ip_ver, __be16 *port,
731 union cma_ip_addr **src, union cma_ip_addr **dst)
733 switch (ps) {
734 case RDMA_PS_SDP:
735 if (sdp_get_majv(((struct sdp_hh *) hdr)->sdp_version) !=
736 SDP_MAJ_VERSION)
737 return -EINVAL;
739 *ip_ver = sdp_get_ip_ver(hdr);
740 *port = ((struct sdp_hh *) hdr)->port;
741 *src = &((struct sdp_hh *) hdr)->src_addr;
742 *dst = &((struct sdp_hh *) hdr)->dst_addr;
743 break;
744 default:
745 if (((struct cma_hdr *) hdr)->cma_version != CMA_VERSION)
746 return -EINVAL;
748 *ip_ver = cma_get_ip_ver(hdr);
749 *port = ((struct cma_hdr *) hdr)->port;
750 *src = &((struct cma_hdr *) hdr)->src_addr;
751 *dst = &((struct cma_hdr *) hdr)->dst_addr;
752 break;
755 if (*ip_ver != 4 && *ip_ver != 6)
756 return -EINVAL;
757 return 0;
760 static void cma_save_net_info(struct rdma_addr *addr,
761 struct rdma_addr *listen_addr,
762 u8 ip_ver, __be16 port,
763 union cma_ip_addr *src, union cma_ip_addr *dst)
765 struct sockaddr_in *listen4, *ip4;
766 struct sockaddr_in6 *listen6, *ip6;
768 switch (ip_ver) {
769 case 4:
770 listen4 = (struct sockaddr_in *) &listen_addr->src_addr;
771 ip4 = (struct sockaddr_in *) &addr->src_addr;
772 ip4->sin_family = listen4->sin_family;
773 ip4->sin_addr.s_addr = dst->ip4.addr;
774 ip4->sin_port = listen4->sin_port;
776 ip4 = (struct sockaddr_in *) &addr->dst_addr;
777 ip4->sin_family = listen4->sin_family;
778 ip4->sin_addr.s_addr = src->ip4.addr;
779 ip4->sin_port = port;
780 break;
781 case 6:
782 listen6 = (struct sockaddr_in6 *) &listen_addr->src_addr;
783 ip6 = (struct sockaddr_in6 *) &addr->src_addr;
784 ip6->sin6_family = listen6->sin6_family;
785 ip6->sin6_addr = dst->ip6;
786 ip6->sin6_port = listen6->sin6_port;
788 ip6 = (struct sockaddr_in6 *) &addr->dst_addr;
789 ip6->sin6_family = listen6->sin6_family;
790 ip6->sin6_addr = src->ip6;
791 ip6->sin6_port = port;
792 break;
793 default:
794 break;
798 static inline int cma_user_data_offset(enum rdma_port_space ps)
800 switch (ps) {
801 case RDMA_PS_SDP:
802 return 0;
803 default:
804 return sizeof(struct cma_hdr);
808 static void cma_cancel_route(struct rdma_id_private *id_priv)
810 switch (rdma_port_get_link_layer(id_priv->id.device, id_priv->id.port_num)) {
811 case IB_LINK_LAYER_INFINIBAND:
812 if (id_priv->query)
813 ib_sa_cancel_query(id_priv->query_id, id_priv->query);
814 break;
815 default:
816 break;
820 static void cma_cancel_listens(struct rdma_id_private *id_priv)
822 struct rdma_id_private *dev_id_priv;
825 * Remove from listen_any_list to prevent added devices from spawning
826 * additional listen requests.
828 mutex_lock(&lock);
829 list_del(&id_priv->list);
831 while (!list_empty(&id_priv->listen_list)) {
832 dev_id_priv = list_entry(id_priv->listen_list.next,
833 struct rdma_id_private, listen_list);
834 /* sync with device removal to avoid duplicate destruction */
835 list_del_init(&dev_id_priv->list);
836 list_del(&dev_id_priv->listen_list);
837 mutex_unlock(&lock);
839 rdma_destroy_id(&dev_id_priv->id);
840 mutex_lock(&lock);
842 mutex_unlock(&lock);
845 static void cma_cancel_operation(struct rdma_id_private *id_priv,
846 enum rdma_cm_state state)
848 switch (state) {
849 case RDMA_CM_ADDR_QUERY:
850 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
851 break;
852 case RDMA_CM_ROUTE_QUERY:
853 cma_cancel_route(id_priv);
854 break;
855 case RDMA_CM_LISTEN:
856 if (cma_any_addr((struct sockaddr *) &id_priv->id.route.addr.src_addr)
857 && !id_priv->cma_dev)
858 cma_cancel_listens(id_priv);
859 break;
860 default:
861 break;
865 static void cma_release_port(struct rdma_id_private *id_priv)
867 struct rdma_bind_list *bind_list = id_priv->bind_list;
869 if (!bind_list)
870 return;
872 mutex_lock(&lock);
873 hlist_del(&id_priv->node);
874 if (hlist_empty(&bind_list->owners)) {
875 idr_remove(bind_list->ps, bind_list->port);
876 kfree(bind_list);
878 mutex_unlock(&lock);
881 static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
883 struct cma_multicast *mc;
885 while (!list_empty(&id_priv->mc_list)) {
886 mc = container_of(id_priv->mc_list.next,
887 struct cma_multicast, list);
888 list_del(&mc->list);
889 switch (rdma_port_get_link_layer(id_priv->cma_dev->device, id_priv->id.port_num)) {
890 case IB_LINK_LAYER_INFINIBAND:
891 ib_sa_free_multicast(mc->multicast.ib);
892 kfree(mc);
893 break;
894 case IB_LINK_LAYER_ETHERNET:
895 kref_put(&mc->mcref, release_mc);
896 break;
897 default:
898 break;
903 void rdma_destroy_id(struct rdma_cm_id *id)
905 struct rdma_id_private *id_priv;
906 enum rdma_cm_state state;
908 id_priv = container_of(id, struct rdma_id_private, id);
909 state = cma_exch(id_priv, RDMA_CM_DESTROYING);
910 cma_cancel_operation(id_priv, state);
913 * Wait for any active callback to finish. New callbacks will find
914 * the id_priv state set to destroying and abort.
916 mutex_lock(&id_priv->handler_mutex);
917 mutex_unlock(&id_priv->handler_mutex);
919 if (id_priv->cma_dev) {
920 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
921 case RDMA_TRANSPORT_IB:
922 if (id_priv->cm_id.ib)
923 ib_destroy_cm_id(id_priv->cm_id.ib);
924 break;
925 case RDMA_TRANSPORT_IWARP:
926 if (id_priv->cm_id.iw)
927 iw_destroy_cm_id(id_priv->cm_id.iw);
928 break;
929 default:
930 break;
932 cma_leave_mc_groups(id_priv);
933 cma_release_dev(id_priv);
936 cma_release_port(id_priv);
937 cma_deref_id(id_priv);
938 wait_for_completion(&id_priv->comp);
940 if (id_priv->internal_id)
941 cma_deref_id(id_priv->id.context);
943 kfree(id_priv->id.route.path_rec);
944 kfree(id_priv);
946 EXPORT_SYMBOL(rdma_destroy_id);
948 static int cma_rep_recv(struct rdma_id_private *id_priv)
950 int ret;
952 ret = cma_modify_qp_rtr(id_priv, NULL);
953 if (ret)
954 goto reject;
956 ret = cma_modify_qp_rts(id_priv, NULL);
957 if (ret)
958 goto reject;
960 ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
961 if (ret)
962 goto reject;
964 return 0;
965 reject:
966 cma_modify_qp_err(id_priv);
967 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
968 NULL, 0, NULL, 0);
969 return ret;
972 static int cma_verify_rep(struct rdma_id_private *id_priv, void *data)
974 if (id_priv->id.ps == RDMA_PS_SDP &&
975 sdp_get_majv(((struct sdp_hah *) data)->sdp_version) !=
976 SDP_MAJ_VERSION)
977 return -EINVAL;
979 return 0;
982 static void cma_set_rep_event_data(struct rdma_cm_event *event,
983 struct ib_cm_rep_event_param *rep_data,
984 void *private_data)
986 event->param.conn.private_data = private_data;
987 event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
988 event->param.conn.responder_resources = rep_data->responder_resources;
989 event->param.conn.initiator_depth = rep_data->initiator_depth;
990 event->param.conn.flow_control = rep_data->flow_control;
991 event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
992 event->param.conn.srq = rep_data->srq;
993 event->param.conn.qp_num = rep_data->remote_qpn;
996 static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
998 struct rdma_id_private *id_priv = cm_id->context;
999 struct rdma_cm_event event;
1000 int ret = 0;
1002 if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
1003 cma_disable_callback(id_priv, RDMA_CM_CONNECT)) ||
1004 (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
1005 cma_disable_callback(id_priv, RDMA_CM_DISCONNECT)))
1006 return 0;
1008 memset(&event, 0, sizeof event);
1009 switch (ib_event->event) {
1010 case IB_CM_REQ_ERROR:
1011 case IB_CM_REP_ERROR:
1012 event.event = RDMA_CM_EVENT_UNREACHABLE;
1013 event.status = -ETIMEDOUT;
1014 break;
1015 case IB_CM_REP_RECEIVED:
1016 event.status = cma_verify_rep(id_priv, ib_event->private_data);
1017 if (event.status)
1018 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
1019 else if (id_priv->id.qp && id_priv->id.ps != RDMA_PS_SDP) {
1020 event.status = cma_rep_recv(id_priv);
1021 event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
1022 RDMA_CM_EVENT_ESTABLISHED;
1023 } else
1024 event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
1025 cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
1026 ib_event->private_data);
1027 break;
1028 case IB_CM_RTU_RECEIVED:
1029 case IB_CM_USER_ESTABLISHED:
1030 event.event = RDMA_CM_EVENT_ESTABLISHED;
1031 break;
1032 case IB_CM_DREQ_ERROR:
1033 event.status = -ETIMEDOUT; /* fall through */
1034 case IB_CM_DREQ_RECEIVED:
1035 case IB_CM_DREP_RECEIVED:
1036 if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT,
1037 RDMA_CM_DISCONNECT))
1038 goto out;
1039 event.event = RDMA_CM_EVENT_DISCONNECTED;
1040 break;
1041 case IB_CM_TIMEWAIT_EXIT:
1042 event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
1043 break;
1044 case IB_CM_MRA_RECEIVED:
1045 /* ignore event */
1046 goto out;
1047 case IB_CM_REJ_RECEIVED:
1048 cma_modify_qp_err(id_priv);
1049 event.status = ib_event->param.rej_rcvd.reason;
1050 event.event = RDMA_CM_EVENT_REJECTED;
1051 event.param.conn.private_data = ib_event->private_data;
1052 event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
1053 break;
1054 default:
1055 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
1056 ib_event->event);
1057 goto out;
1060 ret = id_priv->id.event_handler(&id_priv->id, &event);
1061 if (ret) {
1062 /* Destroy the CM ID by returning a non-zero value. */
1063 id_priv->cm_id.ib = NULL;
1064 cma_exch(id_priv, RDMA_CM_DESTROYING);
1065 mutex_unlock(&id_priv->handler_mutex);
1066 rdma_destroy_id(&id_priv->id);
1067 return ret;
1069 out:
1070 mutex_unlock(&id_priv->handler_mutex);
1071 return ret;
1074 static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
1075 struct ib_cm_event *ib_event)
1077 struct rdma_id_private *id_priv;
1078 struct rdma_cm_id *id;
1079 struct rdma_route *rt;
1080 union cma_ip_addr *src, *dst;
1081 __be16 port;
1082 u8 ip_ver;
1083 int ret;
1085 if (cma_get_net_info(ib_event->private_data, listen_id->ps,
1086 &ip_ver, &port, &src, &dst))
1087 return NULL;
1089 id = rdma_create_id(listen_id->event_handler, listen_id->context,
1090 listen_id->ps, ib_event->param.req_rcvd.qp_type);
1091 if (IS_ERR(id))
1092 return NULL;
1094 cma_save_net_info(&id->route.addr, &listen_id->route.addr,
1095 ip_ver, port, src, dst);
1097 rt = &id->route;
1098 rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
1099 rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
1100 GFP_KERNEL);
1101 if (!rt->path_rec)
1102 goto err;
1104 rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
1105 if (rt->num_paths == 2)
1106 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
1108 if (cma_any_addr((struct sockaddr *) &rt->addr.src_addr)) {
1109 rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
1110 rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
1111 ib_addr_set_pkey(&rt->addr.dev_addr, rt->path_rec[0].pkey);
1112 } else {
1113 ret = rdma_translate_ip((struct sockaddr *) &rt->addr.src_addr,
1114 &rt->addr.dev_addr);
1115 if (ret)
1116 goto err;
1118 rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
1120 id_priv = container_of(id, struct rdma_id_private, id);
1121 id_priv->state = RDMA_CM_CONNECT;
1122 return id_priv;
1124 err:
1125 rdma_destroy_id(id);
1126 return NULL;
1129 static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
1130 struct ib_cm_event *ib_event)
1132 struct rdma_id_private *id_priv;
1133 struct rdma_cm_id *id;
1134 union cma_ip_addr *src, *dst;
1135 __be16 port;
1136 u8 ip_ver;
1137 int ret;
1139 id = rdma_create_id(listen_id->event_handler, listen_id->context,
1140 listen_id->ps, IB_QPT_UD);
1141 if (IS_ERR(id))
1142 return NULL;
1145 if (cma_get_net_info(ib_event->private_data, listen_id->ps,
1146 &ip_ver, &port, &src, &dst))
1147 goto err;
1149 cma_save_net_info(&id->route.addr, &listen_id->route.addr,
1150 ip_ver, port, src, dst);
1152 if (!cma_any_addr((struct sockaddr *) &id->route.addr.src_addr)) {
1153 ret = rdma_translate_ip((struct sockaddr *) &id->route.addr.src_addr,
1154 &id->route.addr.dev_addr);
1155 if (ret)
1156 goto err;
1159 id_priv = container_of(id, struct rdma_id_private, id);
1160 id_priv->state = RDMA_CM_CONNECT;
1161 return id_priv;
1162 err:
1163 rdma_destroy_id(id);
1164 return NULL;
1167 static void cma_set_req_event_data(struct rdma_cm_event *event,
1168 struct ib_cm_req_event_param *req_data,
1169 void *private_data, int offset)
1171 event->param.conn.private_data = private_data + offset;
1172 event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
1173 event->param.conn.responder_resources = req_data->responder_resources;
1174 event->param.conn.initiator_depth = req_data->initiator_depth;
1175 event->param.conn.flow_control = req_data->flow_control;
1176 event->param.conn.retry_count = req_data->retry_count;
1177 event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
1178 event->param.conn.srq = req_data->srq;
1179 event->param.conn.qp_num = req_data->remote_qpn;
1182 static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1184 struct rdma_id_private *listen_id, *conn_id;
1185 struct rdma_cm_event event;
1186 int offset, ret;
1188 listen_id = cm_id->context;
1189 if (cma_disable_callback(listen_id, RDMA_CM_LISTEN))
1190 return -ECONNABORTED;
1192 memset(&event, 0, sizeof event);
1193 offset = cma_user_data_offset(listen_id->id.ps);
1194 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1195 if (listen_id->id.qp_type == IB_QPT_UD) {
1196 conn_id = cma_new_udp_id(&listen_id->id, ib_event);
1197 event.param.ud.private_data = ib_event->private_data + offset;
1198 event.param.ud.private_data_len =
1199 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
1200 } else {
1201 conn_id = cma_new_conn_id(&listen_id->id, ib_event);
1202 cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
1203 ib_event->private_data, offset);
1205 if (!conn_id) {
1206 ret = -ENOMEM;
1207 goto out;
1210 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
1211 ret = cma_acquire_dev(conn_id);
1212 if (ret)
1213 goto release_conn_id;
1215 conn_id->cm_id.ib = cm_id;
1216 cm_id->context = conn_id;
1217 cm_id->cm_handler = cma_ib_handler;
1220 * Protect against the user destroying conn_id from another thread
1221 * until we're done accessing it.
1223 atomic_inc(&conn_id->refcount);
1224 ret = conn_id->id.event_handler(&conn_id->id, &event);
1225 if (!ret) {
1227 * Acquire mutex to prevent user executing rdma_destroy_id()
1228 * while we're accessing the cm_id.
1230 mutex_lock(&lock);
1231 if (cma_comp(conn_id, RDMA_CM_CONNECT) && (conn_id->id.qp_type != IB_QPT_UD))
1232 ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
1233 mutex_unlock(&lock);
1234 mutex_unlock(&conn_id->handler_mutex);
1235 cma_deref_id(conn_id);
1236 goto out;
1238 cma_deref_id(conn_id);
1240 /* Destroy the CM ID by returning a non-zero value. */
1241 conn_id->cm_id.ib = NULL;
1243 release_conn_id:
1244 cma_exch(conn_id, RDMA_CM_DESTROYING);
1245 mutex_unlock(&conn_id->handler_mutex);
1246 rdma_destroy_id(&conn_id->id);
1248 out:
1249 mutex_unlock(&listen_id->handler_mutex);
1250 return ret;
1253 static __be64 cma_get_service_id(enum rdma_port_space ps, struct sockaddr *addr)
1255 return cpu_to_be64(((u64)ps << 16) + be16_to_cpu(cma_port(addr)));
1258 static void cma_set_compare_data(enum rdma_port_space ps, struct sockaddr *addr,
1259 struct ib_cm_compare_data *compare)
1261 struct cma_hdr *cma_data, *cma_mask;
1262 struct sdp_hh *sdp_data, *sdp_mask;
1263 __be32 ip4_addr;
1264 struct in6_addr ip6_addr;
1266 memset(compare, 0, sizeof *compare);
1267 cma_data = (void *) compare->data;
1268 cma_mask = (void *) compare->mask;
1269 sdp_data = (void *) compare->data;
1270 sdp_mask = (void *) compare->mask;
1272 switch (addr->sa_family) {
1273 case AF_INET:
1274 ip4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1275 if (ps == RDMA_PS_SDP) {
1276 sdp_set_ip_ver(sdp_data, 4);
1277 sdp_set_ip_ver(sdp_mask, 0xF);
1278 sdp_data->dst_addr.ip4.addr = ip4_addr;
1279 sdp_mask->dst_addr.ip4.addr = htonl(~0);
1280 } else {
1281 cma_set_ip_ver(cma_data, 4);
1282 cma_set_ip_ver(cma_mask, 0xF);
1283 cma_data->dst_addr.ip4.addr = ip4_addr;
1284 cma_mask->dst_addr.ip4.addr = htonl(~0);
1286 break;
1287 case AF_INET6:
1288 ip6_addr = ((struct sockaddr_in6 *) addr)->sin6_addr;
1289 if (ps == RDMA_PS_SDP) {
1290 sdp_set_ip_ver(sdp_data, 6);
1291 sdp_set_ip_ver(sdp_mask, 0xF);
1292 sdp_data->dst_addr.ip6 = ip6_addr;
1293 memset(&sdp_mask->dst_addr.ip6, 0xFF,
1294 sizeof sdp_mask->dst_addr.ip6);
1295 } else {
1296 cma_set_ip_ver(cma_data, 6);
1297 cma_set_ip_ver(cma_mask, 0xF);
1298 cma_data->dst_addr.ip6 = ip6_addr;
1299 memset(&cma_mask->dst_addr.ip6, 0xFF,
1300 sizeof cma_mask->dst_addr.ip6);
1302 break;
1303 default:
1304 break;
1308 static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
1310 struct rdma_id_private *id_priv = iw_id->context;
1311 struct rdma_cm_event event;
1312 struct sockaddr_in *sin;
1313 int ret = 0;
1315 if (cma_disable_callback(id_priv, RDMA_CM_CONNECT))
1316 return 0;
1318 memset(&event, 0, sizeof event);
1319 switch (iw_event->event) {
1320 case IW_CM_EVENT_CLOSE:
1321 event.event = RDMA_CM_EVENT_DISCONNECTED;
1322 break;
1323 case IW_CM_EVENT_CONNECT_REPLY:
1324 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1325 *sin = iw_event->local_addr;
1326 sin = (struct sockaddr_in *) &id_priv->id.route.addr.dst_addr;
1327 *sin = iw_event->remote_addr;
1328 switch (iw_event->status) {
1329 case 0:
1330 event.event = RDMA_CM_EVENT_ESTABLISHED;
1331 break;
1332 case -ECONNRESET:
1333 case -ECONNREFUSED:
1334 event.event = RDMA_CM_EVENT_REJECTED;
1335 break;
1336 case -ETIMEDOUT:
1337 event.event = RDMA_CM_EVENT_UNREACHABLE;
1338 break;
1339 default:
1340 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
1341 break;
1343 break;
1344 case IW_CM_EVENT_ESTABLISHED:
1345 event.event = RDMA_CM_EVENT_ESTABLISHED;
1346 break;
1347 default:
1348 BUG_ON(1);
1351 event.status = iw_event->status;
1352 event.param.conn.private_data = iw_event->private_data;
1353 event.param.conn.private_data_len = iw_event->private_data_len;
1354 ret = id_priv->id.event_handler(&id_priv->id, &event);
1355 if (ret) {
1356 /* Destroy the CM ID by returning a non-zero value. */
1357 id_priv->cm_id.iw = NULL;
1358 cma_exch(id_priv, RDMA_CM_DESTROYING);
1359 mutex_unlock(&id_priv->handler_mutex);
1360 rdma_destroy_id(&id_priv->id);
1361 return ret;
1364 mutex_unlock(&id_priv->handler_mutex);
1365 return ret;
1368 static int iw_conn_req_handler(struct iw_cm_id *cm_id,
1369 struct iw_cm_event *iw_event)
1371 struct rdma_cm_id *new_cm_id;
1372 struct rdma_id_private *listen_id, *conn_id;
1373 struct sockaddr_in *sin;
1374 struct net_device *dev = NULL;
1375 struct rdma_cm_event event;
1376 int ret;
1377 struct ib_device_attr attr;
1379 listen_id = cm_id->context;
1380 if (cma_disable_callback(listen_id, RDMA_CM_LISTEN))
1381 return -ECONNABORTED;
1383 /* Create a new RDMA id for the new IW CM ID */
1384 new_cm_id = rdma_create_id(listen_id->id.event_handler,
1385 listen_id->id.context,
1386 RDMA_PS_TCP, IB_QPT_RC);
1387 if (IS_ERR(new_cm_id)) {
1388 ret = -ENOMEM;
1389 goto out;
1391 conn_id = container_of(new_cm_id, struct rdma_id_private, id);
1392 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
1393 conn_id->state = RDMA_CM_CONNECT;
1395 dev = ip_dev_find(&init_net, iw_event->local_addr.sin_addr.s_addr);
1396 if (!dev) {
1397 ret = -EADDRNOTAVAIL;
1398 mutex_unlock(&conn_id->handler_mutex);
1399 rdma_destroy_id(new_cm_id);
1400 goto out;
1402 ret = rdma_copy_addr(&conn_id->id.route.addr.dev_addr, dev, NULL);
1403 if (ret) {
1404 mutex_unlock(&conn_id->handler_mutex);
1405 rdma_destroy_id(new_cm_id);
1406 goto out;
1409 ret = cma_acquire_dev(conn_id);
1410 if (ret) {
1411 mutex_unlock(&conn_id->handler_mutex);
1412 rdma_destroy_id(new_cm_id);
1413 goto out;
1416 conn_id->cm_id.iw = cm_id;
1417 cm_id->context = conn_id;
1418 cm_id->cm_handler = cma_iw_handler;
1420 sin = (struct sockaddr_in *) &new_cm_id->route.addr.src_addr;
1421 *sin = iw_event->local_addr;
1422 sin = (struct sockaddr_in *) &new_cm_id->route.addr.dst_addr;
1423 *sin = iw_event->remote_addr;
1425 ret = ib_query_device(conn_id->id.device, &attr);
1426 if (ret) {
1427 mutex_unlock(&conn_id->handler_mutex);
1428 rdma_destroy_id(new_cm_id);
1429 goto out;
1432 memset(&event, 0, sizeof event);
1433 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1434 event.param.conn.private_data = iw_event->private_data;
1435 event.param.conn.private_data_len = iw_event->private_data_len;
1436 event.param.conn.initiator_depth = attr.max_qp_init_rd_atom;
1437 event.param.conn.responder_resources = attr.max_qp_rd_atom;
1440 * Protect against the user destroying conn_id from another thread
1441 * until we're done accessing it.
1443 atomic_inc(&conn_id->refcount);
1444 ret = conn_id->id.event_handler(&conn_id->id, &event);
1445 if (ret) {
1446 /* User wants to destroy the CM ID */
1447 conn_id->cm_id.iw = NULL;
1448 cma_exch(conn_id, RDMA_CM_DESTROYING);
1449 mutex_unlock(&conn_id->handler_mutex);
1450 cma_deref_id(conn_id);
1451 rdma_destroy_id(&conn_id->id);
1452 goto out;
1455 mutex_unlock(&conn_id->handler_mutex);
1456 cma_deref_id(conn_id);
1458 out:
1459 if (dev)
1460 dev_put(dev);
1461 mutex_unlock(&listen_id->handler_mutex);
1462 return ret;
1465 static int cma_ib_listen(struct rdma_id_private *id_priv)
1467 struct ib_cm_compare_data compare_data;
1468 struct sockaddr *addr;
1469 struct ib_cm_id *id;
1470 __be64 svc_id;
1471 int ret;
1473 id = ib_create_cm_id(id_priv->id.device, cma_req_handler, id_priv);
1474 if (IS_ERR(id))
1475 return PTR_ERR(id);
1477 id_priv->cm_id.ib = id;
1479 addr = (struct sockaddr *) &id_priv->id.route.addr.src_addr;
1480 svc_id = cma_get_service_id(id_priv->id.ps, addr);
1481 if (cma_any_addr(addr))
1482 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, NULL);
1483 else {
1484 cma_set_compare_data(id_priv->id.ps, addr, &compare_data);
1485 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, &compare_data);
1488 if (ret) {
1489 ib_destroy_cm_id(id_priv->cm_id.ib);
1490 id_priv->cm_id.ib = NULL;
1493 return ret;
1496 static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
1498 int ret;
1499 struct sockaddr_in *sin;
1500 struct iw_cm_id *id;
1502 id = iw_create_cm_id(id_priv->id.device,
1503 iw_conn_req_handler,
1504 id_priv);
1505 if (IS_ERR(id))
1506 return PTR_ERR(id);
1508 id_priv->cm_id.iw = id;
1510 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1511 id_priv->cm_id.iw->local_addr = *sin;
1513 ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
1515 if (ret) {
1516 iw_destroy_cm_id(id_priv->cm_id.iw);
1517 id_priv->cm_id.iw = NULL;
1520 return ret;
1523 static int cma_listen_handler(struct rdma_cm_id *id,
1524 struct rdma_cm_event *event)
1526 struct rdma_id_private *id_priv = id->context;
1528 id->context = id_priv->id.context;
1529 id->event_handler = id_priv->id.event_handler;
1530 return id_priv->id.event_handler(id, event);
1533 static void cma_listen_on_dev(struct rdma_id_private *id_priv,
1534 struct cma_device *cma_dev)
1536 struct rdma_id_private *dev_id_priv;
1537 struct rdma_cm_id *id;
1538 int ret;
1540 id = rdma_create_id(cma_listen_handler, id_priv, id_priv->id.ps,
1541 id_priv->id.qp_type);
1542 if (IS_ERR(id))
1543 return;
1545 dev_id_priv = container_of(id, struct rdma_id_private, id);
1547 dev_id_priv->state = RDMA_CM_ADDR_BOUND;
1548 memcpy(&id->route.addr.src_addr, &id_priv->id.route.addr.src_addr,
1549 ip_addr_size((struct sockaddr *) &id_priv->id.route.addr.src_addr));
1551 cma_attach_to_dev(dev_id_priv, cma_dev);
1552 list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
1553 atomic_inc(&id_priv->refcount);
1554 dev_id_priv->internal_id = 1;
1556 ret = rdma_listen(id, id_priv->backlog);
1557 if (ret)
1558 printk(KERN_WARNING "RDMA CMA: cma_listen_on_dev, error %d, "
1559 "listening on device %s\n", ret, cma_dev->device->name);
1562 static void cma_listen_on_all(struct rdma_id_private *id_priv)
1564 struct cma_device *cma_dev;
1566 mutex_lock(&lock);
1567 list_add_tail(&id_priv->list, &listen_any_list);
1568 list_for_each_entry(cma_dev, &dev_list, list)
1569 cma_listen_on_dev(id_priv, cma_dev);
1570 mutex_unlock(&lock);
1573 void rdma_set_service_type(struct rdma_cm_id *id, int tos)
1575 struct rdma_id_private *id_priv;
1577 id_priv = container_of(id, struct rdma_id_private, id);
1578 id_priv->tos = (u8) tos;
1580 EXPORT_SYMBOL(rdma_set_service_type);
1582 static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
1583 void *context)
1585 struct cma_work *work = context;
1586 struct rdma_route *route;
1588 route = &work->id->id.route;
1590 if (!status) {
1591 route->num_paths = 1;
1592 *route->path_rec = *path_rec;
1593 } else {
1594 work->old_state = RDMA_CM_ROUTE_QUERY;
1595 work->new_state = RDMA_CM_ADDR_RESOLVED;
1596 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
1597 work->event.status = status;
1600 queue_work(cma_wq, &work->work);
1603 static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
1604 struct cma_work *work)
1606 struct rdma_addr *addr = &id_priv->id.route.addr;
1607 struct ib_sa_path_rec path_rec;
1608 ib_sa_comp_mask comp_mask;
1609 struct sockaddr_in6 *sin6;
1611 memset(&path_rec, 0, sizeof path_rec);
1612 rdma_addr_get_sgid(&addr->dev_addr, &path_rec.sgid);
1613 rdma_addr_get_dgid(&addr->dev_addr, &path_rec.dgid);
1614 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(&addr->dev_addr));
1615 path_rec.numb_path = 1;
1616 path_rec.reversible = 1;
1617 path_rec.service_id = cma_get_service_id(id_priv->id.ps,
1618 (struct sockaddr *) &addr->dst_addr);
1620 comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
1621 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
1622 IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
1624 if (addr->src_addr.ss_family == AF_INET) {
1625 path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
1626 comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
1627 } else {
1628 sin6 = (struct sockaddr_in6 *) &addr->src_addr;
1629 path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
1630 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
1633 id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
1634 id_priv->id.port_num, &path_rec,
1635 comp_mask, timeout_ms,
1636 GFP_KERNEL, cma_query_handler,
1637 work, &id_priv->query);
1639 return (id_priv->query_id < 0) ? id_priv->query_id : 0;
1642 static void cma_work_handler(struct work_struct *_work)
1644 struct cma_work *work = container_of(_work, struct cma_work, work);
1645 struct rdma_id_private *id_priv = work->id;
1646 int destroy = 0;
1648 mutex_lock(&id_priv->handler_mutex);
1649 if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
1650 goto out;
1652 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
1653 cma_exch(id_priv, RDMA_CM_DESTROYING);
1654 destroy = 1;
1656 out:
1657 mutex_unlock(&id_priv->handler_mutex);
1658 cma_deref_id(id_priv);
1659 if (destroy)
1660 rdma_destroy_id(&id_priv->id);
1661 kfree(work);
1664 static void cma_ndev_work_handler(struct work_struct *_work)
1666 struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work);
1667 struct rdma_id_private *id_priv = work->id;
1668 int destroy = 0;
1670 mutex_lock(&id_priv->handler_mutex);
1671 if (id_priv->state == RDMA_CM_DESTROYING ||
1672 id_priv->state == RDMA_CM_DEVICE_REMOVAL)
1673 goto out;
1675 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
1676 cma_exch(id_priv, RDMA_CM_DESTROYING);
1677 destroy = 1;
1680 out:
1681 mutex_unlock(&id_priv->handler_mutex);
1682 cma_deref_id(id_priv);
1683 if (destroy)
1684 rdma_destroy_id(&id_priv->id);
1685 kfree(work);
1688 static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
1690 struct rdma_route *route = &id_priv->id.route;
1691 struct cma_work *work;
1692 int ret;
1694 work = kzalloc(sizeof *work, GFP_KERNEL);
1695 if (!work)
1696 return -ENOMEM;
1698 work->id = id_priv;
1699 INIT_WORK(&work->work, cma_work_handler);
1700 work->old_state = RDMA_CM_ROUTE_QUERY;
1701 work->new_state = RDMA_CM_ROUTE_RESOLVED;
1702 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1704 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
1705 if (!route->path_rec) {
1706 ret = -ENOMEM;
1707 goto err1;
1710 ret = cma_query_ib_route(id_priv, timeout_ms, work);
1711 if (ret)
1712 goto err2;
1714 return 0;
1715 err2:
1716 kfree(route->path_rec);
1717 route->path_rec = NULL;
1718 err1:
1719 kfree(work);
1720 return ret;
1723 int rdma_set_ib_paths(struct rdma_cm_id *id,
1724 struct ib_sa_path_rec *path_rec, int num_paths)
1726 struct rdma_id_private *id_priv;
1727 int ret;
1729 id_priv = container_of(id, struct rdma_id_private, id);
1730 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
1731 RDMA_CM_ROUTE_RESOLVED))
1732 return -EINVAL;
1734 id->route.path_rec = kmemdup(path_rec, sizeof *path_rec * num_paths,
1735 GFP_KERNEL);
1736 if (!id->route.path_rec) {
1737 ret = -ENOMEM;
1738 goto err;
1741 id->route.num_paths = num_paths;
1742 return 0;
1743 err:
1744 cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
1745 return ret;
1747 EXPORT_SYMBOL(rdma_set_ib_paths);
1749 static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
1751 struct cma_work *work;
1753 work = kzalloc(sizeof *work, GFP_KERNEL);
1754 if (!work)
1755 return -ENOMEM;
1757 work->id = id_priv;
1758 INIT_WORK(&work->work, cma_work_handler);
1759 work->old_state = RDMA_CM_ROUTE_QUERY;
1760 work->new_state = RDMA_CM_ROUTE_RESOLVED;
1761 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1762 queue_work(cma_wq, &work->work);
1763 return 0;
1766 static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
1768 struct rdma_route *route = &id_priv->id.route;
1769 struct rdma_addr *addr = &route->addr;
1770 struct cma_work *work;
1771 int ret;
1772 struct sockaddr_in *src_addr = (struct sockaddr_in *)&route->addr.src_addr;
1773 struct sockaddr_in *dst_addr = (struct sockaddr_in *)&route->addr.dst_addr;
1774 struct net_device *ndev = NULL;
1775 u16 vid;
1777 if (src_addr->sin_family != dst_addr->sin_family)
1778 return -EINVAL;
1780 work = kzalloc(sizeof *work, GFP_KERNEL);
1781 if (!work)
1782 return -ENOMEM;
1784 work->id = id_priv;
1785 INIT_WORK(&work->work, cma_work_handler);
1787 route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
1788 if (!route->path_rec) {
1789 ret = -ENOMEM;
1790 goto err1;
1793 route->num_paths = 1;
1795 if (addr->dev_addr.bound_dev_if)
1796 ndev = dev_get_by_index(&init_net, addr->dev_addr.bound_dev_if);
1797 if (!ndev) {
1798 ret = -ENODEV;
1799 goto err2;
1802 vid = rdma_vlan_dev_vlan_id(ndev);
1804 iboe_mac_vlan_to_ll(&route->path_rec->sgid, addr->dev_addr.src_dev_addr, vid);
1805 iboe_mac_vlan_to_ll(&route->path_rec->dgid, addr->dev_addr.dst_dev_addr, vid);
1807 route->path_rec->hop_limit = 1;
1808 route->path_rec->reversible = 1;
1809 route->path_rec->pkey = cpu_to_be16(0xffff);
1810 route->path_rec->mtu_selector = IB_SA_EQ;
1811 route->path_rec->sl = id_priv->tos >> 5;
1813 route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
1814 route->path_rec->rate_selector = IB_SA_EQ;
1815 route->path_rec->rate = iboe_get_rate(ndev);
1816 dev_put(ndev);
1817 route->path_rec->packet_life_time_selector = IB_SA_EQ;
1818 route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
1819 if (!route->path_rec->mtu) {
1820 ret = -EINVAL;
1821 goto err2;
1824 work->old_state = RDMA_CM_ROUTE_QUERY;
1825 work->new_state = RDMA_CM_ROUTE_RESOLVED;
1826 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1827 work->event.status = 0;
1829 queue_work(cma_wq, &work->work);
1831 return 0;
1833 err2:
1834 kfree(route->path_rec);
1835 route->path_rec = NULL;
1836 err1:
1837 kfree(work);
1838 return ret;
1841 int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
1843 struct rdma_id_private *id_priv;
1844 int ret;
1846 id_priv = container_of(id, struct rdma_id_private, id);
1847 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
1848 return -EINVAL;
1850 atomic_inc(&id_priv->refcount);
1851 switch (rdma_node_get_transport(id->device->node_type)) {
1852 case RDMA_TRANSPORT_IB:
1853 switch (rdma_port_get_link_layer(id->device, id->port_num)) {
1854 case IB_LINK_LAYER_INFINIBAND:
1855 ret = cma_resolve_ib_route(id_priv, timeout_ms);
1856 break;
1857 case IB_LINK_LAYER_ETHERNET:
1858 ret = cma_resolve_iboe_route(id_priv);
1859 break;
1860 default:
1861 ret = -ENOSYS;
1863 break;
1864 case RDMA_TRANSPORT_IWARP:
1865 ret = cma_resolve_iw_route(id_priv, timeout_ms);
1866 break;
1867 default:
1868 ret = -ENOSYS;
1869 break;
1871 if (ret)
1872 goto err;
1874 return 0;
1875 err:
1876 cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
1877 cma_deref_id(id_priv);
1878 return ret;
1880 EXPORT_SYMBOL(rdma_resolve_route);
1882 static int cma_bind_loopback(struct rdma_id_private *id_priv)
1884 struct cma_device *cma_dev;
1885 struct ib_port_attr port_attr;
1886 union ib_gid gid;
1887 u16 pkey;
1888 int ret;
1889 u8 p;
1891 mutex_lock(&lock);
1892 if (list_empty(&dev_list)) {
1893 ret = -ENODEV;
1894 goto out;
1896 list_for_each_entry(cma_dev, &dev_list, list)
1897 for (p = 1; p <= cma_dev->device->phys_port_cnt; ++p)
1898 if (!ib_query_port(cma_dev->device, p, &port_attr) &&
1899 port_attr.state == IB_PORT_ACTIVE)
1900 goto port_found;
1902 p = 1;
1903 cma_dev = list_entry(dev_list.next, struct cma_device, list);
1905 port_found:
1906 ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid);
1907 if (ret)
1908 goto out;
1910 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
1911 if (ret)
1912 goto out;
1914 id_priv->id.route.addr.dev_addr.dev_type =
1915 (rdma_port_get_link_layer(cma_dev->device, p) == IB_LINK_LAYER_INFINIBAND) ?
1916 ARPHRD_INFINIBAND : ARPHRD_ETHER;
1918 rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
1919 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
1920 id_priv->id.port_num = p;
1921 cma_attach_to_dev(id_priv, cma_dev);
1922 out:
1923 mutex_unlock(&lock);
1924 return ret;
1927 static void addr_handler(int status, struct sockaddr *src_addr,
1928 struct rdma_dev_addr *dev_addr, void *context)
1930 struct rdma_id_private *id_priv = context;
1931 struct rdma_cm_event event;
1933 memset(&event, 0, sizeof event);
1934 mutex_lock(&id_priv->handler_mutex);
1935 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
1936 RDMA_CM_ADDR_RESOLVED))
1937 goto out;
1939 if (!status && !id_priv->cma_dev)
1940 status = cma_acquire_dev(id_priv);
1942 if (status) {
1943 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
1944 RDMA_CM_ADDR_BOUND))
1945 goto out;
1946 event.event = RDMA_CM_EVENT_ADDR_ERROR;
1947 event.status = status;
1948 } else {
1949 memcpy(&id_priv->id.route.addr.src_addr, src_addr,
1950 ip_addr_size(src_addr));
1951 event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
1954 if (id_priv->id.event_handler(&id_priv->id, &event)) {
1955 cma_exch(id_priv, RDMA_CM_DESTROYING);
1956 mutex_unlock(&id_priv->handler_mutex);
1957 cma_deref_id(id_priv);
1958 rdma_destroy_id(&id_priv->id);
1959 return;
1961 out:
1962 mutex_unlock(&id_priv->handler_mutex);
1963 cma_deref_id(id_priv);
1966 static int cma_resolve_loopback(struct rdma_id_private *id_priv)
1968 struct cma_work *work;
1969 struct sockaddr *src, *dst;
1970 union ib_gid gid;
1971 int ret;
1973 work = kzalloc(sizeof *work, GFP_KERNEL);
1974 if (!work)
1975 return -ENOMEM;
1977 if (!id_priv->cma_dev) {
1978 ret = cma_bind_loopback(id_priv);
1979 if (ret)
1980 goto err;
1983 rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
1984 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
1986 src = (struct sockaddr *) &id_priv->id.route.addr.src_addr;
1987 if (cma_zero_addr(src)) {
1988 dst = (struct sockaddr *) &id_priv->id.route.addr.dst_addr;
1989 if ((src->sa_family = dst->sa_family) == AF_INET) {
1990 ((struct sockaddr_in *) src)->sin_addr.s_addr =
1991 ((struct sockaddr_in *) dst)->sin_addr.s_addr;
1992 } else {
1993 ipv6_addr_copy(&((struct sockaddr_in6 *) src)->sin6_addr,
1994 &((struct sockaddr_in6 *) dst)->sin6_addr);
1998 work->id = id_priv;
1999 INIT_WORK(&work->work, cma_work_handler);
2000 work->old_state = RDMA_CM_ADDR_QUERY;
2001 work->new_state = RDMA_CM_ADDR_RESOLVED;
2002 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2003 queue_work(cma_wq, &work->work);
2004 return 0;
2005 err:
2006 kfree(work);
2007 return ret;
2010 static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2011 struct sockaddr *dst_addr)
2013 if (!src_addr || !src_addr->sa_family) {
2014 src_addr = (struct sockaddr *) &id->route.addr.src_addr;
2015 if ((src_addr->sa_family = dst_addr->sa_family) == AF_INET6) {
2016 ((struct sockaddr_in6 *) src_addr)->sin6_scope_id =
2017 ((struct sockaddr_in6 *) dst_addr)->sin6_scope_id;
2020 return rdma_bind_addr(id, src_addr);
2023 int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2024 struct sockaddr *dst_addr, int timeout_ms)
2026 struct rdma_id_private *id_priv;
2027 int ret;
2029 id_priv = container_of(id, struct rdma_id_private, id);
2030 if (id_priv->state == RDMA_CM_IDLE) {
2031 ret = cma_bind_addr(id, src_addr, dst_addr);
2032 if (ret)
2033 return ret;
2036 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY))
2037 return -EINVAL;
2039 atomic_inc(&id_priv->refcount);
2040 memcpy(&id->route.addr.dst_addr, dst_addr, ip_addr_size(dst_addr));
2041 if (cma_any_addr(dst_addr))
2042 ret = cma_resolve_loopback(id_priv);
2043 else
2044 ret = rdma_resolve_ip(&addr_client, (struct sockaddr *) &id->route.addr.src_addr,
2045 dst_addr, &id->route.addr.dev_addr,
2046 timeout_ms, addr_handler, id_priv);
2047 if (ret)
2048 goto err;
2050 return 0;
2051 err:
2052 cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
2053 cma_deref_id(id_priv);
2054 return ret;
2056 EXPORT_SYMBOL(rdma_resolve_addr);
2058 int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
2060 struct rdma_id_private *id_priv;
2061 unsigned long flags;
2062 int ret;
2064 id_priv = container_of(id, struct rdma_id_private, id);
2065 spin_lock_irqsave(&id_priv->lock, flags);
2066 if (id_priv->state == RDMA_CM_IDLE) {
2067 id_priv->reuseaddr = reuse;
2068 ret = 0;
2069 } else {
2070 ret = -EINVAL;
2072 spin_unlock_irqrestore(&id_priv->lock, flags);
2073 return ret;
2075 EXPORT_SYMBOL(rdma_set_reuseaddr);
2077 static void cma_bind_port(struct rdma_bind_list *bind_list,
2078 struct rdma_id_private *id_priv)
2080 struct sockaddr_in *sin;
2082 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
2083 sin->sin_port = htons(bind_list->port);
2084 id_priv->bind_list = bind_list;
2085 hlist_add_head(&id_priv->node, &bind_list->owners);
2088 static int cma_alloc_port(struct idr *ps, struct rdma_id_private *id_priv,
2089 unsigned short snum)
2091 struct rdma_bind_list *bind_list;
2092 int port, ret;
2094 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
2095 if (!bind_list)
2096 return -ENOMEM;
2098 do {
2099 ret = idr_get_new_above(ps, bind_list, snum, &port);
2100 } while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL));
2102 if (ret)
2103 goto err1;
2105 if (port != snum) {
2106 ret = -EADDRNOTAVAIL;
2107 goto err2;
2110 bind_list->ps = ps;
2111 bind_list->port = (unsigned short) port;
2112 cma_bind_port(bind_list, id_priv);
2113 return 0;
2114 err2:
2115 idr_remove(ps, port);
2116 err1:
2117 kfree(bind_list);
2118 return ret;
2121 static int cma_alloc_any_port(struct idr *ps, struct rdma_id_private *id_priv)
2123 static unsigned int last_used_port;
2124 int low, high, remaining;
2125 unsigned int rover;
2127 inet_get_local_port_range(&low, &high);
2128 remaining = (high - low) + 1;
2129 rover = net_random() % remaining + low;
2130 retry:
2131 if (last_used_port != rover &&
2132 !idr_find(ps, (unsigned short) rover)) {
2133 int ret = cma_alloc_port(ps, id_priv, rover);
2135 * Remember previously used port number in order to avoid
2136 * re-using same port immediately after it is closed.
2138 if (!ret)
2139 last_used_port = rover;
2140 if (ret != -EADDRNOTAVAIL)
2141 return ret;
2143 if (--remaining) {
2144 rover++;
2145 if ((rover < low) || (rover > high))
2146 rover = low;
2147 goto retry;
2149 return -EADDRNOTAVAIL;
2153 * Check that the requested port is available. This is called when trying to
2154 * bind to a specific port, or when trying to listen on a bound port. In
2155 * the latter case, the provided id_priv may already be on the bind_list, but
2156 * we still need to check that it's okay to start listening.
2158 static int cma_check_port(struct rdma_bind_list *bind_list,
2159 struct rdma_id_private *id_priv, uint8_t reuseaddr)
2161 struct rdma_id_private *cur_id;
2162 struct sockaddr *addr, *cur_addr;
2163 struct hlist_node *node;
2165 addr = (struct sockaddr *) &id_priv->id.route.addr.src_addr;
2166 if (cma_any_addr(addr) && !reuseaddr)
2167 return -EADDRNOTAVAIL;
2169 hlist_for_each_entry(cur_id, node, &bind_list->owners, node) {
2170 if (id_priv == cur_id)
2171 continue;
2173 if ((cur_id->state == RDMA_CM_LISTEN) ||
2174 !reuseaddr || !cur_id->reuseaddr) {
2175 cur_addr = (struct sockaddr *) &cur_id->id.route.addr.src_addr;
2176 if (cma_any_addr(cur_addr))
2177 return -EADDRNOTAVAIL;
2179 if (!cma_addr_cmp(addr, cur_addr))
2180 return -EADDRINUSE;
2183 return 0;
2186 static int cma_use_port(struct idr *ps, struct rdma_id_private *id_priv)
2188 struct rdma_bind_list *bind_list;
2189 unsigned short snum;
2190 int ret;
2192 snum = ntohs(cma_port((struct sockaddr *) &id_priv->id.route.addr.src_addr));
2193 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
2194 return -EACCES;
2196 bind_list = idr_find(ps, snum);
2197 if (!bind_list) {
2198 ret = cma_alloc_port(ps, id_priv, snum);
2199 } else {
2200 ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
2201 if (!ret)
2202 cma_bind_port(bind_list, id_priv);
2204 return ret;
2207 static int cma_bind_listen(struct rdma_id_private *id_priv)
2209 struct rdma_bind_list *bind_list = id_priv->bind_list;
2210 int ret = 0;
2212 mutex_lock(&lock);
2213 if (bind_list->owners.first->next)
2214 ret = cma_check_port(bind_list, id_priv, 0);
2215 mutex_unlock(&lock);
2216 return ret;
2219 static int cma_get_port(struct rdma_id_private *id_priv)
2221 struct idr *ps;
2222 int ret;
2224 switch (id_priv->id.ps) {
2225 case RDMA_PS_SDP:
2226 ps = &sdp_ps;
2227 break;
2228 case RDMA_PS_TCP:
2229 ps = &tcp_ps;
2230 break;
2231 case RDMA_PS_UDP:
2232 ps = &udp_ps;
2233 break;
2234 case RDMA_PS_IPOIB:
2235 ps = &ipoib_ps;
2236 break;
2237 default:
2238 return -EPROTONOSUPPORT;
2241 mutex_lock(&lock);
2242 if (cma_any_port((struct sockaddr *) &id_priv->id.route.addr.src_addr))
2243 ret = cma_alloc_any_port(ps, id_priv);
2244 else
2245 ret = cma_use_port(ps, id_priv);
2246 mutex_unlock(&lock);
2248 return ret;
2251 static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
2252 struct sockaddr *addr)
2254 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2255 struct sockaddr_in6 *sin6;
2257 if (addr->sa_family != AF_INET6)
2258 return 0;
2260 sin6 = (struct sockaddr_in6 *) addr;
2261 if ((ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) &&
2262 !sin6->sin6_scope_id)
2263 return -EINVAL;
2265 dev_addr->bound_dev_if = sin6->sin6_scope_id;
2266 #endif
2267 return 0;
2270 int rdma_listen(struct rdma_cm_id *id, int backlog)
2272 struct rdma_id_private *id_priv;
2273 int ret;
2275 id_priv = container_of(id, struct rdma_id_private, id);
2276 if (id_priv->state == RDMA_CM_IDLE) {
2277 ((struct sockaddr *) &id->route.addr.src_addr)->sa_family = AF_INET;
2278 ret = rdma_bind_addr(id, (struct sockaddr *) &id->route.addr.src_addr);
2279 if (ret)
2280 return ret;
2283 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN))
2284 return -EINVAL;
2286 if (id_priv->reuseaddr) {
2287 ret = cma_bind_listen(id_priv);
2288 if (ret)
2289 goto err;
2292 id_priv->backlog = backlog;
2293 if (id->device) {
2294 switch (rdma_node_get_transport(id->device->node_type)) {
2295 case RDMA_TRANSPORT_IB:
2296 ret = cma_ib_listen(id_priv);
2297 if (ret)
2298 goto err;
2299 break;
2300 case RDMA_TRANSPORT_IWARP:
2301 ret = cma_iw_listen(id_priv, backlog);
2302 if (ret)
2303 goto err;
2304 break;
2305 default:
2306 ret = -ENOSYS;
2307 goto err;
2309 } else
2310 cma_listen_on_all(id_priv);
2312 return 0;
2313 err:
2314 id_priv->backlog = 0;
2315 cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
2316 return ret;
2318 EXPORT_SYMBOL(rdma_listen);
2320 int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
2322 struct rdma_id_private *id_priv;
2323 int ret;
2325 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
2326 return -EAFNOSUPPORT;
2328 id_priv = container_of(id, struct rdma_id_private, id);
2329 if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
2330 return -EINVAL;
2332 ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
2333 if (ret)
2334 goto err1;
2336 if (!cma_any_addr(addr)) {
2337 ret = rdma_translate_ip(addr, &id->route.addr.dev_addr);
2338 if (ret)
2339 goto err1;
2341 ret = cma_acquire_dev(id_priv);
2342 if (ret)
2343 goto err1;
2346 memcpy(&id->route.addr.src_addr, addr, ip_addr_size(addr));
2347 ret = cma_get_port(id_priv);
2348 if (ret)
2349 goto err2;
2351 return 0;
2352 err2:
2353 if (id_priv->cma_dev)
2354 cma_release_dev(id_priv);
2355 err1:
2356 cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
2357 return ret;
2359 EXPORT_SYMBOL(rdma_bind_addr);
2361 static int cma_format_hdr(void *hdr, enum rdma_port_space ps,
2362 struct rdma_route *route)
2364 struct cma_hdr *cma_hdr;
2365 struct sdp_hh *sdp_hdr;
2367 if (route->addr.src_addr.ss_family == AF_INET) {
2368 struct sockaddr_in *src4, *dst4;
2370 src4 = (struct sockaddr_in *) &route->addr.src_addr;
2371 dst4 = (struct sockaddr_in *) &route->addr.dst_addr;
2373 switch (ps) {
2374 case RDMA_PS_SDP:
2375 sdp_hdr = hdr;
2376 if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION)
2377 return -EINVAL;
2378 sdp_set_ip_ver(sdp_hdr, 4);
2379 sdp_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
2380 sdp_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
2381 sdp_hdr->port = src4->sin_port;
2382 break;
2383 default:
2384 cma_hdr = hdr;
2385 cma_hdr->cma_version = CMA_VERSION;
2386 cma_set_ip_ver(cma_hdr, 4);
2387 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
2388 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
2389 cma_hdr->port = src4->sin_port;
2390 break;
2392 } else {
2393 struct sockaddr_in6 *src6, *dst6;
2395 src6 = (struct sockaddr_in6 *) &route->addr.src_addr;
2396 dst6 = (struct sockaddr_in6 *) &route->addr.dst_addr;
2398 switch (ps) {
2399 case RDMA_PS_SDP:
2400 sdp_hdr = hdr;
2401 if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION)
2402 return -EINVAL;
2403 sdp_set_ip_ver(sdp_hdr, 6);
2404 sdp_hdr->src_addr.ip6 = src6->sin6_addr;
2405 sdp_hdr->dst_addr.ip6 = dst6->sin6_addr;
2406 sdp_hdr->port = src6->sin6_port;
2407 break;
2408 default:
2409 cma_hdr = hdr;
2410 cma_hdr->cma_version = CMA_VERSION;
2411 cma_set_ip_ver(cma_hdr, 6);
2412 cma_hdr->src_addr.ip6 = src6->sin6_addr;
2413 cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
2414 cma_hdr->port = src6->sin6_port;
2415 break;
2418 return 0;
2421 static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
2422 struct ib_cm_event *ib_event)
2424 struct rdma_id_private *id_priv = cm_id->context;
2425 struct rdma_cm_event event;
2426 struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
2427 int ret = 0;
2429 if (cma_disable_callback(id_priv, RDMA_CM_CONNECT))
2430 return 0;
2432 memset(&event, 0, sizeof event);
2433 switch (ib_event->event) {
2434 case IB_CM_SIDR_REQ_ERROR:
2435 event.event = RDMA_CM_EVENT_UNREACHABLE;
2436 event.status = -ETIMEDOUT;
2437 break;
2438 case IB_CM_SIDR_REP_RECEIVED:
2439 event.param.ud.private_data = ib_event->private_data;
2440 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
2441 if (rep->status != IB_SIDR_SUCCESS) {
2442 event.event = RDMA_CM_EVENT_UNREACHABLE;
2443 event.status = ib_event->param.sidr_rep_rcvd.status;
2444 break;
2446 ret = cma_set_qkey(id_priv);
2447 if (ret) {
2448 event.event = RDMA_CM_EVENT_ADDR_ERROR;
2449 event.status = -EINVAL;
2450 break;
2452 if (id_priv->qkey != rep->qkey) {
2453 event.event = RDMA_CM_EVENT_UNREACHABLE;
2454 event.status = -EINVAL;
2455 break;
2457 ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
2458 id_priv->id.route.path_rec,
2459 &event.param.ud.ah_attr);
2460 event.param.ud.qp_num = rep->qpn;
2461 event.param.ud.qkey = rep->qkey;
2462 event.event = RDMA_CM_EVENT_ESTABLISHED;
2463 event.status = 0;
2464 break;
2465 default:
2466 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
2467 ib_event->event);
2468 goto out;
2471 ret = id_priv->id.event_handler(&id_priv->id, &event);
2472 if (ret) {
2473 /* Destroy the CM ID by returning a non-zero value. */
2474 id_priv->cm_id.ib = NULL;
2475 cma_exch(id_priv, RDMA_CM_DESTROYING);
2476 mutex_unlock(&id_priv->handler_mutex);
2477 rdma_destroy_id(&id_priv->id);
2478 return ret;
2480 out:
2481 mutex_unlock(&id_priv->handler_mutex);
2482 return ret;
2485 static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
2486 struct rdma_conn_param *conn_param)
2488 struct ib_cm_sidr_req_param req;
2489 struct rdma_route *route;
2490 struct ib_cm_id *id;
2491 int ret;
2493 req.private_data_len = sizeof(struct cma_hdr) +
2494 conn_param->private_data_len;
2495 req.private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2496 if (!req.private_data)
2497 return -ENOMEM;
2499 if (conn_param->private_data && conn_param->private_data_len)
2500 memcpy((void *) req.private_data + sizeof(struct cma_hdr),
2501 conn_param->private_data, conn_param->private_data_len);
2503 route = &id_priv->id.route;
2504 ret = cma_format_hdr((void *) req.private_data, id_priv->id.ps, route);
2505 if (ret)
2506 goto out;
2508 id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
2509 id_priv);
2510 if (IS_ERR(id)) {
2511 ret = PTR_ERR(id);
2512 goto out;
2514 id_priv->cm_id.ib = id;
2516 req.path = route->path_rec;
2517 req.service_id = cma_get_service_id(id_priv->id.ps,
2518 (struct sockaddr *) &route->addr.dst_addr);
2519 req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
2520 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2522 ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
2523 if (ret) {
2524 ib_destroy_cm_id(id_priv->cm_id.ib);
2525 id_priv->cm_id.ib = NULL;
2527 out:
2528 kfree(req.private_data);
2529 return ret;
2532 static int cma_connect_ib(struct rdma_id_private *id_priv,
2533 struct rdma_conn_param *conn_param)
2535 struct ib_cm_req_param req;
2536 struct rdma_route *route;
2537 void *private_data;
2538 struct ib_cm_id *id;
2539 int offset, ret;
2541 memset(&req, 0, sizeof req);
2542 offset = cma_user_data_offset(id_priv->id.ps);
2543 req.private_data_len = offset + conn_param->private_data_len;
2544 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2545 if (!private_data)
2546 return -ENOMEM;
2548 if (conn_param->private_data && conn_param->private_data_len)
2549 memcpy(private_data + offset, conn_param->private_data,
2550 conn_param->private_data_len);
2552 id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
2553 if (IS_ERR(id)) {
2554 ret = PTR_ERR(id);
2555 goto out;
2557 id_priv->cm_id.ib = id;
2559 route = &id_priv->id.route;
2560 ret = cma_format_hdr(private_data, id_priv->id.ps, route);
2561 if (ret)
2562 goto out;
2563 req.private_data = private_data;
2565 req.primary_path = &route->path_rec[0];
2566 if (route->num_paths == 2)
2567 req.alternate_path = &route->path_rec[1];
2569 req.service_id = cma_get_service_id(id_priv->id.ps,
2570 (struct sockaddr *) &route->addr.dst_addr);
2571 req.qp_num = id_priv->qp_num;
2572 req.qp_type = IB_QPT_RC;
2573 req.starting_psn = id_priv->seq_num;
2574 req.responder_resources = conn_param->responder_resources;
2575 req.initiator_depth = conn_param->initiator_depth;
2576 req.flow_control = conn_param->flow_control;
2577 req.retry_count = conn_param->retry_count;
2578 req.rnr_retry_count = conn_param->rnr_retry_count;
2579 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2580 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2581 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2582 req.srq = id_priv->srq ? 1 : 0;
2584 ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
2585 out:
2586 if (ret && !IS_ERR(id)) {
2587 ib_destroy_cm_id(id);
2588 id_priv->cm_id.ib = NULL;
2591 kfree(private_data);
2592 return ret;
2595 static int cma_connect_iw(struct rdma_id_private *id_priv,
2596 struct rdma_conn_param *conn_param)
2598 struct iw_cm_id *cm_id;
2599 struct sockaddr_in* sin;
2600 int ret;
2601 struct iw_cm_conn_param iw_param;
2603 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
2604 if (IS_ERR(cm_id))
2605 return PTR_ERR(cm_id);
2607 id_priv->cm_id.iw = cm_id;
2609 sin = (struct sockaddr_in*) &id_priv->id.route.addr.src_addr;
2610 cm_id->local_addr = *sin;
2612 sin = (struct sockaddr_in*) &id_priv->id.route.addr.dst_addr;
2613 cm_id->remote_addr = *sin;
2615 ret = cma_modify_qp_rtr(id_priv, conn_param);
2616 if (ret)
2617 goto out;
2619 iw_param.ord = conn_param->initiator_depth;
2620 iw_param.ird = conn_param->responder_resources;
2621 iw_param.private_data = conn_param->private_data;
2622 iw_param.private_data_len = conn_param->private_data_len;
2623 if (id_priv->id.qp)
2624 iw_param.qpn = id_priv->qp_num;
2625 else
2626 iw_param.qpn = conn_param->qp_num;
2627 ret = iw_cm_connect(cm_id, &iw_param);
2628 out:
2629 if (ret) {
2630 iw_destroy_cm_id(cm_id);
2631 id_priv->cm_id.iw = NULL;
2633 return ret;
2636 int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2638 struct rdma_id_private *id_priv;
2639 int ret;
2641 id_priv = container_of(id, struct rdma_id_private, id);
2642 if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
2643 return -EINVAL;
2645 if (!id->qp) {
2646 id_priv->qp_num = conn_param->qp_num;
2647 id_priv->srq = conn_param->srq;
2650 switch (rdma_node_get_transport(id->device->node_type)) {
2651 case RDMA_TRANSPORT_IB:
2652 if (id->qp_type == IB_QPT_UD)
2653 ret = cma_resolve_ib_udp(id_priv, conn_param);
2654 else
2655 ret = cma_connect_ib(id_priv, conn_param);
2656 break;
2657 case RDMA_TRANSPORT_IWARP:
2658 ret = cma_connect_iw(id_priv, conn_param);
2659 break;
2660 default:
2661 ret = -ENOSYS;
2662 break;
2664 if (ret)
2665 goto err;
2667 return 0;
2668 err:
2669 cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
2670 return ret;
2672 EXPORT_SYMBOL(rdma_connect);
2674 static int cma_accept_ib(struct rdma_id_private *id_priv,
2675 struct rdma_conn_param *conn_param)
2677 struct ib_cm_rep_param rep;
2678 int ret;
2680 ret = cma_modify_qp_rtr(id_priv, conn_param);
2681 if (ret)
2682 goto out;
2684 ret = cma_modify_qp_rts(id_priv, conn_param);
2685 if (ret)
2686 goto out;
2688 memset(&rep, 0, sizeof rep);
2689 rep.qp_num = id_priv->qp_num;
2690 rep.starting_psn = id_priv->seq_num;
2691 rep.private_data = conn_param->private_data;
2692 rep.private_data_len = conn_param->private_data_len;
2693 rep.responder_resources = conn_param->responder_resources;
2694 rep.initiator_depth = conn_param->initiator_depth;
2695 rep.failover_accepted = 0;
2696 rep.flow_control = conn_param->flow_control;
2697 rep.rnr_retry_count = conn_param->rnr_retry_count;
2698 rep.srq = id_priv->srq ? 1 : 0;
2700 ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
2701 out:
2702 return ret;
2705 static int cma_accept_iw(struct rdma_id_private *id_priv,
2706 struct rdma_conn_param *conn_param)
2708 struct iw_cm_conn_param iw_param;
2709 int ret;
2711 ret = cma_modify_qp_rtr(id_priv, conn_param);
2712 if (ret)
2713 return ret;
2715 iw_param.ord = conn_param->initiator_depth;
2716 iw_param.ird = conn_param->responder_resources;
2717 iw_param.private_data = conn_param->private_data;
2718 iw_param.private_data_len = conn_param->private_data_len;
2719 if (id_priv->id.qp) {
2720 iw_param.qpn = id_priv->qp_num;
2721 } else
2722 iw_param.qpn = conn_param->qp_num;
2724 return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
2727 static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
2728 enum ib_cm_sidr_status status,
2729 const void *private_data, int private_data_len)
2731 struct ib_cm_sidr_rep_param rep;
2732 int ret;
2734 memset(&rep, 0, sizeof rep);
2735 rep.status = status;
2736 if (status == IB_SIDR_SUCCESS) {
2737 ret = cma_set_qkey(id_priv);
2738 if (ret)
2739 return ret;
2740 rep.qp_num = id_priv->qp_num;
2741 rep.qkey = id_priv->qkey;
2743 rep.private_data = private_data;
2744 rep.private_data_len = private_data_len;
2746 return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
2749 int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2751 struct rdma_id_private *id_priv;
2752 int ret;
2754 id_priv = container_of(id, struct rdma_id_private, id);
2756 id_priv->owner = task_pid_nr(current);
2758 if (!cma_comp(id_priv, RDMA_CM_CONNECT))
2759 return -EINVAL;
2761 if (!id->qp && conn_param) {
2762 id_priv->qp_num = conn_param->qp_num;
2763 id_priv->srq = conn_param->srq;
2766 switch (rdma_node_get_transport(id->device->node_type)) {
2767 case RDMA_TRANSPORT_IB:
2768 if (id->qp_type == IB_QPT_UD)
2769 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
2770 conn_param->private_data,
2771 conn_param->private_data_len);
2772 else if (conn_param)
2773 ret = cma_accept_ib(id_priv, conn_param);
2774 else
2775 ret = cma_rep_recv(id_priv);
2776 break;
2777 case RDMA_TRANSPORT_IWARP:
2778 ret = cma_accept_iw(id_priv, conn_param);
2779 break;
2780 default:
2781 ret = -ENOSYS;
2782 break;
2785 if (ret)
2786 goto reject;
2788 return 0;
2789 reject:
2790 cma_modify_qp_err(id_priv);
2791 rdma_reject(id, NULL, 0);
2792 return ret;
2794 EXPORT_SYMBOL(rdma_accept);
2796 int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
2798 struct rdma_id_private *id_priv;
2799 int ret;
2801 id_priv = container_of(id, struct rdma_id_private, id);
2802 if (!id_priv->cm_id.ib)
2803 return -EINVAL;
2805 switch (id->device->node_type) {
2806 case RDMA_NODE_IB_CA:
2807 ret = ib_cm_notify(id_priv->cm_id.ib, event);
2808 break;
2809 default:
2810 ret = 0;
2811 break;
2813 return ret;
2815 EXPORT_SYMBOL(rdma_notify);
2817 int rdma_reject(struct rdma_cm_id *id, const void *private_data,
2818 u8 private_data_len)
2820 struct rdma_id_private *id_priv;
2821 int ret;
2823 id_priv = container_of(id, struct rdma_id_private, id);
2824 if (!id_priv->cm_id.ib)
2825 return -EINVAL;
2827 switch (rdma_node_get_transport(id->device->node_type)) {
2828 case RDMA_TRANSPORT_IB:
2829 if (id->qp_type == IB_QPT_UD)
2830 ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT,
2831 private_data, private_data_len);
2832 else
2833 ret = ib_send_cm_rej(id_priv->cm_id.ib,
2834 IB_CM_REJ_CONSUMER_DEFINED, NULL,
2835 0, private_data, private_data_len);
2836 break;
2837 case RDMA_TRANSPORT_IWARP:
2838 ret = iw_cm_reject(id_priv->cm_id.iw,
2839 private_data, private_data_len);
2840 break;
2841 default:
2842 ret = -ENOSYS;
2843 break;
2845 return ret;
2847 EXPORT_SYMBOL(rdma_reject);
2849 int rdma_disconnect(struct rdma_cm_id *id)
2851 struct rdma_id_private *id_priv;
2852 int ret;
2854 id_priv = container_of(id, struct rdma_id_private, id);
2855 if (!id_priv->cm_id.ib)
2856 return -EINVAL;
2858 switch (rdma_node_get_transport(id->device->node_type)) {
2859 case RDMA_TRANSPORT_IB:
2860 ret = cma_modify_qp_err(id_priv);
2861 if (ret)
2862 goto out;
2863 /* Initiate or respond to a disconnect. */
2864 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
2865 ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
2866 break;
2867 case RDMA_TRANSPORT_IWARP:
2868 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
2869 break;
2870 default:
2871 ret = -EINVAL;
2872 break;
2874 out:
2875 return ret;
2877 EXPORT_SYMBOL(rdma_disconnect);
2879 static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
2881 struct rdma_id_private *id_priv;
2882 struct cma_multicast *mc = multicast->context;
2883 struct rdma_cm_event event;
2884 int ret;
2886 id_priv = mc->id_priv;
2887 if (cma_disable_callback(id_priv, RDMA_CM_ADDR_BOUND) &&
2888 cma_disable_callback(id_priv, RDMA_CM_ADDR_RESOLVED))
2889 return 0;
2891 mutex_lock(&id_priv->qp_mutex);
2892 if (!status && id_priv->id.qp)
2893 status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
2894 multicast->rec.mlid);
2895 mutex_unlock(&id_priv->qp_mutex);
2897 memset(&event, 0, sizeof event);
2898 event.status = status;
2899 event.param.ud.private_data = mc->context;
2900 if (!status) {
2901 event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
2902 ib_init_ah_from_mcmember(id_priv->id.device,
2903 id_priv->id.port_num, &multicast->rec,
2904 &event.param.ud.ah_attr);
2905 event.param.ud.qp_num = 0xFFFFFF;
2906 event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
2907 } else
2908 event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
2910 ret = id_priv->id.event_handler(&id_priv->id, &event);
2911 if (ret) {
2912 cma_exch(id_priv, RDMA_CM_DESTROYING);
2913 mutex_unlock(&id_priv->handler_mutex);
2914 rdma_destroy_id(&id_priv->id);
2915 return 0;
2918 mutex_unlock(&id_priv->handler_mutex);
2919 return 0;
2922 static void cma_set_mgid(struct rdma_id_private *id_priv,
2923 struct sockaddr *addr, union ib_gid *mgid)
2925 unsigned char mc_map[MAX_ADDR_LEN];
2926 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2927 struct sockaddr_in *sin = (struct sockaddr_in *) addr;
2928 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
2930 if (cma_any_addr(addr)) {
2931 memset(mgid, 0, sizeof *mgid);
2932 } else if ((addr->sa_family == AF_INET6) &&
2933 ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
2934 0xFF10A01B)) {
2935 /* IPv6 address is an SA assigned MGID. */
2936 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
2937 } else if ((addr->sa_family == AF_INET6)) {
2938 ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
2939 if (id_priv->id.ps == RDMA_PS_UDP)
2940 mc_map[7] = 0x01; /* Use RDMA CM signature */
2941 *mgid = *(union ib_gid *) (mc_map + 4);
2942 } else {
2943 ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
2944 if (id_priv->id.ps == RDMA_PS_UDP)
2945 mc_map[7] = 0x01; /* Use RDMA CM signature */
2946 *mgid = *(union ib_gid *) (mc_map + 4);
2950 static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
2951 struct cma_multicast *mc)
2953 struct ib_sa_mcmember_rec rec;
2954 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2955 ib_sa_comp_mask comp_mask;
2956 int ret;
2958 ib_addr_get_mgid(dev_addr, &rec.mgid);
2959 ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
2960 &rec.mgid, &rec);
2961 if (ret)
2962 return ret;
2964 cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
2965 if (id_priv->id.ps == RDMA_PS_UDP)
2966 rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
2967 rdma_addr_get_sgid(dev_addr, &rec.port_gid);
2968 rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
2969 rec.join_state = 1;
2971 comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
2972 IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
2973 IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
2974 IB_SA_MCMEMBER_REC_FLOW_LABEL |
2975 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
2977 if (id_priv->id.ps == RDMA_PS_IPOIB)
2978 comp_mask |= IB_SA_MCMEMBER_REC_RATE |
2979 IB_SA_MCMEMBER_REC_RATE_SELECTOR;
2981 mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
2982 id_priv->id.port_num, &rec,
2983 comp_mask, GFP_KERNEL,
2984 cma_ib_mc_handler, mc);
2985 if (IS_ERR(mc->multicast.ib))
2986 return PTR_ERR(mc->multicast.ib);
2988 return 0;
2991 static void iboe_mcast_work_handler(struct work_struct *work)
2993 struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work);
2994 struct cma_multicast *mc = mw->mc;
2995 struct ib_sa_multicast *m = mc->multicast.ib;
2997 mc->multicast.ib->context = mc;
2998 cma_ib_mc_handler(0, m);
2999 kref_put(&mc->mcref, release_mc);
3000 kfree(mw);
3003 static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid)
3005 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
3006 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
3008 if (cma_any_addr(addr)) {
3009 memset(mgid, 0, sizeof *mgid);
3010 } else if (addr->sa_family == AF_INET6) {
3011 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
3012 } else {
3013 mgid->raw[0] = 0xff;
3014 mgid->raw[1] = 0x0e;
3015 mgid->raw[2] = 0;
3016 mgid->raw[3] = 0;
3017 mgid->raw[4] = 0;
3018 mgid->raw[5] = 0;
3019 mgid->raw[6] = 0;
3020 mgid->raw[7] = 0;
3021 mgid->raw[8] = 0;
3022 mgid->raw[9] = 0;
3023 mgid->raw[10] = 0xff;
3024 mgid->raw[11] = 0xff;
3025 *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
3029 static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
3030 struct cma_multicast *mc)
3032 struct iboe_mcast_work *work;
3033 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3034 int err;
3035 struct sockaddr *addr = (struct sockaddr *)&mc->addr;
3036 struct net_device *ndev = NULL;
3038 if (cma_zero_addr((struct sockaddr *)&mc->addr))
3039 return -EINVAL;
3041 work = kzalloc(sizeof *work, GFP_KERNEL);
3042 if (!work)
3043 return -ENOMEM;
3045 mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL);
3046 if (!mc->multicast.ib) {
3047 err = -ENOMEM;
3048 goto out1;
3051 cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid);
3053 mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff);
3054 if (id_priv->id.ps == RDMA_PS_UDP)
3055 mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
3057 if (dev_addr->bound_dev_if)
3058 ndev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
3059 if (!ndev) {
3060 err = -ENODEV;
3061 goto out2;
3063 mc->multicast.ib->rec.rate = iboe_get_rate(ndev);
3064 mc->multicast.ib->rec.hop_limit = 1;
3065 mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu);
3066 dev_put(ndev);
3067 if (!mc->multicast.ib->rec.mtu) {
3068 err = -EINVAL;
3069 goto out2;
3071 iboe_addr_get_sgid(dev_addr, &mc->multicast.ib->rec.port_gid);
3072 work->id = id_priv;
3073 work->mc = mc;
3074 INIT_WORK(&work->work, iboe_mcast_work_handler);
3075 kref_get(&mc->mcref);
3076 queue_work(cma_wq, &work->work);
3078 return 0;
3080 out2:
3081 kfree(mc->multicast.ib);
3082 out1:
3083 kfree(work);
3084 return err;
3087 int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
3088 void *context)
3090 struct rdma_id_private *id_priv;
3091 struct cma_multicast *mc;
3092 int ret;
3094 id_priv = container_of(id, struct rdma_id_private, id);
3095 if (!cma_comp(id_priv, RDMA_CM_ADDR_BOUND) &&
3096 !cma_comp(id_priv, RDMA_CM_ADDR_RESOLVED))
3097 return -EINVAL;
3099 mc = kmalloc(sizeof *mc, GFP_KERNEL);
3100 if (!mc)
3101 return -ENOMEM;
3103 memcpy(&mc->addr, addr, ip_addr_size(addr));
3104 mc->context = context;
3105 mc->id_priv = id_priv;
3107 spin_lock(&id_priv->lock);
3108 list_add(&mc->list, &id_priv->mc_list);
3109 spin_unlock(&id_priv->lock);
3111 switch (rdma_node_get_transport(id->device->node_type)) {
3112 case RDMA_TRANSPORT_IB:
3113 switch (rdma_port_get_link_layer(id->device, id->port_num)) {
3114 case IB_LINK_LAYER_INFINIBAND:
3115 ret = cma_join_ib_multicast(id_priv, mc);
3116 break;
3117 case IB_LINK_LAYER_ETHERNET:
3118 kref_init(&mc->mcref);
3119 ret = cma_iboe_join_multicast(id_priv, mc);
3120 break;
3121 default:
3122 ret = -EINVAL;
3124 break;
3125 default:
3126 ret = -ENOSYS;
3127 break;
3130 if (ret) {
3131 spin_lock_irq(&id_priv->lock);
3132 list_del(&mc->list);
3133 spin_unlock_irq(&id_priv->lock);
3134 kfree(mc);
3136 return ret;
3138 EXPORT_SYMBOL(rdma_join_multicast);
3140 void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
3142 struct rdma_id_private *id_priv;
3143 struct cma_multicast *mc;
3145 id_priv = container_of(id, struct rdma_id_private, id);
3146 spin_lock_irq(&id_priv->lock);
3147 list_for_each_entry(mc, &id_priv->mc_list, list) {
3148 if (!memcmp(&mc->addr, addr, ip_addr_size(addr))) {
3149 list_del(&mc->list);
3150 spin_unlock_irq(&id_priv->lock);
3152 if (id->qp)
3153 ib_detach_mcast(id->qp,
3154 &mc->multicast.ib->rec.mgid,
3155 mc->multicast.ib->rec.mlid);
3156 if (rdma_node_get_transport(id_priv->cma_dev->device->node_type) == RDMA_TRANSPORT_IB) {
3157 switch (rdma_port_get_link_layer(id->device, id->port_num)) {
3158 case IB_LINK_LAYER_INFINIBAND:
3159 ib_sa_free_multicast(mc->multicast.ib);
3160 kfree(mc);
3161 break;
3162 case IB_LINK_LAYER_ETHERNET:
3163 kref_put(&mc->mcref, release_mc);
3164 break;
3165 default:
3166 break;
3169 return;
3172 spin_unlock_irq(&id_priv->lock);
3174 EXPORT_SYMBOL(rdma_leave_multicast);
3176 static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
3178 struct rdma_dev_addr *dev_addr;
3179 struct cma_ndev_work *work;
3181 dev_addr = &id_priv->id.route.addr.dev_addr;
3183 if ((dev_addr->bound_dev_if == ndev->ifindex) &&
3184 memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
3185 printk(KERN_INFO "RDMA CM addr change for ndev %s used by id %p\n",
3186 ndev->name, &id_priv->id);
3187 work = kzalloc(sizeof *work, GFP_KERNEL);
3188 if (!work)
3189 return -ENOMEM;
3191 INIT_WORK(&work->work, cma_ndev_work_handler);
3192 work->id = id_priv;
3193 work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
3194 atomic_inc(&id_priv->refcount);
3195 queue_work(cma_wq, &work->work);
3198 return 0;
3201 static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
3202 void *ctx)
3204 struct net_device *ndev = (struct net_device *)ctx;
3205 struct cma_device *cma_dev;
3206 struct rdma_id_private *id_priv;
3207 int ret = NOTIFY_DONE;
3209 if (dev_net(ndev) != &init_net)
3210 return NOTIFY_DONE;
3212 if (event != NETDEV_BONDING_FAILOVER)
3213 return NOTIFY_DONE;
3215 if (!(ndev->flags & IFF_MASTER) || !(ndev->priv_flags & IFF_BONDING))
3216 return NOTIFY_DONE;
3218 mutex_lock(&lock);
3219 list_for_each_entry(cma_dev, &dev_list, list)
3220 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
3221 ret = cma_netdev_change(ndev, id_priv);
3222 if (ret)
3223 goto out;
3226 out:
3227 mutex_unlock(&lock);
3228 return ret;
3231 static struct notifier_block cma_nb = {
3232 .notifier_call = cma_netdev_callback
3235 static void cma_add_one(struct ib_device *device)
3237 struct cma_device *cma_dev;
3238 struct rdma_id_private *id_priv;
3240 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
3241 if (!cma_dev)
3242 return;
3244 cma_dev->device = device;
3246 init_completion(&cma_dev->comp);
3247 atomic_set(&cma_dev->refcount, 1);
3248 INIT_LIST_HEAD(&cma_dev->id_list);
3249 ib_set_client_data(device, &cma_client, cma_dev);
3251 mutex_lock(&lock);
3252 list_add_tail(&cma_dev->list, &dev_list);
3253 list_for_each_entry(id_priv, &listen_any_list, list)
3254 cma_listen_on_dev(id_priv, cma_dev);
3255 mutex_unlock(&lock);
3258 static int cma_remove_id_dev(struct rdma_id_private *id_priv)
3260 struct rdma_cm_event event;
3261 enum rdma_cm_state state;
3262 int ret = 0;
3264 /* Record that we want to remove the device */
3265 state = cma_exch(id_priv, RDMA_CM_DEVICE_REMOVAL);
3266 if (state == RDMA_CM_DESTROYING)
3267 return 0;
3269 cma_cancel_operation(id_priv, state);
3270 mutex_lock(&id_priv->handler_mutex);
3272 /* Check for destruction from another callback. */
3273 if (!cma_comp(id_priv, RDMA_CM_DEVICE_REMOVAL))
3274 goto out;
3276 memset(&event, 0, sizeof event);
3277 event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
3278 ret = id_priv->id.event_handler(&id_priv->id, &event);
3279 out:
3280 mutex_unlock(&id_priv->handler_mutex);
3281 return ret;
3284 static void cma_process_remove(struct cma_device *cma_dev)
3286 struct rdma_id_private *id_priv;
3287 int ret;
3289 mutex_lock(&lock);
3290 while (!list_empty(&cma_dev->id_list)) {
3291 id_priv = list_entry(cma_dev->id_list.next,
3292 struct rdma_id_private, list);
3294 list_del(&id_priv->listen_list);
3295 list_del_init(&id_priv->list);
3296 atomic_inc(&id_priv->refcount);
3297 mutex_unlock(&lock);
3299 ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv);
3300 cma_deref_id(id_priv);
3301 if (ret)
3302 rdma_destroy_id(&id_priv->id);
3304 mutex_lock(&lock);
3306 mutex_unlock(&lock);
3308 cma_deref_dev(cma_dev);
3309 wait_for_completion(&cma_dev->comp);
3312 static void cma_remove_one(struct ib_device *device)
3314 struct cma_device *cma_dev;
3316 cma_dev = ib_get_client_data(device, &cma_client);
3317 if (!cma_dev)
3318 return;
3320 mutex_lock(&lock);
3321 list_del(&cma_dev->list);
3322 mutex_unlock(&lock);
3324 cma_process_remove(cma_dev);
3325 kfree(cma_dev);
3328 static int cma_get_id_stats(struct sk_buff *skb, struct netlink_callback *cb)
3330 struct nlmsghdr *nlh;
3331 struct rdma_cm_id_stats *id_stats;
3332 struct rdma_id_private *id_priv;
3333 struct rdma_cm_id *id = NULL;
3334 struct cma_device *cma_dev;
3335 int i_dev = 0, i_id = 0;
3338 * We export all of the IDs as a sequence of messages. Each
3339 * ID gets its own netlink message.
3341 mutex_lock(&lock);
3343 list_for_each_entry(cma_dev, &dev_list, list) {
3344 if (i_dev < cb->args[0]) {
3345 i_dev++;
3346 continue;
3349 i_id = 0;
3350 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
3351 if (i_id < cb->args[1]) {
3352 i_id++;
3353 continue;
3356 id_stats = ibnl_put_msg(skb, &nlh, cb->nlh->nlmsg_seq,
3357 sizeof *id_stats, RDMA_NL_RDMA_CM,
3358 RDMA_NL_RDMA_CM_ID_STATS);
3359 if (!id_stats)
3360 goto out;
3362 memset(id_stats, 0, sizeof *id_stats);
3363 id = &id_priv->id;
3364 id_stats->node_type = id->route.addr.dev_addr.dev_type;
3365 id_stats->port_num = id->port_num;
3366 id_stats->bound_dev_if =
3367 id->route.addr.dev_addr.bound_dev_if;
3369 if (id->route.addr.src_addr.ss_family == AF_INET) {
3370 if (ibnl_put_attr(skb, nlh,
3371 sizeof(struct sockaddr_in),
3372 &id->route.addr.src_addr,
3373 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR)) {
3374 goto out;
3376 if (ibnl_put_attr(skb, nlh,
3377 sizeof(struct sockaddr_in),
3378 &id->route.addr.dst_addr,
3379 RDMA_NL_RDMA_CM_ATTR_DST_ADDR)) {
3380 goto out;
3382 } else if (id->route.addr.src_addr.ss_family == AF_INET6) {
3383 if (ibnl_put_attr(skb, nlh,
3384 sizeof(struct sockaddr_in6),
3385 &id->route.addr.src_addr,
3386 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR)) {
3387 goto out;
3389 if (ibnl_put_attr(skb, nlh,
3390 sizeof(struct sockaddr_in6),
3391 &id->route.addr.dst_addr,
3392 RDMA_NL_RDMA_CM_ATTR_DST_ADDR)) {
3393 goto out;
3397 id_stats->pid = id_priv->owner;
3398 id_stats->port_space = id->ps;
3399 id_stats->cm_state = id_priv->state;
3400 id_stats->qp_num = id_priv->qp_num;
3401 id_stats->qp_type = id->qp_type;
3403 i_id++;
3406 cb->args[1] = 0;
3407 i_dev++;
3410 out:
3411 mutex_unlock(&lock);
3412 cb->args[0] = i_dev;
3413 cb->args[1] = i_id;
3415 return skb->len;
3418 static const struct ibnl_client_cbs cma_cb_table[] = {
3419 [RDMA_NL_RDMA_CM_ID_STATS] = { .dump = cma_get_id_stats },
3422 static int __init cma_init(void)
3424 int ret;
3426 cma_wq = create_singlethread_workqueue("rdma_cm");
3427 if (!cma_wq)
3428 return -ENOMEM;
3430 ib_sa_register_client(&sa_client);
3431 rdma_addr_register_client(&addr_client);
3432 register_netdevice_notifier(&cma_nb);
3434 ret = ib_register_client(&cma_client);
3435 if (ret)
3436 goto err;
3438 if (ibnl_add_client(RDMA_NL_RDMA_CM, RDMA_NL_RDMA_CM_NUM_OPS, cma_cb_table))
3439 printk(KERN_WARNING "RDMA CMA: failed to add netlink callback\n");
3441 return 0;
3443 err:
3444 unregister_netdevice_notifier(&cma_nb);
3445 rdma_addr_unregister_client(&addr_client);
3446 ib_sa_unregister_client(&sa_client);
3447 destroy_workqueue(cma_wq);
3448 return ret;
3451 static void __exit cma_cleanup(void)
3453 ibnl_remove_client(RDMA_NL_RDMA_CM);
3454 ib_unregister_client(&cma_client);
3455 unregister_netdevice_notifier(&cma_nb);
3456 rdma_addr_unregister_client(&addr_client);
3457 ib_sa_unregister_client(&sa_client);
3458 destroy_workqueue(cma_wq);
3459 idr_destroy(&sdp_ps);
3460 idr_destroy(&tcp_ps);
3461 idr_destroy(&udp_ps);
3462 idr_destroy(&ipoib_ps);
3465 module_init(cma_init);
3466 module_exit(cma_cleanup);