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