x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / infiniband / core / cma.c
blobdab4b41f1715846bd2a26c03fd90f0175766980e
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>
44 #include <linux/module.h>
45 #include <net/route.h>
47 #include <net/tcp.h>
48 #include <net/ipv6.h>
50 #include <rdma/rdma_cm.h>
51 #include <rdma/rdma_cm_ib.h>
52 #include <rdma/rdma_netlink.h>
53 #include <rdma/ib.h>
54 #include <rdma/ib_cache.h>
55 #include <rdma/ib_cm.h>
56 #include <rdma/ib_sa.h>
57 #include <rdma/iw_cm.h>
59 MODULE_AUTHOR("Sean Hefty");
60 MODULE_DESCRIPTION("Generic RDMA CM Agent");
61 MODULE_LICENSE("Dual BSD/GPL");
63 #define CMA_CM_RESPONSE_TIMEOUT 20
64 #define CMA_MAX_CM_RETRIES 15
65 #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
66 #define CMA_IBOE_PACKET_LIFETIME 18
68 static void cma_add_one(struct ib_device *device);
69 static void cma_remove_one(struct ib_device *device);
71 static struct ib_client cma_client = {
72 .name = "cma",
73 .add = cma_add_one,
74 .remove = cma_remove_one
77 static struct ib_sa_client sa_client;
78 static struct rdma_addr_client addr_client;
79 static LIST_HEAD(dev_list);
80 static LIST_HEAD(listen_any_list);
81 static DEFINE_MUTEX(lock);
82 static struct workqueue_struct *cma_wq;
83 static DEFINE_IDR(tcp_ps);
84 static DEFINE_IDR(udp_ps);
85 static DEFINE_IDR(ipoib_ps);
86 static DEFINE_IDR(ib_ps);
88 struct cma_device {
89 struct list_head list;
90 struct ib_device *device;
91 struct completion comp;
92 atomic_t refcount;
93 struct list_head id_list;
96 struct rdma_bind_list {
97 struct idr *ps;
98 struct hlist_head owners;
99 unsigned short port;
102 enum {
103 CMA_OPTION_AFONLY,
107 * Device removal can occur at anytime, so we need extra handling to
108 * serialize notifying the user of device removal with other callbacks.
109 * We do this by disabling removal notification while a callback is in process,
110 * and reporting it after the callback completes.
112 struct rdma_id_private {
113 struct rdma_cm_id id;
115 struct rdma_bind_list *bind_list;
116 struct hlist_node node;
117 struct list_head list; /* listen_any_list or cma_device.list */
118 struct list_head listen_list; /* per device listens */
119 struct cma_device *cma_dev;
120 struct list_head mc_list;
122 int internal_id;
123 enum rdma_cm_state state;
124 spinlock_t lock;
125 struct mutex qp_mutex;
127 struct completion comp;
128 atomic_t refcount;
129 struct mutex handler_mutex;
131 int backlog;
132 int timeout_ms;
133 struct ib_sa_query *query;
134 int query_id;
135 union {
136 struct ib_cm_id *ib;
137 struct iw_cm_id *iw;
138 } cm_id;
140 u32 seq_num;
141 u32 qkey;
142 u32 qp_num;
143 pid_t owner;
144 u32 options;
145 u8 srq;
146 u8 tos;
147 u8 reuseaddr;
148 u8 afonly;
151 struct cma_multicast {
152 struct rdma_id_private *id_priv;
153 union {
154 struct ib_sa_multicast *ib;
155 } multicast;
156 struct list_head list;
157 void *context;
158 struct sockaddr_storage addr;
159 struct kref mcref;
162 struct cma_work {
163 struct work_struct work;
164 struct rdma_id_private *id;
165 enum rdma_cm_state old_state;
166 enum rdma_cm_state new_state;
167 struct rdma_cm_event event;
170 struct cma_ndev_work {
171 struct work_struct work;
172 struct rdma_id_private *id;
173 struct rdma_cm_event event;
176 struct iboe_mcast_work {
177 struct work_struct work;
178 struct rdma_id_private *id;
179 struct cma_multicast *mc;
182 union cma_ip_addr {
183 struct in6_addr ip6;
184 struct {
185 __be32 pad[3];
186 __be32 addr;
187 } ip4;
190 struct cma_hdr {
191 u8 cma_version;
192 u8 ip_version; /* IP version: 7:4 */
193 __be16 port;
194 union cma_ip_addr src_addr;
195 union cma_ip_addr dst_addr;
198 #define CMA_VERSION 0x00
200 static int cma_comp(struct rdma_id_private *id_priv, enum rdma_cm_state comp)
202 unsigned long flags;
203 int ret;
205 spin_lock_irqsave(&id_priv->lock, flags);
206 ret = (id_priv->state == comp);
207 spin_unlock_irqrestore(&id_priv->lock, flags);
208 return ret;
211 static int cma_comp_exch(struct rdma_id_private *id_priv,
212 enum rdma_cm_state comp, enum rdma_cm_state exch)
214 unsigned long flags;
215 int ret;
217 spin_lock_irqsave(&id_priv->lock, flags);
218 if ((ret = (id_priv->state == comp)))
219 id_priv->state = exch;
220 spin_unlock_irqrestore(&id_priv->lock, flags);
221 return ret;
224 static enum rdma_cm_state cma_exch(struct rdma_id_private *id_priv,
225 enum rdma_cm_state exch)
227 unsigned long flags;
228 enum rdma_cm_state old;
230 spin_lock_irqsave(&id_priv->lock, flags);
231 old = id_priv->state;
232 id_priv->state = exch;
233 spin_unlock_irqrestore(&id_priv->lock, flags);
234 return old;
237 static inline u8 cma_get_ip_ver(struct cma_hdr *hdr)
239 return hdr->ip_version >> 4;
242 static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
244 hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
247 static void cma_attach_to_dev(struct rdma_id_private *id_priv,
248 struct cma_device *cma_dev)
250 atomic_inc(&cma_dev->refcount);
251 id_priv->cma_dev = cma_dev;
252 id_priv->id.device = cma_dev->device;
253 id_priv->id.route.addr.dev_addr.transport =
254 rdma_node_get_transport(cma_dev->device->node_type);
255 list_add_tail(&id_priv->list, &cma_dev->id_list);
258 static inline void cma_deref_dev(struct cma_device *cma_dev)
260 if (atomic_dec_and_test(&cma_dev->refcount))
261 complete(&cma_dev->comp);
264 static inline void release_mc(struct kref *kref)
266 struct cma_multicast *mc = container_of(kref, struct cma_multicast, mcref);
268 kfree(mc->multicast.ib);
269 kfree(mc);
272 static void cma_release_dev(struct rdma_id_private *id_priv)
274 mutex_lock(&lock);
275 list_del(&id_priv->list);
276 cma_deref_dev(id_priv->cma_dev);
277 id_priv->cma_dev = NULL;
278 mutex_unlock(&lock);
281 static inline struct sockaddr *cma_src_addr(struct rdma_id_private *id_priv)
283 return (struct sockaddr *) &id_priv->id.route.addr.src_addr;
286 static inline struct sockaddr *cma_dst_addr(struct rdma_id_private *id_priv)
288 return (struct sockaddr *) &id_priv->id.route.addr.dst_addr;
291 static inline unsigned short cma_family(struct rdma_id_private *id_priv)
293 return id_priv->id.route.addr.src_addr.ss_family;
296 static int cma_set_qkey(struct rdma_id_private *id_priv, u32 qkey)
298 struct ib_sa_mcmember_rec rec;
299 int ret = 0;
301 if (id_priv->qkey) {
302 if (qkey && id_priv->qkey != qkey)
303 return -EINVAL;
304 return 0;
307 if (qkey) {
308 id_priv->qkey = qkey;
309 return 0;
312 switch (id_priv->id.ps) {
313 case RDMA_PS_UDP:
314 case RDMA_PS_IB:
315 id_priv->qkey = RDMA_UDP_QKEY;
316 break;
317 case RDMA_PS_IPOIB:
318 ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid);
319 ret = ib_sa_get_mcmember_rec(id_priv->id.device,
320 id_priv->id.port_num, &rec.mgid,
321 &rec);
322 if (!ret)
323 id_priv->qkey = be32_to_cpu(rec.qkey);
324 break;
325 default:
326 break;
328 return ret;
331 static int find_gid_port(struct ib_device *device, union ib_gid *gid, u8 port_num)
333 int i;
334 int err;
335 struct ib_port_attr props;
336 union ib_gid tmp;
338 err = ib_query_port(device, port_num, &props);
339 if (err)
340 return err;
342 for (i = 0; i < props.gid_tbl_len; ++i) {
343 err = ib_query_gid(device, port_num, i, &tmp);
344 if (err)
345 return err;
346 if (!memcmp(&tmp, gid, sizeof tmp))
347 return 0;
350 return -EADDRNOTAVAIL;
353 static void cma_translate_ib(struct sockaddr_ib *sib, struct rdma_dev_addr *dev_addr)
355 dev_addr->dev_type = ARPHRD_INFINIBAND;
356 rdma_addr_set_sgid(dev_addr, (union ib_gid *) &sib->sib_addr);
357 ib_addr_set_pkey(dev_addr, ntohs(sib->sib_pkey));
360 static int cma_translate_addr(struct sockaddr *addr, struct rdma_dev_addr *dev_addr)
362 int ret;
364 if (addr->sa_family != AF_IB) {
365 ret = rdma_translate_ip(addr, dev_addr);
366 } else {
367 cma_translate_ib((struct sockaddr_ib *) addr, dev_addr);
368 ret = 0;
371 return ret;
374 static int cma_acquire_dev(struct rdma_id_private *id_priv)
376 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
377 struct cma_device *cma_dev;
378 union ib_gid gid, iboe_gid;
379 int ret = -ENODEV;
380 u8 port;
381 enum rdma_link_layer dev_ll = dev_addr->dev_type == ARPHRD_INFINIBAND ?
382 IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET;
384 if (dev_ll != IB_LINK_LAYER_INFINIBAND &&
385 id_priv->id.ps == RDMA_PS_IPOIB)
386 return -EINVAL;
388 mutex_lock(&lock);
389 iboe_addr_get_sgid(dev_addr, &iboe_gid);
390 memcpy(&gid, dev_addr->src_dev_addr +
391 rdma_addr_gid_offset(dev_addr), sizeof gid);
392 list_for_each_entry(cma_dev, &dev_list, list) {
393 for (port = 1; port <= cma_dev->device->phys_port_cnt; ++port) {
394 if (rdma_port_get_link_layer(cma_dev->device, port) == dev_ll) {
395 if (rdma_node_get_transport(cma_dev->device->node_type) == RDMA_TRANSPORT_IB &&
396 rdma_port_get_link_layer(cma_dev->device, port) == IB_LINK_LAYER_ETHERNET)
397 ret = find_gid_port(cma_dev->device, &iboe_gid, port);
398 else
399 ret = find_gid_port(cma_dev->device, &gid, port);
401 if (!ret) {
402 id_priv->id.port_num = port;
403 goto out;
409 out:
410 if (!ret)
411 cma_attach_to_dev(id_priv, cma_dev);
413 mutex_unlock(&lock);
414 return ret;
418 * Select the source IB device and address to reach the destination IB address.
420 static int cma_resolve_ib_dev(struct rdma_id_private *id_priv)
422 struct cma_device *cma_dev, *cur_dev;
423 struct sockaddr_ib *addr;
424 union ib_gid gid, sgid, *dgid;
425 u16 pkey, index;
426 u8 p;
427 int i;
429 cma_dev = NULL;
430 addr = (struct sockaddr_ib *) cma_dst_addr(id_priv);
431 dgid = (union ib_gid *) &addr->sib_addr;
432 pkey = ntohs(addr->sib_pkey);
434 list_for_each_entry(cur_dev, &dev_list, list) {
435 if (rdma_node_get_transport(cur_dev->device->node_type) != RDMA_TRANSPORT_IB)
436 continue;
438 for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
439 if (ib_find_cached_pkey(cur_dev->device, p, pkey, &index))
440 continue;
442 for (i = 0; !ib_get_cached_gid(cur_dev->device, p, i, &gid); i++) {
443 if (!memcmp(&gid, dgid, sizeof(gid))) {
444 cma_dev = cur_dev;
445 sgid = gid;
446 id_priv->id.port_num = p;
447 goto found;
450 if (!cma_dev && (gid.global.subnet_prefix ==
451 dgid->global.subnet_prefix)) {
452 cma_dev = cur_dev;
453 sgid = gid;
454 id_priv->id.port_num = p;
460 if (!cma_dev)
461 return -ENODEV;
463 found:
464 cma_attach_to_dev(id_priv, cma_dev);
465 addr = (struct sockaddr_ib *) cma_src_addr(id_priv);
466 memcpy(&addr->sib_addr, &sgid, sizeof sgid);
467 cma_translate_ib(addr, &id_priv->id.route.addr.dev_addr);
468 return 0;
471 static void cma_deref_id(struct rdma_id_private *id_priv)
473 if (atomic_dec_and_test(&id_priv->refcount))
474 complete(&id_priv->comp);
477 static int cma_disable_callback(struct rdma_id_private *id_priv,
478 enum rdma_cm_state state)
480 mutex_lock(&id_priv->handler_mutex);
481 if (id_priv->state != state) {
482 mutex_unlock(&id_priv->handler_mutex);
483 return -EINVAL;
485 return 0;
488 struct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler,
489 void *context, enum rdma_port_space ps,
490 enum ib_qp_type qp_type)
492 struct rdma_id_private *id_priv;
494 id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
495 if (!id_priv)
496 return ERR_PTR(-ENOMEM);
498 id_priv->owner = task_pid_nr(current);
499 id_priv->state = RDMA_CM_IDLE;
500 id_priv->id.context = context;
501 id_priv->id.event_handler = event_handler;
502 id_priv->id.ps = ps;
503 id_priv->id.qp_type = qp_type;
504 spin_lock_init(&id_priv->lock);
505 mutex_init(&id_priv->qp_mutex);
506 init_completion(&id_priv->comp);
507 atomic_set(&id_priv->refcount, 1);
508 mutex_init(&id_priv->handler_mutex);
509 INIT_LIST_HEAD(&id_priv->listen_list);
510 INIT_LIST_HEAD(&id_priv->mc_list);
511 get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
513 return &id_priv->id;
515 EXPORT_SYMBOL(rdma_create_id);
517 static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
519 struct ib_qp_attr qp_attr;
520 int qp_attr_mask, ret;
522 qp_attr.qp_state = IB_QPS_INIT;
523 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
524 if (ret)
525 return ret;
527 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
528 if (ret)
529 return ret;
531 qp_attr.qp_state = IB_QPS_RTR;
532 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
533 if (ret)
534 return ret;
536 qp_attr.qp_state = IB_QPS_RTS;
537 qp_attr.sq_psn = 0;
538 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
540 return ret;
543 static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
545 struct ib_qp_attr qp_attr;
546 int qp_attr_mask, ret;
548 qp_attr.qp_state = IB_QPS_INIT;
549 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
550 if (ret)
551 return ret;
553 return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
556 int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
557 struct ib_qp_init_attr *qp_init_attr)
559 struct rdma_id_private *id_priv;
560 struct ib_qp *qp;
561 int ret;
563 id_priv = container_of(id, struct rdma_id_private, id);
564 if (id->device != pd->device)
565 return -EINVAL;
567 qp = ib_create_qp(pd, qp_init_attr);
568 if (IS_ERR(qp))
569 return PTR_ERR(qp);
571 if (id->qp_type == IB_QPT_UD)
572 ret = cma_init_ud_qp(id_priv, qp);
573 else
574 ret = cma_init_conn_qp(id_priv, qp);
575 if (ret)
576 goto err;
578 id->qp = qp;
579 id_priv->qp_num = qp->qp_num;
580 id_priv->srq = (qp->srq != NULL);
581 return 0;
582 err:
583 ib_destroy_qp(qp);
584 return ret;
586 EXPORT_SYMBOL(rdma_create_qp);
588 void rdma_destroy_qp(struct rdma_cm_id *id)
590 struct rdma_id_private *id_priv;
592 id_priv = container_of(id, struct rdma_id_private, id);
593 mutex_lock(&id_priv->qp_mutex);
594 ib_destroy_qp(id_priv->id.qp);
595 id_priv->id.qp = NULL;
596 mutex_unlock(&id_priv->qp_mutex);
598 EXPORT_SYMBOL(rdma_destroy_qp);
600 static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
601 struct rdma_conn_param *conn_param)
603 struct ib_qp_attr qp_attr;
604 int qp_attr_mask, ret;
606 mutex_lock(&id_priv->qp_mutex);
607 if (!id_priv->id.qp) {
608 ret = 0;
609 goto out;
612 /* Need to update QP attributes from default values. */
613 qp_attr.qp_state = IB_QPS_INIT;
614 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
615 if (ret)
616 goto out;
618 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
619 if (ret)
620 goto out;
622 qp_attr.qp_state = IB_QPS_RTR;
623 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
624 if (ret)
625 goto out;
627 if (conn_param)
628 qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
629 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
630 out:
631 mutex_unlock(&id_priv->qp_mutex);
632 return ret;
635 static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
636 struct rdma_conn_param *conn_param)
638 struct ib_qp_attr qp_attr;
639 int qp_attr_mask, ret;
641 mutex_lock(&id_priv->qp_mutex);
642 if (!id_priv->id.qp) {
643 ret = 0;
644 goto out;
647 qp_attr.qp_state = IB_QPS_RTS;
648 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
649 if (ret)
650 goto out;
652 if (conn_param)
653 qp_attr.max_rd_atomic = conn_param->initiator_depth;
654 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
655 out:
656 mutex_unlock(&id_priv->qp_mutex);
657 return ret;
660 static int cma_modify_qp_err(struct rdma_id_private *id_priv)
662 struct ib_qp_attr qp_attr;
663 int ret;
665 mutex_lock(&id_priv->qp_mutex);
666 if (!id_priv->id.qp) {
667 ret = 0;
668 goto out;
671 qp_attr.qp_state = IB_QPS_ERR;
672 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
673 out:
674 mutex_unlock(&id_priv->qp_mutex);
675 return ret;
678 static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
679 struct ib_qp_attr *qp_attr, int *qp_attr_mask)
681 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
682 int ret;
683 u16 pkey;
685 if (rdma_port_get_link_layer(id_priv->id.device, id_priv->id.port_num) ==
686 IB_LINK_LAYER_INFINIBAND)
687 pkey = ib_addr_get_pkey(dev_addr);
688 else
689 pkey = 0xffff;
691 ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
692 pkey, &qp_attr->pkey_index);
693 if (ret)
694 return ret;
696 qp_attr->port_num = id_priv->id.port_num;
697 *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
699 if (id_priv->id.qp_type == IB_QPT_UD) {
700 ret = cma_set_qkey(id_priv, 0);
701 if (ret)
702 return ret;
704 qp_attr->qkey = id_priv->qkey;
705 *qp_attr_mask |= IB_QP_QKEY;
706 } else {
707 qp_attr->qp_access_flags = 0;
708 *qp_attr_mask |= IB_QP_ACCESS_FLAGS;
710 return 0;
713 int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
714 int *qp_attr_mask)
716 struct rdma_id_private *id_priv;
717 int ret = 0;
719 id_priv = container_of(id, struct rdma_id_private, id);
720 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
721 case RDMA_TRANSPORT_IB:
722 if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD))
723 ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
724 else
725 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
726 qp_attr_mask);
727 if (qp_attr->qp_state == IB_QPS_RTR)
728 qp_attr->rq_psn = id_priv->seq_num;
729 break;
730 case RDMA_TRANSPORT_IWARP:
731 if (!id_priv->cm_id.iw) {
732 qp_attr->qp_access_flags = 0;
733 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
734 } else
735 ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
736 qp_attr_mask);
737 break;
738 default:
739 ret = -ENOSYS;
740 break;
743 return ret;
745 EXPORT_SYMBOL(rdma_init_qp_attr);
747 static inline int cma_zero_addr(struct sockaddr *addr)
749 switch (addr->sa_family) {
750 case AF_INET:
751 return ipv4_is_zeronet(((struct sockaddr_in *)addr)->sin_addr.s_addr);
752 case AF_INET6:
753 return ipv6_addr_any(&((struct sockaddr_in6 *) addr)->sin6_addr);
754 case AF_IB:
755 return ib_addr_any(&((struct sockaddr_ib *) addr)->sib_addr);
756 default:
757 return 0;
761 static inline int cma_loopback_addr(struct sockaddr *addr)
763 switch (addr->sa_family) {
764 case AF_INET:
765 return ipv4_is_loopback(((struct sockaddr_in *) addr)->sin_addr.s_addr);
766 case AF_INET6:
767 return ipv6_addr_loopback(&((struct sockaddr_in6 *) addr)->sin6_addr);
768 case AF_IB:
769 return ib_addr_loopback(&((struct sockaddr_ib *) addr)->sib_addr);
770 default:
771 return 0;
775 static inline int cma_any_addr(struct sockaddr *addr)
777 return cma_zero_addr(addr) || cma_loopback_addr(addr);
780 static int cma_addr_cmp(struct sockaddr *src, struct sockaddr *dst)
782 if (src->sa_family != dst->sa_family)
783 return -1;
785 switch (src->sa_family) {
786 case AF_INET:
787 return ((struct sockaddr_in *) src)->sin_addr.s_addr !=
788 ((struct sockaddr_in *) dst)->sin_addr.s_addr;
789 case AF_INET6:
790 return ipv6_addr_cmp(&((struct sockaddr_in6 *) src)->sin6_addr,
791 &((struct sockaddr_in6 *) dst)->sin6_addr);
792 default:
793 return ib_addr_cmp(&((struct sockaddr_ib *) src)->sib_addr,
794 &((struct sockaddr_ib *) dst)->sib_addr);
798 static __be16 cma_port(struct sockaddr *addr)
800 struct sockaddr_ib *sib;
802 switch (addr->sa_family) {
803 case AF_INET:
804 return ((struct sockaddr_in *) addr)->sin_port;
805 case AF_INET6:
806 return ((struct sockaddr_in6 *) addr)->sin6_port;
807 case AF_IB:
808 sib = (struct sockaddr_ib *) addr;
809 return htons((u16) (be64_to_cpu(sib->sib_sid) &
810 be64_to_cpu(sib->sib_sid_mask)));
811 default:
812 return 0;
816 static inline int cma_any_port(struct sockaddr *addr)
818 return !cma_port(addr);
821 static void cma_save_ib_info(struct rdma_cm_id *id, struct rdma_cm_id *listen_id,
822 struct ib_sa_path_rec *path)
824 struct sockaddr_ib *listen_ib, *ib;
826 listen_ib = (struct sockaddr_ib *) &listen_id->route.addr.src_addr;
827 ib = (struct sockaddr_ib *) &id->route.addr.src_addr;
828 ib->sib_family = listen_ib->sib_family;
829 ib->sib_pkey = path->pkey;
830 ib->sib_flowinfo = path->flow_label;
831 memcpy(&ib->sib_addr, &path->sgid, 16);
832 ib->sib_sid = listen_ib->sib_sid;
833 ib->sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL);
834 ib->sib_scope_id = listen_ib->sib_scope_id;
836 ib = (struct sockaddr_ib *) &id->route.addr.dst_addr;
837 ib->sib_family = listen_ib->sib_family;
838 ib->sib_pkey = path->pkey;
839 ib->sib_flowinfo = path->flow_label;
840 memcpy(&ib->sib_addr, &path->dgid, 16);
843 static void cma_save_ip4_info(struct rdma_cm_id *id, struct rdma_cm_id *listen_id,
844 struct cma_hdr *hdr)
846 struct sockaddr_in *listen4, *ip4;
848 listen4 = (struct sockaddr_in *) &listen_id->route.addr.src_addr;
849 ip4 = (struct sockaddr_in *) &id->route.addr.src_addr;
850 ip4->sin_family = listen4->sin_family;
851 ip4->sin_addr.s_addr = hdr->dst_addr.ip4.addr;
852 ip4->sin_port = listen4->sin_port;
854 ip4 = (struct sockaddr_in *) &id->route.addr.dst_addr;
855 ip4->sin_family = listen4->sin_family;
856 ip4->sin_addr.s_addr = hdr->src_addr.ip4.addr;
857 ip4->sin_port = hdr->port;
860 static void cma_save_ip6_info(struct rdma_cm_id *id, struct rdma_cm_id *listen_id,
861 struct cma_hdr *hdr)
863 struct sockaddr_in6 *listen6, *ip6;
865 listen6 = (struct sockaddr_in6 *) &listen_id->route.addr.src_addr;
866 ip6 = (struct sockaddr_in6 *) &id->route.addr.src_addr;
867 ip6->sin6_family = listen6->sin6_family;
868 ip6->sin6_addr = hdr->dst_addr.ip6;
869 ip6->sin6_port = listen6->sin6_port;
871 ip6 = (struct sockaddr_in6 *) &id->route.addr.dst_addr;
872 ip6->sin6_family = listen6->sin6_family;
873 ip6->sin6_addr = hdr->src_addr.ip6;
874 ip6->sin6_port = hdr->port;
877 static int cma_save_net_info(struct rdma_cm_id *id, struct rdma_cm_id *listen_id,
878 struct ib_cm_event *ib_event)
880 struct cma_hdr *hdr;
882 if ((listen_id->route.addr.src_addr.ss_family == AF_IB) &&
883 (ib_event->event == IB_CM_REQ_RECEIVED)) {
884 cma_save_ib_info(id, listen_id, ib_event->param.req_rcvd.primary_path);
885 return 0;
888 hdr = ib_event->private_data;
889 if (hdr->cma_version != CMA_VERSION)
890 return -EINVAL;
892 switch (cma_get_ip_ver(hdr)) {
893 case 4:
894 cma_save_ip4_info(id, listen_id, hdr);
895 break;
896 case 6:
897 cma_save_ip6_info(id, listen_id, hdr);
898 break;
899 default:
900 return -EINVAL;
902 return 0;
905 static inline int cma_user_data_offset(struct rdma_id_private *id_priv)
907 return cma_family(id_priv) == AF_IB ? 0 : sizeof(struct cma_hdr);
910 static void cma_cancel_route(struct rdma_id_private *id_priv)
912 switch (rdma_port_get_link_layer(id_priv->id.device, id_priv->id.port_num)) {
913 case IB_LINK_LAYER_INFINIBAND:
914 if (id_priv->query)
915 ib_sa_cancel_query(id_priv->query_id, id_priv->query);
916 break;
917 default:
918 break;
922 static void cma_cancel_listens(struct rdma_id_private *id_priv)
924 struct rdma_id_private *dev_id_priv;
927 * Remove from listen_any_list to prevent added devices from spawning
928 * additional listen requests.
930 mutex_lock(&lock);
931 list_del(&id_priv->list);
933 while (!list_empty(&id_priv->listen_list)) {
934 dev_id_priv = list_entry(id_priv->listen_list.next,
935 struct rdma_id_private, listen_list);
936 /* sync with device removal to avoid duplicate destruction */
937 list_del_init(&dev_id_priv->list);
938 list_del(&dev_id_priv->listen_list);
939 mutex_unlock(&lock);
941 rdma_destroy_id(&dev_id_priv->id);
942 mutex_lock(&lock);
944 mutex_unlock(&lock);
947 static void cma_cancel_operation(struct rdma_id_private *id_priv,
948 enum rdma_cm_state state)
950 switch (state) {
951 case RDMA_CM_ADDR_QUERY:
952 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
953 break;
954 case RDMA_CM_ROUTE_QUERY:
955 cma_cancel_route(id_priv);
956 break;
957 case RDMA_CM_LISTEN:
958 if (cma_any_addr(cma_src_addr(id_priv)) && !id_priv->cma_dev)
959 cma_cancel_listens(id_priv);
960 break;
961 default:
962 break;
966 static void cma_release_port(struct rdma_id_private *id_priv)
968 struct rdma_bind_list *bind_list = id_priv->bind_list;
970 if (!bind_list)
971 return;
973 mutex_lock(&lock);
974 hlist_del(&id_priv->node);
975 if (hlist_empty(&bind_list->owners)) {
976 idr_remove(bind_list->ps, bind_list->port);
977 kfree(bind_list);
979 mutex_unlock(&lock);
982 static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
984 struct cma_multicast *mc;
986 while (!list_empty(&id_priv->mc_list)) {
987 mc = container_of(id_priv->mc_list.next,
988 struct cma_multicast, list);
989 list_del(&mc->list);
990 switch (rdma_port_get_link_layer(id_priv->cma_dev->device, id_priv->id.port_num)) {
991 case IB_LINK_LAYER_INFINIBAND:
992 ib_sa_free_multicast(mc->multicast.ib);
993 kfree(mc);
994 break;
995 case IB_LINK_LAYER_ETHERNET:
996 kref_put(&mc->mcref, release_mc);
997 break;
998 default:
999 break;
1004 void rdma_destroy_id(struct rdma_cm_id *id)
1006 struct rdma_id_private *id_priv;
1007 enum rdma_cm_state state;
1009 id_priv = container_of(id, struct rdma_id_private, id);
1010 state = cma_exch(id_priv, RDMA_CM_DESTROYING);
1011 cma_cancel_operation(id_priv, state);
1014 * Wait for any active callback to finish. New callbacks will find
1015 * the id_priv state set to destroying and abort.
1017 mutex_lock(&id_priv->handler_mutex);
1018 mutex_unlock(&id_priv->handler_mutex);
1020 if (id_priv->cma_dev) {
1021 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
1022 case RDMA_TRANSPORT_IB:
1023 if (id_priv->cm_id.ib)
1024 ib_destroy_cm_id(id_priv->cm_id.ib);
1025 break;
1026 case RDMA_TRANSPORT_IWARP:
1027 if (id_priv->cm_id.iw)
1028 iw_destroy_cm_id(id_priv->cm_id.iw);
1029 break;
1030 default:
1031 break;
1033 cma_leave_mc_groups(id_priv);
1034 cma_release_dev(id_priv);
1037 cma_release_port(id_priv);
1038 cma_deref_id(id_priv);
1039 wait_for_completion(&id_priv->comp);
1041 if (id_priv->internal_id)
1042 cma_deref_id(id_priv->id.context);
1044 kfree(id_priv->id.route.path_rec);
1045 kfree(id_priv);
1047 EXPORT_SYMBOL(rdma_destroy_id);
1049 static int cma_rep_recv(struct rdma_id_private *id_priv)
1051 int ret;
1053 ret = cma_modify_qp_rtr(id_priv, NULL);
1054 if (ret)
1055 goto reject;
1057 ret = cma_modify_qp_rts(id_priv, NULL);
1058 if (ret)
1059 goto reject;
1061 ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
1062 if (ret)
1063 goto reject;
1065 return 0;
1066 reject:
1067 cma_modify_qp_err(id_priv);
1068 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
1069 NULL, 0, NULL, 0);
1070 return ret;
1073 static void cma_set_rep_event_data(struct rdma_cm_event *event,
1074 struct ib_cm_rep_event_param *rep_data,
1075 void *private_data)
1077 event->param.conn.private_data = private_data;
1078 event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
1079 event->param.conn.responder_resources = rep_data->responder_resources;
1080 event->param.conn.initiator_depth = rep_data->initiator_depth;
1081 event->param.conn.flow_control = rep_data->flow_control;
1082 event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
1083 event->param.conn.srq = rep_data->srq;
1084 event->param.conn.qp_num = rep_data->remote_qpn;
1087 static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1089 struct rdma_id_private *id_priv = cm_id->context;
1090 struct rdma_cm_event event;
1091 int ret = 0;
1093 if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
1094 cma_disable_callback(id_priv, RDMA_CM_CONNECT)) ||
1095 (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
1096 cma_disable_callback(id_priv, RDMA_CM_DISCONNECT)))
1097 return 0;
1099 memset(&event, 0, sizeof event);
1100 switch (ib_event->event) {
1101 case IB_CM_REQ_ERROR:
1102 case IB_CM_REP_ERROR:
1103 event.event = RDMA_CM_EVENT_UNREACHABLE;
1104 event.status = -ETIMEDOUT;
1105 break;
1106 case IB_CM_REP_RECEIVED:
1107 if (id_priv->id.qp) {
1108 event.status = cma_rep_recv(id_priv);
1109 event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
1110 RDMA_CM_EVENT_ESTABLISHED;
1111 } else {
1112 event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
1114 cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
1115 ib_event->private_data);
1116 break;
1117 case IB_CM_RTU_RECEIVED:
1118 case IB_CM_USER_ESTABLISHED:
1119 event.event = RDMA_CM_EVENT_ESTABLISHED;
1120 break;
1121 case IB_CM_DREQ_ERROR:
1122 event.status = -ETIMEDOUT; /* fall through */
1123 case IB_CM_DREQ_RECEIVED:
1124 case IB_CM_DREP_RECEIVED:
1125 if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT,
1126 RDMA_CM_DISCONNECT))
1127 goto out;
1128 event.event = RDMA_CM_EVENT_DISCONNECTED;
1129 break;
1130 case IB_CM_TIMEWAIT_EXIT:
1131 event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
1132 break;
1133 case IB_CM_MRA_RECEIVED:
1134 /* ignore event */
1135 goto out;
1136 case IB_CM_REJ_RECEIVED:
1137 cma_modify_qp_err(id_priv);
1138 event.status = ib_event->param.rej_rcvd.reason;
1139 event.event = RDMA_CM_EVENT_REJECTED;
1140 event.param.conn.private_data = ib_event->private_data;
1141 event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
1142 break;
1143 default:
1144 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
1145 ib_event->event);
1146 goto out;
1149 ret = id_priv->id.event_handler(&id_priv->id, &event);
1150 if (ret) {
1151 /* Destroy the CM ID by returning a non-zero value. */
1152 id_priv->cm_id.ib = NULL;
1153 cma_exch(id_priv, RDMA_CM_DESTROYING);
1154 mutex_unlock(&id_priv->handler_mutex);
1155 rdma_destroy_id(&id_priv->id);
1156 return ret;
1158 out:
1159 mutex_unlock(&id_priv->handler_mutex);
1160 return ret;
1163 static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
1164 struct ib_cm_event *ib_event)
1166 struct rdma_id_private *id_priv;
1167 struct rdma_cm_id *id;
1168 struct rdma_route *rt;
1169 int ret;
1171 id = rdma_create_id(listen_id->event_handler, listen_id->context,
1172 listen_id->ps, ib_event->param.req_rcvd.qp_type);
1173 if (IS_ERR(id))
1174 return NULL;
1176 id_priv = container_of(id, struct rdma_id_private, id);
1177 if (cma_save_net_info(id, listen_id, ib_event))
1178 goto err;
1180 rt = &id->route;
1181 rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
1182 rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
1183 GFP_KERNEL);
1184 if (!rt->path_rec)
1185 goto err;
1187 rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
1188 if (rt->num_paths == 2)
1189 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
1191 if (cma_any_addr(cma_src_addr(id_priv))) {
1192 rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
1193 rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
1194 ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
1195 } else {
1196 ret = cma_translate_addr(cma_src_addr(id_priv), &rt->addr.dev_addr);
1197 if (ret)
1198 goto err;
1200 rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
1202 id_priv->state = RDMA_CM_CONNECT;
1203 return id_priv;
1205 err:
1206 rdma_destroy_id(id);
1207 return NULL;
1210 static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
1211 struct ib_cm_event *ib_event)
1213 struct rdma_id_private *id_priv;
1214 struct rdma_cm_id *id;
1215 int ret;
1217 id = rdma_create_id(listen_id->event_handler, listen_id->context,
1218 listen_id->ps, IB_QPT_UD);
1219 if (IS_ERR(id))
1220 return NULL;
1222 id_priv = container_of(id, struct rdma_id_private, id);
1223 if (cma_save_net_info(id, listen_id, ib_event))
1224 goto err;
1226 if (!cma_any_addr((struct sockaddr *) &id->route.addr.src_addr)) {
1227 ret = cma_translate_addr(cma_src_addr(id_priv), &id->route.addr.dev_addr);
1228 if (ret)
1229 goto err;
1232 id_priv->state = RDMA_CM_CONNECT;
1233 return id_priv;
1234 err:
1235 rdma_destroy_id(id);
1236 return NULL;
1239 static void cma_set_req_event_data(struct rdma_cm_event *event,
1240 struct ib_cm_req_event_param *req_data,
1241 void *private_data, int offset)
1243 event->param.conn.private_data = private_data + offset;
1244 event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
1245 event->param.conn.responder_resources = req_data->responder_resources;
1246 event->param.conn.initiator_depth = req_data->initiator_depth;
1247 event->param.conn.flow_control = req_data->flow_control;
1248 event->param.conn.retry_count = req_data->retry_count;
1249 event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
1250 event->param.conn.srq = req_data->srq;
1251 event->param.conn.qp_num = req_data->remote_qpn;
1254 static int cma_check_req_qp_type(struct rdma_cm_id *id, struct ib_cm_event *ib_event)
1256 return (((ib_event->event == IB_CM_REQ_RECEIVED) &&
1257 (ib_event->param.req_rcvd.qp_type == id->qp_type)) ||
1258 ((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) &&
1259 (id->qp_type == IB_QPT_UD)) ||
1260 (!id->qp_type));
1263 static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1265 struct rdma_id_private *listen_id, *conn_id;
1266 struct rdma_cm_event event;
1267 int offset, ret;
1269 listen_id = cm_id->context;
1270 if (!cma_check_req_qp_type(&listen_id->id, ib_event))
1271 return -EINVAL;
1273 if (cma_disable_callback(listen_id, RDMA_CM_LISTEN))
1274 return -ECONNABORTED;
1276 memset(&event, 0, sizeof event);
1277 offset = cma_user_data_offset(listen_id);
1278 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1279 if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) {
1280 conn_id = cma_new_udp_id(&listen_id->id, ib_event);
1281 event.param.ud.private_data = ib_event->private_data + offset;
1282 event.param.ud.private_data_len =
1283 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
1284 } else {
1285 conn_id = cma_new_conn_id(&listen_id->id, ib_event);
1286 cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
1287 ib_event->private_data, offset);
1289 if (!conn_id) {
1290 ret = -ENOMEM;
1291 goto err1;
1294 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
1295 ret = cma_acquire_dev(conn_id);
1296 if (ret)
1297 goto err2;
1299 conn_id->cm_id.ib = cm_id;
1300 cm_id->context = conn_id;
1301 cm_id->cm_handler = cma_ib_handler;
1304 * Protect against the user destroying conn_id from another thread
1305 * until we're done accessing it.
1307 atomic_inc(&conn_id->refcount);
1308 ret = conn_id->id.event_handler(&conn_id->id, &event);
1309 if (ret)
1310 goto err3;
1313 * Acquire mutex to prevent user executing rdma_destroy_id()
1314 * while we're accessing the cm_id.
1316 mutex_lock(&lock);
1317 if (cma_comp(conn_id, RDMA_CM_CONNECT) && (conn_id->id.qp_type != IB_QPT_UD))
1318 ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
1319 mutex_unlock(&lock);
1320 mutex_unlock(&conn_id->handler_mutex);
1321 mutex_unlock(&listen_id->handler_mutex);
1322 cma_deref_id(conn_id);
1323 return 0;
1325 err3:
1326 cma_deref_id(conn_id);
1327 /* Destroy the CM ID by returning a non-zero value. */
1328 conn_id->cm_id.ib = NULL;
1329 err2:
1330 cma_exch(conn_id, RDMA_CM_DESTROYING);
1331 mutex_unlock(&conn_id->handler_mutex);
1332 err1:
1333 mutex_unlock(&listen_id->handler_mutex);
1334 if (conn_id)
1335 rdma_destroy_id(&conn_id->id);
1336 return ret;
1339 __be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr)
1341 if (addr->sa_family == AF_IB)
1342 return ((struct sockaddr_ib *) addr)->sib_sid;
1344 return cpu_to_be64(((u64)id->ps << 16) + be16_to_cpu(cma_port(addr)));
1346 EXPORT_SYMBOL(rdma_get_service_id);
1348 static void cma_set_compare_data(enum rdma_port_space ps, struct sockaddr *addr,
1349 struct ib_cm_compare_data *compare)
1351 struct cma_hdr *cma_data, *cma_mask;
1352 __be32 ip4_addr;
1353 struct in6_addr ip6_addr;
1355 memset(compare, 0, sizeof *compare);
1356 cma_data = (void *) compare->data;
1357 cma_mask = (void *) compare->mask;
1359 switch (addr->sa_family) {
1360 case AF_INET:
1361 ip4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1362 cma_set_ip_ver(cma_data, 4);
1363 cma_set_ip_ver(cma_mask, 0xF);
1364 if (!cma_any_addr(addr)) {
1365 cma_data->dst_addr.ip4.addr = ip4_addr;
1366 cma_mask->dst_addr.ip4.addr = htonl(~0);
1368 break;
1369 case AF_INET6:
1370 ip6_addr = ((struct sockaddr_in6 *) addr)->sin6_addr;
1371 cma_set_ip_ver(cma_data, 6);
1372 cma_set_ip_ver(cma_mask, 0xF);
1373 if (!cma_any_addr(addr)) {
1374 cma_data->dst_addr.ip6 = ip6_addr;
1375 memset(&cma_mask->dst_addr.ip6, 0xFF,
1376 sizeof cma_mask->dst_addr.ip6);
1378 break;
1379 default:
1380 break;
1384 static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
1386 struct rdma_id_private *id_priv = iw_id->context;
1387 struct rdma_cm_event event;
1388 int ret = 0;
1389 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
1390 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
1392 if (cma_disable_callback(id_priv, RDMA_CM_CONNECT))
1393 return 0;
1395 memset(&event, 0, sizeof event);
1396 switch (iw_event->event) {
1397 case IW_CM_EVENT_CLOSE:
1398 event.event = RDMA_CM_EVENT_DISCONNECTED;
1399 break;
1400 case IW_CM_EVENT_CONNECT_REPLY:
1401 memcpy(cma_src_addr(id_priv), laddr,
1402 rdma_addr_size(laddr));
1403 memcpy(cma_dst_addr(id_priv), raddr,
1404 rdma_addr_size(raddr));
1405 switch (iw_event->status) {
1406 case 0:
1407 event.event = RDMA_CM_EVENT_ESTABLISHED;
1408 event.param.conn.initiator_depth = iw_event->ird;
1409 event.param.conn.responder_resources = iw_event->ord;
1410 break;
1411 case -ECONNRESET:
1412 case -ECONNREFUSED:
1413 event.event = RDMA_CM_EVENT_REJECTED;
1414 break;
1415 case -ETIMEDOUT:
1416 event.event = RDMA_CM_EVENT_UNREACHABLE;
1417 break;
1418 default:
1419 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
1420 break;
1422 break;
1423 case IW_CM_EVENT_ESTABLISHED:
1424 event.event = RDMA_CM_EVENT_ESTABLISHED;
1425 event.param.conn.initiator_depth = iw_event->ird;
1426 event.param.conn.responder_resources = iw_event->ord;
1427 break;
1428 default:
1429 BUG_ON(1);
1432 event.status = iw_event->status;
1433 event.param.conn.private_data = iw_event->private_data;
1434 event.param.conn.private_data_len = iw_event->private_data_len;
1435 ret = id_priv->id.event_handler(&id_priv->id, &event);
1436 if (ret) {
1437 /* Destroy the CM ID by returning a non-zero value. */
1438 id_priv->cm_id.iw = NULL;
1439 cma_exch(id_priv, RDMA_CM_DESTROYING);
1440 mutex_unlock(&id_priv->handler_mutex);
1441 rdma_destroy_id(&id_priv->id);
1442 return ret;
1445 mutex_unlock(&id_priv->handler_mutex);
1446 return ret;
1449 static int iw_conn_req_handler(struct iw_cm_id *cm_id,
1450 struct iw_cm_event *iw_event)
1452 struct rdma_cm_id *new_cm_id;
1453 struct rdma_id_private *listen_id, *conn_id;
1454 struct net_device *dev = NULL;
1455 struct rdma_cm_event event;
1456 int ret;
1457 struct ib_device_attr attr;
1458 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
1459 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
1461 listen_id = cm_id->context;
1462 if (cma_disable_callback(listen_id, RDMA_CM_LISTEN))
1463 return -ECONNABORTED;
1465 /* Create a new RDMA id for the new IW CM ID */
1466 new_cm_id = rdma_create_id(listen_id->id.event_handler,
1467 listen_id->id.context,
1468 RDMA_PS_TCP, IB_QPT_RC);
1469 if (IS_ERR(new_cm_id)) {
1470 ret = -ENOMEM;
1471 goto out;
1473 conn_id = container_of(new_cm_id, struct rdma_id_private, id);
1474 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
1475 conn_id->state = RDMA_CM_CONNECT;
1477 ret = rdma_translate_ip(laddr, &conn_id->id.route.addr.dev_addr);
1478 if (ret) {
1479 mutex_unlock(&conn_id->handler_mutex);
1480 rdma_destroy_id(new_cm_id);
1481 goto out;
1484 ret = cma_acquire_dev(conn_id);
1485 if (ret) {
1486 mutex_unlock(&conn_id->handler_mutex);
1487 rdma_destroy_id(new_cm_id);
1488 goto out;
1491 conn_id->cm_id.iw = cm_id;
1492 cm_id->context = conn_id;
1493 cm_id->cm_handler = cma_iw_handler;
1495 memcpy(cma_src_addr(conn_id), laddr, rdma_addr_size(laddr));
1496 memcpy(cma_dst_addr(conn_id), raddr, rdma_addr_size(raddr));
1498 ret = ib_query_device(conn_id->id.device, &attr);
1499 if (ret) {
1500 mutex_unlock(&conn_id->handler_mutex);
1501 rdma_destroy_id(new_cm_id);
1502 goto out;
1505 memset(&event, 0, sizeof event);
1506 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1507 event.param.conn.private_data = iw_event->private_data;
1508 event.param.conn.private_data_len = iw_event->private_data_len;
1509 event.param.conn.initiator_depth = iw_event->ird;
1510 event.param.conn.responder_resources = iw_event->ord;
1513 * Protect against the user destroying conn_id from another thread
1514 * until we're done accessing it.
1516 atomic_inc(&conn_id->refcount);
1517 ret = conn_id->id.event_handler(&conn_id->id, &event);
1518 if (ret) {
1519 /* User wants to destroy the CM ID */
1520 conn_id->cm_id.iw = NULL;
1521 cma_exch(conn_id, RDMA_CM_DESTROYING);
1522 mutex_unlock(&conn_id->handler_mutex);
1523 cma_deref_id(conn_id);
1524 rdma_destroy_id(&conn_id->id);
1525 goto out;
1528 mutex_unlock(&conn_id->handler_mutex);
1529 cma_deref_id(conn_id);
1531 out:
1532 if (dev)
1533 dev_put(dev);
1534 mutex_unlock(&listen_id->handler_mutex);
1535 return ret;
1538 static int cma_ib_listen(struct rdma_id_private *id_priv)
1540 struct ib_cm_compare_data compare_data;
1541 struct sockaddr *addr;
1542 struct ib_cm_id *id;
1543 __be64 svc_id;
1544 int ret;
1546 id = ib_create_cm_id(id_priv->id.device, cma_req_handler, id_priv);
1547 if (IS_ERR(id))
1548 return PTR_ERR(id);
1550 id_priv->cm_id.ib = id;
1552 addr = cma_src_addr(id_priv);
1553 svc_id = rdma_get_service_id(&id_priv->id, addr);
1554 if (cma_any_addr(addr) && !id_priv->afonly)
1555 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, NULL);
1556 else {
1557 cma_set_compare_data(id_priv->id.ps, addr, &compare_data);
1558 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, &compare_data);
1561 if (ret) {
1562 ib_destroy_cm_id(id_priv->cm_id.ib);
1563 id_priv->cm_id.ib = NULL;
1566 return ret;
1569 static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
1571 int ret;
1572 struct iw_cm_id *id;
1574 id = iw_create_cm_id(id_priv->id.device,
1575 iw_conn_req_handler,
1576 id_priv);
1577 if (IS_ERR(id))
1578 return PTR_ERR(id);
1580 id_priv->cm_id.iw = id;
1582 memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv),
1583 rdma_addr_size(cma_src_addr(id_priv)));
1585 ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
1587 if (ret) {
1588 iw_destroy_cm_id(id_priv->cm_id.iw);
1589 id_priv->cm_id.iw = NULL;
1592 return ret;
1595 static int cma_listen_handler(struct rdma_cm_id *id,
1596 struct rdma_cm_event *event)
1598 struct rdma_id_private *id_priv = id->context;
1600 id->context = id_priv->id.context;
1601 id->event_handler = id_priv->id.event_handler;
1602 return id_priv->id.event_handler(id, event);
1605 static void cma_listen_on_dev(struct rdma_id_private *id_priv,
1606 struct cma_device *cma_dev)
1608 struct rdma_id_private *dev_id_priv;
1609 struct rdma_cm_id *id;
1610 int ret;
1612 if (cma_family(id_priv) == AF_IB &&
1613 rdma_node_get_transport(cma_dev->device->node_type) != RDMA_TRANSPORT_IB)
1614 return;
1616 id = rdma_create_id(cma_listen_handler, id_priv, id_priv->id.ps,
1617 id_priv->id.qp_type);
1618 if (IS_ERR(id))
1619 return;
1621 dev_id_priv = container_of(id, struct rdma_id_private, id);
1623 dev_id_priv->state = RDMA_CM_ADDR_BOUND;
1624 memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv),
1625 rdma_addr_size(cma_src_addr(id_priv)));
1627 cma_attach_to_dev(dev_id_priv, cma_dev);
1628 list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
1629 atomic_inc(&id_priv->refcount);
1630 dev_id_priv->internal_id = 1;
1631 dev_id_priv->afonly = id_priv->afonly;
1633 ret = rdma_listen(id, id_priv->backlog);
1634 if (ret)
1635 printk(KERN_WARNING "RDMA CMA: cma_listen_on_dev, error %d, "
1636 "listening on device %s\n", ret, cma_dev->device->name);
1639 static void cma_listen_on_all(struct rdma_id_private *id_priv)
1641 struct cma_device *cma_dev;
1643 mutex_lock(&lock);
1644 list_add_tail(&id_priv->list, &listen_any_list);
1645 list_for_each_entry(cma_dev, &dev_list, list)
1646 cma_listen_on_dev(id_priv, cma_dev);
1647 mutex_unlock(&lock);
1650 void rdma_set_service_type(struct rdma_cm_id *id, int tos)
1652 struct rdma_id_private *id_priv;
1654 id_priv = container_of(id, struct rdma_id_private, id);
1655 id_priv->tos = (u8) tos;
1657 EXPORT_SYMBOL(rdma_set_service_type);
1659 static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
1660 void *context)
1662 struct cma_work *work = context;
1663 struct rdma_route *route;
1665 route = &work->id->id.route;
1667 if (!status) {
1668 route->num_paths = 1;
1669 *route->path_rec = *path_rec;
1670 } else {
1671 work->old_state = RDMA_CM_ROUTE_QUERY;
1672 work->new_state = RDMA_CM_ADDR_RESOLVED;
1673 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
1674 work->event.status = status;
1677 queue_work(cma_wq, &work->work);
1680 static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
1681 struct cma_work *work)
1683 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
1684 struct ib_sa_path_rec path_rec;
1685 ib_sa_comp_mask comp_mask;
1686 struct sockaddr_in6 *sin6;
1687 struct sockaddr_ib *sib;
1689 memset(&path_rec, 0, sizeof path_rec);
1690 rdma_addr_get_sgid(dev_addr, &path_rec.sgid);
1691 rdma_addr_get_dgid(dev_addr, &path_rec.dgid);
1692 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
1693 path_rec.numb_path = 1;
1694 path_rec.reversible = 1;
1695 path_rec.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
1697 comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
1698 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
1699 IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
1701 switch (cma_family(id_priv)) {
1702 case AF_INET:
1703 path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
1704 comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
1705 break;
1706 case AF_INET6:
1707 sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
1708 path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
1709 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
1710 break;
1711 case AF_IB:
1712 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
1713 path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20);
1714 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
1715 break;
1718 id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
1719 id_priv->id.port_num, &path_rec,
1720 comp_mask, timeout_ms,
1721 GFP_KERNEL, cma_query_handler,
1722 work, &id_priv->query);
1724 return (id_priv->query_id < 0) ? id_priv->query_id : 0;
1727 static void cma_work_handler(struct work_struct *_work)
1729 struct cma_work *work = container_of(_work, struct cma_work, work);
1730 struct rdma_id_private *id_priv = work->id;
1731 int destroy = 0;
1733 mutex_lock(&id_priv->handler_mutex);
1734 if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
1735 goto out;
1737 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
1738 cma_exch(id_priv, RDMA_CM_DESTROYING);
1739 destroy = 1;
1741 out:
1742 mutex_unlock(&id_priv->handler_mutex);
1743 cma_deref_id(id_priv);
1744 if (destroy)
1745 rdma_destroy_id(&id_priv->id);
1746 kfree(work);
1749 static void cma_ndev_work_handler(struct work_struct *_work)
1751 struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work);
1752 struct rdma_id_private *id_priv = work->id;
1753 int destroy = 0;
1755 mutex_lock(&id_priv->handler_mutex);
1756 if (id_priv->state == RDMA_CM_DESTROYING ||
1757 id_priv->state == RDMA_CM_DEVICE_REMOVAL)
1758 goto out;
1760 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
1761 cma_exch(id_priv, RDMA_CM_DESTROYING);
1762 destroy = 1;
1765 out:
1766 mutex_unlock(&id_priv->handler_mutex);
1767 cma_deref_id(id_priv);
1768 if (destroy)
1769 rdma_destroy_id(&id_priv->id);
1770 kfree(work);
1773 static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
1775 struct rdma_route *route = &id_priv->id.route;
1776 struct cma_work *work;
1777 int ret;
1779 work = kzalloc(sizeof *work, GFP_KERNEL);
1780 if (!work)
1781 return -ENOMEM;
1783 work->id = id_priv;
1784 INIT_WORK(&work->work, cma_work_handler);
1785 work->old_state = RDMA_CM_ROUTE_QUERY;
1786 work->new_state = RDMA_CM_ROUTE_RESOLVED;
1787 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1789 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
1790 if (!route->path_rec) {
1791 ret = -ENOMEM;
1792 goto err1;
1795 ret = cma_query_ib_route(id_priv, timeout_ms, work);
1796 if (ret)
1797 goto err2;
1799 return 0;
1800 err2:
1801 kfree(route->path_rec);
1802 route->path_rec = NULL;
1803 err1:
1804 kfree(work);
1805 return ret;
1808 int rdma_set_ib_paths(struct rdma_cm_id *id,
1809 struct ib_sa_path_rec *path_rec, int num_paths)
1811 struct rdma_id_private *id_priv;
1812 int ret;
1814 id_priv = container_of(id, struct rdma_id_private, id);
1815 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
1816 RDMA_CM_ROUTE_RESOLVED))
1817 return -EINVAL;
1819 id->route.path_rec = kmemdup(path_rec, sizeof *path_rec * num_paths,
1820 GFP_KERNEL);
1821 if (!id->route.path_rec) {
1822 ret = -ENOMEM;
1823 goto err;
1826 id->route.num_paths = num_paths;
1827 return 0;
1828 err:
1829 cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
1830 return ret;
1832 EXPORT_SYMBOL(rdma_set_ib_paths);
1834 static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
1836 struct cma_work *work;
1838 work = kzalloc(sizeof *work, GFP_KERNEL);
1839 if (!work)
1840 return -ENOMEM;
1842 work->id = id_priv;
1843 INIT_WORK(&work->work, cma_work_handler);
1844 work->old_state = RDMA_CM_ROUTE_QUERY;
1845 work->new_state = RDMA_CM_ROUTE_RESOLVED;
1846 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1847 queue_work(cma_wq, &work->work);
1848 return 0;
1851 static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
1853 struct rdma_route *route = &id_priv->id.route;
1854 struct rdma_addr *addr = &route->addr;
1855 struct cma_work *work;
1856 int ret;
1857 struct net_device *ndev = NULL;
1858 u16 vid;
1860 work = kzalloc(sizeof *work, GFP_KERNEL);
1861 if (!work)
1862 return -ENOMEM;
1864 work->id = id_priv;
1865 INIT_WORK(&work->work, cma_work_handler);
1867 route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
1868 if (!route->path_rec) {
1869 ret = -ENOMEM;
1870 goto err1;
1873 route->num_paths = 1;
1875 if (addr->dev_addr.bound_dev_if)
1876 ndev = dev_get_by_index(&init_net, addr->dev_addr.bound_dev_if);
1877 if (!ndev) {
1878 ret = -ENODEV;
1879 goto err2;
1882 vid = rdma_vlan_dev_vlan_id(ndev);
1884 iboe_mac_vlan_to_ll(&route->path_rec->sgid, addr->dev_addr.src_dev_addr, vid);
1885 iboe_mac_vlan_to_ll(&route->path_rec->dgid, addr->dev_addr.dst_dev_addr, vid);
1887 route->path_rec->hop_limit = 1;
1888 route->path_rec->reversible = 1;
1889 route->path_rec->pkey = cpu_to_be16(0xffff);
1890 route->path_rec->mtu_selector = IB_SA_EQ;
1891 route->path_rec->sl = netdev_get_prio_tc_map(
1892 ndev->priv_flags & IFF_802_1Q_VLAN ?
1893 vlan_dev_real_dev(ndev) : ndev,
1894 rt_tos2priority(id_priv->tos));
1896 route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
1897 route->path_rec->rate_selector = IB_SA_EQ;
1898 route->path_rec->rate = iboe_get_rate(ndev);
1899 dev_put(ndev);
1900 route->path_rec->packet_life_time_selector = IB_SA_EQ;
1901 route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
1902 if (!route->path_rec->mtu) {
1903 ret = -EINVAL;
1904 goto err2;
1907 work->old_state = RDMA_CM_ROUTE_QUERY;
1908 work->new_state = RDMA_CM_ROUTE_RESOLVED;
1909 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1910 work->event.status = 0;
1912 queue_work(cma_wq, &work->work);
1914 return 0;
1916 err2:
1917 kfree(route->path_rec);
1918 route->path_rec = NULL;
1919 err1:
1920 kfree(work);
1921 return ret;
1924 int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
1926 struct rdma_id_private *id_priv;
1927 int ret;
1929 id_priv = container_of(id, struct rdma_id_private, id);
1930 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
1931 return -EINVAL;
1933 atomic_inc(&id_priv->refcount);
1934 switch (rdma_node_get_transport(id->device->node_type)) {
1935 case RDMA_TRANSPORT_IB:
1936 switch (rdma_port_get_link_layer(id->device, id->port_num)) {
1937 case IB_LINK_LAYER_INFINIBAND:
1938 ret = cma_resolve_ib_route(id_priv, timeout_ms);
1939 break;
1940 case IB_LINK_LAYER_ETHERNET:
1941 ret = cma_resolve_iboe_route(id_priv);
1942 break;
1943 default:
1944 ret = -ENOSYS;
1946 break;
1947 case RDMA_TRANSPORT_IWARP:
1948 ret = cma_resolve_iw_route(id_priv, timeout_ms);
1949 break;
1950 default:
1951 ret = -ENOSYS;
1952 break;
1954 if (ret)
1955 goto err;
1957 return 0;
1958 err:
1959 cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
1960 cma_deref_id(id_priv);
1961 return ret;
1963 EXPORT_SYMBOL(rdma_resolve_route);
1965 static void cma_set_loopback(struct sockaddr *addr)
1967 switch (addr->sa_family) {
1968 case AF_INET:
1969 ((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1970 break;
1971 case AF_INET6:
1972 ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr,
1973 0, 0, 0, htonl(1));
1974 break;
1975 default:
1976 ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr,
1977 0, 0, 0, htonl(1));
1978 break;
1982 static int cma_bind_loopback(struct rdma_id_private *id_priv)
1984 struct cma_device *cma_dev, *cur_dev;
1985 struct ib_port_attr port_attr;
1986 union ib_gid gid;
1987 u16 pkey;
1988 int ret;
1989 u8 p;
1991 cma_dev = NULL;
1992 mutex_lock(&lock);
1993 list_for_each_entry(cur_dev, &dev_list, list) {
1994 if (cma_family(id_priv) == AF_IB &&
1995 rdma_node_get_transport(cur_dev->device->node_type) != RDMA_TRANSPORT_IB)
1996 continue;
1998 if (!cma_dev)
1999 cma_dev = cur_dev;
2001 for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
2002 if (!ib_query_port(cur_dev->device, p, &port_attr) &&
2003 port_attr.state == IB_PORT_ACTIVE) {
2004 cma_dev = cur_dev;
2005 goto port_found;
2010 if (!cma_dev) {
2011 ret = -ENODEV;
2012 goto out;
2015 p = 1;
2017 port_found:
2018 ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid);
2019 if (ret)
2020 goto out;
2022 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
2023 if (ret)
2024 goto out;
2026 id_priv->id.route.addr.dev_addr.dev_type =
2027 (rdma_port_get_link_layer(cma_dev->device, p) == IB_LINK_LAYER_INFINIBAND) ?
2028 ARPHRD_INFINIBAND : ARPHRD_ETHER;
2030 rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
2031 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
2032 id_priv->id.port_num = p;
2033 cma_attach_to_dev(id_priv, cma_dev);
2034 cma_set_loopback(cma_src_addr(id_priv));
2035 out:
2036 mutex_unlock(&lock);
2037 return ret;
2040 static void addr_handler(int status, struct sockaddr *src_addr,
2041 struct rdma_dev_addr *dev_addr, void *context)
2043 struct rdma_id_private *id_priv = context;
2044 struct rdma_cm_event event;
2046 memset(&event, 0, sizeof event);
2047 mutex_lock(&id_priv->handler_mutex);
2048 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
2049 RDMA_CM_ADDR_RESOLVED))
2050 goto out;
2052 if (!status && !id_priv->cma_dev)
2053 status = cma_acquire_dev(id_priv);
2055 if (status) {
2056 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2057 RDMA_CM_ADDR_BOUND))
2058 goto out;
2059 event.event = RDMA_CM_EVENT_ADDR_ERROR;
2060 event.status = status;
2061 } else {
2062 memcpy(cma_src_addr(id_priv), src_addr, rdma_addr_size(src_addr));
2063 event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2066 if (id_priv->id.event_handler(&id_priv->id, &event)) {
2067 cma_exch(id_priv, RDMA_CM_DESTROYING);
2068 mutex_unlock(&id_priv->handler_mutex);
2069 cma_deref_id(id_priv);
2070 rdma_destroy_id(&id_priv->id);
2071 return;
2073 out:
2074 mutex_unlock(&id_priv->handler_mutex);
2075 cma_deref_id(id_priv);
2078 static int cma_resolve_loopback(struct rdma_id_private *id_priv)
2080 struct cma_work *work;
2081 union ib_gid gid;
2082 int ret;
2084 work = kzalloc(sizeof *work, GFP_KERNEL);
2085 if (!work)
2086 return -ENOMEM;
2088 if (!id_priv->cma_dev) {
2089 ret = cma_bind_loopback(id_priv);
2090 if (ret)
2091 goto err;
2094 rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
2095 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
2097 work->id = id_priv;
2098 INIT_WORK(&work->work, cma_work_handler);
2099 work->old_state = RDMA_CM_ADDR_QUERY;
2100 work->new_state = RDMA_CM_ADDR_RESOLVED;
2101 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2102 queue_work(cma_wq, &work->work);
2103 return 0;
2104 err:
2105 kfree(work);
2106 return ret;
2109 static int cma_resolve_ib_addr(struct rdma_id_private *id_priv)
2111 struct cma_work *work;
2112 int ret;
2114 work = kzalloc(sizeof *work, GFP_KERNEL);
2115 if (!work)
2116 return -ENOMEM;
2118 if (!id_priv->cma_dev) {
2119 ret = cma_resolve_ib_dev(id_priv);
2120 if (ret)
2121 goto err;
2124 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *)
2125 &(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr));
2127 work->id = id_priv;
2128 INIT_WORK(&work->work, cma_work_handler);
2129 work->old_state = RDMA_CM_ADDR_QUERY;
2130 work->new_state = RDMA_CM_ADDR_RESOLVED;
2131 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2132 queue_work(cma_wq, &work->work);
2133 return 0;
2134 err:
2135 kfree(work);
2136 return ret;
2139 static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2140 struct sockaddr *dst_addr)
2142 if (!src_addr || !src_addr->sa_family) {
2143 src_addr = (struct sockaddr *) &id->route.addr.src_addr;
2144 src_addr->sa_family = dst_addr->sa_family;
2145 if (dst_addr->sa_family == AF_INET6) {
2146 ((struct sockaddr_in6 *) src_addr)->sin6_scope_id =
2147 ((struct sockaddr_in6 *) dst_addr)->sin6_scope_id;
2148 } else if (dst_addr->sa_family == AF_IB) {
2149 ((struct sockaddr_ib *) src_addr)->sib_pkey =
2150 ((struct sockaddr_ib *) dst_addr)->sib_pkey;
2153 return rdma_bind_addr(id, src_addr);
2156 int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2157 struct sockaddr *dst_addr, int timeout_ms)
2159 struct rdma_id_private *id_priv;
2160 int ret;
2162 id_priv = container_of(id, struct rdma_id_private, id);
2163 if (id_priv->state == RDMA_CM_IDLE) {
2164 ret = cma_bind_addr(id, src_addr, dst_addr);
2165 if (ret)
2166 return ret;
2169 if (cma_family(id_priv) != dst_addr->sa_family)
2170 return -EINVAL;
2172 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY))
2173 return -EINVAL;
2175 atomic_inc(&id_priv->refcount);
2176 memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr));
2177 if (cma_any_addr(dst_addr)) {
2178 ret = cma_resolve_loopback(id_priv);
2179 } else {
2180 if (dst_addr->sa_family == AF_IB) {
2181 ret = cma_resolve_ib_addr(id_priv);
2182 } else {
2183 ret = rdma_resolve_ip(&addr_client, cma_src_addr(id_priv),
2184 dst_addr, &id->route.addr.dev_addr,
2185 timeout_ms, addr_handler, id_priv);
2188 if (ret)
2189 goto err;
2191 return 0;
2192 err:
2193 cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
2194 cma_deref_id(id_priv);
2195 return ret;
2197 EXPORT_SYMBOL(rdma_resolve_addr);
2199 int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
2201 struct rdma_id_private *id_priv;
2202 unsigned long flags;
2203 int ret;
2205 id_priv = container_of(id, struct rdma_id_private, id);
2206 spin_lock_irqsave(&id_priv->lock, flags);
2207 if (reuse || id_priv->state == RDMA_CM_IDLE) {
2208 id_priv->reuseaddr = reuse;
2209 ret = 0;
2210 } else {
2211 ret = -EINVAL;
2213 spin_unlock_irqrestore(&id_priv->lock, flags);
2214 return ret;
2216 EXPORT_SYMBOL(rdma_set_reuseaddr);
2218 int rdma_set_afonly(struct rdma_cm_id *id, int afonly)
2220 struct rdma_id_private *id_priv;
2221 unsigned long flags;
2222 int ret;
2224 id_priv = container_of(id, struct rdma_id_private, id);
2225 spin_lock_irqsave(&id_priv->lock, flags);
2226 if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) {
2227 id_priv->options |= (1 << CMA_OPTION_AFONLY);
2228 id_priv->afonly = afonly;
2229 ret = 0;
2230 } else {
2231 ret = -EINVAL;
2233 spin_unlock_irqrestore(&id_priv->lock, flags);
2234 return ret;
2236 EXPORT_SYMBOL(rdma_set_afonly);
2238 static void cma_bind_port(struct rdma_bind_list *bind_list,
2239 struct rdma_id_private *id_priv)
2241 struct sockaddr *addr;
2242 struct sockaddr_ib *sib;
2243 u64 sid, mask;
2244 __be16 port;
2246 addr = cma_src_addr(id_priv);
2247 port = htons(bind_list->port);
2249 switch (addr->sa_family) {
2250 case AF_INET:
2251 ((struct sockaddr_in *) addr)->sin_port = port;
2252 break;
2253 case AF_INET6:
2254 ((struct sockaddr_in6 *) addr)->sin6_port = port;
2255 break;
2256 case AF_IB:
2257 sib = (struct sockaddr_ib *) addr;
2258 sid = be64_to_cpu(sib->sib_sid);
2259 mask = be64_to_cpu(sib->sib_sid_mask);
2260 sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port));
2261 sib->sib_sid_mask = cpu_to_be64(~0ULL);
2262 break;
2264 id_priv->bind_list = bind_list;
2265 hlist_add_head(&id_priv->node, &bind_list->owners);
2268 static int cma_alloc_port(struct idr *ps, struct rdma_id_private *id_priv,
2269 unsigned short snum)
2271 struct rdma_bind_list *bind_list;
2272 int ret;
2274 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
2275 if (!bind_list)
2276 return -ENOMEM;
2278 ret = idr_alloc(ps, bind_list, snum, snum + 1, GFP_KERNEL);
2279 if (ret < 0)
2280 goto err;
2282 bind_list->ps = ps;
2283 bind_list->port = (unsigned short)ret;
2284 cma_bind_port(bind_list, id_priv);
2285 return 0;
2286 err:
2287 kfree(bind_list);
2288 return ret == -ENOSPC ? -EADDRNOTAVAIL : ret;
2291 static int cma_alloc_any_port(struct idr *ps, struct rdma_id_private *id_priv)
2293 static unsigned int last_used_port;
2294 int low, high, remaining;
2295 unsigned int rover;
2297 inet_get_local_port_range(&low, &high);
2298 remaining = (high - low) + 1;
2299 rover = net_random() % remaining + low;
2300 retry:
2301 if (last_used_port != rover &&
2302 !idr_find(ps, (unsigned short) rover)) {
2303 int ret = cma_alloc_port(ps, id_priv, rover);
2305 * Remember previously used port number in order to avoid
2306 * re-using same port immediately after it is closed.
2308 if (!ret)
2309 last_used_port = rover;
2310 if (ret != -EADDRNOTAVAIL)
2311 return ret;
2313 if (--remaining) {
2314 rover++;
2315 if ((rover < low) || (rover > high))
2316 rover = low;
2317 goto retry;
2319 return -EADDRNOTAVAIL;
2323 * Check that the requested port is available. This is called when trying to
2324 * bind to a specific port, or when trying to listen on a bound port. In
2325 * the latter case, the provided id_priv may already be on the bind_list, but
2326 * we still need to check that it's okay to start listening.
2328 static int cma_check_port(struct rdma_bind_list *bind_list,
2329 struct rdma_id_private *id_priv, uint8_t reuseaddr)
2331 struct rdma_id_private *cur_id;
2332 struct sockaddr *addr, *cur_addr;
2334 addr = cma_src_addr(id_priv);
2335 hlist_for_each_entry(cur_id, &bind_list->owners, node) {
2336 if (id_priv == cur_id)
2337 continue;
2339 if ((cur_id->state != RDMA_CM_LISTEN) && reuseaddr &&
2340 cur_id->reuseaddr)
2341 continue;
2343 cur_addr = cma_src_addr(cur_id);
2344 if (id_priv->afonly && cur_id->afonly &&
2345 (addr->sa_family != cur_addr->sa_family))
2346 continue;
2348 if (cma_any_addr(addr) || cma_any_addr(cur_addr))
2349 return -EADDRNOTAVAIL;
2351 if (!cma_addr_cmp(addr, cur_addr))
2352 return -EADDRINUSE;
2354 return 0;
2357 static int cma_use_port(struct idr *ps, struct rdma_id_private *id_priv)
2359 struct rdma_bind_list *bind_list;
2360 unsigned short snum;
2361 int ret;
2363 snum = ntohs(cma_port(cma_src_addr(id_priv)));
2364 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
2365 return -EACCES;
2367 bind_list = idr_find(ps, snum);
2368 if (!bind_list) {
2369 ret = cma_alloc_port(ps, id_priv, snum);
2370 } else {
2371 ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
2372 if (!ret)
2373 cma_bind_port(bind_list, id_priv);
2375 return ret;
2378 static int cma_bind_listen(struct rdma_id_private *id_priv)
2380 struct rdma_bind_list *bind_list = id_priv->bind_list;
2381 int ret = 0;
2383 mutex_lock(&lock);
2384 if (bind_list->owners.first->next)
2385 ret = cma_check_port(bind_list, id_priv, 0);
2386 mutex_unlock(&lock);
2387 return ret;
2390 static struct idr *cma_select_inet_ps(struct rdma_id_private *id_priv)
2392 switch (id_priv->id.ps) {
2393 case RDMA_PS_TCP:
2394 return &tcp_ps;
2395 case RDMA_PS_UDP:
2396 return &udp_ps;
2397 case RDMA_PS_IPOIB:
2398 return &ipoib_ps;
2399 case RDMA_PS_IB:
2400 return &ib_ps;
2401 default:
2402 return NULL;
2406 static struct idr *cma_select_ib_ps(struct rdma_id_private *id_priv)
2408 struct idr *ps = NULL;
2409 struct sockaddr_ib *sib;
2410 u64 sid_ps, mask, sid;
2412 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
2413 mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK;
2414 sid = be64_to_cpu(sib->sib_sid) & mask;
2416 if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) {
2417 sid_ps = RDMA_IB_IP_PS_IB;
2418 ps = &ib_ps;
2419 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) &&
2420 (sid == (RDMA_IB_IP_PS_TCP & mask))) {
2421 sid_ps = RDMA_IB_IP_PS_TCP;
2422 ps = &tcp_ps;
2423 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) &&
2424 (sid == (RDMA_IB_IP_PS_UDP & mask))) {
2425 sid_ps = RDMA_IB_IP_PS_UDP;
2426 ps = &udp_ps;
2429 if (ps) {
2430 sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib)));
2431 sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK |
2432 be64_to_cpu(sib->sib_sid_mask));
2434 return ps;
2437 static int cma_get_port(struct rdma_id_private *id_priv)
2439 struct idr *ps;
2440 int ret;
2442 if (cma_family(id_priv) != AF_IB)
2443 ps = cma_select_inet_ps(id_priv);
2444 else
2445 ps = cma_select_ib_ps(id_priv);
2446 if (!ps)
2447 return -EPROTONOSUPPORT;
2449 mutex_lock(&lock);
2450 if (cma_any_port(cma_src_addr(id_priv)))
2451 ret = cma_alloc_any_port(ps, id_priv);
2452 else
2453 ret = cma_use_port(ps, id_priv);
2454 mutex_unlock(&lock);
2456 return ret;
2459 static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
2460 struct sockaddr *addr)
2462 #if IS_ENABLED(CONFIG_IPV6)
2463 struct sockaddr_in6 *sin6;
2465 if (addr->sa_family != AF_INET6)
2466 return 0;
2468 sin6 = (struct sockaddr_in6 *) addr;
2469 if ((ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) &&
2470 !sin6->sin6_scope_id)
2471 return -EINVAL;
2473 dev_addr->bound_dev_if = sin6->sin6_scope_id;
2474 #endif
2475 return 0;
2478 int rdma_listen(struct rdma_cm_id *id, int backlog)
2480 struct rdma_id_private *id_priv;
2481 int ret;
2483 id_priv = container_of(id, struct rdma_id_private, id);
2484 if (id_priv->state == RDMA_CM_IDLE) {
2485 id->route.addr.src_addr.ss_family = AF_INET;
2486 ret = rdma_bind_addr(id, cma_src_addr(id_priv));
2487 if (ret)
2488 return ret;
2491 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN))
2492 return -EINVAL;
2494 if (id_priv->reuseaddr) {
2495 ret = cma_bind_listen(id_priv);
2496 if (ret)
2497 goto err;
2500 id_priv->backlog = backlog;
2501 if (id->device) {
2502 switch (rdma_node_get_transport(id->device->node_type)) {
2503 case RDMA_TRANSPORT_IB:
2504 ret = cma_ib_listen(id_priv);
2505 if (ret)
2506 goto err;
2507 break;
2508 case RDMA_TRANSPORT_IWARP:
2509 ret = cma_iw_listen(id_priv, backlog);
2510 if (ret)
2511 goto err;
2512 break;
2513 default:
2514 ret = -ENOSYS;
2515 goto err;
2517 } else
2518 cma_listen_on_all(id_priv);
2520 return 0;
2521 err:
2522 id_priv->backlog = 0;
2523 cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
2524 return ret;
2526 EXPORT_SYMBOL(rdma_listen);
2528 int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
2530 struct rdma_id_private *id_priv;
2531 int ret;
2533 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 &&
2534 addr->sa_family != AF_IB)
2535 return -EAFNOSUPPORT;
2537 id_priv = container_of(id, struct rdma_id_private, id);
2538 if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
2539 return -EINVAL;
2541 ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
2542 if (ret)
2543 goto err1;
2545 if (!cma_any_addr(addr)) {
2546 ret = cma_translate_addr(addr, &id->route.addr.dev_addr);
2547 if (ret)
2548 goto err1;
2550 ret = cma_acquire_dev(id_priv);
2551 if (ret)
2552 goto err1;
2555 memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr));
2556 if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) {
2557 if (addr->sa_family == AF_INET)
2558 id_priv->afonly = 1;
2559 #if IS_ENABLED(CONFIG_IPV6)
2560 else if (addr->sa_family == AF_INET6)
2561 id_priv->afonly = init_net.ipv6.sysctl.bindv6only;
2562 #endif
2564 ret = cma_get_port(id_priv);
2565 if (ret)
2566 goto err2;
2568 return 0;
2569 err2:
2570 if (id_priv->cma_dev)
2571 cma_release_dev(id_priv);
2572 err1:
2573 cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
2574 return ret;
2576 EXPORT_SYMBOL(rdma_bind_addr);
2578 static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv)
2580 struct cma_hdr *cma_hdr;
2582 cma_hdr = hdr;
2583 cma_hdr->cma_version = CMA_VERSION;
2584 if (cma_family(id_priv) == AF_INET) {
2585 struct sockaddr_in *src4, *dst4;
2587 src4 = (struct sockaddr_in *) cma_src_addr(id_priv);
2588 dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv);
2590 cma_set_ip_ver(cma_hdr, 4);
2591 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
2592 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
2593 cma_hdr->port = src4->sin_port;
2594 } else if (cma_family(id_priv) == AF_INET6) {
2595 struct sockaddr_in6 *src6, *dst6;
2597 src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
2598 dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv);
2600 cma_set_ip_ver(cma_hdr, 6);
2601 cma_hdr->src_addr.ip6 = src6->sin6_addr;
2602 cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
2603 cma_hdr->port = src6->sin6_port;
2605 return 0;
2608 static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
2609 struct ib_cm_event *ib_event)
2611 struct rdma_id_private *id_priv = cm_id->context;
2612 struct rdma_cm_event event;
2613 struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
2614 int ret = 0;
2616 if (cma_disable_callback(id_priv, RDMA_CM_CONNECT))
2617 return 0;
2619 memset(&event, 0, sizeof event);
2620 switch (ib_event->event) {
2621 case IB_CM_SIDR_REQ_ERROR:
2622 event.event = RDMA_CM_EVENT_UNREACHABLE;
2623 event.status = -ETIMEDOUT;
2624 break;
2625 case IB_CM_SIDR_REP_RECEIVED:
2626 event.param.ud.private_data = ib_event->private_data;
2627 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
2628 if (rep->status != IB_SIDR_SUCCESS) {
2629 event.event = RDMA_CM_EVENT_UNREACHABLE;
2630 event.status = ib_event->param.sidr_rep_rcvd.status;
2631 break;
2633 ret = cma_set_qkey(id_priv, rep->qkey);
2634 if (ret) {
2635 event.event = RDMA_CM_EVENT_ADDR_ERROR;
2636 event.status = ret;
2637 break;
2639 ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
2640 id_priv->id.route.path_rec,
2641 &event.param.ud.ah_attr);
2642 event.param.ud.qp_num = rep->qpn;
2643 event.param.ud.qkey = rep->qkey;
2644 event.event = RDMA_CM_EVENT_ESTABLISHED;
2645 event.status = 0;
2646 break;
2647 default:
2648 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
2649 ib_event->event);
2650 goto out;
2653 ret = id_priv->id.event_handler(&id_priv->id, &event);
2654 if (ret) {
2655 /* Destroy the CM ID by returning a non-zero value. */
2656 id_priv->cm_id.ib = NULL;
2657 cma_exch(id_priv, RDMA_CM_DESTROYING);
2658 mutex_unlock(&id_priv->handler_mutex);
2659 rdma_destroy_id(&id_priv->id);
2660 return ret;
2662 out:
2663 mutex_unlock(&id_priv->handler_mutex);
2664 return ret;
2667 static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
2668 struct rdma_conn_param *conn_param)
2670 struct ib_cm_sidr_req_param req;
2671 struct ib_cm_id *id;
2672 void *private_data;
2673 int offset, ret;
2675 memset(&req, 0, sizeof req);
2676 offset = cma_user_data_offset(id_priv);
2677 req.private_data_len = offset + conn_param->private_data_len;
2678 if (req.private_data_len < conn_param->private_data_len)
2679 return -EINVAL;
2681 if (req.private_data_len) {
2682 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2683 if (!private_data)
2684 return -ENOMEM;
2685 } else {
2686 private_data = NULL;
2689 if (conn_param->private_data && conn_param->private_data_len)
2690 memcpy(private_data + offset, conn_param->private_data,
2691 conn_param->private_data_len);
2693 if (private_data) {
2694 ret = cma_format_hdr(private_data, id_priv);
2695 if (ret)
2696 goto out;
2697 req.private_data = private_data;
2700 id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
2701 id_priv);
2702 if (IS_ERR(id)) {
2703 ret = PTR_ERR(id);
2704 goto out;
2706 id_priv->cm_id.ib = id;
2708 req.path = id_priv->id.route.path_rec;
2709 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
2710 req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
2711 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2713 ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
2714 if (ret) {
2715 ib_destroy_cm_id(id_priv->cm_id.ib);
2716 id_priv->cm_id.ib = NULL;
2718 out:
2719 kfree(private_data);
2720 return ret;
2723 static int cma_connect_ib(struct rdma_id_private *id_priv,
2724 struct rdma_conn_param *conn_param)
2726 struct ib_cm_req_param req;
2727 struct rdma_route *route;
2728 void *private_data;
2729 struct ib_cm_id *id;
2730 int offset, ret;
2732 memset(&req, 0, sizeof req);
2733 offset = cma_user_data_offset(id_priv);
2734 req.private_data_len = offset + conn_param->private_data_len;
2735 if (req.private_data_len < conn_param->private_data_len)
2736 return -EINVAL;
2738 if (req.private_data_len) {
2739 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2740 if (!private_data)
2741 return -ENOMEM;
2742 } else {
2743 private_data = NULL;
2746 if (conn_param->private_data && conn_param->private_data_len)
2747 memcpy(private_data + offset, conn_param->private_data,
2748 conn_param->private_data_len);
2750 id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
2751 if (IS_ERR(id)) {
2752 ret = PTR_ERR(id);
2753 goto out;
2755 id_priv->cm_id.ib = id;
2757 route = &id_priv->id.route;
2758 if (private_data) {
2759 ret = cma_format_hdr(private_data, id_priv);
2760 if (ret)
2761 goto out;
2762 req.private_data = private_data;
2765 req.primary_path = &route->path_rec[0];
2766 if (route->num_paths == 2)
2767 req.alternate_path = &route->path_rec[1];
2769 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
2770 req.qp_num = id_priv->qp_num;
2771 req.qp_type = id_priv->id.qp_type;
2772 req.starting_psn = id_priv->seq_num;
2773 req.responder_resources = conn_param->responder_resources;
2774 req.initiator_depth = conn_param->initiator_depth;
2775 req.flow_control = conn_param->flow_control;
2776 req.retry_count = min_t(u8, 7, conn_param->retry_count);
2777 req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
2778 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2779 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2780 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2781 req.srq = id_priv->srq ? 1 : 0;
2783 ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
2784 out:
2785 if (ret && !IS_ERR(id)) {
2786 ib_destroy_cm_id(id);
2787 id_priv->cm_id.ib = NULL;
2790 kfree(private_data);
2791 return ret;
2794 static int cma_connect_iw(struct rdma_id_private *id_priv,
2795 struct rdma_conn_param *conn_param)
2797 struct iw_cm_id *cm_id;
2798 int ret;
2799 struct iw_cm_conn_param iw_param;
2801 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
2802 if (IS_ERR(cm_id))
2803 return PTR_ERR(cm_id);
2805 id_priv->cm_id.iw = cm_id;
2807 memcpy(&cm_id->local_addr, cma_src_addr(id_priv),
2808 rdma_addr_size(cma_src_addr(id_priv)));
2809 memcpy(&cm_id->remote_addr, cma_dst_addr(id_priv),
2810 rdma_addr_size(cma_dst_addr(id_priv)));
2812 ret = cma_modify_qp_rtr(id_priv, conn_param);
2813 if (ret)
2814 goto out;
2816 if (conn_param) {
2817 iw_param.ord = conn_param->initiator_depth;
2818 iw_param.ird = conn_param->responder_resources;
2819 iw_param.private_data = conn_param->private_data;
2820 iw_param.private_data_len = conn_param->private_data_len;
2821 iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num;
2822 } else {
2823 memset(&iw_param, 0, sizeof iw_param);
2824 iw_param.qpn = id_priv->qp_num;
2826 ret = iw_cm_connect(cm_id, &iw_param);
2827 out:
2828 if (ret) {
2829 iw_destroy_cm_id(cm_id);
2830 id_priv->cm_id.iw = NULL;
2832 return ret;
2835 int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2837 struct rdma_id_private *id_priv;
2838 int ret;
2840 id_priv = container_of(id, struct rdma_id_private, id);
2841 if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
2842 return -EINVAL;
2844 if (!id->qp) {
2845 id_priv->qp_num = conn_param->qp_num;
2846 id_priv->srq = conn_param->srq;
2849 switch (rdma_node_get_transport(id->device->node_type)) {
2850 case RDMA_TRANSPORT_IB:
2851 if (id->qp_type == IB_QPT_UD)
2852 ret = cma_resolve_ib_udp(id_priv, conn_param);
2853 else
2854 ret = cma_connect_ib(id_priv, conn_param);
2855 break;
2856 case RDMA_TRANSPORT_IWARP:
2857 ret = cma_connect_iw(id_priv, conn_param);
2858 break;
2859 default:
2860 ret = -ENOSYS;
2861 break;
2863 if (ret)
2864 goto err;
2866 return 0;
2867 err:
2868 cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
2869 return ret;
2871 EXPORT_SYMBOL(rdma_connect);
2873 static int cma_accept_ib(struct rdma_id_private *id_priv,
2874 struct rdma_conn_param *conn_param)
2876 struct ib_cm_rep_param rep;
2877 int ret;
2879 ret = cma_modify_qp_rtr(id_priv, conn_param);
2880 if (ret)
2881 goto out;
2883 ret = cma_modify_qp_rts(id_priv, conn_param);
2884 if (ret)
2885 goto out;
2887 memset(&rep, 0, sizeof rep);
2888 rep.qp_num = id_priv->qp_num;
2889 rep.starting_psn = id_priv->seq_num;
2890 rep.private_data = conn_param->private_data;
2891 rep.private_data_len = conn_param->private_data_len;
2892 rep.responder_resources = conn_param->responder_resources;
2893 rep.initiator_depth = conn_param->initiator_depth;
2894 rep.failover_accepted = 0;
2895 rep.flow_control = conn_param->flow_control;
2896 rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
2897 rep.srq = id_priv->srq ? 1 : 0;
2899 ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
2900 out:
2901 return ret;
2904 static int cma_accept_iw(struct rdma_id_private *id_priv,
2905 struct rdma_conn_param *conn_param)
2907 struct iw_cm_conn_param iw_param;
2908 int ret;
2910 ret = cma_modify_qp_rtr(id_priv, conn_param);
2911 if (ret)
2912 return ret;
2914 iw_param.ord = conn_param->initiator_depth;
2915 iw_param.ird = conn_param->responder_resources;
2916 iw_param.private_data = conn_param->private_data;
2917 iw_param.private_data_len = conn_param->private_data_len;
2918 if (id_priv->id.qp) {
2919 iw_param.qpn = id_priv->qp_num;
2920 } else
2921 iw_param.qpn = conn_param->qp_num;
2923 return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
2926 static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
2927 enum ib_cm_sidr_status status, u32 qkey,
2928 const void *private_data, int private_data_len)
2930 struct ib_cm_sidr_rep_param rep;
2931 int ret;
2933 memset(&rep, 0, sizeof rep);
2934 rep.status = status;
2935 if (status == IB_SIDR_SUCCESS) {
2936 ret = cma_set_qkey(id_priv, qkey);
2937 if (ret)
2938 return ret;
2939 rep.qp_num = id_priv->qp_num;
2940 rep.qkey = id_priv->qkey;
2942 rep.private_data = private_data;
2943 rep.private_data_len = private_data_len;
2945 return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
2948 int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2950 struct rdma_id_private *id_priv;
2951 int ret;
2953 id_priv = container_of(id, struct rdma_id_private, id);
2955 id_priv->owner = task_pid_nr(current);
2957 if (!cma_comp(id_priv, RDMA_CM_CONNECT))
2958 return -EINVAL;
2960 if (!id->qp && conn_param) {
2961 id_priv->qp_num = conn_param->qp_num;
2962 id_priv->srq = conn_param->srq;
2965 switch (rdma_node_get_transport(id->device->node_type)) {
2966 case RDMA_TRANSPORT_IB:
2967 if (id->qp_type == IB_QPT_UD) {
2968 if (conn_param)
2969 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
2970 conn_param->qkey,
2971 conn_param->private_data,
2972 conn_param->private_data_len);
2973 else
2974 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
2975 0, NULL, 0);
2976 } else {
2977 if (conn_param)
2978 ret = cma_accept_ib(id_priv, conn_param);
2979 else
2980 ret = cma_rep_recv(id_priv);
2982 break;
2983 case RDMA_TRANSPORT_IWARP:
2984 ret = cma_accept_iw(id_priv, conn_param);
2985 break;
2986 default:
2987 ret = -ENOSYS;
2988 break;
2991 if (ret)
2992 goto reject;
2994 return 0;
2995 reject:
2996 cma_modify_qp_err(id_priv);
2997 rdma_reject(id, NULL, 0);
2998 return ret;
3000 EXPORT_SYMBOL(rdma_accept);
3002 int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
3004 struct rdma_id_private *id_priv;
3005 int ret;
3007 id_priv = container_of(id, struct rdma_id_private, id);
3008 if (!id_priv->cm_id.ib)
3009 return -EINVAL;
3011 switch (id->device->node_type) {
3012 case RDMA_NODE_IB_CA:
3013 ret = ib_cm_notify(id_priv->cm_id.ib, event);
3014 break;
3015 default:
3016 ret = 0;
3017 break;
3019 return ret;
3021 EXPORT_SYMBOL(rdma_notify);
3023 int rdma_reject(struct rdma_cm_id *id, const void *private_data,
3024 u8 private_data_len)
3026 struct rdma_id_private *id_priv;
3027 int ret;
3029 id_priv = container_of(id, struct rdma_id_private, id);
3030 if (!id_priv->cm_id.ib)
3031 return -EINVAL;
3033 switch (rdma_node_get_transport(id->device->node_type)) {
3034 case RDMA_TRANSPORT_IB:
3035 if (id->qp_type == IB_QPT_UD)
3036 ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0,
3037 private_data, private_data_len);
3038 else
3039 ret = ib_send_cm_rej(id_priv->cm_id.ib,
3040 IB_CM_REJ_CONSUMER_DEFINED, NULL,
3041 0, private_data, private_data_len);
3042 break;
3043 case RDMA_TRANSPORT_IWARP:
3044 ret = iw_cm_reject(id_priv->cm_id.iw,
3045 private_data, private_data_len);
3046 break;
3047 default:
3048 ret = -ENOSYS;
3049 break;
3051 return ret;
3053 EXPORT_SYMBOL(rdma_reject);
3055 int rdma_disconnect(struct rdma_cm_id *id)
3057 struct rdma_id_private *id_priv;
3058 int ret;
3060 id_priv = container_of(id, struct rdma_id_private, id);
3061 if (!id_priv->cm_id.ib)
3062 return -EINVAL;
3064 switch (rdma_node_get_transport(id->device->node_type)) {
3065 case RDMA_TRANSPORT_IB:
3066 ret = cma_modify_qp_err(id_priv);
3067 if (ret)
3068 goto out;
3069 /* Initiate or respond to a disconnect. */
3070 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
3071 ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
3072 break;
3073 case RDMA_TRANSPORT_IWARP:
3074 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
3075 break;
3076 default:
3077 ret = -EINVAL;
3078 break;
3080 out:
3081 return ret;
3083 EXPORT_SYMBOL(rdma_disconnect);
3085 static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
3087 struct rdma_id_private *id_priv;
3088 struct cma_multicast *mc = multicast->context;
3089 struct rdma_cm_event event;
3090 int ret;
3092 id_priv = mc->id_priv;
3093 if (cma_disable_callback(id_priv, RDMA_CM_ADDR_BOUND) &&
3094 cma_disable_callback(id_priv, RDMA_CM_ADDR_RESOLVED))
3095 return 0;
3097 if (!status)
3098 status = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey));
3099 mutex_lock(&id_priv->qp_mutex);
3100 if (!status && id_priv->id.qp)
3101 status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
3102 be16_to_cpu(multicast->rec.mlid));
3103 mutex_unlock(&id_priv->qp_mutex);
3105 memset(&event, 0, sizeof event);
3106 event.status = status;
3107 event.param.ud.private_data = mc->context;
3108 if (!status) {
3109 event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
3110 ib_init_ah_from_mcmember(id_priv->id.device,
3111 id_priv->id.port_num, &multicast->rec,
3112 &event.param.ud.ah_attr);
3113 event.param.ud.qp_num = 0xFFFFFF;
3114 event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
3115 } else
3116 event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
3118 ret = id_priv->id.event_handler(&id_priv->id, &event);
3119 if (ret) {
3120 cma_exch(id_priv, RDMA_CM_DESTROYING);
3121 mutex_unlock(&id_priv->handler_mutex);
3122 rdma_destroy_id(&id_priv->id);
3123 return 0;
3126 mutex_unlock(&id_priv->handler_mutex);
3127 return 0;
3130 static void cma_set_mgid(struct rdma_id_private *id_priv,
3131 struct sockaddr *addr, union ib_gid *mgid)
3133 unsigned char mc_map[MAX_ADDR_LEN];
3134 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3135 struct sockaddr_in *sin = (struct sockaddr_in *) addr;
3136 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
3138 if (cma_any_addr(addr)) {
3139 memset(mgid, 0, sizeof *mgid);
3140 } else if ((addr->sa_family == AF_INET6) &&
3141 ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
3142 0xFF10A01B)) {
3143 /* IPv6 address is an SA assigned MGID. */
3144 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
3145 } else if (addr->sa_family == AF_IB) {
3146 memcpy(mgid, &((struct sockaddr_ib *) addr)->sib_addr, sizeof *mgid);
3147 } else if ((addr->sa_family == AF_INET6)) {
3148 ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
3149 if (id_priv->id.ps == RDMA_PS_UDP)
3150 mc_map[7] = 0x01; /* Use RDMA CM signature */
3151 *mgid = *(union ib_gid *) (mc_map + 4);
3152 } else {
3153 ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
3154 if (id_priv->id.ps == RDMA_PS_UDP)
3155 mc_map[7] = 0x01; /* Use RDMA CM signature */
3156 *mgid = *(union ib_gid *) (mc_map + 4);
3160 static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
3161 struct cma_multicast *mc)
3163 struct ib_sa_mcmember_rec rec;
3164 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3165 ib_sa_comp_mask comp_mask;
3166 int ret;
3168 ib_addr_get_mgid(dev_addr, &rec.mgid);
3169 ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
3170 &rec.mgid, &rec);
3171 if (ret)
3172 return ret;
3174 ret = cma_set_qkey(id_priv, 0);
3175 if (ret)
3176 return ret;
3178 cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
3179 rec.qkey = cpu_to_be32(id_priv->qkey);
3180 rdma_addr_get_sgid(dev_addr, &rec.port_gid);
3181 rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
3182 rec.join_state = 1;
3184 comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
3185 IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
3186 IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
3187 IB_SA_MCMEMBER_REC_FLOW_LABEL |
3188 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
3190 if (id_priv->id.ps == RDMA_PS_IPOIB)
3191 comp_mask |= IB_SA_MCMEMBER_REC_RATE |
3192 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
3193 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
3194 IB_SA_MCMEMBER_REC_MTU |
3195 IB_SA_MCMEMBER_REC_HOP_LIMIT;
3197 mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
3198 id_priv->id.port_num, &rec,
3199 comp_mask, GFP_KERNEL,
3200 cma_ib_mc_handler, mc);
3201 return PTR_ERR_OR_ZERO(mc->multicast.ib);
3204 static void iboe_mcast_work_handler(struct work_struct *work)
3206 struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work);
3207 struct cma_multicast *mc = mw->mc;
3208 struct ib_sa_multicast *m = mc->multicast.ib;
3210 mc->multicast.ib->context = mc;
3211 cma_ib_mc_handler(0, m);
3212 kref_put(&mc->mcref, release_mc);
3213 kfree(mw);
3216 static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid)
3218 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
3219 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
3221 if (cma_any_addr(addr)) {
3222 memset(mgid, 0, sizeof *mgid);
3223 } else if (addr->sa_family == AF_INET6) {
3224 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
3225 } else {
3226 mgid->raw[0] = 0xff;
3227 mgid->raw[1] = 0x0e;
3228 mgid->raw[2] = 0;
3229 mgid->raw[3] = 0;
3230 mgid->raw[4] = 0;
3231 mgid->raw[5] = 0;
3232 mgid->raw[6] = 0;
3233 mgid->raw[7] = 0;
3234 mgid->raw[8] = 0;
3235 mgid->raw[9] = 0;
3236 mgid->raw[10] = 0xff;
3237 mgid->raw[11] = 0xff;
3238 *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
3242 static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
3243 struct cma_multicast *mc)
3245 struct iboe_mcast_work *work;
3246 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3247 int err;
3248 struct sockaddr *addr = (struct sockaddr *)&mc->addr;
3249 struct net_device *ndev = NULL;
3251 if (cma_zero_addr((struct sockaddr *)&mc->addr))
3252 return -EINVAL;
3254 work = kzalloc(sizeof *work, GFP_KERNEL);
3255 if (!work)
3256 return -ENOMEM;
3258 mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL);
3259 if (!mc->multicast.ib) {
3260 err = -ENOMEM;
3261 goto out1;
3264 cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid);
3266 mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff);
3267 if (id_priv->id.ps == RDMA_PS_UDP)
3268 mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
3270 if (dev_addr->bound_dev_if)
3271 ndev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
3272 if (!ndev) {
3273 err = -ENODEV;
3274 goto out2;
3276 mc->multicast.ib->rec.rate = iboe_get_rate(ndev);
3277 mc->multicast.ib->rec.hop_limit = 1;
3278 mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu);
3279 dev_put(ndev);
3280 if (!mc->multicast.ib->rec.mtu) {
3281 err = -EINVAL;
3282 goto out2;
3284 iboe_addr_get_sgid(dev_addr, &mc->multicast.ib->rec.port_gid);
3285 work->id = id_priv;
3286 work->mc = mc;
3287 INIT_WORK(&work->work, iboe_mcast_work_handler);
3288 kref_get(&mc->mcref);
3289 queue_work(cma_wq, &work->work);
3291 return 0;
3293 out2:
3294 kfree(mc->multicast.ib);
3295 out1:
3296 kfree(work);
3297 return err;
3300 int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
3301 void *context)
3303 struct rdma_id_private *id_priv;
3304 struct cma_multicast *mc;
3305 int ret;
3307 id_priv = container_of(id, struct rdma_id_private, id);
3308 if (!cma_comp(id_priv, RDMA_CM_ADDR_BOUND) &&
3309 !cma_comp(id_priv, RDMA_CM_ADDR_RESOLVED))
3310 return -EINVAL;
3312 mc = kmalloc(sizeof *mc, GFP_KERNEL);
3313 if (!mc)
3314 return -ENOMEM;
3316 memcpy(&mc->addr, addr, rdma_addr_size(addr));
3317 mc->context = context;
3318 mc->id_priv = id_priv;
3320 spin_lock(&id_priv->lock);
3321 list_add(&mc->list, &id_priv->mc_list);
3322 spin_unlock(&id_priv->lock);
3324 switch (rdma_node_get_transport(id->device->node_type)) {
3325 case RDMA_TRANSPORT_IB:
3326 switch (rdma_port_get_link_layer(id->device, id->port_num)) {
3327 case IB_LINK_LAYER_INFINIBAND:
3328 ret = cma_join_ib_multicast(id_priv, mc);
3329 break;
3330 case IB_LINK_LAYER_ETHERNET:
3331 kref_init(&mc->mcref);
3332 ret = cma_iboe_join_multicast(id_priv, mc);
3333 break;
3334 default:
3335 ret = -EINVAL;
3337 break;
3338 default:
3339 ret = -ENOSYS;
3340 break;
3343 if (ret) {
3344 spin_lock_irq(&id_priv->lock);
3345 list_del(&mc->list);
3346 spin_unlock_irq(&id_priv->lock);
3347 kfree(mc);
3349 return ret;
3351 EXPORT_SYMBOL(rdma_join_multicast);
3353 void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
3355 struct rdma_id_private *id_priv;
3356 struct cma_multicast *mc;
3358 id_priv = container_of(id, struct rdma_id_private, id);
3359 spin_lock_irq(&id_priv->lock);
3360 list_for_each_entry(mc, &id_priv->mc_list, list) {
3361 if (!memcmp(&mc->addr, addr, rdma_addr_size(addr))) {
3362 list_del(&mc->list);
3363 spin_unlock_irq(&id_priv->lock);
3365 if (id->qp)
3366 ib_detach_mcast(id->qp,
3367 &mc->multicast.ib->rec.mgid,
3368 be16_to_cpu(mc->multicast.ib->rec.mlid));
3369 if (rdma_node_get_transport(id_priv->cma_dev->device->node_type) == RDMA_TRANSPORT_IB) {
3370 switch (rdma_port_get_link_layer(id->device, id->port_num)) {
3371 case IB_LINK_LAYER_INFINIBAND:
3372 ib_sa_free_multicast(mc->multicast.ib);
3373 kfree(mc);
3374 break;
3375 case IB_LINK_LAYER_ETHERNET:
3376 kref_put(&mc->mcref, release_mc);
3377 break;
3378 default:
3379 break;
3382 return;
3385 spin_unlock_irq(&id_priv->lock);
3387 EXPORT_SYMBOL(rdma_leave_multicast);
3389 static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
3391 struct rdma_dev_addr *dev_addr;
3392 struct cma_ndev_work *work;
3394 dev_addr = &id_priv->id.route.addr.dev_addr;
3396 if ((dev_addr->bound_dev_if == ndev->ifindex) &&
3397 memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
3398 printk(KERN_INFO "RDMA CM addr change for ndev %s used by id %p\n",
3399 ndev->name, &id_priv->id);
3400 work = kzalloc(sizeof *work, GFP_KERNEL);
3401 if (!work)
3402 return -ENOMEM;
3404 INIT_WORK(&work->work, cma_ndev_work_handler);
3405 work->id = id_priv;
3406 work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
3407 atomic_inc(&id_priv->refcount);
3408 queue_work(cma_wq, &work->work);
3411 return 0;
3414 static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
3415 void *ptr)
3417 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
3418 struct cma_device *cma_dev;
3419 struct rdma_id_private *id_priv;
3420 int ret = NOTIFY_DONE;
3422 if (dev_net(ndev) != &init_net)
3423 return NOTIFY_DONE;
3425 if (event != NETDEV_BONDING_FAILOVER)
3426 return NOTIFY_DONE;
3428 if (!(ndev->flags & IFF_MASTER) || !(ndev->priv_flags & IFF_BONDING))
3429 return NOTIFY_DONE;
3431 mutex_lock(&lock);
3432 list_for_each_entry(cma_dev, &dev_list, list)
3433 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
3434 ret = cma_netdev_change(ndev, id_priv);
3435 if (ret)
3436 goto out;
3439 out:
3440 mutex_unlock(&lock);
3441 return ret;
3444 static struct notifier_block cma_nb = {
3445 .notifier_call = cma_netdev_callback
3448 static void cma_add_one(struct ib_device *device)
3450 struct cma_device *cma_dev;
3451 struct rdma_id_private *id_priv;
3453 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
3454 if (!cma_dev)
3455 return;
3457 cma_dev->device = device;
3459 init_completion(&cma_dev->comp);
3460 atomic_set(&cma_dev->refcount, 1);
3461 INIT_LIST_HEAD(&cma_dev->id_list);
3462 ib_set_client_data(device, &cma_client, cma_dev);
3464 mutex_lock(&lock);
3465 list_add_tail(&cma_dev->list, &dev_list);
3466 list_for_each_entry(id_priv, &listen_any_list, list)
3467 cma_listen_on_dev(id_priv, cma_dev);
3468 mutex_unlock(&lock);
3471 static int cma_remove_id_dev(struct rdma_id_private *id_priv)
3473 struct rdma_cm_event event;
3474 enum rdma_cm_state state;
3475 int ret = 0;
3477 /* Record that we want to remove the device */
3478 state = cma_exch(id_priv, RDMA_CM_DEVICE_REMOVAL);
3479 if (state == RDMA_CM_DESTROYING)
3480 return 0;
3482 cma_cancel_operation(id_priv, state);
3483 mutex_lock(&id_priv->handler_mutex);
3485 /* Check for destruction from another callback. */
3486 if (!cma_comp(id_priv, RDMA_CM_DEVICE_REMOVAL))
3487 goto out;
3489 memset(&event, 0, sizeof event);
3490 event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
3491 ret = id_priv->id.event_handler(&id_priv->id, &event);
3492 out:
3493 mutex_unlock(&id_priv->handler_mutex);
3494 return ret;
3497 static void cma_process_remove(struct cma_device *cma_dev)
3499 struct rdma_id_private *id_priv;
3500 int ret;
3502 mutex_lock(&lock);
3503 while (!list_empty(&cma_dev->id_list)) {
3504 id_priv = list_entry(cma_dev->id_list.next,
3505 struct rdma_id_private, list);
3507 list_del(&id_priv->listen_list);
3508 list_del_init(&id_priv->list);
3509 atomic_inc(&id_priv->refcount);
3510 mutex_unlock(&lock);
3512 ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv);
3513 cma_deref_id(id_priv);
3514 if (ret)
3515 rdma_destroy_id(&id_priv->id);
3517 mutex_lock(&lock);
3519 mutex_unlock(&lock);
3521 cma_deref_dev(cma_dev);
3522 wait_for_completion(&cma_dev->comp);
3525 static void cma_remove_one(struct ib_device *device)
3527 struct cma_device *cma_dev;
3529 cma_dev = ib_get_client_data(device, &cma_client);
3530 if (!cma_dev)
3531 return;
3533 mutex_lock(&lock);
3534 list_del(&cma_dev->list);
3535 mutex_unlock(&lock);
3537 cma_process_remove(cma_dev);
3538 kfree(cma_dev);
3541 static int cma_get_id_stats(struct sk_buff *skb, struct netlink_callback *cb)
3543 struct nlmsghdr *nlh;
3544 struct rdma_cm_id_stats *id_stats;
3545 struct rdma_id_private *id_priv;
3546 struct rdma_cm_id *id = NULL;
3547 struct cma_device *cma_dev;
3548 int i_dev = 0, i_id = 0;
3551 * We export all of the IDs as a sequence of messages. Each
3552 * ID gets its own netlink message.
3554 mutex_lock(&lock);
3556 list_for_each_entry(cma_dev, &dev_list, list) {
3557 if (i_dev < cb->args[0]) {
3558 i_dev++;
3559 continue;
3562 i_id = 0;
3563 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
3564 if (i_id < cb->args[1]) {
3565 i_id++;
3566 continue;
3569 id_stats = ibnl_put_msg(skb, &nlh, cb->nlh->nlmsg_seq,
3570 sizeof *id_stats, RDMA_NL_RDMA_CM,
3571 RDMA_NL_RDMA_CM_ID_STATS);
3572 if (!id_stats)
3573 goto out;
3575 memset(id_stats, 0, sizeof *id_stats);
3576 id = &id_priv->id;
3577 id_stats->node_type = id->route.addr.dev_addr.dev_type;
3578 id_stats->port_num = id->port_num;
3579 id_stats->bound_dev_if =
3580 id->route.addr.dev_addr.bound_dev_if;
3582 if (ibnl_put_attr(skb, nlh,
3583 rdma_addr_size(cma_src_addr(id_priv)),
3584 cma_src_addr(id_priv),
3585 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR))
3586 goto out;
3587 if (ibnl_put_attr(skb, nlh,
3588 rdma_addr_size(cma_src_addr(id_priv)),
3589 cma_dst_addr(id_priv),
3590 RDMA_NL_RDMA_CM_ATTR_DST_ADDR))
3591 goto out;
3593 id_stats->pid = id_priv->owner;
3594 id_stats->port_space = id->ps;
3595 id_stats->cm_state = id_priv->state;
3596 id_stats->qp_num = id_priv->qp_num;
3597 id_stats->qp_type = id->qp_type;
3599 i_id++;
3602 cb->args[1] = 0;
3603 i_dev++;
3606 out:
3607 mutex_unlock(&lock);
3608 cb->args[0] = i_dev;
3609 cb->args[1] = i_id;
3611 return skb->len;
3614 static const struct ibnl_client_cbs cma_cb_table[] = {
3615 [RDMA_NL_RDMA_CM_ID_STATS] = { .dump = cma_get_id_stats,
3616 .module = THIS_MODULE },
3619 static int __init cma_init(void)
3621 int ret;
3623 cma_wq = create_singlethread_workqueue("rdma_cm");
3624 if (!cma_wq)
3625 return -ENOMEM;
3627 ib_sa_register_client(&sa_client);
3628 rdma_addr_register_client(&addr_client);
3629 register_netdevice_notifier(&cma_nb);
3631 ret = ib_register_client(&cma_client);
3632 if (ret)
3633 goto err;
3635 if (ibnl_add_client(RDMA_NL_RDMA_CM, RDMA_NL_RDMA_CM_NUM_OPS, cma_cb_table))
3636 printk(KERN_WARNING "RDMA CMA: failed to add netlink callback\n");
3638 return 0;
3640 err:
3641 unregister_netdevice_notifier(&cma_nb);
3642 rdma_addr_unregister_client(&addr_client);
3643 ib_sa_unregister_client(&sa_client);
3644 destroy_workqueue(cma_wq);
3645 return ret;
3648 static void __exit cma_cleanup(void)
3650 ibnl_remove_client(RDMA_NL_RDMA_CM);
3651 ib_unregister_client(&cma_client);
3652 unregister_netdevice_notifier(&cma_nb);
3653 rdma_addr_unregister_client(&addr_client);
3654 ib_sa_unregister_client(&sa_client);
3655 destroy_workqueue(cma_wq);
3656 idr_destroy(&tcp_ps);
3657 idr_destroy(&udp_ps);
3658 idr_destroy(&ipoib_ps);
3659 idr_destroy(&ib_ps);
3662 module_init(cma_init);
3663 module_exit(cma_cleanup);