RDMA/ucma: Support querying when IB paths are not reversible
[linux-2.6-block.git] / drivers / infiniband / core / cma.c
CommitLineData
e51060f0
SH
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.
6 *
a9474917
SH
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:
e51060f0 12 *
a9474917
SH
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
e51060f0 16 *
a9474917
SH
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer.
e51060f0 20 *
a9474917
SH
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.
e51060f0 25 *
a9474917
SH
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.
e51060f0
SH
34 */
35
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>
07ebafba 42#include <linux/inetdevice.h>
5a0e3ad6 43#include <linux/slab.h>
e4dd23d7 44#include <linux/module.h>
366cddb4 45#include <net/route.h>
e51060f0
SH
46
47#include <net/tcp.h>
1f5175ad 48#include <net/ipv6.h>
e51060f0
SH
49
50#include <rdma/rdma_cm.h>
51#include <rdma/rdma_cm_ib.h>
753f618a 52#include <rdma/rdma_netlink.h>
2e2d190c 53#include <rdma/ib.h>
e51060f0
SH
54#include <rdma/ib_cache.h>
55#include <rdma/ib_cm.h>
56#include <rdma/ib_sa.h>
07ebafba 57#include <rdma/iw_cm.h>
e51060f0
SH
58
59MODULE_AUTHOR("Sean Hefty");
60MODULE_DESCRIPTION("Generic RDMA CM Agent");
61MODULE_LICENSE("Dual BSD/GPL");
62
63#define CMA_CM_RESPONSE_TIMEOUT 20
d5bb7599 64#define CMA_MAX_CM_RETRIES 15
dcb3f974 65#define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
3c86aa70 66#define CMA_IBOE_PACKET_LIFETIME 18
e51060f0
SH
67
68static void cma_add_one(struct ib_device *device);
69static void cma_remove_one(struct ib_device *device);
70
71static struct ib_client cma_client = {
72 .name = "cma",
73 .add = cma_add_one,
74 .remove = cma_remove_one
75};
76
c1a0b23b 77static struct ib_sa_client sa_client;
7a118df3 78static struct rdma_addr_client addr_client;
e51060f0
SH
79static LIST_HEAD(dev_list);
80static LIST_HEAD(listen_any_list);
81static DEFINE_MUTEX(lock);
82static struct workqueue_struct *cma_wq;
e51060f0 83static DEFINE_IDR(tcp_ps);
628e5f6d 84static DEFINE_IDR(udp_ps);
c8f6a362 85static DEFINE_IDR(ipoib_ps);
2d2e9415 86static DEFINE_IDR(ib_ps);
e51060f0
SH
87
88struct cma_device {
89 struct list_head list;
90 struct ib_device *device;
e51060f0
SH
91 struct completion comp;
92 atomic_t refcount;
93 struct list_head id_list;
94};
95
e51060f0
SH
96struct rdma_bind_list {
97 struct idr *ps;
98 struct hlist_head owners;
99 unsigned short port;
100};
101
68602120
SH
102enum {
103 CMA_OPTION_AFONLY,
104};
105
e51060f0
SH
106/*
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.
111 */
112struct rdma_id_private {
113 struct rdma_cm_id id;
114
115 struct rdma_bind_list *bind_list;
116 struct hlist_node node;
d02d1f53
SH
117 struct list_head list; /* listen_any_list or cma_device.list */
118 struct list_head listen_list; /* per device listens */
e51060f0 119 struct cma_device *cma_dev;
c8f6a362 120 struct list_head mc_list;
e51060f0 121
d02d1f53 122 int internal_id;
550e5ca7 123 enum rdma_cm_state state;
e51060f0 124 spinlock_t lock;
c5483388
SH
125 struct mutex qp_mutex;
126
e51060f0
SH
127 struct completion comp;
128 atomic_t refcount;
de910bd9 129 struct mutex handler_mutex;
e51060f0
SH
130
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;
07ebafba 137 struct iw_cm_id *iw;
e51060f0
SH
138 } cm_id;
139
140 u32 seq_num;
c8f6a362 141 u32 qkey;
e51060f0 142 u32 qp_num;
83e9502d 143 pid_t owner;
68602120 144 u32 options;
e51060f0 145 u8 srq;
a81c994d 146 u8 tos;
a9bb7912 147 u8 reuseaddr;
5b0ec991 148 u8 afonly;
e51060f0
SH
149};
150
c8f6a362
SH
151struct 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;
3f446754 158 struct sockaddr_storage addr;
3c86aa70 159 struct kref mcref;
c8f6a362
SH
160};
161
e51060f0
SH
162struct cma_work {
163 struct work_struct work;
164 struct rdma_id_private *id;
550e5ca7
NM
165 enum rdma_cm_state old_state;
166 enum rdma_cm_state new_state;
e51060f0
SH
167 struct rdma_cm_event event;
168};
169
dd5bdff8
OG
170struct cma_ndev_work {
171 struct work_struct work;
172 struct rdma_id_private *id;
173 struct rdma_cm_event event;
174};
175
3c86aa70
EC
176struct iboe_mcast_work {
177 struct work_struct work;
178 struct rdma_id_private *id;
179 struct cma_multicast *mc;
180};
181
e51060f0
SH
182union cma_ip_addr {
183 struct in6_addr ip6;
184 struct {
1b90c137
AV
185 __be32 pad[3];
186 __be32 addr;
e51060f0
SH
187 } ip4;
188};
189
190struct cma_hdr {
191 u8 cma_version;
192 u8 ip_version; /* IP version: 7:4 */
1b90c137 193 __be16 port;
e51060f0
SH
194 union cma_ip_addr src_addr;
195 union cma_ip_addr dst_addr;
196};
197
e51060f0 198#define CMA_VERSION 0x00
e51060f0 199
550e5ca7 200static int cma_comp(struct rdma_id_private *id_priv, enum rdma_cm_state comp)
e51060f0
SH
201{
202 unsigned long flags;
203 int ret;
204
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;
209}
210
211static int cma_comp_exch(struct rdma_id_private *id_priv,
550e5ca7 212 enum rdma_cm_state comp, enum rdma_cm_state exch)
e51060f0
SH
213{
214 unsigned long flags;
215 int ret;
216
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;
222}
223
550e5ca7
NM
224static enum rdma_cm_state cma_exch(struct rdma_id_private *id_priv,
225 enum rdma_cm_state exch)
e51060f0
SH
226{
227 unsigned long flags;
550e5ca7 228 enum rdma_cm_state old;
e51060f0
SH
229
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;
235}
236
237static inline u8 cma_get_ip_ver(struct cma_hdr *hdr)
238{
239 return hdr->ip_version >> 4;
240}
241
242static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
243{
244 hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
245}
246
e51060f0
SH
247static void cma_attach_to_dev(struct rdma_id_private *id_priv,
248 struct cma_device *cma_dev)
249{
250 atomic_inc(&cma_dev->refcount);
251 id_priv->cma_dev = cma_dev;
252 id_priv->id.device = cma_dev->device;
3c86aa70
EC
253 id_priv->id.route.addr.dev_addr.transport =
254 rdma_node_get_transport(cma_dev->device->node_type);
e51060f0
SH
255 list_add_tail(&id_priv->list, &cma_dev->id_list);
256}
257
258static inline void cma_deref_dev(struct cma_device *cma_dev)
259{
260 if (atomic_dec_and_test(&cma_dev->refcount))
261 complete(&cma_dev->comp);
262}
263
3c86aa70
EC
264static inline void release_mc(struct kref *kref)
265{
266 struct cma_multicast *mc = container_of(kref, struct cma_multicast, mcref);
267
268 kfree(mc->multicast.ib);
269 kfree(mc);
270}
271
a396d43a 272static void cma_release_dev(struct rdma_id_private *id_priv)
e51060f0 273{
a396d43a 274 mutex_lock(&lock);
e51060f0
SH
275 list_del(&id_priv->list);
276 cma_deref_dev(id_priv->cma_dev);
277 id_priv->cma_dev = NULL;
a396d43a 278 mutex_unlock(&lock);
e51060f0
SH
279}
280
f4753834
SH
281static inline struct sockaddr *cma_src_addr(struct rdma_id_private *id_priv)
282{
283 return (struct sockaddr *) &id_priv->id.route.addr.src_addr;
284}
285
286static inline struct sockaddr *cma_dst_addr(struct rdma_id_private *id_priv)
287{
288 return (struct sockaddr *) &id_priv->id.route.addr.dst_addr;
289}
290
291static inline unsigned short cma_family(struct rdma_id_private *id_priv)
292{
293 return id_priv->id.route.addr.src_addr.ss_family;
294}
295
5c438135 296static int cma_set_qkey(struct rdma_id_private *id_priv, u32 qkey)
c8f6a362
SH
297{
298 struct ib_sa_mcmember_rec rec;
299 int ret = 0;
300
5c438135
SH
301 if (id_priv->qkey) {
302 if (qkey && id_priv->qkey != qkey)
303 return -EINVAL;
d2ca39f2 304 return 0;
5c438135
SH
305 }
306
307 if (qkey) {
308 id_priv->qkey = qkey;
309 return 0;
310 }
d2ca39f2
YE
311
312 switch (id_priv->id.ps) {
c8f6a362 313 case RDMA_PS_UDP:
5c438135 314 case RDMA_PS_IB:
d2ca39f2 315 id_priv->qkey = RDMA_UDP_QKEY;
c8f6a362
SH
316 break;
317 case RDMA_PS_IPOIB:
d2ca39f2
YE
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);
c8f6a362
SH
324 break;
325 default:
326 break;
327 }
328 return ret;
329}
330
3c86aa70
EC
331static int find_gid_port(struct ib_device *device, union ib_gid *gid, u8 port_num)
332{
333 int i;
334 int err;
335 struct ib_port_attr props;
336 union ib_gid tmp;
337
338 err = ib_query_port(device, port_num, &props);
339 if (err)
63f05be2 340 return err;
3c86aa70
EC
341
342 for (i = 0; i < props.gid_tbl_len; ++i) {
343 err = ib_query_gid(device, port_num, i, &tmp);
344 if (err)
63f05be2 345 return err;
3c86aa70
EC
346 if (!memcmp(&tmp, gid, sizeof tmp))
347 return 0;
348 }
349
63f05be2 350 return -EADDRNOTAVAIL;
3c86aa70
EC
351}
352
680f920a
SH
353static void cma_translate_ib(struct sockaddr_ib *sib, struct rdma_dev_addr *dev_addr)
354{
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));
358}
359
360static int cma_translate_addr(struct sockaddr *addr, struct rdma_dev_addr *dev_addr)
361{
362 int ret;
363
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;
369 }
370
371 return ret;
372}
373
07ebafba 374static int cma_acquire_dev(struct rdma_id_private *id_priv)
e51060f0 375{
c8f6a362 376 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
e51060f0 377 struct cma_device *cma_dev;
3c86aa70 378 union ib_gid gid, iboe_gid;
e51060f0 379 int ret = -ENODEV;
3c86aa70
EC
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;
e51060f0 383
2efdd6a0
MS
384 if (dev_ll != IB_LINK_LAYER_INFINIBAND &&
385 id_priv->id.ps == RDMA_PS_IPOIB)
386 return -EINVAL;
387
a396d43a 388 mutex_lock(&lock);
3c86aa70
EC
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);
e51060f0 392 list_for_each_entry(cma_dev, &dev_list, list) {
3c86aa70
EC
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);
400
401 if (!ret) {
402 id_priv->id.port_num = port;
403 goto out;
63f05be2 404 }
3c86aa70 405 }
e51060f0
SH
406 }
407 }
3c86aa70
EC
408
409out:
410 if (!ret)
411 cma_attach_to_dev(id_priv, cma_dev);
412
a396d43a 413 mutex_unlock(&lock);
e51060f0
SH
414 return ret;
415}
416
f17df3b0
SH
417/*
418 * Select the source IB device and address to reach the destination IB address.
419 */
420static int cma_resolve_ib_dev(struct rdma_id_private *id_priv)
421{
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 port, p;
427 int i;
428
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);
433
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;
437
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;
441
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 port = p;
447 goto found;
448 }
449
450 if (!cma_dev && (gid.global.subnet_prefix ==
451 dgid->global.subnet_prefix)) {
452 cma_dev = cur_dev;
453 sgid = gid;
454 port = p;
455 }
456 }
457 }
458 }
459
460 if (!cma_dev)
461 return -ENODEV;
462
463found:
464 cma_attach_to_dev(id_priv, cma_dev);
465 id_priv->id.port_num = port;
466 addr = (struct sockaddr_ib *) cma_src_addr(id_priv);
467 memcpy(&addr->sib_addr, &sgid, sizeof sgid);
468 cma_translate_ib(addr, &id_priv->id.route.addr.dev_addr);
469 return 0;
470}
471
e51060f0
SH
472static void cma_deref_id(struct rdma_id_private *id_priv)
473{
474 if (atomic_dec_and_test(&id_priv->refcount))
475 complete(&id_priv->comp);
476}
477
de910bd9 478static int cma_disable_callback(struct rdma_id_private *id_priv,
550e5ca7 479 enum rdma_cm_state state)
8aa08602 480{
de910bd9
OG
481 mutex_lock(&id_priv->handler_mutex);
482 if (id_priv->state != state) {
483 mutex_unlock(&id_priv->handler_mutex);
484 return -EINVAL;
485 }
486 return 0;
e51060f0
SH
487}
488
489struct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler,
b26f9b99
SH
490 void *context, enum rdma_port_space ps,
491 enum ib_qp_type qp_type)
e51060f0
SH
492{
493 struct rdma_id_private *id_priv;
494
495 id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
496 if (!id_priv)
497 return ERR_PTR(-ENOMEM);
498
83e9502d 499 id_priv->owner = task_pid_nr(current);
550e5ca7 500 id_priv->state = RDMA_CM_IDLE;
e51060f0
SH
501 id_priv->id.context = context;
502 id_priv->id.event_handler = event_handler;
503 id_priv->id.ps = ps;
b26f9b99 504 id_priv->id.qp_type = qp_type;
e51060f0 505 spin_lock_init(&id_priv->lock);
c5483388 506 mutex_init(&id_priv->qp_mutex);
e51060f0
SH
507 init_completion(&id_priv->comp);
508 atomic_set(&id_priv->refcount, 1);
de910bd9 509 mutex_init(&id_priv->handler_mutex);
e51060f0 510 INIT_LIST_HEAD(&id_priv->listen_list);
c8f6a362 511 INIT_LIST_HEAD(&id_priv->mc_list);
e51060f0
SH
512 get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
513
514 return &id_priv->id;
515}
516EXPORT_SYMBOL(rdma_create_id);
517
c8f6a362 518static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
e51060f0
SH
519{
520 struct ib_qp_attr qp_attr;
c8f6a362 521 int qp_attr_mask, ret;
e51060f0 522
c8f6a362
SH
523 qp_attr.qp_state = IB_QPS_INIT;
524 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
e51060f0
SH
525 if (ret)
526 return ret;
527
c8f6a362
SH
528 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
529 if (ret)
530 return ret;
531
532 qp_attr.qp_state = IB_QPS_RTR;
533 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
534 if (ret)
535 return ret;
536
537 qp_attr.qp_state = IB_QPS_RTS;
538 qp_attr.sq_psn = 0;
539 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
540
541 return ret;
e51060f0
SH
542}
543
c8f6a362 544static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
07ebafba
TT
545{
546 struct ib_qp_attr qp_attr;
c8f6a362 547 int qp_attr_mask, ret;
07ebafba
TT
548
549 qp_attr.qp_state = IB_QPS_INIT;
c8f6a362
SH
550 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
551 if (ret)
552 return ret;
07ebafba 553
c8f6a362 554 return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
07ebafba
TT
555}
556
e51060f0
SH
557int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
558 struct ib_qp_init_attr *qp_init_attr)
559{
560 struct rdma_id_private *id_priv;
561 struct ib_qp *qp;
562 int ret;
563
564 id_priv = container_of(id, struct rdma_id_private, id);
565 if (id->device != pd->device)
566 return -EINVAL;
567
568 qp = ib_create_qp(pd, qp_init_attr);
569 if (IS_ERR(qp))
570 return PTR_ERR(qp);
571
b26f9b99 572 if (id->qp_type == IB_QPT_UD)
c8f6a362
SH
573 ret = cma_init_ud_qp(id_priv, qp);
574 else
575 ret = cma_init_conn_qp(id_priv, qp);
e51060f0
SH
576 if (ret)
577 goto err;
578
579 id->qp = qp;
580 id_priv->qp_num = qp->qp_num;
e51060f0
SH
581 id_priv->srq = (qp->srq != NULL);
582 return 0;
583err:
584 ib_destroy_qp(qp);
585 return ret;
586}
587EXPORT_SYMBOL(rdma_create_qp);
588
589void rdma_destroy_qp(struct rdma_cm_id *id)
590{
c5483388
SH
591 struct rdma_id_private *id_priv;
592
593 id_priv = container_of(id, struct rdma_id_private, id);
594 mutex_lock(&id_priv->qp_mutex);
595 ib_destroy_qp(id_priv->id.qp);
596 id_priv->id.qp = NULL;
597 mutex_unlock(&id_priv->qp_mutex);
e51060f0
SH
598}
599EXPORT_SYMBOL(rdma_destroy_qp);
600
5851bb89
SH
601static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
602 struct rdma_conn_param *conn_param)
e51060f0
SH
603{
604 struct ib_qp_attr qp_attr;
605 int qp_attr_mask, ret;
606
c5483388
SH
607 mutex_lock(&id_priv->qp_mutex);
608 if (!id_priv->id.qp) {
609 ret = 0;
610 goto out;
611 }
e51060f0
SH
612
613 /* Need to update QP attributes from default values. */
614 qp_attr.qp_state = IB_QPS_INIT;
c5483388 615 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
e51060f0 616 if (ret)
c5483388 617 goto out;
e51060f0 618
c5483388 619 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
e51060f0 620 if (ret)
c5483388 621 goto out;
e51060f0
SH
622
623 qp_attr.qp_state = IB_QPS_RTR;
c5483388 624 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
e51060f0 625 if (ret)
c5483388 626 goto out;
e51060f0 627
5851bb89
SH
628 if (conn_param)
629 qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
c5483388
SH
630 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
631out:
632 mutex_unlock(&id_priv->qp_mutex);
633 return ret;
e51060f0
SH
634}
635
5851bb89
SH
636static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
637 struct rdma_conn_param *conn_param)
e51060f0
SH
638{
639 struct ib_qp_attr qp_attr;
640 int qp_attr_mask, ret;
641
c5483388
SH
642 mutex_lock(&id_priv->qp_mutex);
643 if (!id_priv->id.qp) {
644 ret = 0;
645 goto out;
646 }
e51060f0
SH
647
648 qp_attr.qp_state = IB_QPS_RTS;
c5483388 649 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
e51060f0 650 if (ret)
c5483388 651 goto out;
e51060f0 652
5851bb89
SH
653 if (conn_param)
654 qp_attr.max_rd_atomic = conn_param->initiator_depth;
c5483388
SH
655 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
656out:
657 mutex_unlock(&id_priv->qp_mutex);
658 return ret;
e51060f0
SH
659}
660
c5483388 661static int cma_modify_qp_err(struct rdma_id_private *id_priv)
e51060f0
SH
662{
663 struct ib_qp_attr qp_attr;
c5483388 664 int ret;
e51060f0 665
c5483388
SH
666 mutex_lock(&id_priv->qp_mutex);
667 if (!id_priv->id.qp) {
668 ret = 0;
669 goto out;
670 }
e51060f0
SH
671
672 qp_attr.qp_state = IB_QPS_ERR;
c5483388
SH
673 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
674out:
675 mutex_unlock(&id_priv->qp_mutex);
676 return ret;
e51060f0
SH
677}
678
c8f6a362
SH
679static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
680 struct ib_qp_attr *qp_attr, int *qp_attr_mask)
681{
682 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
683 int ret;
3c86aa70
EC
684 u16 pkey;
685
686 if (rdma_port_get_link_layer(id_priv->id.device, id_priv->id.port_num) ==
687 IB_LINK_LAYER_INFINIBAND)
688 pkey = ib_addr_get_pkey(dev_addr);
689 else
690 pkey = 0xffff;
c8f6a362
SH
691
692 ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
3c86aa70 693 pkey, &qp_attr->pkey_index);
c8f6a362
SH
694 if (ret)
695 return ret;
696
697 qp_attr->port_num = id_priv->id.port_num;
698 *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
699
b26f9b99 700 if (id_priv->id.qp_type == IB_QPT_UD) {
5c438135 701 ret = cma_set_qkey(id_priv, 0);
d2ca39f2
YE
702 if (ret)
703 return ret;
704
c8f6a362
SH
705 qp_attr->qkey = id_priv->qkey;
706 *qp_attr_mask |= IB_QP_QKEY;
707 } else {
708 qp_attr->qp_access_flags = 0;
709 *qp_attr_mask |= IB_QP_ACCESS_FLAGS;
710 }
711 return 0;
712}
713
e51060f0
SH
714int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
715 int *qp_attr_mask)
716{
717 struct rdma_id_private *id_priv;
c8f6a362 718 int ret = 0;
e51060f0
SH
719
720 id_priv = container_of(id, struct rdma_id_private, id);
07ebafba
TT
721 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
722 case RDMA_TRANSPORT_IB:
b26f9b99 723 if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD))
c8f6a362
SH
724 ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
725 else
726 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
727 qp_attr_mask);
e51060f0
SH
728 if (qp_attr->qp_state == IB_QPS_RTR)
729 qp_attr->rq_psn = id_priv->seq_num;
730 break;
07ebafba 731 case RDMA_TRANSPORT_IWARP:
c8f6a362 732 if (!id_priv->cm_id.iw) {
8f076531 733 qp_attr->qp_access_flags = 0;
c8f6a362
SH
734 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
735 } else
736 ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
737 qp_attr_mask);
07ebafba 738 break;
e51060f0
SH
739 default:
740 ret = -ENOSYS;
741 break;
742 }
743
744 return ret;
745}
746EXPORT_SYMBOL(rdma_init_qp_attr);
747
748static inline int cma_zero_addr(struct sockaddr *addr)
749{
2e2d190c
SH
750 switch (addr->sa_family) {
751 case AF_INET:
752 return ipv4_is_zeronet(((struct sockaddr_in *)addr)->sin_addr.s_addr);
753 case AF_INET6:
754 return ipv6_addr_any(&((struct sockaddr_in6 *) addr)->sin6_addr);
755 case AF_IB:
756 return ib_addr_any(&((struct sockaddr_ib *) addr)->sib_addr);
757 default:
758 return 0;
e51060f0
SH
759 }
760}
761
762static inline int cma_loopback_addr(struct sockaddr *addr)
763{
2e2d190c
SH
764 switch (addr->sa_family) {
765 case AF_INET:
766 return ipv4_is_loopback(((struct sockaddr_in *) addr)->sin_addr.s_addr);
767 case AF_INET6:
768 return ipv6_addr_loopback(&((struct sockaddr_in6 *) addr)->sin6_addr);
769 case AF_IB:
770 return ib_addr_loopback(&((struct sockaddr_ib *) addr)->sib_addr);
771 default:
772 return 0;
773 }
e51060f0
SH
774}
775
776static inline int cma_any_addr(struct sockaddr *addr)
777{
778 return cma_zero_addr(addr) || cma_loopback_addr(addr);
779}
780
43b752da
HS
781static int cma_addr_cmp(struct sockaddr *src, struct sockaddr *dst)
782{
783 if (src->sa_family != dst->sa_family)
784 return -1;
785
786 switch (src->sa_family) {
787 case AF_INET:
788 return ((struct sockaddr_in *) src)->sin_addr.s_addr !=
789 ((struct sockaddr_in *) dst)->sin_addr.s_addr;
2e2d190c 790 case AF_INET6:
43b752da
HS
791 return ipv6_addr_cmp(&((struct sockaddr_in6 *) src)->sin6_addr,
792 &((struct sockaddr_in6 *) dst)->sin6_addr);
2e2d190c
SH
793 default:
794 return ib_addr_cmp(&((struct sockaddr_ib *) src)->sib_addr,
795 &((struct sockaddr_ib *) dst)->sib_addr);
43b752da
HS
796 }
797}
798
58afdcb7 799static __be16 cma_port(struct sockaddr *addr)
628e5f6d 800{
58afdcb7
SH
801 struct sockaddr_ib *sib;
802
803 switch (addr->sa_family) {
804 case AF_INET:
628e5f6d 805 return ((struct sockaddr_in *) addr)->sin_port;
58afdcb7 806 case AF_INET6:
628e5f6d 807 return ((struct sockaddr_in6 *) addr)->sin6_port;
58afdcb7
SH
808 case AF_IB:
809 sib = (struct sockaddr_ib *) addr;
810 return htons((u16) (be64_to_cpu(sib->sib_sid) &
811 be64_to_cpu(sib->sib_sid_mask)));
812 default:
813 return 0;
814 }
628e5f6d
SH
815}
816
e51060f0
SH
817static inline int cma_any_port(struct sockaddr *addr)
818{
628e5f6d 819 return !cma_port(addr);
e51060f0
SH
820}
821
fbaa1a6d
SH
822static void cma_save_ib_info(struct rdma_cm_id *id, struct rdma_cm_id *listen_id,
823 struct ib_sa_path_rec *path)
e51060f0 824{
fbaa1a6d
SH
825 struct sockaddr_ib *listen_ib, *ib;
826
827 listen_ib = (struct sockaddr_ib *) &listen_id->route.addr.src_addr;
828 ib = (struct sockaddr_ib *) &id->route.addr.src_addr;
829 ib->sib_family = listen_ib->sib_family;
830 ib->sib_pkey = path->pkey;
831 ib->sib_flowinfo = path->flow_label;
832 memcpy(&ib->sib_addr, &path->sgid, 16);
833 ib->sib_sid = listen_ib->sib_sid;
834 ib->sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL);
835 ib->sib_scope_id = listen_ib->sib_scope_id;
836
837 ib = (struct sockaddr_ib *) &id->route.addr.dst_addr;
838 ib->sib_family = listen_ib->sib_family;
839 ib->sib_pkey = path->pkey;
840 ib->sib_flowinfo = path->flow_label;
841 memcpy(&ib->sib_addr, &path->dgid, 16);
842}
e51060f0 843
fbaa1a6d
SH
844static void cma_save_ip4_info(struct rdma_cm_id *id, struct rdma_cm_id *listen_id,
845 struct cma_hdr *hdr)
846{
847 struct sockaddr_in *listen4, *ip4;
e51060f0 848
fbaa1a6d
SH
849 listen4 = (struct sockaddr_in *) &listen_id->route.addr.src_addr;
850 ip4 = (struct sockaddr_in *) &id->route.addr.src_addr;
851 ip4->sin_family = listen4->sin_family;
852 ip4->sin_addr.s_addr = hdr->dst_addr.ip4.addr;
853 ip4->sin_port = listen4->sin_port;
854
855 ip4 = (struct sockaddr_in *) &id->route.addr.dst_addr;
856 ip4->sin_family = listen4->sin_family;
857 ip4->sin_addr.s_addr = hdr->src_addr.ip4.addr;
858 ip4->sin_port = hdr->port;
e51060f0
SH
859}
860
fbaa1a6d
SH
861static void cma_save_ip6_info(struct rdma_cm_id *id, struct rdma_cm_id *listen_id,
862 struct cma_hdr *hdr)
e51060f0 863{
e51060f0
SH
864 struct sockaddr_in6 *listen6, *ip6;
865
fbaa1a6d
SH
866 listen6 = (struct sockaddr_in6 *) &listen_id->route.addr.src_addr;
867 ip6 = (struct sockaddr_in6 *) &id->route.addr.src_addr;
868 ip6->sin6_family = listen6->sin6_family;
869 ip6->sin6_addr = hdr->dst_addr.ip6;
870 ip6->sin6_port = listen6->sin6_port;
871
872 ip6 = (struct sockaddr_in6 *) &id->route.addr.dst_addr;
873 ip6->sin6_family = listen6->sin6_family;
874 ip6->sin6_addr = hdr->src_addr.ip6;
875 ip6->sin6_port = hdr->port;
876}
877
878static int cma_save_net_info(struct rdma_cm_id *id, struct rdma_cm_id *listen_id,
879 struct ib_cm_event *ib_event)
880{
881 struct cma_hdr *hdr;
882
883 if (listen_id->route.addr.src_addr.ss_family == AF_IB) {
884 cma_save_ib_info(id, listen_id, ib_event->param.req_rcvd.primary_path);
885 return 0;
886 }
887
888 hdr = ib_event->private_data;
889 if (hdr->cma_version != CMA_VERSION)
890 return -EINVAL;
891
892 switch (cma_get_ip_ver(hdr)) {
e51060f0 893 case 4:
fbaa1a6d 894 cma_save_ip4_info(id, listen_id, hdr);
e51060f0
SH
895 break;
896 case 6:
fbaa1a6d 897 cma_save_ip6_info(id, listen_id, hdr);
e51060f0
SH
898 break;
899 default:
fbaa1a6d 900 return -EINVAL;
e51060f0 901 }
fbaa1a6d 902 return 0;
e51060f0
SH
903}
904
e8160e15 905static inline int cma_user_data_offset(struct rdma_id_private *id_priv)
e51060f0 906{
e8160e15 907 return cma_family(id_priv) == AF_IB ? 0 : sizeof(struct cma_hdr);
e51060f0
SH
908}
909
e51060f0
SH
910static void cma_cancel_route(struct rdma_id_private *id_priv)
911{
3c86aa70
EC
912 switch (rdma_port_get_link_layer(id_priv->id.device, id_priv->id.port_num)) {
913 case IB_LINK_LAYER_INFINIBAND:
e51060f0
SH
914 if (id_priv->query)
915 ib_sa_cancel_query(id_priv->query_id, id_priv->query);
916 break;
917 default:
918 break;
919 }
920}
921
e51060f0
SH
922static void cma_cancel_listens(struct rdma_id_private *id_priv)
923{
924 struct rdma_id_private *dev_id_priv;
925
d02d1f53
SH
926 /*
927 * Remove from listen_any_list to prevent added devices from spawning
928 * additional listen requests.
929 */
e51060f0
SH
930 mutex_lock(&lock);
931 list_del(&id_priv->list);
932
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);
d02d1f53
SH
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);
940
941 rdma_destroy_id(&dev_id_priv->id);
942 mutex_lock(&lock);
e51060f0
SH
943 }
944 mutex_unlock(&lock);
945}
946
947static void cma_cancel_operation(struct rdma_id_private *id_priv,
550e5ca7 948 enum rdma_cm_state state)
e51060f0
SH
949{
950 switch (state) {
550e5ca7 951 case RDMA_CM_ADDR_QUERY:
e51060f0
SH
952 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
953 break;
550e5ca7 954 case RDMA_CM_ROUTE_QUERY:
e51060f0
SH
955 cma_cancel_route(id_priv);
956 break;
550e5ca7 957 case RDMA_CM_LISTEN:
f4753834 958 if (cma_any_addr(cma_src_addr(id_priv)) && !id_priv->cma_dev)
e51060f0
SH
959 cma_cancel_listens(id_priv);
960 break;
961 default:
962 break;
963 }
964}
965
966static void cma_release_port(struct rdma_id_private *id_priv)
967{
968 struct rdma_bind_list *bind_list = id_priv->bind_list;
969
970 if (!bind_list)
971 return;
972
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);
978 }
979 mutex_unlock(&lock);
980}
981
c8f6a362
SH
982static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
983{
984 struct cma_multicast *mc;
985
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);
3c86aa70
EC
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;
1000 }
c8f6a362
SH
1001 }
1002}
1003
e51060f0
SH
1004void rdma_destroy_id(struct rdma_cm_id *id)
1005{
1006 struct rdma_id_private *id_priv;
550e5ca7 1007 enum rdma_cm_state state;
e51060f0
SH
1008
1009 id_priv = container_of(id, struct rdma_id_private, id);
550e5ca7 1010 state = cma_exch(id_priv, RDMA_CM_DESTROYING);
e51060f0
SH
1011 cma_cancel_operation(id_priv, state);
1012
a396d43a
SH
1013 /*
1014 * Wait for any active callback to finish. New callbacks will find
1015 * the id_priv state set to destroying and abort.
1016 */
1017 mutex_lock(&id_priv->handler_mutex);
1018 mutex_unlock(&id_priv->handler_mutex);
1019
e51060f0 1020 if (id_priv->cma_dev) {
3c86aa70 1021 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
07ebafba 1022 case RDMA_TRANSPORT_IB:
0c9361fc 1023 if (id_priv->cm_id.ib)
e51060f0
SH
1024 ib_destroy_cm_id(id_priv->cm_id.ib);
1025 break;
07ebafba 1026 case RDMA_TRANSPORT_IWARP:
0c9361fc 1027 if (id_priv->cm_id.iw)
07ebafba
TT
1028 iw_destroy_cm_id(id_priv->cm_id.iw);
1029 break;
e51060f0
SH
1030 default:
1031 break;
1032 }
c8f6a362 1033 cma_leave_mc_groups(id_priv);
a396d43a 1034 cma_release_dev(id_priv);
e51060f0
SH
1035 }
1036
1037 cma_release_port(id_priv);
1038 cma_deref_id(id_priv);
1039 wait_for_completion(&id_priv->comp);
1040
d02d1f53
SH
1041 if (id_priv->internal_id)
1042 cma_deref_id(id_priv->id.context);
1043
e51060f0
SH
1044 kfree(id_priv->id.route.path_rec);
1045 kfree(id_priv);
1046}
1047EXPORT_SYMBOL(rdma_destroy_id);
1048
1049static int cma_rep_recv(struct rdma_id_private *id_priv)
1050{
1051 int ret;
1052
5851bb89 1053 ret = cma_modify_qp_rtr(id_priv, NULL);
e51060f0
SH
1054 if (ret)
1055 goto reject;
1056
5851bb89 1057 ret = cma_modify_qp_rts(id_priv, NULL);
e51060f0
SH
1058 if (ret)
1059 goto reject;
1060
1061 ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
1062 if (ret)
1063 goto reject;
1064
1065 return 0;
1066reject:
c5483388 1067 cma_modify_qp_err(id_priv);
e51060f0
SH
1068 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
1069 NULL, 0, NULL, 0);
1070 return ret;
1071}
1072
a1b1b61f
SH
1073static void cma_set_rep_event_data(struct rdma_cm_event *event,
1074 struct ib_cm_rep_event_param *rep_data,
1075 void *private_data)
1076{
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;
1085}
1086
e51060f0
SH
1087static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1088{
1089 struct rdma_id_private *id_priv = cm_id->context;
a1b1b61f
SH
1090 struct rdma_cm_event event;
1091 int ret = 0;
e51060f0 1092
38ca83a5 1093 if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
550e5ca7 1094 cma_disable_callback(id_priv, RDMA_CM_CONNECT)) ||
38ca83a5 1095 (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
550e5ca7 1096 cma_disable_callback(id_priv, RDMA_CM_DISCONNECT)))
8aa08602 1097 return 0;
e51060f0 1098
a1b1b61f 1099 memset(&event, 0, sizeof event);
e51060f0
SH
1100 switch (ib_event->event) {
1101 case IB_CM_REQ_ERROR:
1102 case IB_CM_REP_ERROR:
a1b1b61f
SH
1103 event.event = RDMA_CM_EVENT_UNREACHABLE;
1104 event.status = -ETIMEDOUT;
e51060f0
SH
1105 break;
1106 case IB_CM_REP_RECEIVED:
01602f11 1107 if (id_priv->id.qp) {
a1b1b61f
SH
1108 event.status = cma_rep_recv(id_priv);
1109 event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
1110 RDMA_CM_EVENT_ESTABLISHED;
01602f11 1111 } else {
a1b1b61f 1112 event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
01602f11 1113 }
a1b1b61f
SH
1114 cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
1115 ib_event->private_data);
e51060f0
SH
1116 break;
1117 case IB_CM_RTU_RECEIVED:
0fe313b0
SH
1118 case IB_CM_USER_ESTABLISHED:
1119 event.event = RDMA_CM_EVENT_ESTABLISHED;
e51060f0
SH
1120 break;
1121 case IB_CM_DREQ_ERROR:
a1b1b61f 1122 event.status = -ETIMEDOUT; /* fall through */
e51060f0
SH
1123 case IB_CM_DREQ_RECEIVED:
1124 case IB_CM_DREP_RECEIVED:
550e5ca7
NM
1125 if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT,
1126 RDMA_CM_DISCONNECT))
e51060f0 1127 goto out;
a1b1b61f 1128 event.event = RDMA_CM_EVENT_DISCONNECTED;
e51060f0
SH
1129 break;
1130 case IB_CM_TIMEWAIT_EXIT:
38ca83a5
AV
1131 event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
1132 break;
e51060f0
SH
1133 case IB_CM_MRA_RECEIVED:
1134 /* ignore event */
1135 goto out;
1136 case IB_CM_REJ_RECEIVED:
c5483388 1137 cma_modify_qp_err(id_priv);
a1b1b61f
SH
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;
e51060f0
SH
1142 break;
1143 default:
468f2239 1144 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
e51060f0
SH
1145 ib_event->event);
1146 goto out;
1147 }
1148
a1b1b61f 1149 ret = id_priv->id.event_handler(&id_priv->id, &event);
e51060f0
SH
1150 if (ret) {
1151 /* Destroy the CM ID by returning a non-zero value. */
1152 id_priv->cm_id.ib = NULL;
550e5ca7 1153 cma_exch(id_priv, RDMA_CM_DESTROYING);
de910bd9 1154 mutex_unlock(&id_priv->handler_mutex);
e51060f0
SH
1155 rdma_destroy_id(&id_priv->id);
1156 return ret;
1157 }
1158out:
de910bd9 1159 mutex_unlock(&id_priv->handler_mutex);
e51060f0
SH
1160 return ret;
1161}
1162
628e5f6d
SH
1163static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
1164 struct ib_cm_event *ib_event)
e51060f0
SH
1165{
1166 struct rdma_id_private *id_priv;
1167 struct rdma_cm_id *id;
1168 struct rdma_route *rt;
64c5e613 1169 int ret;
e51060f0
SH
1170
1171 id = rdma_create_id(listen_id->event_handler, listen_id->context,
b26f9b99 1172 listen_id->ps, ib_event->param.req_rcvd.qp_type);
e51060f0 1173 if (IS_ERR(id))
0c9361fc 1174 return NULL;
3f168d2b 1175
f4753834 1176 id_priv = container_of(id, struct rdma_id_private, id);
fbaa1a6d
SH
1177 if (cma_save_net_info(id, listen_id, ib_event))
1178 goto err;
e51060f0
SH
1179
1180 rt = &id->route;
1181 rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
3f168d2b
KK
1182 rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
1183 GFP_KERNEL);
e51060f0 1184 if (!rt->path_rec)
0c9361fc 1185 goto err;
e51060f0 1186
e51060f0
SH
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;
1190
f4753834 1191 if (cma_any_addr(cma_src_addr(id_priv))) {
6f8372b6
SH
1192 rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
1193 rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
46ea5061 1194 ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
6f8372b6 1195 } else {
f4753834 1196 ret = cma_translate_addr(cma_src_addr(id_priv), &rt->addr.dev_addr);
6f8372b6 1197 if (ret)
0c9361fc 1198 goto err;
6f8372b6
SH
1199 }
1200 rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
e51060f0 1201
550e5ca7 1202 id_priv->state = RDMA_CM_CONNECT;
e51060f0 1203 return id_priv;
3f168d2b 1204
3f168d2b 1205err:
0c9361fc 1206 rdma_destroy_id(id);
e51060f0
SH
1207 return NULL;
1208}
1209
628e5f6d
SH
1210static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
1211 struct ib_cm_event *ib_event)
1212{
1213 struct rdma_id_private *id_priv;
1214 struct rdma_cm_id *id;
628e5f6d
SH
1215 int ret;
1216
1217 id = rdma_create_id(listen_id->event_handler, listen_id->context,
b26f9b99 1218 listen_id->ps, IB_QPT_UD);
628e5f6d
SH
1219 if (IS_ERR(id))
1220 return NULL;
1221
f4753834 1222 id_priv = container_of(id, struct rdma_id_private, id);
fbaa1a6d 1223 if (cma_save_net_info(id, listen_id, ib_event))
628e5f6d
SH
1224 goto err;
1225
6f8372b6 1226 if (!cma_any_addr((struct sockaddr *) &id->route.addr.src_addr)) {
f4753834 1227 ret = cma_translate_addr(cma_src_addr(id_priv), &id->route.addr.dev_addr);
6f8372b6
SH
1228 if (ret)
1229 goto err;
1230 }
628e5f6d 1231
550e5ca7 1232 id_priv->state = RDMA_CM_CONNECT;
628e5f6d
SH
1233 return id_priv;
1234err:
1235 rdma_destroy_id(id);
1236 return NULL;
1237}
1238
a1b1b61f
SH
1239static 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)
1242{
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;
1252}
1253
9595480c
HS
1254static int cma_check_req_qp_type(struct rdma_cm_id *id, struct ib_cm_event *ib_event)
1255{
4dd81e89 1256 return (((ib_event->event == IB_CM_REQ_RECEIVED) &&
9595480c
HS
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));
1261}
1262
e51060f0
SH
1263static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1264{
1265 struct rdma_id_private *listen_id, *conn_id;
a1b1b61f 1266 struct rdma_cm_event event;
e51060f0
SH
1267 int offset, ret;
1268
1269 listen_id = cm_id->context;
9595480c
HS
1270 if (!cma_check_req_qp_type(&listen_id->id, ib_event))
1271 return -EINVAL;
1272
550e5ca7 1273 if (cma_disable_callback(listen_id, RDMA_CM_LISTEN))
8aa08602 1274 return -ECONNABORTED;
e51060f0 1275
628e5f6d 1276 memset(&event, 0, sizeof event);
e8160e15 1277 offset = cma_user_data_offset(listen_id);
628e5f6d 1278 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
9595480c 1279 if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) {
628e5f6d
SH
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);
1288 }
e51060f0
SH
1289 if (!conn_id) {
1290 ret = -ENOMEM;
b6cec8aa 1291 goto err1;
e51060f0
SH
1292 }
1293
de910bd9 1294 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
07ebafba 1295 ret = cma_acquire_dev(conn_id);
a1a733f6 1296 if (ret)
b6cec8aa 1297 goto err2;
e51060f0
SH
1298
1299 conn_id->cm_id.ib = cm_id;
1300 cm_id->context = conn_id;
1301 cm_id->cm_handler = cma_ib_handler;
1302
25ae21a1
SH
1303 /*
1304 * Protect against the user destroying conn_id from another thread
1305 * until we're done accessing it.
1306 */
1307 atomic_inc(&conn_id->refcount);
a1b1b61f 1308 ret = conn_id->id.event_handler(&conn_id->id, &event);
b6cec8aa
SH
1309 if (ret)
1310 goto err3;
1311
1312 /*
1313 * Acquire mutex to prevent user executing rdma_destroy_id()
1314 * while we're accessing the cm_id.
1315 */
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);
25ae21a1 1322 cma_deref_id(conn_id);
b6cec8aa 1323 return 0;
a1a733f6 1324
b6cec8aa
SH
1325err3:
1326 cma_deref_id(conn_id);
a1a733f6
KK
1327 /* Destroy the CM ID by returning a non-zero value. */
1328 conn_id->cm_id.ib = NULL;
b6cec8aa 1329err2:
550e5ca7 1330 cma_exch(conn_id, RDMA_CM_DESTROYING);
de910bd9 1331 mutex_unlock(&conn_id->handler_mutex);
b6cec8aa 1332err1:
de910bd9 1333 mutex_unlock(&listen_id->handler_mutex);
b6cec8aa
SH
1334 if (conn_id)
1335 rdma_destroy_id(&conn_id->id);
e51060f0
SH
1336 return ret;
1337}
1338
1339static __be64 cma_get_service_id(enum rdma_port_space ps, struct sockaddr *addr)
1340{
496ce3ce
SH
1341 if (addr->sa_family == AF_IB)
1342 return ((struct sockaddr_ib *) addr)->sib_sid;
1343
628e5f6d 1344 return cpu_to_be64(((u64)ps << 16) + be16_to_cpu(cma_port(addr)));
e51060f0
SH
1345}
1346
1347static void cma_set_compare_data(enum rdma_port_space ps, struct sockaddr *addr,
1348 struct ib_cm_compare_data *compare)
1349{
1350 struct cma_hdr *cma_data, *cma_mask;
1b90c137 1351 __be32 ip4_addr;
e51060f0
SH
1352 struct in6_addr ip6_addr;
1353
1354 memset(compare, 0, sizeof *compare);
1355 cma_data = (void *) compare->data;
1356 cma_mask = (void *) compare->mask;
e51060f0
SH
1357
1358 switch (addr->sa_family) {
1359 case AF_INET:
1360 ip4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
01602f11
SH
1361 cma_set_ip_ver(cma_data, 4);
1362 cma_set_ip_ver(cma_mask, 0xF);
1363 if (!cma_any_addr(addr)) {
1364 cma_data->dst_addr.ip4.addr = ip4_addr;
1365 cma_mask->dst_addr.ip4.addr = htonl(~0);
e51060f0
SH
1366 }
1367 break;
1368 case AF_INET6:
1369 ip6_addr = ((struct sockaddr_in6 *) addr)->sin6_addr;
01602f11
SH
1370 cma_set_ip_ver(cma_data, 6);
1371 cma_set_ip_ver(cma_mask, 0xF);
1372 if (!cma_any_addr(addr)) {
1373 cma_data->dst_addr.ip6 = ip6_addr;
1374 memset(&cma_mask->dst_addr.ip6, 0xFF,
1375 sizeof cma_mask->dst_addr.ip6);
e51060f0
SH
1376 }
1377 break;
1378 default:
1379 break;
1380 }
1381}
1382
07ebafba
TT
1383static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
1384{
1385 struct rdma_id_private *id_priv = iw_id->context;
a1b1b61f 1386 struct rdma_cm_event event;
07ebafba
TT
1387 struct sockaddr_in *sin;
1388 int ret = 0;
1389
550e5ca7 1390 if (cma_disable_callback(id_priv, RDMA_CM_CONNECT))
be65f086 1391 return 0;
07ebafba 1392
be65f086 1393 memset(&event, 0, sizeof event);
07ebafba
TT
1394 switch (iw_event->event) {
1395 case IW_CM_EVENT_CLOSE:
a1b1b61f 1396 event.event = RDMA_CM_EVENT_DISCONNECTED;
07ebafba
TT
1397 break;
1398 case IW_CM_EVENT_CONNECT_REPLY:
f4753834 1399 sin = (struct sockaddr_in *) cma_src_addr(id_priv);
07ebafba 1400 *sin = iw_event->local_addr;
f4753834 1401 sin = (struct sockaddr_in *) cma_dst_addr(id_priv);
07ebafba 1402 *sin = iw_event->remote_addr;
881a045f
SW
1403 switch (iw_event->status) {
1404 case 0:
a1b1b61f 1405 event.event = RDMA_CM_EVENT_ESTABLISHED;
3ebeebc3
KS
1406 event.param.conn.initiator_depth = iw_event->ird;
1407 event.param.conn.responder_resources = iw_event->ord;
881a045f
SW
1408 break;
1409 case -ECONNRESET:
1410 case -ECONNREFUSED:
1411 event.event = RDMA_CM_EVENT_REJECTED;
1412 break;
1413 case -ETIMEDOUT:
1414 event.event = RDMA_CM_EVENT_UNREACHABLE;
1415 break;
1416 default:
1417 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
1418 break;
1419 }
07ebafba
TT
1420 break;
1421 case IW_CM_EVENT_ESTABLISHED:
a1b1b61f 1422 event.event = RDMA_CM_EVENT_ESTABLISHED;
3ebeebc3
KS
1423 event.param.conn.initiator_depth = iw_event->ird;
1424 event.param.conn.responder_resources = iw_event->ord;
07ebafba
TT
1425 break;
1426 default:
1427 BUG_ON(1);
1428 }
1429
a1b1b61f
SH
1430 event.status = iw_event->status;
1431 event.param.conn.private_data = iw_event->private_data;
1432 event.param.conn.private_data_len = iw_event->private_data_len;
1433 ret = id_priv->id.event_handler(&id_priv->id, &event);
07ebafba
TT
1434 if (ret) {
1435 /* Destroy the CM ID by returning a non-zero value. */
1436 id_priv->cm_id.iw = NULL;
550e5ca7 1437 cma_exch(id_priv, RDMA_CM_DESTROYING);
de910bd9 1438 mutex_unlock(&id_priv->handler_mutex);
07ebafba
TT
1439 rdma_destroy_id(&id_priv->id);
1440 return ret;
1441 }
1442
de910bd9 1443 mutex_unlock(&id_priv->handler_mutex);
07ebafba
TT
1444 return ret;
1445}
1446
1447static int iw_conn_req_handler(struct iw_cm_id *cm_id,
1448 struct iw_cm_event *iw_event)
1449{
1450 struct rdma_cm_id *new_cm_id;
1451 struct rdma_id_private *listen_id, *conn_id;
1452 struct sockaddr_in *sin;
1453 struct net_device *dev = NULL;
a1b1b61f 1454 struct rdma_cm_event event;
07ebafba 1455 int ret;
8d8293cf 1456 struct ib_device_attr attr;
07ebafba
TT
1457
1458 listen_id = cm_id->context;
550e5ca7 1459 if (cma_disable_callback(listen_id, RDMA_CM_LISTEN))
8aa08602 1460 return -ECONNABORTED;
07ebafba
TT
1461
1462 /* Create a new RDMA id for the new IW CM ID */
1463 new_cm_id = rdma_create_id(listen_id->id.event_handler,
1464 listen_id->id.context,
b26f9b99 1465 RDMA_PS_TCP, IB_QPT_RC);
10f32065 1466 if (IS_ERR(new_cm_id)) {
07ebafba
TT
1467 ret = -ENOMEM;
1468 goto out;
1469 }
1470 conn_id = container_of(new_cm_id, struct rdma_id_private, id);
de910bd9 1471 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
550e5ca7 1472 conn_id->state = RDMA_CM_CONNECT;
07ebafba 1473
1ab35276 1474 dev = ip_dev_find(&init_net, iw_event->local_addr.sin_addr.s_addr);
07ebafba
TT
1475 if (!dev) {
1476 ret = -EADDRNOTAVAIL;
de910bd9 1477 mutex_unlock(&conn_id->handler_mutex);
07ebafba
TT
1478 rdma_destroy_id(new_cm_id);
1479 goto out;
1480 }
1481 ret = rdma_copy_addr(&conn_id->id.route.addr.dev_addr, dev, NULL);
1482 if (ret) {
de910bd9 1483 mutex_unlock(&conn_id->handler_mutex);
07ebafba
TT
1484 rdma_destroy_id(new_cm_id);
1485 goto out;
1486 }
1487
1488 ret = cma_acquire_dev(conn_id);
1489 if (ret) {
de910bd9 1490 mutex_unlock(&conn_id->handler_mutex);
07ebafba
TT
1491 rdma_destroy_id(new_cm_id);
1492 goto out;
1493 }
1494
1495 conn_id->cm_id.iw = cm_id;
1496 cm_id->context = conn_id;
1497 cm_id->cm_handler = cma_iw_handler;
1498
f4753834 1499 sin = (struct sockaddr_in *) cma_src_addr(conn_id);
07ebafba 1500 *sin = iw_event->local_addr;
f4753834 1501 sin = (struct sockaddr_in *) cma_dst_addr(conn_id);
07ebafba
TT
1502 *sin = iw_event->remote_addr;
1503
8d8293cf
SW
1504 ret = ib_query_device(conn_id->id.device, &attr);
1505 if (ret) {
de910bd9 1506 mutex_unlock(&conn_id->handler_mutex);
8d8293cf
SW
1507 rdma_destroy_id(new_cm_id);
1508 goto out;
1509 }
1510
a1b1b61f
SH
1511 memset(&event, 0, sizeof event);
1512 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1513 event.param.conn.private_data = iw_event->private_data;
1514 event.param.conn.private_data_len = iw_event->private_data_len;
3ebeebc3
KS
1515 event.param.conn.initiator_depth = iw_event->ird;
1516 event.param.conn.responder_resources = iw_event->ord;
25ae21a1
SH
1517
1518 /*
1519 * Protect against the user destroying conn_id from another thread
1520 * until we're done accessing it.
1521 */
1522 atomic_inc(&conn_id->refcount);
a1b1b61f 1523 ret = conn_id->id.event_handler(&conn_id->id, &event);
07ebafba
TT
1524 if (ret) {
1525 /* User wants to destroy the CM ID */
1526 conn_id->cm_id.iw = NULL;
550e5ca7 1527 cma_exch(conn_id, RDMA_CM_DESTROYING);
de910bd9 1528 mutex_unlock(&conn_id->handler_mutex);
25ae21a1 1529 cma_deref_id(conn_id);
07ebafba 1530 rdma_destroy_id(&conn_id->id);
de910bd9 1531 goto out;
07ebafba
TT
1532 }
1533
de910bd9 1534 mutex_unlock(&conn_id->handler_mutex);
25ae21a1 1535 cma_deref_id(conn_id);
de910bd9 1536
07ebafba
TT
1537out:
1538 if (dev)
1539 dev_put(dev);
de910bd9 1540 mutex_unlock(&listen_id->handler_mutex);
07ebafba
TT
1541 return ret;
1542}
1543
e51060f0
SH
1544static int cma_ib_listen(struct rdma_id_private *id_priv)
1545{
1546 struct ib_cm_compare_data compare_data;
1547 struct sockaddr *addr;
0c9361fc 1548 struct ib_cm_id *id;
e51060f0
SH
1549 __be64 svc_id;
1550 int ret;
1551
0c9361fc
JM
1552 id = ib_create_cm_id(id_priv->id.device, cma_req_handler, id_priv);
1553 if (IS_ERR(id))
1554 return PTR_ERR(id);
1555
1556 id_priv->cm_id.ib = id;
e51060f0 1557
f4753834 1558 addr = cma_src_addr(id_priv);
e51060f0 1559 svc_id = cma_get_service_id(id_priv->id.ps, addr);
406b6a25 1560 if (cma_any_addr(addr) && !id_priv->afonly)
e51060f0
SH
1561 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, NULL);
1562 else {
1563 cma_set_compare_data(id_priv->id.ps, addr, &compare_data);
1564 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, &compare_data);
1565 }
1566
1567 if (ret) {
1568 ib_destroy_cm_id(id_priv->cm_id.ib);
1569 id_priv->cm_id.ib = NULL;
1570 }
1571
1572 return ret;
1573}
1574
07ebafba
TT
1575static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
1576{
1577 int ret;
1578 struct sockaddr_in *sin;
0c9361fc
JM
1579 struct iw_cm_id *id;
1580
1581 id = iw_create_cm_id(id_priv->id.device,
1582 iw_conn_req_handler,
1583 id_priv);
1584 if (IS_ERR(id))
1585 return PTR_ERR(id);
07ebafba 1586
0c9361fc 1587 id_priv->cm_id.iw = id;
07ebafba 1588
f4753834 1589 sin = (struct sockaddr_in *) cma_src_addr(id_priv);
07ebafba
TT
1590 id_priv->cm_id.iw->local_addr = *sin;
1591
1592 ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
1593
1594 if (ret) {
1595 iw_destroy_cm_id(id_priv->cm_id.iw);
1596 id_priv->cm_id.iw = NULL;
1597 }
1598
1599 return ret;
1600}
1601
e51060f0
SH
1602static int cma_listen_handler(struct rdma_cm_id *id,
1603 struct rdma_cm_event *event)
1604{
1605 struct rdma_id_private *id_priv = id->context;
1606
1607 id->context = id_priv->id.context;
1608 id->event_handler = id_priv->id.event_handler;
1609 return id_priv->id.event_handler(id, event);
1610}
1611
1612static void cma_listen_on_dev(struct rdma_id_private *id_priv,
1613 struct cma_device *cma_dev)
1614{
1615 struct rdma_id_private *dev_id_priv;
1616 struct rdma_cm_id *id;
1617 int ret;
1618
94d0c939
SH
1619 if (cma_family(id_priv) == AF_IB &&
1620 rdma_node_get_transport(cma_dev->device->node_type) != RDMA_TRANSPORT_IB)
1621 return;
1622
b26f9b99
SH
1623 id = rdma_create_id(cma_listen_handler, id_priv, id_priv->id.ps,
1624 id_priv->id.qp_type);
e51060f0
SH
1625 if (IS_ERR(id))
1626 return;
1627
1628 dev_id_priv = container_of(id, struct rdma_id_private, id);
1629
550e5ca7 1630 dev_id_priv->state = RDMA_CM_ADDR_BOUND;
f4753834
SH
1631 memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv),
1632 rdma_addr_size(cma_src_addr(id_priv)));
e51060f0
SH
1633
1634 cma_attach_to_dev(dev_id_priv, cma_dev);
1635 list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
d02d1f53
SH
1636 atomic_inc(&id_priv->refcount);
1637 dev_id_priv->internal_id = 1;
5b0ec991 1638 dev_id_priv->afonly = id_priv->afonly;
e51060f0
SH
1639
1640 ret = rdma_listen(id, id_priv->backlog);
1641 if (ret)
d02d1f53 1642 printk(KERN_WARNING "RDMA CMA: cma_listen_on_dev, error %d, "
468f2239 1643 "listening on device %s\n", ret, cma_dev->device->name);
e51060f0
SH
1644}
1645
1646static void cma_listen_on_all(struct rdma_id_private *id_priv)
1647{
1648 struct cma_device *cma_dev;
1649
1650 mutex_lock(&lock);
1651 list_add_tail(&id_priv->list, &listen_any_list);
1652 list_for_each_entry(cma_dev, &dev_list, list)
1653 cma_listen_on_dev(id_priv, cma_dev);
1654 mutex_unlock(&lock);
1655}
1656
a81c994d
SH
1657void rdma_set_service_type(struct rdma_cm_id *id, int tos)
1658{
1659 struct rdma_id_private *id_priv;
1660
1661 id_priv = container_of(id, struct rdma_id_private, id);
1662 id_priv->tos = (u8) tos;
1663}
1664EXPORT_SYMBOL(rdma_set_service_type);
1665
e51060f0
SH
1666static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
1667 void *context)
1668{
1669 struct cma_work *work = context;
1670 struct rdma_route *route;
1671
1672 route = &work->id->id.route;
1673
1674 if (!status) {
1675 route->num_paths = 1;
1676 *route->path_rec = *path_rec;
1677 } else {
550e5ca7
NM
1678 work->old_state = RDMA_CM_ROUTE_QUERY;
1679 work->new_state = RDMA_CM_ADDR_RESOLVED;
e51060f0 1680 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
8f0472d3 1681 work->event.status = status;
e51060f0
SH
1682 }
1683
1684 queue_work(cma_wq, &work->work);
1685}
1686
1687static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
1688 struct cma_work *work)
1689{
f4753834 1690 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
e51060f0 1691 struct ib_sa_path_rec path_rec;
a81c994d
SH
1692 ib_sa_comp_mask comp_mask;
1693 struct sockaddr_in6 *sin6;
f68194ca 1694 struct sockaddr_ib *sib;
e51060f0
SH
1695
1696 memset(&path_rec, 0, sizeof path_rec);
f4753834
SH
1697 rdma_addr_get_sgid(dev_addr, &path_rec.sgid);
1698 rdma_addr_get_dgid(dev_addr, &path_rec.dgid);
1699 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
e51060f0 1700 path_rec.numb_path = 1;
962063e6 1701 path_rec.reversible = 1;
f4753834 1702 path_rec.service_id = cma_get_service_id(id_priv->id.ps, cma_dst_addr(id_priv));
a81c994d
SH
1703
1704 comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
1705 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
1706 IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
1707
f68194ca
SH
1708 switch (cma_family(id_priv)) {
1709 case AF_INET:
a81c994d
SH
1710 path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
1711 comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
f68194ca
SH
1712 break;
1713 case AF_INET6:
f4753834 1714 sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
a81c994d
SH
1715 path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
1716 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
f68194ca
SH
1717 break;
1718 case AF_IB:
1719 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
1720 path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20);
1721 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
1722 break;
a81c994d 1723 }
e51060f0 1724
c1a0b23b 1725 id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
a81c994d
SH
1726 id_priv->id.port_num, &path_rec,
1727 comp_mask, timeout_ms,
1728 GFP_KERNEL, cma_query_handler,
1729 work, &id_priv->query);
e51060f0
SH
1730
1731 return (id_priv->query_id < 0) ? id_priv->query_id : 0;
1732}
1733
c4028958 1734static void cma_work_handler(struct work_struct *_work)
e51060f0 1735{
c4028958 1736 struct cma_work *work = container_of(_work, struct cma_work, work);
e51060f0
SH
1737 struct rdma_id_private *id_priv = work->id;
1738 int destroy = 0;
1739
de910bd9 1740 mutex_lock(&id_priv->handler_mutex);
e51060f0
SH
1741 if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
1742 goto out;
1743
1744 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
550e5ca7 1745 cma_exch(id_priv, RDMA_CM_DESTROYING);
e51060f0
SH
1746 destroy = 1;
1747 }
1748out:
de910bd9 1749 mutex_unlock(&id_priv->handler_mutex);
e51060f0
SH
1750 cma_deref_id(id_priv);
1751 if (destroy)
1752 rdma_destroy_id(&id_priv->id);
1753 kfree(work);
1754}
1755
dd5bdff8
OG
1756static void cma_ndev_work_handler(struct work_struct *_work)
1757{
1758 struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work);
1759 struct rdma_id_private *id_priv = work->id;
1760 int destroy = 0;
1761
1762 mutex_lock(&id_priv->handler_mutex);
550e5ca7
NM
1763 if (id_priv->state == RDMA_CM_DESTROYING ||
1764 id_priv->state == RDMA_CM_DEVICE_REMOVAL)
dd5bdff8
OG
1765 goto out;
1766
1767 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
550e5ca7 1768 cma_exch(id_priv, RDMA_CM_DESTROYING);
dd5bdff8
OG
1769 destroy = 1;
1770 }
1771
1772out:
1773 mutex_unlock(&id_priv->handler_mutex);
1774 cma_deref_id(id_priv);
1775 if (destroy)
1776 rdma_destroy_id(&id_priv->id);
1777 kfree(work);
1778}
1779
e51060f0
SH
1780static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
1781{
1782 struct rdma_route *route = &id_priv->id.route;
1783 struct cma_work *work;
1784 int ret;
1785
1786 work = kzalloc(sizeof *work, GFP_KERNEL);
1787 if (!work)
1788 return -ENOMEM;
1789
1790 work->id = id_priv;
c4028958 1791 INIT_WORK(&work->work, cma_work_handler);
550e5ca7
NM
1792 work->old_state = RDMA_CM_ROUTE_QUERY;
1793 work->new_state = RDMA_CM_ROUTE_RESOLVED;
e51060f0
SH
1794 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1795
1796 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
1797 if (!route->path_rec) {
1798 ret = -ENOMEM;
1799 goto err1;
1800 }
1801
1802 ret = cma_query_ib_route(id_priv, timeout_ms, work);
1803 if (ret)
1804 goto err2;
1805
1806 return 0;
1807err2:
1808 kfree(route->path_rec);
1809 route->path_rec = NULL;
1810err1:
1811 kfree(work);
1812 return ret;
1813}
1814
1815int rdma_set_ib_paths(struct rdma_cm_id *id,
1816 struct ib_sa_path_rec *path_rec, int num_paths)
1817{
1818 struct rdma_id_private *id_priv;
1819 int ret;
1820
1821 id_priv = container_of(id, struct rdma_id_private, id);
550e5ca7
NM
1822 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
1823 RDMA_CM_ROUTE_RESOLVED))
e51060f0
SH
1824 return -EINVAL;
1825
9893e742
JL
1826 id->route.path_rec = kmemdup(path_rec, sizeof *path_rec * num_paths,
1827 GFP_KERNEL);
e51060f0
SH
1828 if (!id->route.path_rec) {
1829 ret = -ENOMEM;
1830 goto err;
1831 }
1832
ae2d9293 1833 id->route.num_paths = num_paths;
e51060f0
SH
1834 return 0;
1835err:
550e5ca7 1836 cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
e51060f0
SH
1837 return ret;
1838}
1839EXPORT_SYMBOL(rdma_set_ib_paths);
1840
07ebafba
TT
1841static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
1842{
1843 struct cma_work *work;
1844
1845 work = kzalloc(sizeof *work, GFP_KERNEL);
1846 if (!work)
1847 return -ENOMEM;
1848
1849 work->id = id_priv;
c4028958 1850 INIT_WORK(&work->work, cma_work_handler);
550e5ca7
NM
1851 work->old_state = RDMA_CM_ROUTE_QUERY;
1852 work->new_state = RDMA_CM_ROUTE_RESOLVED;
07ebafba
TT
1853 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1854 queue_work(cma_wq, &work->work);
1855 return 0;
1856}
1857
3c86aa70
EC
1858static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
1859{
1860 struct rdma_route *route = &id_priv->id.route;
1861 struct rdma_addr *addr = &route->addr;
1862 struct cma_work *work;
1863 int ret;
3c86aa70 1864 struct net_device *ndev = NULL;
af7bd463 1865 u16 vid;
3c86aa70 1866
3c86aa70
EC
1867 work = kzalloc(sizeof *work, GFP_KERNEL);
1868 if (!work)
1869 return -ENOMEM;
1870
1871 work->id = id_priv;
1872 INIT_WORK(&work->work, cma_work_handler);
1873
1874 route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
1875 if (!route->path_rec) {
1876 ret = -ENOMEM;
1877 goto err1;
1878 }
1879
1880 route->num_paths = 1;
1881
3c86aa70
EC
1882 if (addr->dev_addr.bound_dev_if)
1883 ndev = dev_get_by_index(&init_net, addr->dev_addr.bound_dev_if);
1884 if (!ndev) {
1885 ret = -ENODEV;
1886 goto err2;
1887 }
1888
af7bd463
EC
1889 vid = rdma_vlan_dev_vlan_id(ndev);
1890
1891 iboe_mac_vlan_to_ll(&route->path_rec->sgid, addr->dev_addr.src_dev_addr, vid);
1892 iboe_mac_vlan_to_ll(&route->path_rec->dgid, addr->dev_addr.dst_dev_addr, vid);
1893
1894 route->path_rec->hop_limit = 1;
1895 route->path_rec->reversible = 1;
1896 route->path_rec->pkey = cpu_to_be16(0xffff);
1897 route->path_rec->mtu_selector = IB_SA_EQ;
366cddb4
AV
1898 route->path_rec->sl = netdev_get_prio_tc_map(
1899 ndev->priv_flags & IFF_802_1Q_VLAN ?
1900 vlan_dev_real_dev(ndev) : ndev,
1901 rt_tos2priority(id_priv->tos));
af7bd463 1902
3c86aa70
EC
1903 route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
1904 route->path_rec->rate_selector = IB_SA_EQ;
1905 route->path_rec->rate = iboe_get_rate(ndev);
1906 dev_put(ndev);
1907 route->path_rec->packet_life_time_selector = IB_SA_EQ;
1908 route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
1909 if (!route->path_rec->mtu) {
1910 ret = -EINVAL;
1911 goto err2;
1912 }
1913
550e5ca7
NM
1914 work->old_state = RDMA_CM_ROUTE_QUERY;
1915 work->new_state = RDMA_CM_ROUTE_RESOLVED;
3c86aa70
EC
1916 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1917 work->event.status = 0;
1918
1919 queue_work(cma_wq, &work->work);
1920
1921 return 0;
1922
1923err2:
1924 kfree(route->path_rec);
1925 route->path_rec = NULL;
1926err1:
1927 kfree(work);
1928 return ret;
1929}
1930
e51060f0
SH
1931int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
1932{
1933 struct rdma_id_private *id_priv;
1934 int ret;
1935
1936 id_priv = container_of(id, struct rdma_id_private, id);
550e5ca7 1937 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
e51060f0
SH
1938 return -EINVAL;
1939
1940 atomic_inc(&id_priv->refcount);
07ebafba
TT
1941 switch (rdma_node_get_transport(id->device->node_type)) {
1942 case RDMA_TRANSPORT_IB:
3c86aa70
EC
1943 switch (rdma_port_get_link_layer(id->device, id->port_num)) {
1944 case IB_LINK_LAYER_INFINIBAND:
1945 ret = cma_resolve_ib_route(id_priv, timeout_ms);
1946 break;
1947 case IB_LINK_LAYER_ETHERNET:
1948 ret = cma_resolve_iboe_route(id_priv);
1949 break;
1950 default:
1951 ret = -ENOSYS;
1952 }
e51060f0 1953 break;
07ebafba
TT
1954 case RDMA_TRANSPORT_IWARP:
1955 ret = cma_resolve_iw_route(id_priv, timeout_ms);
1956 break;
e51060f0
SH
1957 default:
1958 ret = -ENOSYS;
1959 break;
1960 }
1961 if (ret)
1962 goto err;
1963
1964 return 0;
1965err:
550e5ca7 1966 cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
e51060f0
SH
1967 cma_deref_id(id_priv);
1968 return ret;
1969}
1970EXPORT_SYMBOL(rdma_resolve_route);
1971
6a3e362d
SH
1972static void cma_set_loopback(struct sockaddr *addr)
1973{
1974 switch (addr->sa_family) {
1975 case AF_INET:
1976 ((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1977 break;
1978 case AF_INET6:
1979 ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr,
1980 0, 0, 0, htonl(1));
1981 break;
1982 default:
1983 ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr,
1984 0, 0, 0, htonl(1));
1985 break;
1986 }
1987}
1988
e51060f0
SH
1989static int cma_bind_loopback(struct rdma_id_private *id_priv)
1990{
b0569e40 1991 struct cma_device *cma_dev, *cur_dev;
e51060f0 1992 struct ib_port_attr port_attr;
f0ee3404 1993 union ib_gid gid;
e51060f0
SH
1994 u16 pkey;
1995 int ret;
1996 u8 p;
1997
b0569e40 1998 cma_dev = NULL;
e51060f0 1999 mutex_lock(&lock);
b0569e40
SH
2000 list_for_each_entry(cur_dev, &dev_list, list) {
2001 if (cma_family(id_priv) == AF_IB &&
2002 rdma_node_get_transport(cur_dev->device->node_type) != RDMA_TRANSPORT_IB)
2003 continue;
2004
2005 if (!cma_dev)
2006 cma_dev = cur_dev;
2007
2008 for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
2009 if (!ib_query_port(cur_dev->device, p, &port_attr) &&
2010 port_attr.state == IB_PORT_ACTIVE) {
2011 cma_dev = cur_dev;
2012 goto port_found;
2013 }
2014 }
2015 }
2016
2017 if (!cma_dev) {
e82153b5
KK
2018 ret = -ENODEV;
2019 goto out;
2020 }
e51060f0 2021
e82153b5 2022 p = 1;
e51060f0
SH
2023
2024port_found:
f0ee3404 2025 ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid);
e51060f0
SH
2026 if (ret)
2027 goto out;
2028
2029 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
2030 if (ret)
2031 goto out;
2032
6f8372b6 2033 id_priv->id.route.addr.dev_addr.dev_type =
3c86aa70 2034 (rdma_port_get_link_layer(cma_dev->device, p) == IB_LINK_LAYER_INFINIBAND) ?
6f8372b6
SH
2035 ARPHRD_INFINIBAND : ARPHRD_ETHER;
2036
2037 rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
e51060f0
SH
2038 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
2039 id_priv->id.port_num = p;
2040 cma_attach_to_dev(id_priv, cma_dev);
f4753834 2041 cma_set_loopback(cma_src_addr(id_priv));
e51060f0
SH
2042out:
2043 mutex_unlock(&lock);
2044 return ret;
2045}
2046
2047static void addr_handler(int status, struct sockaddr *src_addr,
2048 struct rdma_dev_addr *dev_addr, void *context)
2049{
2050 struct rdma_id_private *id_priv = context;
a1b1b61f 2051 struct rdma_cm_event event;
e51060f0 2052
a1b1b61f 2053 memset(&event, 0, sizeof event);
de910bd9 2054 mutex_lock(&id_priv->handler_mutex);
550e5ca7
NM
2055 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
2056 RDMA_CM_ADDR_RESOLVED))
61a73c70 2057 goto out;
61a73c70
SH
2058
2059 if (!status && !id_priv->cma_dev)
e51060f0
SH
2060 status = cma_acquire_dev(id_priv);
2061
2062 if (status) {
550e5ca7
NM
2063 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2064 RDMA_CM_ADDR_BOUND))
e51060f0 2065 goto out;
a1b1b61f
SH
2066 event.event = RDMA_CM_EVENT_ADDR_ERROR;
2067 event.status = status;
e51060f0 2068 } else {
f4753834 2069 memcpy(cma_src_addr(id_priv), src_addr, rdma_addr_size(src_addr));
a1b1b61f 2070 event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
e51060f0
SH
2071 }
2072
a1b1b61f 2073 if (id_priv->id.event_handler(&id_priv->id, &event)) {
550e5ca7 2074 cma_exch(id_priv, RDMA_CM_DESTROYING);
de910bd9 2075 mutex_unlock(&id_priv->handler_mutex);
e51060f0
SH
2076 cma_deref_id(id_priv);
2077 rdma_destroy_id(&id_priv->id);
2078 return;
2079 }
2080out:
de910bd9 2081 mutex_unlock(&id_priv->handler_mutex);
e51060f0
SH
2082 cma_deref_id(id_priv);
2083}
2084
2085static int cma_resolve_loopback(struct rdma_id_private *id_priv)
2086{
2087 struct cma_work *work;
f0ee3404 2088 union ib_gid gid;
e51060f0
SH
2089 int ret;
2090
2091 work = kzalloc(sizeof *work, GFP_KERNEL);
2092 if (!work)
2093 return -ENOMEM;
2094
2095 if (!id_priv->cma_dev) {
2096 ret = cma_bind_loopback(id_priv);
2097 if (ret)
2098 goto err;
2099 }
2100
6f8372b6
SH
2101 rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
2102 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
e51060f0 2103
e51060f0 2104 work->id = id_priv;
c4028958 2105 INIT_WORK(&work->work, cma_work_handler);
550e5ca7
NM
2106 work->old_state = RDMA_CM_ADDR_QUERY;
2107 work->new_state = RDMA_CM_ADDR_RESOLVED;
e51060f0
SH
2108 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2109 queue_work(cma_wq, &work->work);
2110 return 0;
2111err:
2112 kfree(work);
2113 return ret;
2114}
2115
f17df3b0
SH
2116static int cma_resolve_ib_addr(struct rdma_id_private *id_priv)
2117{
2118 struct cma_work *work;
2119 int ret;
2120
2121 work = kzalloc(sizeof *work, GFP_KERNEL);
2122 if (!work)
2123 return -ENOMEM;
2124
2125 if (!id_priv->cma_dev) {
2126 ret = cma_resolve_ib_dev(id_priv);
2127 if (ret)
2128 goto err;
2129 }
2130
2131 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *)
2132 &(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr));
2133
2134 work->id = id_priv;
2135 INIT_WORK(&work->work, cma_work_handler);
2136 work->old_state = RDMA_CM_ADDR_QUERY;
2137 work->new_state = RDMA_CM_ADDR_RESOLVED;
2138 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2139 queue_work(cma_wq, &work->work);
2140 return 0;
2141err:
2142 kfree(work);
2143 return ret;
2144}
2145
e51060f0
SH
2146static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2147 struct sockaddr *dst_addr)
2148{
d14714df
SH
2149 if (!src_addr || !src_addr->sa_family) {
2150 src_addr = (struct sockaddr *) &id->route.addr.src_addr;
f17df3b0
SH
2151 src_addr->sa_family = dst_addr->sa_family;
2152 if (dst_addr->sa_family == AF_INET6) {
d14714df
SH
2153 ((struct sockaddr_in6 *) src_addr)->sin6_scope_id =
2154 ((struct sockaddr_in6 *) dst_addr)->sin6_scope_id;
f17df3b0
SH
2155 } else if (dst_addr->sa_family == AF_IB) {
2156 ((struct sockaddr_ib *) src_addr)->sib_pkey =
2157 ((struct sockaddr_ib *) dst_addr)->sib_pkey;
d14714df
SH
2158 }
2159 }
2160 return rdma_bind_addr(id, src_addr);
e51060f0
SH
2161}
2162
2163int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2164 struct sockaddr *dst_addr, int timeout_ms)
2165{
2166 struct rdma_id_private *id_priv;
2167 int ret;
2168
2169 id_priv = container_of(id, struct rdma_id_private, id);
550e5ca7 2170 if (id_priv->state == RDMA_CM_IDLE) {
e51060f0
SH
2171 ret = cma_bind_addr(id, src_addr, dst_addr);
2172 if (ret)
2173 return ret;
2174 }
2175
4ae7152e
SH
2176 if (cma_family(id_priv) != dst_addr->sa_family)
2177 return -EINVAL;
2178
550e5ca7 2179 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY))
e51060f0
SH
2180 return -EINVAL;
2181
2182 atomic_inc(&id_priv->refcount);
f4753834 2183 memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr));
f17df3b0 2184 if (cma_any_addr(dst_addr)) {
e51060f0 2185 ret = cma_resolve_loopback(id_priv);
f17df3b0
SH
2186 } else {
2187 if (dst_addr->sa_family == AF_IB) {
2188 ret = cma_resolve_ib_addr(id_priv);
2189 } else {
2190 ret = rdma_resolve_ip(&addr_client, cma_src_addr(id_priv),
2191 dst_addr, &id->route.addr.dev_addr,
2192 timeout_ms, addr_handler, id_priv);
2193 }
2194 }
e51060f0
SH
2195 if (ret)
2196 goto err;
2197
2198 return 0;
2199err:
550e5ca7 2200 cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
e51060f0
SH
2201 cma_deref_id(id_priv);
2202 return ret;
2203}
2204EXPORT_SYMBOL(rdma_resolve_addr);
2205
a9bb7912
HS
2206int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
2207{
2208 struct rdma_id_private *id_priv;
2209 unsigned long flags;
2210 int ret;
2211
2212 id_priv = container_of(id, struct rdma_id_private, id);
2213 spin_lock_irqsave(&id_priv->lock, flags);
c8dea2f9 2214 if (reuse || id_priv->state == RDMA_CM_IDLE) {
a9bb7912
HS
2215 id_priv->reuseaddr = reuse;
2216 ret = 0;
2217 } else {
2218 ret = -EINVAL;
2219 }
2220 spin_unlock_irqrestore(&id_priv->lock, flags);
2221 return ret;
2222}
2223EXPORT_SYMBOL(rdma_set_reuseaddr);
2224
68602120
SH
2225int rdma_set_afonly(struct rdma_cm_id *id, int afonly)
2226{
2227 struct rdma_id_private *id_priv;
2228 unsigned long flags;
2229 int ret;
2230
2231 id_priv = container_of(id, struct rdma_id_private, id);
2232 spin_lock_irqsave(&id_priv->lock, flags);
2233 if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) {
2234 id_priv->options |= (1 << CMA_OPTION_AFONLY);
2235 id_priv->afonly = afonly;
2236 ret = 0;
2237 } else {
2238 ret = -EINVAL;
2239 }
2240 spin_unlock_irqrestore(&id_priv->lock, flags);
2241 return ret;
2242}
2243EXPORT_SYMBOL(rdma_set_afonly);
2244
e51060f0
SH
2245static void cma_bind_port(struct rdma_bind_list *bind_list,
2246 struct rdma_id_private *id_priv)
2247{
58afdcb7
SH
2248 struct sockaddr *addr;
2249 struct sockaddr_ib *sib;
2250 u64 sid, mask;
2251 __be16 port;
e51060f0 2252
f4753834 2253 addr = cma_src_addr(id_priv);
58afdcb7
SH
2254 port = htons(bind_list->port);
2255
2256 switch (addr->sa_family) {
2257 case AF_INET:
2258 ((struct sockaddr_in *) addr)->sin_port = port;
2259 break;
2260 case AF_INET6:
2261 ((struct sockaddr_in6 *) addr)->sin6_port = port;
2262 break;
2263 case AF_IB:
2264 sib = (struct sockaddr_ib *) addr;
2265 sid = be64_to_cpu(sib->sib_sid);
2266 mask = be64_to_cpu(sib->sib_sid_mask);
2267 sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port));
2268 sib->sib_sid_mask = cpu_to_be64(~0ULL);
2269 break;
2270 }
e51060f0
SH
2271 id_priv->bind_list = bind_list;
2272 hlist_add_head(&id_priv->node, &bind_list->owners);
2273}
2274
2275static int cma_alloc_port(struct idr *ps, struct rdma_id_private *id_priv,
2276 unsigned short snum)
2277{
2278 struct rdma_bind_list *bind_list;
3b069c5d 2279 int ret;
e51060f0 2280
cb164b8c 2281 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
e51060f0
SH
2282 if (!bind_list)
2283 return -ENOMEM;
2284
3b069c5d
TH
2285 ret = idr_alloc(ps, bind_list, snum, snum + 1, GFP_KERNEL);
2286 if (ret < 0)
2287 goto err;
aedec080
SH
2288
2289 bind_list->ps = ps;
3b069c5d 2290 bind_list->port = (unsigned short)ret;
aedec080
SH
2291 cma_bind_port(bind_list, id_priv);
2292 return 0;
3b069c5d 2293err:
aedec080 2294 kfree(bind_list);
3b069c5d 2295 return ret == -ENOSPC ? -EADDRNOTAVAIL : ret;
aedec080 2296}
e51060f0 2297
aedec080
SH
2298static int cma_alloc_any_port(struct idr *ps, struct rdma_id_private *id_priv)
2299{
5d7220e8
TH
2300 static unsigned int last_used_port;
2301 int low, high, remaining;
2302 unsigned int rover;
e51060f0 2303
227b60f5 2304 inet_get_local_port_range(&low, &high);
5d7220e8
TH
2305 remaining = (high - low) + 1;
2306 rover = net_random() % remaining + low;
2307retry:
2308 if (last_used_port != rover &&
2309 !idr_find(ps, (unsigned short) rover)) {
2310 int ret = cma_alloc_port(ps, id_priv, rover);
2311 /*
2312 * Remember previously used port number in order to avoid
2313 * re-using same port immediately after it is closed.
2314 */
2315 if (!ret)
2316 last_used_port = rover;
2317 if (ret != -EADDRNOTAVAIL)
2318 return ret;
e51060f0 2319 }
5d7220e8
TH
2320 if (--remaining) {
2321 rover++;
2322 if ((rover < low) || (rover > high))
2323 rover = low;
2324 goto retry;
2325 }
2326 return -EADDRNOTAVAIL;
e51060f0
SH
2327}
2328
a9bb7912
HS
2329/*
2330 * Check that the requested port is available. This is called when trying to
2331 * bind to a specific port, or when trying to listen on a bound port. In
2332 * the latter case, the provided id_priv may already be on the bind_list, but
2333 * we still need to check that it's okay to start listening.
2334 */
2335static int cma_check_port(struct rdma_bind_list *bind_list,
2336 struct rdma_id_private *id_priv, uint8_t reuseaddr)
e51060f0
SH
2337{
2338 struct rdma_id_private *cur_id;
43b752da 2339 struct sockaddr *addr, *cur_addr;
e51060f0 2340
f4753834 2341 addr = cma_src_addr(id_priv);
b67bfe0d 2342 hlist_for_each_entry(cur_id, &bind_list->owners, node) {
a9bb7912
HS
2343 if (id_priv == cur_id)
2344 continue;
3cd96564 2345
5b0ec991
SH
2346 if ((cur_id->state != RDMA_CM_LISTEN) && reuseaddr &&
2347 cur_id->reuseaddr)
2348 continue;
e51060f0 2349
f4753834 2350 cur_addr = cma_src_addr(cur_id);
5b0ec991
SH
2351 if (id_priv->afonly && cur_id->afonly &&
2352 (addr->sa_family != cur_addr->sa_family))
2353 continue;
2354
2355 if (cma_any_addr(addr) || cma_any_addr(cur_addr))
2356 return -EADDRNOTAVAIL;
2357
2358 if (!cma_addr_cmp(addr, cur_addr))
2359 return -EADDRINUSE;
a9bb7912 2360 }
e51060f0
SH
2361 return 0;
2362}
2363
a9bb7912
HS
2364static int cma_use_port(struct idr *ps, struct rdma_id_private *id_priv)
2365{
2366 struct rdma_bind_list *bind_list;
2367 unsigned short snum;
2368 int ret;
2369
f4753834 2370 snum = ntohs(cma_port(cma_src_addr(id_priv)));
a9bb7912
HS
2371 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
2372 return -EACCES;
2373
2374 bind_list = idr_find(ps, snum);
2375 if (!bind_list) {
2376 ret = cma_alloc_port(ps, id_priv, snum);
2377 } else {
2378 ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
2379 if (!ret)
2380 cma_bind_port(bind_list, id_priv);
2381 }
2382 return ret;
2383}
2384
2385static int cma_bind_listen(struct rdma_id_private *id_priv)
2386{
2387 struct rdma_bind_list *bind_list = id_priv->bind_list;
2388 int ret = 0;
2389
2390 mutex_lock(&lock);
2391 if (bind_list->owners.first->next)
2392 ret = cma_check_port(bind_list, id_priv, 0);
2393 mutex_unlock(&lock);
2394 return ret;
2395}
2396
58afdcb7 2397static struct idr *cma_select_inet_ps(struct rdma_id_private *id_priv)
e51060f0 2398{
e51060f0 2399 switch (id_priv->id.ps) {
e51060f0 2400 case RDMA_PS_TCP:
58afdcb7 2401 return &tcp_ps;
628e5f6d 2402 case RDMA_PS_UDP:
58afdcb7 2403 return &udp_ps;
c8f6a362 2404 case RDMA_PS_IPOIB:
58afdcb7 2405 return &ipoib_ps;
2d2e9415 2406 case RDMA_PS_IB:
58afdcb7 2407 return &ib_ps;
e51060f0 2408 default:
58afdcb7
SH
2409 return NULL;
2410 }
2411}
2412
2413static struct idr *cma_select_ib_ps(struct rdma_id_private *id_priv)
2414{
2415 struct idr *ps = NULL;
2416 struct sockaddr_ib *sib;
2417 u64 sid_ps, mask, sid;
2418
f4753834 2419 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
58afdcb7
SH
2420 mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK;
2421 sid = be64_to_cpu(sib->sib_sid) & mask;
2422
2423 if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) {
2424 sid_ps = RDMA_IB_IP_PS_IB;
2425 ps = &ib_ps;
2426 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) &&
2427 (sid == (RDMA_IB_IP_PS_TCP & mask))) {
2428 sid_ps = RDMA_IB_IP_PS_TCP;
2429 ps = &tcp_ps;
2430 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) &&
2431 (sid == (RDMA_IB_IP_PS_UDP & mask))) {
2432 sid_ps = RDMA_IB_IP_PS_UDP;
2433 ps = &udp_ps;
e51060f0
SH
2434 }
2435
58afdcb7
SH
2436 if (ps) {
2437 sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib)));
2438 sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK |
2439 be64_to_cpu(sib->sib_sid_mask));
2440 }
2441 return ps;
2442}
2443
2444static int cma_get_port(struct rdma_id_private *id_priv)
2445{
2446 struct idr *ps;
2447 int ret;
2448
f4753834 2449 if (cma_family(id_priv) != AF_IB)
58afdcb7
SH
2450 ps = cma_select_inet_ps(id_priv);
2451 else
2452 ps = cma_select_ib_ps(id_priv);
2453 if (!ps)
2454 return -EPROTONOSUPPORT;
2455
e51060f0 2456 mutex_lock(&lock);
f4753834 2457 if (cma_any_port(cma_src_addr(id_priv)))
aedec080 2458 ret = cma_alloc_any_port(ps, id_priv);
e51060f0
SH
2459 else
2460 ret = cma_use_port(ps, id_priv);
2461 mutex_unlock(&lock);
2462
2463 return ret;
2464}
2465
d14714df
SH
2466static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
2467 struct sockaddr *addr)
2468{
d90f9b35 2469#if IS_ENABLED(CONFIG_IPV6)
d14714df
SH
2470 struct sockaddr_in6 *sin6;
2471
2472 if (addr->sa_family != AF_INET6)
2473 return 0;
2474
2475 sin6 = (struct sockaddr_in6 *) addr;
2476 if ((ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) &&
2477 !sin6->sin6_scope_id)
2478 return -EINVAL;
2479
2480 dev_addr->bound_dev_if = sin6->sin6_scope_id;
2481#endif
2482 return 0;
2483}
2484
a9bb7912
HS
2485int rdma_listen(struct rdma_cm_id *id, int backlog)
2486{
2487 struct rdma_id_private *id_priv;
2488 int ret;
2489
2490 id_priv = container_of(id, struct rdma_id_private, id);
550e5ca7 2491 if (id_priv->state == RDMA_CM_IDLE) {
f4753834
SH
2492 id->route.addr.src_addr.ss_family = AF_INET;
2493 ret = rdma_bind_addr(id, cma_src_addr(id_priv));
a9bb7912
HS
2494 if (ret)
2495 return ret;
2496 }
2497
550e5ca7 2498 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN))
a9bb7912
HS
2499 return -EINVAL;
2500
2501 if (id_priv->reuseaddr) {
2502 ret = cma_bind_listen(id_priv);
2503 if (ret)
2504 goto err;
2505 }
2506
2507 id_priv->backlog = backlog;
2508 if (id->device) {
2509 switch (rdma_node_get_transport(id->device->node_type)) {
2510 case RDMA_TRANSPORT_IB:
2511 ret = cma_ib_listen(id_priv);
2512 if (ret)
2513 goto err;
2514 break;
2515 case RDMA_TRANSPORT_IWARP:
2516 ret = cma_iw_listen(id_priv, backlog);
2517 if (ret)
2518 goto err;
2519 break;
2520 default:
2521 ret = -ENOSYS;
2522 goto err;
2523 }
2524 } else
2525 cma_listen_on_all(id_priv);
2526
2527 return 0;
2528err:
2529 id_priv->backlog = 0;
550e5ca7 2530 cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
a9bb7912
HS
2531 return ret;
2532}
2533EXPORT_SYMBOL(rdma_listen);
2534
e51060f0
SH
2535int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
2536{
2537 struct rdma_id_private *id_priv;
2538 int ret;
2539
680f920a
SH
2540 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 &&
2541 addr->sa_family != AF_IB)
e51060f0
SH
2542 return -EAFNOSUPPORT;
2543
2544 id_priv = container_of(id, struct rdma_id_private, id);
550e5ca7 2545 if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
e51060f0
SH
2546 return -EINVAL;
2547
d14714df
SH
2548 ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
2549 if (ret)
2550 goto err1;
2551
8523c048 2552 if (!cma_any_addr(addr)) {
680f920a 2553 ret = cma_translate_addr(addr, &id->route.addr.dev_addr);
e51060f0 2554 if (ret)
255d0c14
KK
2555 goto err1;
2556
255d0c14 2557 ret = cma_acquire_dev(id_priv);
255d0c14
KK
2558 if (ret)
2559 goto err1;
e51060f0
SH
2560 }
2561
f4753834 2562 memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr));
68602120
SH
2563 if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) {
2564 if (addr->sa_family == AF_INET)
2565 id_priv->afonly = 1;
5b0ec991 2566#if IS_ENABLED(CONFIG_IPV6)
68602120
SH
2567 else if (addr->sa_family == AF_INET6)
2568 id_priv->afonly = init_net.ipv6.sysctl.bindv6only;
5b0ec991 2569#endif
68602120 2570 }
e51060f0
SH
2571 ret = cma_get_port(id_priv);
2572 if (ret)
255d0c14 2573 goto err2;
e51060f0
SH
2574
2575 return 0;
255d0c14 2576err2:
a396d43a
SH
2577 if (id_priv->cma_dev)
2578 cma_release_dev(id_priv);
255d0c14 2579err1:
550e5ca7 2580 cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
e51060f0
SH
2581 return ret;
2582}
2583EXPORT_SYMBOL(rdma_bind_addr);
2584
f4753834 2585static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv)
e51060f0 2586{
e51060f0 2587 struct cma_hdr *cma_hdr;
e51060f0 2588
01602f11
SH
2589 cma_hdr = hdr;
2590 cma_hdr->cma_version = CMA_VERSION;
f4753834 2591 if (cma_family(id_priv) == AF_INET) {
1f5175ad
AS
2592 struct sockaddr_in *src4, *dst4;
2593
f4753834
SH
2594 src4 = (struct sockaddr_in *) cma_src_addr(id_priv);
2595 dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv);
1f5175ad 2596
01602f11
SH
2597 cma_set_ip_ver(cma_hdr, 4);
2598 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
2599 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
2600 cma_hdr->port = src4->sin_port;
e8160e15 2601 } else if (cma_family(id_priv) == AF_INET6) {
1f5175ad
AS
2602 struct sockaddr_in6 *src6, *dst6;
2603
f4753834
SH
2604 src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
2605 dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv);
1f5175ad 2606
01602f11
SH
2607 cma_set_ip_ver(cma_hdr, 6);
2608 cma_hdr->src_addr.ip6 = src6->sin6_addr;
2609 cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
2610 cma_hdr->port = src6->sin6_port;
e51060f0
SH
2611 }
2612 return 0;
2613}
2614
628e5f6d
SH
2615static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
2616 struct ib_cm_event *ib_event)
2617{
2618 struct rdma_id_private *id_priv = cm_id->context;
2619 struct rdma_cm_event event;
2620 struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
2621 int ret = 0;
2622
550e5ca7 2623 if (cma_disable_callback(id_priv, RDMA_CM_CONNECT))
8aa08602 2624 return 0;
628e5f6d 2625
8aa08602 2626 memset(&event, 0, sizeof event);
628e5f6d
SH
2627 switch (ib_event->event) {
2628 case IB_CM_SIDR_REQ_ERROR:
2629 event.event = RDMA_CM_EVENT_UNREACHABLE;
2630 event.status = -ETIMEDOUT;
2631 break;
2632 case IB_CM_SIDR_REP_RECEIVED:
2633 event.param.ud.private_data = ib_event->private_data;
2634 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
2635 if (rep->status != IB_SIDR_SUCCESS) {
2636 event.event = RDMA_CM_EVENT_UNREACHABLE;
2637 event.status = ib_event->param.sidr_rep_rcvd.status;
2638 break;
2639 }
5c438135 2640 ret = cma_set_qkey(id_priv, rep->qkey);
d2ca39f2
YE
2641 if (ret) {
2642 event.event = RDMA_CM_EVENT_ADDR_ERROR;
5c438135 2643 event.status = ret;
628e5f6d
SH
2644 break;
2645 }
2646 ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
2647 id_priv->id.route.path_rec,
2648 &event.param.ud.ah_attr);
2649 event.param.ud.qp_num = rep->qpn;
2650 event.param.ud.qkey = rep->qkey;
2651 event.event = RDMA_CM_EVENT_ESTABLISHED;
2652 event.status = 0;
2653 break;
2654 default:
468f2239 2655 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
628e5f6d
SH
2656 ib_event->event);
2657 goto out;
2658 }
2659
2660 ret = id_priv->id.event_handler(&id_priv->id, &event);
2661 if (ret) {
2662 /* Destroy the CM ID by returning a non-zero value. */
2663 id_priv->cm_id.ib = NULL;
550e5ca7 2664 cma_exch(id_priv, RDMA_CM_DESTROYING);
de910bd9 2665 mutex_unlock(&id_priv->handler_mutex);
628e5f6d
SH
2666 rdma_destroy_id(&id_priv->id);
2667 return ret;
2668 }
2669out:
de910bd9 2670 mutex_unlock(&id_priv->handler_mutex);
628e5f6d
SH
2671 return ret;
2672}
2673
2674static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
2675 struct rdma_conn_param *conn_param)
2676{
2677 struct ib_cm_sidr_req_param req;
0c9361fc 2678 struct ib_cm_id *id;
e8160e15 2679 int offset, ret;
628e5f6d 2680
e8160e15
SH
2681 offset = cma_user_data_offset(id_priv);
2682 req.private_data_len = offset + conn_param->private_data_len;
04ded167
SH
2683 if (req.private_data_len < conn_param->private_data_len)
2684 return -EINVAL;
2685
e8160e15
SH
2686 if (req.private_data_len) {
2687 req.private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2688 if (!req.private_data)
2689 return -ENOMEM;
2690 } else {
2691 req.private_data = NULL;
2692 }
628e5f6d
SH
2693
2694 if (conn_param->private_data && conn_param->private_data_len)
e8160e15 2695 memcpy((void *) req.private_data + offset,
628e5f6d
SH
2696 conn_param->private_data, conn_param->private_data_len);
2697
e8160e15
SH
2698 if (req.private_data) {
2699 ret = cma_format_hdr((void *) req.private_data, id_priv);
2700 if (ret)
2701 goto out;
2702 }
628e5f6d 2703
0c9361fc
JM
2704 id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
2705 id_priv);
2706 if (IS_ERR(id)) {
2707 ret = PTR_ERR(id);
628e5f6d
SH
2708 goto out;
2709 }
0c9361fc 2710 id_priv->cm_id.ib = id;
628e5f6d 2711
f4753834
SH
2712 req.path = id_priv->id.route.path_rec;
2713 req.service_id = cma_get_service_id(id_priv->id.ps, cma_dst_addr(id_priv));
628e5f6d
SH
2714 req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
2715 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2716
2717 ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
2718 if (ret) {
2719 ib_destroy_cm_id(id_priv->cm_id.ib);
2720 id_priv->cm_id.ib = NULL;
2721 }
2722out:
2723 kfree(req.private_data);
2724 return ret;
2725}
2726
e51060f0
SH
2727static int cma_connect_ib(struct rdma_id_private *id_priv,
2728 struct rdma_conn_param *conn_param)
2729{
2730 struct ib_cm_req_param req;
2731 struct rdma_route *route;
2732 void *private_data;
0c9361fc 2733 struct ib_cm_id *id;
e51060f0
SH
2734 int offset, ret;
2735
2736 memset(&req, 0, sizeof req);
e8160e15 2737 offset = cma_user_data_offset(id_priv);
e51060f0 2738 req.private_data_len = offset + conn_param->private_data_len;
04ded167
SH
2739 if (req.private_data_len < conn_param->private_data_len)
2740 return -EINVAL;
2741
e8160e15
SH
2742 if (req.private_data_len) {
2743 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2744 if (!private_data)
2745 return -ENOMEM;
2746 } else {
2747 private_data = NULL;
2748 }
e51060f0
SH
2749
2750 if (conn_param->private_data && conn_param->private_data_len)
2751 memcpy(private_data + offset, conn_param->private_data,
2752 conn_param->private_data_len);
2753
0c9361fc
JM
2754 id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
2755 if (IS_ERR(id)) {
2756 ret = PTR_ERR(id);
e51060f0
SH
2757 goto out;
2758 }
0c9361fc 2759 id_priv->cm_id.ib = id;
e51060f0
SH
2760
2761 route = &id_priv->id.route;
e8160e15
SH
2762 if (private_data) {
2763 ret = cma_format_hdr(private_data, id_priv);
2764 if (ret)
2765 goto out;
2766 req.private_data = private_data;
2767 }
e51060f0
SH
2768
2769 req.primary_path = &route->path_rec[0];
2770 if (route->num_paths == 2)
2771 req.alternate_path = &route->path_rec[1];
2772
f4753834 2773 req.service_id = cma_get_service_id(id_priv->id.ps, cma_dst_addr(id_priv));
e51060f0 2774 req.qp_num = id_priv->qp_num;
18c441a6 2775 req.qp_type = id_priv->id.qp_type;
e51060f0
SH
2776 req.starting_psn = id_priv->seq_num;
2777 req.responder_resources = conn_param->responder_resources;
2778 req.initiator_depth = conn_param->initiator_depth;
2779 req.flow_control = conn_param->flow_control;
4ede178a
SH
2780 req.retry_count = min_t(u8, 7, conn_param->retry_count);
2781 req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
e51060f0
SH
2782 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2783 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2784 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2785 req.srq = id_priv->srq ? 1 : 0;
2786
2787 ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
2788out:
0c9361fc
JM
2789 if (ret && !IS_ERR(id)) {
2790 ib_destroy_cm_id(id);
675a027c
KK
2791 id_priv->cm_id.ib = NULL;
2792 }
2793
e51060f0
SH
2794 kfree(private_data);
2795 return ret;
2796}
2797
07ebafba
TT
2798static int cma_connect_iw(struct rdma_id_private *id_priv,
2799 struct rdma_conn_param *conn_param)
2800{
2801 struct iw_cm_id *cm_id;
2802 struct sockaddr_in* sin;
2803 int ret;
2804 struct iw_cm_conn_param iw_param;
2805
2806 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
0c9361fc
JM
2807 if (IS_ERR(cm_id))
2808 return PTR_ERR(cm_id);
07ebafba
TT
2809
2810 id_priv->cm_id.iw = cm_id;
2811
f4753834 2812 sin = (struct sockaddr_in *) cma_src_addr(id_priv);
07ebafba
TT
2813 cm_id->local_addr = *sin;
2814
f4753834 2815 sin = (struct sockaddr_in *) cma_dst_addr(id_priv);
07ebafba
TT
2816 cm_id->remote_addr = *sin;
2817
5851bb89 2818 ret = cma_modify_qp_rtr(id_priv, conn_param);
675a027c
KK
2819 if (ret)
2820 goto out;
07ebafba 2821
f45ee80e
HS
2822 if (conn_param) {
2823 iw_param.ord = conn_param->initiator_depth;
2824 iw_param.ird = conn_param->responder_resources;
2825 iw_param.private_data = conn_param->private_data;
2826 iw_param.private_data_len = conn_param->private_data_len;
2827 iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num;
2828 } else {
2829 memset(&iw_param, 0, sizeof iw_param);
07ebafba 2830 iw_param.qpn = id_priv->qp_num;
f45ee80e 2831 }
07ebafba
TT
2832 ret = iw_cm_connect(cm_id, &iw_param);
2833out:
0c9361fc 2834 if (ret) {
675a027c
KK
2835 iw_destroy_cm_id(cm_id);
2836 id_priv->cm_id.iw = NULL;
2837 }
07ebafba
TT
2838 return ret;
2839}
2840
e51060f0
SH
2841int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2842{
2843 struct rdma_id_private *id_priv;
2844 int ret;
2845
2846 id_priv = container_of(id, struct rdma_id_private, id);
550e5ca7 2847 if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
e51060f0
SH
2848 return -EINVAL;
2849
2850 if (!id->qp) {
2851 id_priv->qp_num = conn_param->qp_num;
e51060f0
SH
2852 id_priv->srq = conn_param->srq;
2853 }
2854
07ebafba
TT
2855 switch (rdma_node_get_transport(id->device->node_type)) {
2856 case RDMA_TRANSPORT_IB:
b26f9b99 2857 if (id->qp_type == IB_QPT_UD)
628e5f6d
SH
2858 ret = cma_resolve_ib_udp(id_priv, conn_param);
2859 else
2860 ret = cma_connect_ib(id_priv, conn_param);
e51060f0 2861 break;
07ebafba
TT
2862 case RDMA_TRANSPORT_IWARP:
2863 ret = cma_connect_iw(id_priv, conn_param);
2864 break;
e51060f0
SH
2865 default:
2866 ret = -ENOSYS;
2867 break;
2868 }
2869 if (ret)
2870 goto err;
2871
2872 return 0;
2873err:
550e5ca7 2874 cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
e51060f0
SH
2875 return ret;
2876}
2877EXPORT_SYMBOL(rdma_connect);
2878
2879static int cma_accept_ib(struct rdma_id_private *id_priv,
2880 struct rdma_conn_param *conn_param)
2881{
2882 struct ib_cm_rep_param rep;
5851bb89 2883 int ret;
0fe313b0 2884
5851bb89
SH
2885 ret = cma_modify_qp_rtr(id_priv, conn_param);
2886 if (ret)
2887 goto out;
0fe313b0 2888
5851bb89
SH
2889 ret = cma_modify_qp_rts(id_priv, conn_param);
2890 if (ret)
2891 goto out;
e51060f0
SH
2892
2893 memset(&rep, 0, sizeof rep);
2894 rep.qp_num = id_priv->qp_num;
2895 rep.starting_psn = id_priv->seq_num;
2896 rep.private_data = conn_param->private_data;
2897 rep.private_data_len = conn_param->private_data_len;
2898 rep.responder_resources = conn_param->responder_resources;
2899 rep.initiator_depth = conn_param->initiator_depth;
e51060f0
SH
2900 rep.failover_accepted = 0;
2901 rep.flow_control = conn_param->flow_control;
4ede178a 2902 rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
e51060f0
SH
2903 rep.srq = id_priv->srq ? 1 : 0;
2904
0fe313b0
SH
2905 ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
2906out:
2907 return ret;
e51060f0
SH
2908}
2909
07ebafba
TT
2910static int cma_accept_iw(struct rdma_id_private *id_priv,
2911 struct rdma_conn_param *conn_param)
2912{
2913 struct iw_cm_conn_param iw_param;
2914 int ret;
2915
5851bb89 2916 ret = cma_modify_qp_rtr(id_priv, conn_param);
07ebafba
TT
2917 if (ret)
2918 return ret;
2919
2920 iw_param.ord = conn_param->initiator_depth;
2921 iw_param.ird = conn_param->responder_resources;
2922 iw_param.private_data = conn_param->private_data;
2923 iw_param.private_data_len = conn_param->private_data_len;
2924 if (id_priv->id.qp) {
2925 iw_param.qpn = id_priv->qp_num;
2926 } else
2927 iw_param.qpn = conn_param->qp_num;
2928
2929 return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
2930}
2931
628e5f6d 2932static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
5c438135 2933 enum ib_cm_sidr_status status, u32 qkey,
628e5f6d
SH
2934 const void *private_data, int private_data_len)
2935{
2936 struct ib_cm_sidr_rep_param rep;
d2ca39f2 2937 int ret;
628e5f6d
SH
2938
2939 memset(&rep, 0, sizeof rep);
2940 rep.status = status;
2941 if (status == IB_SIDR_SUCCESS) {
5c438135 2942 ret = cma_set_qkey(id_priv, qkey);
d2ca39f2
YE
2943 if (ret)
2944 return ret;
628e5f6d 2945 rep.qp_num = id_priv->qp_num;
c8f6a362 2946 rep.qkey = id_priv->qkey;
628e5f6d
SH
2947 }
2948 rep.private_data = private_data;
2949 rep.private_data_len = private_data_len;
2950
2951 return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
2952}
2953
e51060f0
SH
2954int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2955{
2956 struct rdma_id_private *id_priv;
2957 int ret;
2958
2959 id_priv = container_of(id, struct rdma_id_private, id);
83e9502d
NM
2960
2961 id_priv->owner = task_pid_nr(current);
2962
550e5ca7 2963 if (!cma_comp(id_priv, RDMA_CM_CONNECT))
e51060f0
SH
2964 return -EINVAL;
2965
2966 if (!id->qp && conn_param) {
2967 id_priv->qp_num = conn_param->qp_num;
e51060f0
SH
2968 id_priv->srq = conn_param->srq;
2969 }
2970
07ebafba
TT
2971 switch (rdma_node_get_transport(id->device->node_type)) {
2972 case RDMA_TRANSPORT_IB:
f45ee80e
HS
2973 if (id->qp_type == IB_QPT_UD) {
2974 if (conn_param)
2975 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
5c438135 2976 conn_param->qkey,
f45ee80e
HS
2977 conn_param->private_data,
2978 conn_param->private_data_len);
2979 else
2980 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
5c438135 2981 0, NULL, 0);
f45ee80e
HS
2982 } else {
2983 if (conn_param)
2984 ret = cma_accept_ib(id_priv, conn_param);
2985 else
2986 ret = cma_rep_recv(id_priv);
2987 }
e51060f0 2988 break;
07ebafba
TT
2989 case RDMA_TRANSPORT_IWARP:
2990 ret = cma_accept_iw(id_priv, conn_param);
2991 break;
e51060f0
SH
2992 default:
2993 ret = -ENOSYS;
2994 break;
2995 }
2996
2997 if (ret)
2998 goto reject;
2999
3000 return 0;
3001reject:
c5483388 3002 cma_modify_qp_err(id_priv);
e51060f0
SH
3003 rdma_reject(id, NULL, 0);
3004 return ret;
3005}
3006EXPORT_SYMBOL(rdma_accept);
3007
0fe313b0
SH
3008int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
3009{
3010 struct rdma_id_private *id_priv;
3011 int ret;
3012
3013 id_priv = container_of(id, struct rdma_id_private, id);
0c9361fc 3014 if (!id_priv->cm_id.ib)
0fe313b0
SH
3015 return -EINVAL;
3016
3017 switch (id->device->node_type) {
3018 case RDMA_NODE_IB_CA:
3019 ret = ib_cm_notify(id_priv->cm_id.ib, event);
3020 break;
3021 default:
3022 ret = 0;
3023 break;
3024 }
3025 return ret;
3026}
3027EXPORT_SYMBOL(rdma_notify);
3028
e51060f0
SH
3029int rdma_reject(struct rdma_cm_id *id, const void *private_data,
3030 u8 private_data_len)
3031{
3032 struct rdma_id_private *id_priv;
3033 int ret;
3034
3035 id_priv = container_of(id, struct rdma_id_private, id);
0c9361fc 3036 if (!id_priv->cm_id.ib)
e51060f0
SH
3037 return -EINVAL;
3038
07ebafba
TT
3039 switch (rdma_node_get_transport(id->device->node_type)) {
3040 case RDMA_TRANSPORT_IB:
b26f9b99 3041 if (id->qp_type == IB_QPT_UD)
5c438135 3042 ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0,
628e5f6d
SH
3043 private_data, private_data_len);
3044 else
3045 ret = ib_send_cm_rej(id_priv->cm_id.ib,
3046 IB_CM_REJ_CONSUMER_DEFINED, NULL,
3047 0, private_data, private_data_len);
e51060f0 3048 break;
07ebafba
TT
3049 case RDMA_TRANSPORT_IWARP:
3050 ret = iw_cm_reject(id_priv->cm_id.iw,
3051 private_data, private_data_len);
3052 break;
e51060f0
SH
3053 default:
3054 ret = -ENOSYS;
3055 break;
3056 }
3057 return ret;
3058}
3059EXPORT_SYMBOL(rdma_reject);
3060
3061int rdma_disconnect(struct rdma_cm_id *id)
3062{
3063 struct rdma_id_private *id_priv;
3064 int ret;
3065
3066 id_priv = container_of(id, struct rdma_id_private, id);
0c9361fc 3067 if (!id_priv->cm_id.ib)
e51060f0
SH
3068 return -EINVAL;
3069
07ebafba
TT
3070 switch (rdma_node_get_transport(id->device->node_type)) {
3071 case RDMA_TRANSPORT_IB:
c5483388 3072 ret = cma_modify_qp_err(id_priv);
07ebafba
TT
3073 if (ret)
3074 goto out;
e51060f0
SH
3075 /* Initiate or respond to a disconnect. */
3076 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
3077 ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
3078 break;
07ebafba
TT
3079 case RDMA_TRANSPORT_IWARP:
3080 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
3081 break;
e51060f0 3082 default:
07ebafba 3083 ret = -EINVAL;
e51060f0
SH
3084 break;
3085 }
3086out:
3087 return ret;
3088}
3089EXPORT_SYMBOL(rdma_disconnect);
3090
c8f6a362
SH
3091static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
3092{
3093 struct rdma_id_private *id_priv;
3094 struct cma_multicast *mc = multicast->context;
3095 struct rdma_cm_event event;
3096 int ret;
3097
3098 id_priv = mc->id_priv;
550e5ca7
NM
3099 if (cma_disable_callback(id_priv, RDMA_CM_ADDR_BOUND) &&
3100 cma_disable_callback(id_priv, RDMA_CM_ADDR_RESOLVED))
8aa08602 3101 return 0;
c8f6a362 3102
5c438135
SH
3103 if (!status)
3104 status = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey));
c5483388 3105 mutex_lock(&id_priv->qp_mutex);
c8f6a362
SH
3106 if (!status && id_priv->id.qp)
3107 status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
46ea5061 3108 be16_to_cpu(multicast->rec.mlid));
c5483388 3109 mutex_unlock(&id_priv->qp_mutex);
c8f6a362
SH
3110
3111 memset(&event, 0, sizeof event);
3112 event.status = status;
3113 event.param.ud.private_data = mc->context;
3114 if (!status) {
3115 event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
3116 ib_init_ah_from_mcmember(id_priv->id.device,
3117 id_priv->id.port_num, &multicast->rec,
3118 &event.param.ud.ah_attr);
3119 event.param.ud.qp_num = 0xFFFFFF;
3120 event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
3121 } else
3122 event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
3123
3124 ret = id_priv->id.event_handler(&id_priv->id, &event);
3125 if (ret) {
550e5ca7 3126 cma_exch(id_priv, RDMA_CM_DESTROYING);
de910bd9 3127 mutex_unlock(&id_priv->handler_mutex);
c8f6a362
SH
3128 rdma_destroy_id(&id_priv->id);
3129 return 0;
3130 }
8aa08602 3131
de910bd9 3132 mutex_unlock(&id_priv->handler_mutex);
c8f6a362
SH
3133 return 0;
3134}
3135
3136static void cma_set_mgid(struct rdma_id_private *id_priv,
3137 struct sockaddr *addr, union ib_gid *mgid)
3138{
3139 unsigned char mc_map[MAX_ADDR_LEN];
3140 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3141 struct sockaddr_in *sin = (struct sockaddr_in *) addr;
3142 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
3143
3144 if (cma_any_addr(addr)) {
3145 memset(mgid, 0, sizeof *mgid);
3146 } else if ((addr->sa_family == AF_INET6) &&
1c9b2819 3147 ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
c8f6a362
SH
3148 0xFF10A01B)) {
3149 /* IPv6 address is an SA assigned MGID. */
3150 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
e2e62697
JG
3151 } else if ((addr->sa_family == AF_INET6)) {
3152 ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
3153 if (id_priv->id.ps == RDMA_PS_UDP)
3154 mc_map[7] = 0x01; /* Use RDMA CM signature */
3155 *mgid = *(union ib_gid *) (mc_map + 4);
c8f6a362 3156 } else {
a9e527e3 3157 ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
c8f6a362
SH
3158 if (id_priv->id.ps == RDMA_PS_UDP)
3159 mc_map[7] = 0x01; /* Use RDMA CM signature */
c8f6a362
SH
3160 *mgid = *(union ib_gid *) (mc_map + 4);
3161 }
3162}
3163
3164static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
3165 struct cma_multicast *mc)
3166{
3167 struct ib_sa_mcmember_rec rec;
3168 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3169 ib_sa_comp_mask comp_mask;
3170 int ret;
3171
3172 ib_addr_get_mgid(dev_addr, &rec.mgid);
3173 ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
3174 &rec.mgid, &rec);
3175 if (ret)
3176 return ret;
3177
3f446754 3178 cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
c8f6a362
SH
3179 if (id_priv->id.ps == RDMA_PS_UDP)
3180 rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
6f8372b6 3181 rdma_addr_get_sgid(dev_addr, &rec.port_gid);
c8f6a362
SH
3182 rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
3183 rec.join_state = 1;
3184
3185 comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
3186 IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
3187 IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
3188 IB_SA_MCMEMBER_REC_FLOW_LABEL |
3189 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
3190
84adeee9
YE
3191 if (id_priv->id.ps == RDMA_PS_IPOIB)
3192 comp_mask |= IB_SA_MCMEMBER_REC_RATE |
2a22fb8c
DB
3193 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
3194 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
3195 IB_SA_MCMEMBER_REC_MTU |
3196 IB_SA_MCMEMBER_REC_HOP_LIMIT;
84adeee9 3197
c8f6a362
SH
3198 mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
3199 id_priv->id.port_num, &rec,
3200 comp_mask, GFP_KERNEL,
3201 cma_ib_mc_handler, mc);
4e289045 3202 return PTR_RET(mc->multicast.ib);
c8f6a362
SH
3203}
3204
3c86aa70
EC
3205static void iboe_mcast_work_handler(struct work_struct *work)
3206{
3207 struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work);
3208 struct cma_multicast *mc = mw->mc;
3209 struct ib_sa_multicast *m = mc->multicast.ib;
3210
3211 mc->multicast.ib->context = mc;
3212 cma_ib_mc_handler(0, m);
3213 kref_put(&mc->mcref, release_mc);
3214 kfree(mw);
3215}
3216
3217static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid)
3218{
3219 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
3220 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
3221
3222 if (cma_any_addr(addr)) {
3223 memset(mgid, 0, sizeof *mgid);
3224 } else if (addr->sa_family == AF_INET6) {
3225 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
3226 } else {
3227 mgid->raw[0] = 0xff;
3228 mgid->raw[1] = 0x0e;
3229 mgid->raw[2] = 0;
3230 mgid->raw[3] = 0;
3231 mgid->raw[4] = 0;
3232 mgid->raw[5] = 0;
3233 mgid->raw[6] = 0;
3234 mgid->raw[7] = 0;
3235 mgid->raw[8] = 0;
3236 mgid->raw[9] = 0;
3237 mgid->raw[10] = 0xff;
3238 mgid->raw[11] = 0xff;
3239 *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
3240 }
3241}
3242
3243static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
3244 struct cma_multicast *mc)
3245{
3246 struct iboe_mcast_work *work;
3247 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3248 int err;
3249 struct sockaddr *addr = (struct sockaddr *)&mc->addr;
3250 struct net_device *ndev = NULL;
3251
3252 if (cma_zero_addr((struct sockaddr *)&mc->addr))
3253 return -EINVAL;
3254
3255 work = kzalloc(sizeof *work, GFP_KERNEL);
3256 if (!work)
3257 return -ENOMEM;
3258
3259 mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL);
3260 if (!mc->multicast.ib) {
3261 err = -ENOMEM;
3262 goto out1;
3263 }
3264
3265 cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid);
3266
3267 mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff);
3268 if (id_priv->id.ps == RDMA_PS_UDP)
3269 mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
3270
3271 if (dev_addr->bound_dev_if)
3272 ndev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
3273 if (!ndev) {
3274 err = -ENODEV;
3275 goto out2;
3276 }
3277 mc->multicast.ib->rec.rate = iboe_get_rate(ndev);
3278 mc->multicast.ib->rec.hop_limit = 1;
3279 mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu);
3280 dev_put(ndev);
3281 if (!mc->multicast.ib->rec.mtu) {
3282 err = -EINVAL;
3283 goto out2;
3284 }
3285 iboe_addr_get_sgid(dev_addr, &mc->multicast.ib->rec.port_gid);
3286 work->id = id_priv;
3287 work->mc = mc;
3288 INIT_WORK(&work->work, iboe_mcast_work_handler);
3289 kref_get(&mc->mcref);
3290 queue_work(cma_wq, &work->work);
3291
3292 return 0;
3293
3294out2:
3295 kfree(mc->multicast.ib);
3296out1:
3297 kfree(work);
3298 return err;
3299}
3300
c8f6a362
SH
3301int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
3302 void *context)
3303{
3304 struct rdma_id_private *id_priv;
3305 struct cma_multicast *mc;
3306 int ret;
3307
3308 id_priv = container_of(id, struct rdma_id_private, id);
550e5ca7
NM
3309 if (!cma_comp(id_priv, RDMA_CM_ADDR_BOUND) &&
3310 !cma_comp(id_priv, RDMA_CM_ADDR_RESOLVED))
c8f6a362
SH
3311 return -EINVAL;
3312
3313 mc = kmalloc(sizeof *mc, GFP_KERNEL);
3314 if (!mc)
3315 return -ENOMEM;
3316
ef560861 3317 memcpy(&mc->addr, addr, rdma_addr_size(addr));
c8f6a362
SH
3318 mc->context = context;
3319 mc->id_priv = id_priv;
3320
3321 spin_lock(&id_priv->lock);
3322 list_add(&mc->list, &id_priv->mc_list);
3323 spin_unlock(&id_priv->lock);
3324
3325 switch (rdma_node_get_transport(id->device->node_type)) {
3326 case RDMA_TRANSPORT_IB:
3c86aa70
EC
3327 switch (rdma_port_get_link_layer(id->device, id->port_num)) {
3328 case IB_LINK_LAYER_INFINIBAND:
3329 ret = cma_join_ib_multicast(id_priv, mc);
3330 break;
3331 case IB_LINK_LAYER_ETHERNET:
3332 kref_init(&mc->mcref);
3333 ret = cma_iboe_join_multicast(id_priv, mc);
3334 break;
3335 default:
3336 ret = -EINVAL;
3337 }
c8f6a362
SH
3338 break;
3339 default:
3340 ret = -ENOSYS;
3341 break;
3342 }
3343
3344 if (ret) {
3345 spin_lock_irq(&id_priv->lock);
3346 list_del(&mc->list);
3347 spin_unlock_irq(&id_priv->lock);
3348 kfree(mc);
3349 }
3350 return ret;
3351}
3352EXPORT_SYMBOL(rdma_join_multicast);
3353
3354void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
3355{
3356 struct rdma_id_private *id_priv;
3357 struct cma_multicast *mc;
3358
3359 id_priv = container_of(id, struct rdma_id_private, id);
3360 spin_lock_irq(&id_priv->lock);
3361 list_for_each_entry(mc, &id_priv->mc_list, list) {
ef560861 3362 if (!memcmp(&mc->addr, addr, rdma_addr_size(addr))) {
c8f6a362
SH
3363 list_del(&mc->list);
3364 spin_unlock_irq(&id_priv->lock);
3365
3366 if (id->qp)
3367 ib_detach_mcast(id->qp,
3368 &mc->multicast.ib->rec.mgid,
46ea5061 3369 be16_to_cpu(mc->multicast.ib->rec.mlid));
3c86aa70
EC
3370 if (rdma_node_get_transport(id_priv->cma_dev->device->node_type) == RDMA_TRANSPORT_IB) {
3371 switch (rdma_port_get_link_layer(id->device, id->port_num)) {
3372 case IB_LINK_LAYER_INFINIBAND:
3373 ib_sa_free_multicast(mc->multicast.ib);
3374 kfree(mc);
3375 break;
3376 case IB_LINK_LAYER_ETHERNET:
3377 kref_put(&mc->mcref, release_mc);
3378 break;
3379 default:
3380 break;
3381 }
3382 }
c8f6a362
SH
3383 return;
3384 }
3385 }
3386 spin_unlock_irq(&id_priv->lock);
3387}
3388EXPORT_SYMBOL(rdma_leave_multicast);
3389
dd5bdff8
OG
3390static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
3391{
3392 struct rdma_dev_addr *dev_addr;
3393 struct cma_ndev_work *work;
3394
3395 dev_addr = &id_priv->id.route.addr.dev_addr;
3396
6266ed6e 3397 if ((dev_addr->bound_dev_if == ndev->ifindex) &&
dd5bdff8
OG
3398 memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
3399 printk(KERN_INFO "RDMA CM addr change for ndev %s used by id %p\n",
3400 ndev->name, &id_priv->id);
3401 work = kzalloc(sizeof *work, GFP_KERNEL);
3402 if (!work)
3403 return -ENOMEM;
3404
3405 INIT_WORK(&work->work, cma_ndev_work_handler);
3406 work->id = id_priv;
3407 work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
3408 atomic_inc(&id_priv->refcount);
3409 queue_work(cma_wq, &work->work);
3410 }
3411
3412 return 0;
3413}
3414
3415static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
351638e7 3416 void *ptr)
dd5bdff8 3417{
351638e7 3418 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
dd5bdff8
OG
3419 struct cma_device *cma_dev;
3420 struct rdma_id_private *id_priv;
3421 int ret = NOTIFY_DONE;
3422
3423 if (dev_net(ndev) != &init_net)
3424 return NOTIFY_DONE;
3425
3426 if (event != NETDEV_BONDING_FAILOVER)
3427 return NOTIFY_DONE;
3428
3429 if (!(ndev->flags & IFF_MASTER) || !(ndev->priv_flags & IFF_BONDING))
3430 return NOTIFY_DONE;
3431
3432 mutex_lock(&lock);
3433 list_for_each_entry(cma_dev, &dev_list, list)
3434 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
3435 ret = cma_netdev_change(ndev, id_priv);
3436 if (ret)
3437 goto out;
3438 }
3439
3440out:
3441 mutex_unlock(&lock);
3442 return ret;
3443}
3444
3445static struct notifier_block cma_nb = {
3446 .notifier_call = cma_netdev_callback
3447};
3448
e51060f0
SH
3449static void cma_add_one(struct ib_device *device)
3450{
3451 struct cma_device *cma_dev;
3452 struct rdma_id_private *id_priv;
3453
3454 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
3455 if (!cma_dev)
3456 return;
3457
3458 cma_dev->device = device;
e51060f0
SH
3459
3460 init_completion(&cma_dev->comp);
3461 atomic_set(&cma_dev->refcount, 1);
3462 INIT_LIST_HEAD(&cma_dev->id_list);
3463 ib_set_client_data(device, &cma_client, cma_dev);
3464
3465 mutex_lock(&lock);
3466 list_add_tail(&cma_dev->list, &dev_list);
3467 list_for_each_entry(id_priv, &listen_any_list, list)
3468 cma_listen_on_dev(id_priv, cma_dev);
3469 mutex_unlock(&lock);
e51060f0
SH
3470}
3471
3472static int cma_remove_id_dev(struct rdma_id_private *id_priv)
3473{
a1b1b61f 3474 struct rdma_cm_event event;
550e5ca7 3475 enum rdma_cm_state state;
de910bd9 3476 int ret = 0;
e51060f0
SH
3477
3478 /* Record that we want to remove the device */
550e5ca7
NM
3479 state = cma_exch(id_priv, RDMA_CM_DEVICE_REMOVAL);
3480 if (state == RDMA_CM_DESTROYING)
e51060f0
SH
3481 return 0;
3482
3483 cma_cancel_operation(id_priv, state);
de910bd9 3484 mutex_lock(&id_priv->handler_mutex);
e51060f0
SH
3485
3486 /* Check for destruction from another callback. */
550e5ca7 3487 if (!cma_comp(id_priv, RDMA_CM_DEVICE_REMOVAL))
de910bd9 3488 goto out;
e51060f0 3489
a1b1b61f
SH
3490 memset(&event, 0, sizeof event);
3491 event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
de910bd9
OG
3492 ret = id_priv->id.event_handler(&id_priv->id, &event);
3493out:
3494 mutex_unlock(&id_priv->handler_mutex);
3495 return ret;
e51060f0
SH
3496}
3497
3498static void cma_process_remove(struct cma_device *cma_dev)
3499{
e51060f0
SH
3500 struct rdma_id_private *id_priv;
3501 int ret;
3502
e51060f0
SH
3503 mutex_lock(&lock);
3504 while (!list_empty(&cma_dev->id_list)) {
3505 id_priv = list_entry(cma_dev->id_list.next,
3506 struct rdma_id_private, list);
3507
d02d1f53 3508 list_del(&id_priv->listen_list);
94de178a 3509 list_del_init(&id_priv->list);
e51060f0
SH
3510 atomic_inc(&id_priv->refcount);
3511 mutex_unlock(&lock);
3512
d02d1f53 3513 ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv);
e51060f0
SH
3514 cma_deref_id(id_priv);
3515 if (ret)
3516 rdma_destroy_id(&id_priv->id);
3517
3518 mutex_lock(&lock);
3519 }
3520 mutex_unlock(&lock);
3521
3522 cma_deref_dev(cma_dev);
3523 wait_for_completion(&cma_dev->comp);
3524}
3525
3526static void cma_remove_one(struct ib_device *device)
3527{
3528 struct cma_device *cma_dev;
3529
3530 cma_dev = ib_get_client_data(device, &cma_client);
3531 if (!cma_dev)
3532 return;
3533
3534 mutex_lock(&lock);
3535 list_del(&cma_dev->list);
3536 mutex_unlock(&lock);
3537
3538 cma_process_remove(cma_dev);
3539 kfree(cma_dev);
3540}
3541
753f618a
NM
3542static int cma_get_id_stats(struct sk_buff *skb, struct netlink_callback *cb)
3543{
3544 struct nlmsghdr *nlh;
3545 struct rdma_cm_id_stats *id_stats;
3546 struct rdma_id_private *id_priv;
3547 struct rdma_cm_id *id = NULL;
3548 struct cma_device *cma_dev;
3549 int i_dev = 0, i_id = 0;
3550
3551 /*
3552 * We export all of the IDs as a sequence of messages. Each
3553 * ID gets its own netlink message.
3554 */
3555 mutex_lock(&lock);
3556
3557 list_for_each_entry(cma_dev, &dev_list, list) {
3558 if (i_dev < cb->args[0]) {
3559 i_dev++;
3560 continue;
3561 }
3562
3563 i_id = 0;
3564 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
3565 if (i_id < cb->args[1]) {
3566 i_id++;
3567 continue;
3568 }
3569
3570 id_stats = ibnl_put_msg(skb, &nlh, cb->nlh->nlmsg_seq,
3571 sizeof *id_stats, RDMA_NL_RDMA_CM,
3572 RDMA_NL_RDMA_CM_ID_STATS);
3573 if (!id_stats)
3574 goto out;
3575
3576 memset(id_stats, 0, sizeof *id_stats);
3577 id = &id_priv->id;
3578 id_stats->node_type = id->route.addr.dev_addr.dev_type;
3579 id_stats->port_num = id->port_num;
3580 id_stats->bound_dev_if =
3581 id->route.addr.dev_addr.bound_dev_if;
3582
f4753834 3583 if (cma_family(id_priv) == AF_INET) {
753f618a
NM
3584 if (ibnl_put_attr(skb, nlh,
3585 sizeof(struct sockaddr_in),
f4753834 3586 cma_src_addr(id_priv),
753f618a
NM
3587 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR)) {
3588 goto out;
3589 }
3590 if (ibnl_put_attr(skb, nlh,
3591 sizeof(struct sockaddr_in),
f4753834 3592 cma_dst_addr(id_priv),
753f618a
NM
3593 RDMA_NL_RDMA_CM_ATTR_DST_ADDR)) {
3594 goto out;
3595 }
f4753834 3596 } else if (cma_family(id_priv) == AF_INET6) {
753f618a
NM
3597 if (ibnl_put_attr(skb, nlh,
3598 sizeof(struct sockaddr_in6),
f4753834 3599 cma_src_addr(id_priv),
753f618a
NM
3600 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR)) {
3601 goto out;
3602 }
3603 if (ibnl_put_attr(skb, nlh,
3604 sizeof(struct sockaddr_in6),
f4753834 3605 cma_dst_addr(id_priv),
753f618a
NM
3606 RDMA_NL_RDMA_CM_ATTR_DST_ADDR)) {
3607 goto out;
3608 }
3609 }
3610
83e9502d 3611 id_stats->pid = id_priv->owner;
753f618a
NM
3612 id_stats->port_space = id->ps;
3613 id_stats->cm_state = id_priv->state;
3614 id_stats->qp_num = id_priv->qp_num;
3615 id_stats->qp_type = id->qp_type;
3616
3617 i_id++;
3618 }
3619
3620 cb->args[1] = 0;
3621 i_dev++;
3622 }
3623
3624out:
3625 mutex_unlock(&lock);
3626 cb->args[0] = i_dev;
3627 cb->args[1] = i_id;
3628
3629 return skb->len;
3630}
3631
3632static const struct ibnl_client_cbs cma_cb_table[] = {
809d5fc9
G
3633 [RDMA_NL_RDMA_CM_ID_STATS] = { .dump = cma_get_id_stats,
3634 .module = THIS_MODULE },
753f618a
NM
3635};
3636
716abb1f 3637static int __init cma_init(void)
e51060f0 3638{
5d7220e8 3639 int ret;
227b60f5 3640
c7f743a6 3641 cma_wq = create_singlethread_workqueue("rdma_cm");
e51060f0
SH
3642 if (!cma_wq)
3643 return -ENOMEM;
3644
c1a0b23b 3645 ib_sa_register_client(&sa_client);
7a118df3 3646 rdma_addr_register_client(&addr_client);
dd5bdff8 3647 register_netdevice_notifier(&cma_nb);
c1a0b23b 3648
e51060f0
SH
3649 ret = ib_register_client(&cma_client);
3650 if (ret)
3651 goto err;
753f618a
NM
3652
3653 if (ibnl_add_client(RDMA_NL_RDMA_CM, RDMA_NL_RDMA_CM_NUM_OPS, cma_cb_table))
3654 printk(KERN_WARNING "RDMA CMA: failed to add netlink callback\n");
3655
e51060f0
SH
3656 return 0;
3657
3658err:
dd5bdff8 3659 unregister_netdevice_notifier(&cma_nb);
7a118df3 3660 rdma_addr_unregister_client(&addr_client);
c1a0b23b 3661 ib_sa_unregister_client(&sa_client);
e51060f0
SH
3662 destroy_workqueue(cma_wq);
3663 return ret;
3664}
3665
716abb1f 3666static void __exit cma_cleanup(void)
e51060f0 3667{
753f618a 3668 ibnl_remove_client(RDMA_NL_RDMA_CM);
e51060f0 3669 ib_unregister_client(&cma_client);
dd5bdff8 3670 unregister_netdevice_notifier(&cma_nb);
7a118df3 3671 rdma_addr_unregister_client(&addr_client);
c1a0b23b 3672 ib_sa_unregister_client(&sa_client);
e51060f0 3673 destroy_workqueue(cma_wq);
e51060f0 3674 idr_destroy(&tcp_ps);
628e5f6d 3675 idr_destroy(&udp_ps);
c8f6a362 3676 idr_destroy(&ipoib_ps);
2d2e9415 3677 idr_destroy(&ib_ps);
e51060f0
SH
3678}
3679
3680module_init(cma_init);
3681module_exit(cma_cleanup);