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