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