blob: 533193d4e5dffa177f7747f69d33456c2cb7e764 [file] [log] [blame]
Sean Heftye51060f2006-06-17 20:37:29 -07001/*
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 *
7 * This Software is licensed under one of the following licenses:
8 *
9 * 1) under the terms of the "Common Public License 1.0" a copy of which is
10 * available from the Open Source Initiative, see
11 * http://www.opensource.org/licenses/cpl.php.
12 *
13 * 2) under the terms of the "The BSD License" a copy of which is
14 * available from the Open Source Initiative, see
15 * http://www.opensource.org/licenses/bsd-license.php.
16 *
17 * 3) under the terms of the "GNU General Public License (GPL) Version 2" a
18 * copy of which is available from the Open Source Initiative, see
19 * http://www.opensource.org/licenses/gpl-license.php.
20 *
21 * Licensee has the right to choose one of the above licenses.
22 *
23 * Redistributions of source code must retain the above copyright
24 * notice and one of the license notices.
25 *
26 * Redistributions in binary form must reproduce both the above copyright
27 * notice, one of the license notices in the documentation
28 * and/or other materials provided with the distribution.
29 *
30 */
31
32#include <linux/completion.h>
33#include <linux/in.h>
34#include <linux/in6.h>
35#include <linux/mutex.h>
36#include <linux/random.h>
37#include <linux/idr.h>
Tom Tucker07ebafb2006-08-03 16:02:42 -050038#include <linux/inetdevice.h>
Sean Heftye51060f2006-06-17 20:37:29 -070039
40#include <net/tcp.h>
41
42#include <rdma/rdma_cm.h>
43#include <rdma/rdma_cm_ib.h>
44#include <rdma/ib_cache.h>
45#include <rdma/ib_cm.h>
46#include <rdma/ib_sa.h>
Tom Tucker07ebafb2006-08-03 16:02:42 -050047#include <rdma/iw_cm.h>
Sean Heftye51060f2006-06-17 20:37:29 -070048
49MODULE_AUTHOR("Sean Hefty");
50MODULE_DESCRIPTION("Generic RDMA CM Agent");
51MODULE_LICENSE("Dual BSD/GPL");
52
53#define CMA_CM_RESPONSE_TIMEOUT 20
Michael S. Tsirkind5bb7592006-09-13 15:01:54 +030054#define CMA_MAX_CM_RETRIES 15
Sean Heftye51060f2006-06-17 20:37:29 -070055
56static void cma_add_one(struct ib_device *device);
57static void cma_remove_one(struct ib_device *device);
58
59static struct ib_client cma_client = {
60 .name = "cma",
61 .add = cma_add_one,
62 .remove = cma_remove_one
63};
64
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -070065static struct ib_sa_client sa_client;
Sean Hefty7a118df2006-10-31 11:12:59 -080066static struct rdma_addr_client addr_client;
Sean Heftye51060f2006-06-17 20:37:29 -070067static LIST_HEAD(dev_list);
68static LIST_HEAD(listen_any_list);
69static DEFINE_MUTEX(lock);
70static struct workqueue_struct *cma_wq;
71static DEFINE_IDR(sdp_ps);
72static DEFINE_IDR(tcp_ps);
Sean Hefty628e5f62006-11-30 16:44:16 -080073static DEFINE_IDR(udp_ps);
Sean Heftye51060f2006-06-17 20:37:29 -070074
75struct cma_device {
76 struct list_head list;
77 struct ib_device *device;
78 __be64 node_guid;
79 struct completion comp;
80 atomic_t refcount;
81 struct list_head id_list;
82};
83
84enum cma_state {
85 CMA_IDLE,
86 CMA_ADDR_QUERY,
87 CMA_ADDR_RESOLVED,
88 CMA_ROUTE_QUERY,
89 CMA_ROUTE_RESOLVED,
90 CMA_CONNECT,
91 CMA_DISCONNECT,
92 CMA_ADDR_BOUND,
93 CMA_LISTEN,
94 CMA_DEVICE_REMOVAL,
95 CMA_DESTROYING
96};
97
98struct rdma_bind_list {
99 struct idr *ps;
100 struct hlist_head owners;
101 unsigned short port;
102};
103
104/*
105 * Device removal can occur at anytime, so we need extra handling to
106 * serialize notifying the user of device removal with other callbacks.
107 * We do this by disabling removal notification while a callback is in process,
108 * and reporting it after the callback completes.
109 */
110struct rdma_id_private {
111 struct rdma_cm_id id;
112
113 struct rdma_bind_list *bind_list;
114 struct hlist_node node;
115 struct list_head list;
116 struct list_head listen_list;
117 struct cma_device *cma_dev;
118
119 enum cma_state state;
120 spinlock_t lock;
121 struct completion comp;
122 atomic_t refcount;
123 wait_queue_head_t wait_remove;
124 atomic_t dev_remove;
125
126 int backlog;
127 int timeout_ms;
128 struct ib_sa_query *query;
129 int query_id;
130 union {
131 struct ib_cm_id *ib;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500132 struct iw_cm_id *iw;
Sean Heftye51060f2006-06-17 20:37:29 -0700133 } cm_id;
134
135 u32 seq_num;
136 u32 qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -0700137 u8 srq;
138};
139
140struct cma_work {
141 struct work_struct work;
142 struct rdma_id_private *id;
143 enum cma_state old_state;
144 enum cma_state new_state;
145 struct rdma_cm_event event;
146};
147
148union cma_ip_addr {
149 struct in6_addr ip6;
150 struct {
151 __u32 pad[3];
152 __u32 addr;
153 } ip4;
154};
155
156struct cma_hdr {
157 u8 cma_version;
158 u8 ip_version; /* IP version: 7:4 */
159 __u16 port;
160 union cma_ip_addr src_addr;
161 union cma_ip_addr dst_addr;
162};
163
164struct sdp_hh {
165 u8 bsdh[16];
166 u8 sdp_version; /* Major version: 7:4 */
167 u8 ip_version; /* IP version: 7:4 */
168 u8 sdp_specific1[10];
169 __u16 port;
170 __u16 sdp_specific2;
171 union cma_ip_addr src_addr;
172 union cma_ip_addr dst_addr;
173};
174
175struct sdp_hah {
176 u8 bsdh[16];
177 u8 sdp_version;
178};
179
180#define CMA_VERSION 0x00
181#define SDP_MAJ_VERSION 0x2
182
183static int cma_comp(struct rdma_id_private *id_priv, enum cma_state comp)
184{
185 unsigned long flags;
186 int ret;
187
188 spin_lock_irqsave(&id_priv->lock, flags);
189 ret = (id_priv->state == comp);
190 spin_unlock_irqrestore(&id_priv->lock, flags);
191 return ret;
192}
193
194static int cma_comp_exch(struct rdma_id_private *id_priv,
195 enum cma_state comp, enum cma_state exch)
196{
197 unsigned long flags;
198 int ret;
199
200 spin_lock_irqsave(&id_priv->lock, flags);
201 if ((ret = (id_priv->state == comp)))
202 id_priv->state = exch;
203 spin_unlock_irqrestore(&id_priv->lock, flags);
204 return ret;
205}
206
207static enum cma_state cma_exch(struct rdma_id_private *id_priv,
208 enum cma_state exch)
209{
210 unsigned long flags;
211 enum cma_state old;
212
213 spin_lock_irqsave(&id_priv->lock, flags);
214 old = id_priv->state;
215 id_priv->state = exch;
216 spin_unlock_irqrestore(&id_priv->lock, flags);
217 return old;
218}
219
220static inline u8 cma_get_ip_ver(struct cma_hdr *hdr)
221{
222 return hdr->ip_version >> 4;
223}
224
225static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
226{
227 hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
228}
229
230static inline u8 sdp_get_majv(u8 sdp_version)
231{
232 return sdp_version >> 4;
233}
234
235static inline u8 sdp_get_ip_ver(struct sdp_hh *hh)
236{
237 return hh->ip_version >> 4;
238}
239
240static inline void sdp_set_ip_ver(struct sdp_hh *hh, u8 ip_ver)
241{
242 hh->ip_version = (ip_ver << 4) | (hh->ip_version & 0xF);
243}
244
245static void cma_attach_to_dev(struct rdma_id_private *id_priv,
246 struct cma_device *cma_dev)
247{
248 atomic_inc(&cma_dev->refcount);
249 id_priv->cma_dev = cma_dev;
250 id_priv->id.device = cma_dev->device;
251 list_add_tail(&id_priv->list, &cma_dev->id_list);
252}
253
254static inline void cma_deref_dev(struct cma_device *cma_dev)
255{
256 if (atomic_dec_and_test(&cma_dev->refcount))
257 complete(&cma_dev->comp);
258}
259
260static void cma_detach_from_dev(struct rdma_id_private *id_priv)
261{
262 list_del(&id_priv->list);
263 cma_deref_dev(id_priv->cma_dev);
264 id_priv->cma_dev = NULL;
265}
266
Tom Tucker07ebafb2006-08-03 16:02:42 -0500267static int cma_acquire_dev(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700268{
Tom Tucker07ebafb2006-08-03 16:02:42 -0500269 enum rdma_node_type dev_type = id_priv->id.route.addr.dev_addr.dev_type;
Sean Heftye51060f2006-06-17 20:37:29 -0700270 struct cma_device *cma_dev;
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -0700271 union ib_gid gid;
Sean Heftye51060f2006-06-17 20:37:29 -0700272 int ret = -ENODEV;
273
Tom Tucker07ebafb2006-08-03 16:02:42 -0500274 switch (rdma_node_get_transport(dev_type)) {
275 case RDMA_TRANSPORT_IB:
276 ib_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
277 break;
278 case RDMA_TRANSPORT_IWARP:
279 iw_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
280 break;
281 default:
282 return -ENODEV;
283 }
Sean Hefty61a73c72006-09-01 15:33:55 -0700284
Sean Heftye51060f2006-06-17 20:37:29 -0700285 list_for_each_entry(cma_dev, &dev_list, list) {
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -0700286 ret = ib_find_cached_gid(cma_dev->device, &gid,
Sean Heftye51060f2006-06-17 20:37:29 -0700287 &id_priv->id.port_num, NULL);
288 if (!ret) {
289 cma_attach_to_dev(id_priv, cma_dev);
290 break;
291 }
292 }
Sean Heftye51060f2006-06-17 20:37:29 -0700293 return ret;
294}
295
Sean Heftye51060f2006-06-17 20:37:29 -0700296static void cma_deref_id(struct rdma_id_private *id_priv)
297{
298 if (atomic_dec_and_test(&id_priv->refcount))
299 complete(&id_priv->comp);
300}
301
302static void cma_release_remove(struct rdma_id_private *id_priv)
303{
304 if (atomic_dec_and_test(&id_priv->dev_remove))
305 wake_up(&id_priv->wait_remove);
306}
307
308struct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler,
309 void *context, enum rdma_port_space ps)
310{
311 struct rdma_id_private *id_priv;
312
313 id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
314 if (!id_priv)
315 return ERR_PTR(-ENOMEM);
316
317 id_priv->state = CMA_IDLE;
318 id_priv->id.context = context;
319 id_priv->id.event_handler = event_handler;
320 id_priv->id.ps = ps;
321 spin_lock_init(&id_priv->lock);
322 init_completion(&id_priv->comp);
323 atomic_set(&id_priv->refcount, 1);
324 init_waitqueue_head(&id_priv->wait_remove);
325 atomic_set(&id_priv->dev_remove, 0);
326 INIT_LIST_HEAD(&id_priv->listen_list);
327 get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
328
329 return &id_priv->id;
330}
331EXPORT_SYMBOL(rdma_create_id);
332
333static int cma_init_ib_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
334{
335 struct ib_qp_attr qp_attr;
336 struct rdma_dev_addr *dev_addr;
337 int ret;
338
339 dev_addr = &id_priv->id.route.addr.dev_addr;
340 ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
341 ib_addr_get_pkey(dev_addr),
342 &qp_attr.pkey_index);
343 if (ret)
344 return ret;
345
346 qp_attr.qp_state = IB_QPS_INIT;
Dotan Barake31353ea2006-10-24 13:35:27 -0700347 qp_attr.qp_access_flags = 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700348 qp_attr.port_num = id_priv->id.port_num;
349 return ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_ACCESS_FLAGS |
350 IB_QP_PKEY_INDEX | IB_QP_PORT);
351}
352
Tom Tucker07ebafb2006-08-03 16:02:42 -0500353static int cma_init_iw_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
354{
355 struct ib_qp_attr qp_attr;
356
357 qp_attr.qp_state = IB_QPS_INIT;
358 qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE;
359
360 return ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_ACCESS_FLAGS);
361}
362
Sean Heftye51060f2006-06-17 20:37:29 -0700363int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
364 struct ib_qp_init_attr *qp_init_attr)
365{
366 struct rdma_id_private *id_priv;
367 struct ib_qp *qp;
368 int ret;
369
370 id_priv = container_of(id, struct rdma_id_private, id);
371 if (id->device != pd->device)
372 return -EINVAL;
373
374 qp = ib_create_qp(pd, qp_init_attr);
375 if (IS_ERR(qp))
376 return PTR_ERR(qp);
377
Tom Tucker07ebafb2006-08-03 16:02:42 -0500378 switch (rdma_node_get_transport(id->device->node_type)) {
379 case RDMA_TRANSPORT_IB:
Sean Heftye51060f2006-06-17 20:37:29 -0700380 ret = cma_init_ib_qp(id_priv, qp);
381 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500382 case RDMA_TRANSPORT_IWARP:
383 ret = cma_init_iw_qp(id_priv, qp);
384 break;
Sean Heftye51060f2006-06-17 20:37:29 -0700385 default:
386 ret = -ENOSYS;
387 break;
388 }
389
390 if (ret)
391 goto err;
392
393 id->qp = qp;
394 id_priv->qp_num = qp->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -0700395 id_priv->srq = (qp->srq != NULL);
396 return 0;
397err:
398 ib_destroy_qp(qp);
399 return ret;
400}
401EXPORT_SYMBOL(rdma_create_qp);
402
403void rdma_destroy_qp(struct rdma_cm_id *id)
404{
405 ib_destroy_qp(id->qp);
406}
407EXPORT_SYMBOL(rdma_destroy_qp);
408
409static int cma_modify_qp_rtr(struct rdma_cm_id *id)
410{
411 struct ib_qp_attr qp_attr;
412 int qp_attr_mask, ret;
413
414 if (!id->qp)
415 return 0;
416
417 /* Need to update QP attributes from default values. */
418 qp_attr.qp_state = IB_QPS_INIT;
419 ret = rdma_init_qp_attr(id, &qp_attr, &qp_attr_mask);
420 if (ret)
421 return ret;
422
423 ret = ib_modify_qp(id->qp, &qp_attr, qp_attr_mask);
424 if (ret)
425 return ret;
426
427 qp_attr.qp_state = IB_QPS_RTR;
428 ret = rdma_init_qp_attr(id, &qp_attr, &qp_attr_mask);
429 if (ret)
430 return ret;
431
432 return ib_modify_qp(id->qp, &qp_attr, qp_attr_mask);
433}
434
435static int cma_modify_qp_rts(struct rdma_cm_id *id)
436{
437 struct ib_qp_attr qp_attr;
438 int qp_attr_mask, ret;
439
440 if (!id->qp)
441 return 0;
442
443 qp_attr.qp_state = IB_QPS_RTS;
444 ret = rdma_init_qp_attr(id, &qp_attr, &qp_attr_mask);
445 if (ret)
446 return ret;
447
448 return ib_modify_qp(id->qp, &qp_attr, qp_attr_mask);
449}
450
451static int cma_modify_qp_err(struct rdma_cm_id *id)
452{
453 struct ib_qp_attr qp_attr;
454
455 if (!id->qp)
456 return 0;
457
458 qp_attr.qp_state = IB_QPS_ERR;
459 return ib_modify_qp(id->qp, &qp_attr, IB_QP_STATE);
460}
461
462int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
463 int *qp_attr_mask)
464{
465 struct rdma_id_private *id_priv;
466 int ret;
467
468 id_priv = container_of(id, struct rdma_id_private, id);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500469 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
470 case RDMA_TRANSPORT_IB:
Sean Heftye51060f2006-06-17 20:37:29 -0700471 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
472 qp_attr_mask);
473 if (qp_attr->qp_state == IB_QPS_RTR)
474 qp_attr->rq_psn = id_priv->seq_num;
475 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500476 case RDMA_TRANSPORT_IWARP:
477 ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
478 qp_attr_mask);
479 break;
Sean Heftye51060f2006-06-17 20:37:29 -0700480 default:
481 ret = -ENOSYS;
482 break;
483 }
484
485 return ret;
486}
487EXPORT_SYMBOL(rdma_init_qp_attr);
488
489static inline int cma_zero_addr(struct sockaddr *addr)
490{
491 struct in6_addr *ip6;
492
493 if (addr->sa_family == AF_INET)
494 return ZERONET(((struct sockaddr_in *) addr)->sin_addr.s_addr);
495 else {
496 ip6 = &((struct sockaddr_in6 *) addr)->sin6_addr;
497 return (ip6->s6_addr32[0] | ip6->s6_addr32[1] |
Eric Sesterhenn5fd571c2006-06-21 20:56:26 +0200498 ip6->s6_addr32[2] | ip6->s6_addr32[3]) == 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700499 }
500}
501
502static inline int cma_loopback_addr(struct sockaddr *addr)
503{
504 return LOOPBACK(((struct sockaddr_in *) addr)->sin_addr.s_addr);
505}
506
507static inline int cma_any_addr(struct sockaddr *addr)
508{
509 return cma_zero_addr(addr) || cma_loopback_addr(addr);
510}
511
Sean Hefty628e5f62006-11-30 16:44:16 -0800512static inline __be16 cma_port(struct sockaddr *addr)
513{
514 if (addr->sa_family == AF_INET)
515 return ((struct sockaddr_in *) addr)->sin_port;
516 else
517 return ((struct sockaddr_in6 *) addr)->sin6_port;
518}
519
Sean Heftye51060f2006-06-17 20:37:29 -0700520static inline int cma_any_port(struct sockaddr *addr)
521{
Sean Hefty628e5f62006-11-30 16:44:16 -0800522 return !cma_port(addr);
Sean Heftye51060f2006-06-17 20:37:29 -0700523}
524
525static int cma_get_net_info(void *hdr, enum rdma_port_space ps,
526 u8 *ip_ver, __u16 *port,
527 union cma_ip_addr **src, union cma_ip_addr **dst)
528{
529 switch (ps) {
530 case RDMA_PS_SDP:
531 if (sdp_get_majv(((struct sdp_hh *) hdr)->sdp_version) !=
532 SDP_MAJ_VERSION)
533 return -EINVAL;
534
535 *ip_ver = sdp_get_ip_ver(hdr);
536 *port = ((struct sdp_hh *) hdr)->port;
537 *src = &((struct sdp_hh *) hdr)->src_addr;
538 *dst = &((struct sdp_hh *) hdr)->dst_addr;
539 break;
540 default:
541 if (((struct cma_hdr *) hdr)->cma_version != CMA_VERSION)
542 return -EINVAL;
543
544 *ip_ver = cma_get_ip_ver(hdr);
545 *port = ((struct cma_hdr *) hdr)->port;
546 *src = &((struct cma_hdr *) hdr)->src_addr;
547 *dst = &((struct cma_hdr *) hdr)->dst_addr;
548 break;
549 }
550
551 if (*ip_ver != 4 && *ip_ver != 6)
552 return -EINVAL;
553 return 0;
554}
555
556static void cma_save_net_info(struct rdma_addr *addr,
557 struct rdma_addr *listen_addr,
558 u8 ip_ver, __u16 port,
559 union cma_ip_addr *src, union cma_ip_addr *dst)
560{
561 struct sockaddr_in *listen4, *ip4;
562 struct sockaddr_in6 *listen6, *ip6;
563
564 switch (ip_ver) {
565 case 4:
566 listen4 = (struct sockaddr_in *) &listen_addr->src_addr;
567 ip4 = (struct sockaddr_in *) &addr->src_addr;
568 ip4->sin_family = listen4->sin_family;
569 ip4->sin_addr.s_addr = dst->ip4.addr;
570 ip4->sin_port = listen4->sin_port;
571
572 ip4 = (struct sockaddr_in *) &addr->dst_addr;
573 ip4->sin_family = listen4->sin_family;
574 ip4->sin_addr.s_addr = src->ip4.addr;
575 ip4->sin_port = port;
576 break;
577 case 6:
578 listen6 = (struct sockaddr_in6 *) &listen_addr->src_addr;
579 ip6 = (struct sockaddr_in6 *) &addr->src_addr;
580 ip6->sin6_family = listen6->sin6_family;
581 ip6->sin6_addr = dst->ip6;
582 ip6->sin6_port = listen6->sin6_port;
583
584 ip6 = (struct sockaddr_in6 *) &addr->dst_addr;
585 ip6->sin6_family = listen6->sin6_family;
586 ip6->sin6_addr = src->ip6;
587 ip6->sin6_port = port;
588 break;
589 default:
590 break;
591 }
592}
593
594static inline int cma_user_data_offset(enum rdma_port_space ps)
595{
596 switch (ps) {
597 case RDMA_PS_SDP:
598 return 0;
599 default:
600 return sizeof(struct cma_hdr);
601 }
602}
603
Sean Heftye51060f2006-06-17 20:37:29 -0700604static void cma_cancel_route(struct rdma_id_private *id_priv)
605{
Tom Tucker07ebafb2006-08-03 16:02:42 -0500606 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
607 case RDMA_TRANSPORT_IB:
Sean Heftye51060f2006-06-17 20:37:29 -0700608 if (id_priv->query)
609 ib_sa_cancel_query(id_priv->query_id, id_priv->query);
610 break;
611 default:
612 break;
613 }
614}
615
616static inline int cma_internal_listen(struct rdma_id_private *id_priv)
617{
618 return (id_priv->state == CMA_LISTEN) && id_priv->cma_dev &&
619 cma_any_addr(&id_priv->id.route.addr.src_addr);
620}
621
622static void cma_destroy_listen(struct rdma_id_private *id_priv)
623{
624 cma_exch(id_priv, CMA_DESTROYING);
625
626 if (id_priv->cma_dev) {
Tom Tucker07ebafb2006-08-03 16:02:42 -0500627 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
628 case RDMA_TRANSPORT_IB:
Roland Dreier3cd96562006-09-22 15:22:46 -0700629 if (id_priv->cm_id.ib && !IS_ERR(id_priv->cm_id.ib))
Sean Heftye51060f2006-06-17 20:37:29 -0700630 ib_destroy_cm_id(id_priv->cm_id.ib);
631 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500632 case RDMA_TRANSPORT_IWARP:
633 if (id_priv->cm_id.iw && !IS_ERR(id_priv->cm_id.iw))
634 iw_destroy_cm_id(id_priv->cm_id.iw);
635 break;
Sean Heftye51060f2006-06-17 20:37:29 -0700636 default:
637 break;
638 }
639 cma_detach_from_dev(id_priv);
640 }
641 list_del(&id_priv->listen_list);
642
643 cma_deref_id(id_priv);
644 wait_for_completion(&id_priv->comp);
645
646 kfree(id_priv);
647}
648
649static void cma_cancel_listens(struct rdma_id_private *id_priv)
650{
651 struct rdma_id_private *dev_id_priv;
652
653 mutex_lock(&lock);
654 list_del(&id_priv->list);
655
656 while (!list_empty(&id_priv->listen_list)) {
657 dev_id_priv = list_entry(id_priv->listen_list.next,
658 struct rdma_id_private, listen_list);
659 cma_destroy_listen(dev_id_priv);
660 }
661 mutex_unlock(&lock);
662}
663
664static void cma_cancel_operation(struct rdma_id_private *id_priv,
665 enum cma_state state)
666{
667 switch (state) {
668 case CMA_ADDR_QUERY:
669 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
670 break;
671 case CMA_ROUTE_QUERY:
672 cma_cancel_route(id_priv);
673 break;
674 case CMA_LISTEN:
675 if (cma_any_addr(&id_priv->id.route.addr.src_addr) &&
676 !id_priv->cma_dev)
677 cma_cancel_listens(id_priv);
678 break;
679 default:
680 break;
681 }
682}
683
684static void cma_release_port(struct rdma_id_private *id_priv)
685{
686 struct rdma_bind_list *bind_list = id_priv->bind_list;
687
688 if (!bind_list)
689 return;
690
691 mutex_lock(&lock);
692 hlist_del(&id_priv->node);
693 if (hlist_empty(&bind_list->owners)) {
694 idr_remove(bind_list->ps, bind_list->port);
695 kfree(bind_list);
696 }
697 mutex_unlock(&lock);
698}
699
700void rdma_destroy_id(struct rdma_cm_id *id)
701{
702 struct rdma_id_private *id_priv;
703 enum cma_state state;
704
705 id_priv = container_of(id, struct rdma_id_private, id);
706 state = cma_exch(id_priv, CMA_DESTROYING);
707 cma_cancel_operation(id_priv, state);
708
Sean Hefty61a73c72006-09-01 15:33:55 -0700709 mutex_lock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700710 if (id_priv->cma_dev) {
Sean Hefty61a73c72006-09-01 15:33:55 -0700711 mutex_unlock(&lock);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500712 switch (rdma_node_get_transport(id->device->node_type)) {
713 case RDMA_TRANSPORT_IB:
Roland Dreier3cd96562006-09-22 15:22:46 -0700714 if (id_priv->cm_id.ib && !IS_ERR(id_priv->cm_id.ib))
Sean Heftye51060f2006-06-17 20:37:29 -0700715 ib_destroy_cm_id(id_priv->cm_id.ib);
716 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500717 case RDMA_TRANSPORT_IWARP:
718 if (id_priv->cm_id.iw && !IS_ERR(id_priv->cm_id.iw))
719 iw_destroy_cm_id(id_priv->cm_id.iw);
720 break;
Sean Heftye51060f2006-06-17 20:37:29 -0700721 default:
722 break;
723 }
Roland Dreier3cd96562006-09-22 15:22:46 -0700724 mutex_lock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700725 cma_detach_from_dev(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -0700726 }
Sean Hefty61a73c72006-09-01 15:33:55 -0700727 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700728
729 cma_release_port(id_priv);
730 cma_deref_id(id_priv);
731 wait_for_completion(&id_priv->comp);
732
733 kfree(id_priv->id.route.path_rec);
734 kfree(id_priv);
735}
736EXPORT_SYMBOL(rdma_destroy_id);
737
738static int cma_rep_recv(struct rdma_id_private *id_priv)
739{
740 int ret;
741
742 ret = cma_modify_qp_rtr(&id_priv->id);
743 if (ret)
744 goto reject;
745
746 ret = cma_modify_qp_rts(&id_priv->id);
747 if (ret)
748 goto reject;
749
750 ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
751 if (ret)
752 goto reject;
753
754 return 0;
755reject:
756 cma_modify_qp_err(&id_priv->id);
757 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
758 NULL, 0, NULL, 0);
759 return ret;
760}
761
762static int cma_verify_rep(struct rdma_id_private *id_priv, void *data)
763{
764 if (id_priv->id.ps == RDMA_PS_SDP &&
765 sdp_get_majv(((struct sdp_hah *) data)->sdp_version) !=
766 SDP_MAJ_VERSION)
767 return -EINVAL;
768
769 return 0;
770}
771
Sean Heftya1b1b612006-11-30 16:33:14 -0800772static void cma_set_rep_event_data(struct rdma_cm_event *event,
773 struct ib_cm_rep_event_param *rep_data,
774 void *private_data)
775{
776 event->param.conn.private_data = private_data;
777 event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
778 event->param.conn.responder_resources = rep_data->responder_resources;
779 event->param.conn.initiator_depth = rep_data->initiator_depth;
780 event->param.conn.flow_control = rep_data->flow_control;
781 event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
782 event->param.conn.srq = rep_data->srq;
783 event->param.conn.qp_num = rep_data->remote_qpn;
784}
785
Sean Heftye51060f2006-06-17 20:37:29 -0700786static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
787{
788 struct rdma_id_private *id_priv = cm_id->context;
Sean Heftya1b1b612006-11-30 16:33:14 -0800789 struct rdma_cm_event event;
790 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700791
792 atomic_inc(&id_priv->dev_remove);
793 if (!cma_comp(id_priv, CMA_CONNECT))
794 goto out;
795
Sean Heftya1b1b612006-11-30 16:33:14 -0800796 memset(&event, 0, sizeof event);
Sean Heftye51060f2006-06-17 20:37:29 -0700797 switch (ib_event->event) {
798 case IB_CM_REQ_ERROR:
799 case IB_CM_REP_ERROR:
Sean Heftya1b1b612006-11-30 16:33:14 -0800800 event.event = RDMA_CM_EVENT_UNREACHABLE;
801 event.status = -ETIMEDOUT;
Sean Heftye51060f2006-06-17 20:37:29 -0700802 break;
803 case IB_CM_REP_RECEIVED:
Sean Heftya1b1b612006-11-30 16:33:14 -0800804 event.status = cma_verify_rep(id_priv, ib_event->private_data);
805 if (event.status)
806 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
Sean Heftye51060f2006-06-17 20:37:29 -0700807 else if (id_priv->id.qp && id_priv->id.ps != RDMA_PS_SDP) {
Sean Heftya1b1b612006-11-30 16:33:14 -0800808 event.status = cma_rep_recv(id_priv);
809 event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
810 RDMA_CM_EVENT_ESTABLISHED;
Sean Heftye51060f2006-06-17 20:37:29 -0700811 } else
Sean Heftya1b1b612006-11-30 16:33:14 -0800812 event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
813 cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
814 ib_event->private_data);
Sean Heftye51060f2006-06-17 20:37:29 -0700815 break;
816 case IB_CM_RTU_RECEIVED:
Sean Hefty0fe313b2006-11-30 16:37:15 -0800817 case IB_CM_USER_ESTABLISHED:
818 event.event = RDMA_CM_EVENT_ESTABLISHED;
Sean Heftye51060f2006-06-17 20:37:29 -0700819 break;
820 case IB_CM_DREQ_ERROR:
Sean Heftya1b1b612006-11-30 16:33:14 -0800821 event.status = -ETIMEDOUT; /* fall through */
Sean Heftye51060f2006-06-17 20:37:29 -0700822 case IB_CM_DREQ_RECEIVED:
823 case IB_CM_DREP_RECEIVED:
824 if (!cma_comp_exch(id_priv, CMA_CONNECT, CMA_DISCONNECT))
825 goto out;
Sean Heftya1b1b612006-11-30 16:33:14 -0800826 event.event = RDMA_CM_EVENT_DISCONNECTED;
Sean Heftye51060f2006-06-17 20:37:29 -0700827 break;
828 case IB_CM_TIMEWAIT_EXIT:
829 case IB_CM_MRA_RECEIVED:
830 /* ignore event */
831 goto out;
832 case IB_CM_REJ_RECEIVED:
833 cma_modify_qp_err(&id_priv->id);
Sean Heftya1b1b612006-11-30 16:33:14 -0800834 event.status = ib_event->param.rej_rcvd.reason;
835 event.event = RDMA_CM_EVENT_REJECTED;
836 event.param.conn.private_data = ib_event->private_data;
837 event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
Sean Heftye51060f2006-06-17 20:37:29 -0700838 break;
839 default:
840 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d",
841 ib_event->event);
842 goto out;
843 }
844
Sean Heftya1b1b612006-11-30 16:33:14 -0800845 ret = id_priv->id.event_handler(&id_priv->id, &event);
Sean Heftye51060f2006-06-17 20:37:29 -0700846 if (ret) {
847 /* Destroy the CM ID by returning a non-zero value. */
848 id_priv->cm_id.ib = NULL;
849 cma_exch(id_priv, CMA_DESTROYING);
850 cma_release_remove(id_priv);
851 rdma_destroy_id(&id_priv->id);
852 return ret;
853 }
854out:
855 cma_release_remove(id_priv);
856 return ret;
857}
858
Sean Hefty628e5f62006-11-30 16:44:16 -0800859static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
860 struct ib_cm_event *ib_event)
Sean Heftye51060f2006-06-17 20:37:29 -0700861{
862 struct rdma_id_private *id_priv;
863 struct rdma_cm_id *id;
864 struct rdma_route *rt;
865 union cma_ip_addr *src, *dst;
866 __u16 port;
867 u8 ip_ver;
868
Sean Heftye51060f2006-06-17 20:37:29 -0700869 if (cma_get_net_info(ib_event->private_data, listen_id->ps,
870 &ip_ver, &port, &src, &dst))
871 goto err;
872
Krishna Kumar3f168d22006-09-29 12:09:51 -0700873 id = rdma_create_id(listen_id->event_handler, listen_id->context,
874 listen_id->ps);
875 if (IS_ERR(id))
876 goto err;
877
Sean Heftye51060f2006-06-17 20:37:29 -0700878 cma_save_net_info(&id->route.addr, &listen_id->route.addr,
879 ip_ver, port, src, dst);
Krishna Kumar3f168d22006-09-29 12:09:51 -0700880
881 rt = &id->route;
882 rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
883 rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
884 GFP_KERNEL);
885 if (!rt->path_rec)
886 goto destroy_id;
887
Sean Heftye51060f2006-06-17 20:37:29 -0700888 rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
889 if (rt->num_paths == 2)
890 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
891
892 ib_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
893 ib_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
894 ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
Tom Tucker07ebafb2006-08-03 16:02:42 -0500895 rt->addr.dev_addr.dev_type = RDMA_NODE_IB_CA;
Sean Heftye51060f2006-06-17 20:37:29 -0700896
897 id_priv = container_of(id, struct rdma_id_private, id);
898 id_priv->state = CMA_CONNECT;
899 return id_priv;
Krishna Kumar3f168d22006-09-29 12:09:51 -0700900
901destroy_id:
Sean Heftye51060f2006-06-17 20:37:29 -0700902 rdma_destroy_id(id);
Krishna Kumar3f168d22006-09-29 12:09:51 -0700903err:
Sean Heftye51060f2006-06-17 20:37:29 -0700904 return NULL;
905}
906
Sean Hefty628e5f62006-11-30 16:44:16 -0800907static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
908 struct ib_cm_event *ib_event)
909{
910 struct rdma_id_private *id_priv;
911 struct rdma_cm_id *id;
912 union cma_ip_addr *src, *dst;
913 __u16 port;
914 u8 ip_ver;
915 int ret;
916
917 id = rdma_create_id(listen_id->event_handler, listen_id->context,
918 listen_id->ps);
919 if (IS_ERR(id))
920 return NULL;
921
922
923 if (cma_get_net_info(ib_event->private_data, listen_id->ps,
924 &ip_ver, &port, &src, &dst))
925 goto err;
926
927 cma_save_net_info(&id->route.addr, &listen_id->route.addr,
928 ip_ver, port, src, dst);
929
930 ret = rdma_translate_ip(&id->route.addr.src_addr,
931 &id->route.addr.dev_addr);
932 if (ret)
933 goto err;
934
935 id_priv = container_of(id, struct rdma_id_private, id);
936 id_priv->state = CMA_CONNECT;
937 return id_priv;
938err:
939 rdma_destroy_id(id);
940 return NULL;
941}
942
Sean Heftya1b1b612006-11-30 16:33:14 -0800943static void cma_set_req_event_data(struct rdma_cm_event *event,
944 struct ib_cm_req_event_param *req_data,
945 void *private_data, int offset)
946{
947 event->param.conn.private_data = private_data + offset;
948 event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
949 event->param.conn.responder_resources = req_data->responder_resources;
950 event->param.conn.initiator_depth = req_data->initiator_depth;
951 event->param.conn.flow_control = req_data->flow_control;
952 event->param.conn.retry_count = req_data->retry_count;
953 event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
954 event->param.conn.srq = req_data->srq;
955 event->param.conn.qp_num = req_data->remote_qpn;
956}
957
Sean Heftye51060f2006-06-17 20:37:29 -0700958static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
959{
960 struct rdma_id_private *listen_id, *conn_id;
Sean Heftya1b1b612006-11-30 16:33:14 -0800961 struct rdma_cm_event event;
Sean Heftye51060f2006-06-17 20:37:29 -0700962 int offset, ret;
963
964 listen_id = cm_id->context;
965 atomic_inc(&listen_id->dev_remove);
966 if (!cma_comp(listen_id, CMA_LISTEN)) {
967 ret = -ECONNABORTED;
968 goto out;
969 }
970
Sean Hefty628e5f62006-11-30 16:44:16 -0800971 memset(&event, 0, sizeof event);
972 offset = cma_user_data_offset(listen_id->id.ps);
973 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
974 if (listen_id->id.ps == RDMA_PS_UDP) {
975 conn_id = cma_new_udp_id(&listen_id->id, ib_event);
976 event.param.ud.private_data = ib_event->private_data + offset;
977 event.param.ud.private_data_len =
978 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
979 } else {
980 conn_id = cma_new_conn_id(&listen_id->id, ib_event);
981 cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
982 ib_event->private_data, offset);
983 }
Sean Heftye51060f2006-06-17 20:37:29 -0700984 if (!conn_id) {
985 ret = -ENOMEM;
986 goto out;
987 }
988
989 atomic_inc(&conn_id->dev_remove);
Sean Hefty61a73c72006-09-01 15:33:55 -0700990 mutex_lock(&lock);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500991 ret = cma_acquire_dev(conn_id);
Sean Hefty61a73c72006-09-01 15:33:55 -0700992 mutex_unlock(&lock);
Krishna Kumara1a733f2006-10-17 10:09:11 +0530993 if (ret)
994 goto release_conn_id;
Sean Heftye51060f2006-06-17 20:37:29 -0700995
996 conn_id->cm_id.ib = cm_id;
997 cm_id->context = conn_id;
998 cm_id->cm_handler = cma_ib_handler;
999
Sean Heftya1b1b612006-11-30 16:33:14 -08001000 ret = conn_id->id.event_handler(&conn_id->id, &event);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301001 if (!ret)
1002 goto out;
1003
1004 /* Destroy the CM ID by returning a non-zero value. */
1005 conn_id->cm_id.ib = NULL;
1006
1007release_conn_id:
1008 cma_exch(conn_id, CMA_DESTROYING);
1009 cma_release_remove(conn_id);
1010 rdma_destroy_id(&conn_id->id);
1011
Sean Heftye51060f2006-06-17 20:37:29 -07001012out:
1013 cma_release_remove(listen_id);
1014 return ret;
1015}
1016
1017static __be64 cma_get_service_id(enum rdma_port_space ps, struct sockaddr *addr)
1018{
Sean Hefty628e5f62006-11-30 16:44:16 -08001019 return cpu_to_be64(((u64)ps << 16) + be16_to_cpu(cma_port(addr)));
Sean Heftye51060f2006-06-17 20:37:29 -07001020}
1021
1022static void cma_set_compare_data(enum rdma_port_space ps, struct sockaddr *addr,
1023 struct ib_cm_compare_data *compare)
1024{
1025 struct cma_hdr *cma_data, *cma_mask;
1026 struct sdp_hh *sdp_data, *sdp_mask;
1027 __u32 ip4_addr;
1028 struct in6_addr ip6_addr;
1029
1030 memset(compare, 0, sizeof *compare);
1031 cma_data = (void *) compare->data;
1032 cma_mask = (void *) compare->mask;
1033 sdp_data = (void *) compare->data;
1034 sdp_mask = (void *) compare->mask;
1035
1036 switch (addr->sa_family) {
1037 case AF_INET:
1038 ip4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1039 if (ps == RDMA_PS_SDP) {
1040 sdp_set_ip_ver(sdp_data, 4);
1041 sdp_set_ip_ver(sdp_mask, 0xF);
1042 sdp_data->dst_addr.ip4.addr = ip4_addr;
1043 sdp_mask->dst_addr.ip4.addr = ~0;
1044 } else {
1045 cma_set_ip_ver(cma_data, 4);
1046 cma_set_ip_ver(cma_mask, 0xF);
1047 cma_data->dst_addr.ip4.addr = ip4_addr;
1048 cma_mask->dst_addr.ip4.addr = ~0;
1049 }
1050 break;
1051 case AF_INET6:
1052 ip6_addr = ((struct sockaddr_in6 *) addr)->sin6_addr;
1053 if (ps == RDMA_PS_SDP) {
1054 sdp_set_ip_ver(sdp_data, 6);
1055 sdp_set_ip_ver(sdp_mask, 0xF);
1056 sdp_data->dst_addr.ip6 = ip6_addr;
1057 memset(&sdp_mask->dst_addr.ip6, 0xFF,
1058 sizeof sdp_mask->dst_addr.ip6);
1059 } else {
1060 cma_set_ip_ver(cma_data, 6);
1061 cma_set_ip_ver(cma_mask, 0xF);
1062 cma_data->dst_addr.ip6 = ip6_addr;
1063 memset(&cma_mask->dst_addr.ip6, 0xFF,
1064 sizeof cma_mask->dst_addr.ip6);
1065 }
1066 break;
1067 default:
1068 break;
1069 }
1070}
1071
Tom Tucker07ebafb2006-08-03 16:02:42 -05001072static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
1073{
1074 struct rdma_id_private *id_priv = iw_id->context;
Sean Heftya1b1b612006-11-30 16:33:14 -08001075 struct rdma_cm_event event;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001076 struct sockaddr_in *sin;
1077 int ret = 0;
1078
Sean Heftya1b1b612006-11-30 16:33:14 -08001079 memset(&event, 0, sizeof event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001080 atomic_inc(&id_priv->dev_remove);
1081
1082 switch (iw_event->event) {
1083 case IW_CM_EVENT_CLOSE:
Sean Heftya1b1b612006-11-30 16:33:14 -08001084 event.event = RDMA_CM_EVENT_DISCONNECTED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001085 break;
1086 case IW_CM_EVENT_CONNECT_REPLY:
1087 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1088 *sin = iw_event->local_addr;
1089 sin = (struct sockaddr_in *) &id_priv->id.route.addr.dst_addr;
1090 *sin = iw_event->remote_addr;
1091 if (iw_event->status)
Sean Heftya1b1b612006-11-30 16:33:14 -08001092 event.event = RDMA_CM_EVENT_REJECTED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001093 else
Sean Heftya1b1b612006-11-30 16:33:14 -08001094 event.event = RDMA_CM_EVENT_ESTABLISHED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001095 break;
1096 case IW_CM_EVENT_ESTABLISHED:
Sean Heftya1b1b612006-11-30 16:33:14 -08001097 event.event = RDMA_CM_EVENT_ESTABLISHED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001098 break;
1099 default:
1100 BUG_ON(1);
1101 }
1102
Sean Heftya1b1b612006-11-30 16:33:14 -08001103 event.status = iw_event->status;
1104 event.param.conn.private_data = iw_event->private_data;
1105 event.param.conn.private_data_len = iw_event->private_data_len;
1106 ret = id_priv->id.event_handler(&id_priv->id, &event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001107 if (ret) {
1108 /* Destroy the CM ID by returning a non-zero value. */
1109 id_priv->cm_id.iw = NULL;
1110 cma_exch(id_priv, CMA_DESTROYING);
1111 cma_release_remove(id_priv);
1112 rdma_destroy_id(&id_priv->id);
1113 return ret;
1114 }
1115
1116 cma_release_remove(id_priv);
1117 return ret;
1118}
1119
1120static int iw_conn_req_handler(struct iw_cm_id *cm_id,
1121 struct iw_cm_event *iw_event)
1122{
1123 struct rdma_cm_id *new_cm_id;
1124 struct rdma_id_private *listen_id, *conn_id;
1125 struct sockaddr_in *sin;
1126 struct net_device *dev = NULL;
Sean Heftya1b1b612006-11-30 16:33:14 -08001127 struct rdma_cm_event event;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001128 int ret;
1129
1130 listen_id = cm_id->context;
1131 atomic_inc(&listen_id->dev_remove);
1132 if (!cma_comp(listen_id, CMA_LISTEN)) {
1133 ret = -ECONNABORTED;
1134 goto out;
1135 }
1136
1137 /* Create a new RDMA id for the new IW CM ID */
1138 new_cm_id = rdma_create_id(listen_id->id.event_handler,
1139 listen_id->id.context,
1140 RDMA_PS_TCP);
1141 if (!new_cm_id) {
1142 ret = -ENOMEM;
1143 goto out;
1144 }
1145 conn_id = container_of(new_cm_id, struct rdma_id_private, id);
1146 atomic_inc(&conn_id->dev_remove);
1147 conn_id->state = CMA_CONNECT;
1148
1149 dev = ip_dev_find(iw_event->local_addr.sin_addr.s_addr);
1150 if (!dev) {
1151 ret = -EADDRNOTAVAIL;
1152 cma_release_remove(conn_id);
1153 rdma_destroy_id(new_cm_id);
1154 goto out;
1155 }
1156 ret = rdma_copy_addr(&conn_id->id.route.addr.dev_addr, dev, NULL);
1157 if (ret) {
1158 cma_release_remove(conn_id);
1159 rdma_destroy_id(new_cm_id);
1160 goto out;
1161 }
1162
Sean Hefty61a73c72006-09-01 15:33:55 -07001163 mutex_lock(&lock);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001164 ret = cma_acquire_dev(conn_id);
Sean Hefty61a73c72006-09-01 15:33:55 -07001165 mutex_unlock(&lock);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001166 if (ret) {
1167 cma_release_remove(conn_id);
1168 rdma_destroy_id(new_cm_id);
1169 goto out;
1170 }
1171
1172 conn_id->cm_id.iw = cm_id;
1173 cm_id->context = conn_id;
1174 cm_id->cm_handler = cma_iw_handler;
1175
1176 sin = (struct sockaddr_in *) &new_cm_id->route.addr.src_addr;
1177 *sin = iw_event->local_addr;
1178 sin = (struct sockaddr_in *) &new_cm_id->route.addr.dst_addr;
1179 *sin = iw_event->remote_addr;
1180
Sean Heftya1b1b612006-11-30 16:33:14 -08001181 memset(&event, 0, sizeof event);
1182 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1183 event.param.conn.private_data = iw_event->private_data;
1184 event.param.conn.private_data_len = iw_event->private_data_len;
1185 ret = conn_id->id.event_handler(&conn_id->id, &event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001186 if (ret) {
1187 /* User wants to destroy the CM ID */
1188 conn_id->cm_id.iw = NULL;
1189 cma_exch(conn_id, CMA_DESTROYING);
1190 cma_release_remove(conn_id);
1191 rdma_destroy_id(&conn_id->id);
1192 }
1193
1194out:
1195 if (dev)
1196 dev_put(dev);
1197 cma_release_remove(listen_id);
1198 return ret;
1199}
1200
Sean Heftye51060f2006-06-17 20:37:29 -07001201static int cma_ib_listen(struct rdma_id_private *id_priv)
1202{
1203 struct ib_cm_compare_data compare_data;
1204 struct sockaddr *addr;
1205 __be64 svc_id;
1206 int ret;
1207
1208 id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_req_handler,
1209 id_priv);
1210 if (IS_ERR(id_priv->cm_id.ib))
1211 return PTR_ERR(id_priv->cm_id.ib);
1212
1213 addr = &id_priv->id.route.addr.src_addr;
1214 svc_id = cma_get_service_id(id_priv->id.ps, addr);
1215 if (cma_any_addr(addr))
1216 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, NULL);
1217 else {
1218 cma_set_compare_data(id_priv->id.ps, addr, &compare_data);
1219 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, &compare_data);
1220 }
1221
1222 if (ret) {
1223 ib_destroy_cm_id(id_priv->cm_id.ib);
1224 id_priv->cm_id.ib = NULL;
1225 }
1226
1227 return ret;
1228}
1229
Tom Tucker07ebafb2006-08-03 16:02:42 -05001230static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
1231{
1232 int ret;
1233 struct sockaddr_in *sin;
1234
1235 id_priv->cm_id.iw = iw_create_cm_id(id_priv->id.device,
1236 iw_conn_req_handler,
1237 id_priv);
1238 if (IS_ERR(id_priv->cm_id.iw))
1239 return PTR_ERR(id_priv->cm_id.iw);
1240
1241 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1242 id_priv->cm_id.iw->local_addr = *sin;
1243
1244 ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
1245
1246 if (ret) {
1247 iw_destroy_cm_id(id_priv->cm_id.iw);
1248 id_priv->cm_id.iw = NULL;
1249 }
1250
1251 return ret;
1252}
1253
Sean Heftye51060f2006-06-17 20:37:29 -07001254static int cma_listen_handler(struct rdma_cm_id *id,
1255 struct rdma_cm_event *event)
1256{
1257 struct rdma_id_private *id_priv = id->context;
1258
1259 id->context = id_priv->id.context;
1260 id->event_handler = id_priv->id.event_handler;
1261 return id_priv->id.event_handler(id, event);
1262}
1263
1264static void cma_listen_on_dev(struct rdma_id_private *id_priv,
1265 struct cma_device *cma_dev)
1266{
1267 struct rdma_id_private *dev_id_priv;
1268 struct rdma_cm_id *id;
1269 int ret;
1270
1271 id = rdma_create_id(cma_listen_handler, id_priv, id_priv->id.ps);
1272 if (IS_ERR(id))
1273 return;
1274
1275 dev_id_priv = container_of(id, struct rdma_id_private, id);
1276
1277 dev_id_priv->state = CMA_ADDR_BOUND;
1278 memcpy(&id->route.addr.src_addr, &id_priv->id.route.addr.src_addr,
1279 ip_addr_size(&id_priv->id.route.addr.src_addr));
1280
1281 cma_attach_to_dev(dev_id_priv, cma_dev);
1282 list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
1283
1284 ret = rdma_listen(id, id_priv->backlog);
1285 if (ret)
1286 goto err;
1287
1288 return;
1289err:
1290 cma_destroy_listen(dev_id_priv);
1291}
1292
1293static void cma_listen_on_all(struct rdma_id_private *id_priv)
1294{
1295 struct cma_device *cma_dev;
1296
1297 mutex_lock(&lock);
1298 list_add_tail(&id_priv->list, &listen_any_list);
1299 list_for_each_entry(cma_dev, &dev_list, list)
1300 cma_listen_on_dev(id_priv, cma_dev);
1301 mutex_unlock(&lock);
1302}
1303
1304static int cma_bind_any(struct rdma_cm_id *id, sa_family_t af)
1305{
1306 struct sockaddr_in addr_in;
1307
1308 memset(&addr_in, 0, sizeof addr_in);
1309 addr_in.sin_family = af;
1310 return rdma_bind_addr(id, (struct sockaddr *) &addr_in);
1311}
1312
1313int rdma_listen(struct rdma_cm_id *id, int backlog)
1314{
1315 struct rdma_id_private *id_priv;
1316 int ret;
1317
1318 id_priv = container_of(id, struct rdma_id_private, id);
1319 if (id_priv->state == CMA_IDLE) {
1320 ret = cma_bind_any(id, AF_INET);
1321 if (ret)
1322 return ret;
1323 }
1324
1325 if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_LISTEN))
1326 return -EINVAL;
1327
1328 id_priv->backlog = backlog;
1329 if (id->device) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05001330 switch (rdma_node_get_transport(id->device->node_type)) {
1331 case RDMA_TRANSPORT_IB:
Sean Heftye51060f2006-06-17 20:37:29 -07001332 ret = cma_ib_listen(id_priv);
1333 if (ret)
1334 goto err;
1335 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001336 case RDMA_TRANSPORT_IWARP:
1337 ret = cma_iw_listen(id_priv, backlog);
1338 if (ret)
1339 goto err;
1340 break;
Sean Heftye51060f2006-06-17 20:37:29 -07001341 default:
1342 ret = -ENOSYS;
1343 goto err;
1344 }
1345 } else
1346 cma_listen_on_all(id_priv);
1347
1348 return 0;
1349err:
1350 id_priv->backlog = 0;
1351 cma_comp_exch(id_priv, CMA_LISTEN, CMA_ADDR_BOUND);
1352 return ret;
1353}
1354EXPORT_SYMBOL(rdma_listen);
1355
1356static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
1357 void *context)
1358{
1359 struct cma_work *work = context;
1360 struct rdma_route *route;
1361
1362 route = &work->id->id.route;
1363
1364 if (!status) {
1365 route->num_paths = 1;
1366 *route->path_rec = *path_rec;
1367 } else {
1368 work->old_state = CMA_ROUTE_QUERY;
1369 work->new_state = CMA_ADDR_RESOLVED;
1370 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
Sean Hefty8f0472d2006-09-29 11:57:09 -07001371 work->event.status = status;
Sean Heftye51060f2006-06-17 20:37:29 -07001372 }
1373
1374 queue_work(cma_wq, &work->work);
1375}
1376
1377static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
1378 struct cma_work *work)
1379{
1380 struct rdma_dev_addr *addr = &id_priv->id.route.addr.dev_addr;
1381 struct ib_sa_path_rec path_rec;
1382
1383 memset(&path_rec, 0, sizeof path_rec);
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07001384 ib_addr_get_sgid(addr, &path_rec.sgid);
1385 ib_addr_get_dgid(addr, &path_rec.dgid);
Sean Heftye51060f2006-06-17 20:37:29 -07001386 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(addr));
1387 path_rec.numb_path = 1;
1388
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001389 id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
Sean Heftye51060f2006-06-17 20:37:29 -07001390 id_priv->id.port_num, &path_rec,
1391 IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
1392 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH,
1393 timeout_ms, GFP_KERNEL,
1394 cma_query_handler, work, &id_priv->query);
1395
1396 return (id_priv->query_id < 0) ? id_priv->query_id : 0;
1397}
1398
David Howellsc4028952006-11-22 14:57:56 +00001399static void cma_work_handler(struct work_struct *_work)
Sean Heftye51060f2006-06-17 20:37:29 -07001400{
David Howellsc4028952006-11-22 14:57:56 +00001401 struct cma_work *work = container_of(_work, struct cma_work, work);
Sean Heftye51060f2006-06-17 20:37:29 -07001402 struct rdma_id_private *id_priv = work->id;
1403 int destroy = 0;
1404
1405 atomic_inc(&id_priv->dev_remove);
1406 if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
1407 goto out;
1408
1409 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
1410 cma_exch(id_priv, CMA_DESTROYING);
1411 destroy = 1;
1412 }
1413out:
1414 cma_release_remove(id_priv);
1415 cma_deref_id(id_priv);
1416 if (destroy)
1417 rdma_destroy_id(&id_priv->id);
1418 kfree(work);
1419}
1420
1421static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
1422{
1423 struct rdma_route *route = &id_priv->id.route;
1424 struct cma_work *work;
1425 int ret;
1426
1427 work = kzalloc(sizeof *work, GFP_KERNEL);
1428 if (!work)
1429 return -ENOMEM;
1430
1431 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00001432 INIT_WORK(&work->work, cma_work_handler);
Sean Heftye51060f2006-06-17 20:37:29 -07001433 work->old_state = CMA_ROUTE_QUERY;
1434 work->new_state = CMA_ROUTE_RESOLVED;
1435 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1436
1437 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
1438 if (!route->path_rec) {
1439 ret = -ENOMEM;
1440 goto err1;
1441 }
1442
1443 ret = cma_query_ib_route(id_priv, timeout_ms, work);
1444 if (ret)
1445 goto err2;
1446
1447 return 0;
1448err2:
1449 kfree(route->path_rec);
1450 route->path_rec = NULL;
1451err1:
1452 kfree(work);
1453 return ret;
1454}
1455
1456int rdma_set_ib_paths(struct rdma_cm_id *id,
1457 struct ib_sa_path_rec *path_rec, int num_paths)
1458{
1459 struct rdma_id_private *id_priv;
1460 int ret;
1461
1462 id_priv = container_of(id, struct rdma_id_private, id);
1463 if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ROUTE_RESOLVED))
1464 return -EINVAL;
1465
1466 id->route.path_rec = kmalloc(sizeof *path_rec * num_paths, GFP_KERNEL);
1467 if (!id->route.path_rec) {
1468 ret = -ENOMEM;
1469 goto err;
1470 }
1471
1472 memcpy(id->route.path_rec, path_rec, sizeof *path_rec * num_paths);
1473 return 0;
1474err:
1475 cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_ADDR_RESOLVED);
1476 return ret;
1477}
1478EXPORT_SYMBOL(rdma_set_ib_paths);
1479
Tom Tucker07ebafb2006-08-03 16:02:42 -05001480static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
1481{
1482 struct cma_work *work;
1483
1484 work = kzalloc(sizeof *work, GFP_KERNEL);
1485 if (!work)
1486 return -ENOMEM;
1487
1488 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00001489 INIT_WORK(&work->work, cma_work_handler);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001490 work->old_state = CMA_ROUTE_QUERY;
1491 work->new_state = CMA_ROUTE_RESOLVED;
1492 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1493 queue_work(cma_wq, &work->work);
1494 return 0;
1495}
1496
Sean Heftye51060f2006-06-17 20:37:29 -07001497int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
1498{
1499 struct rdma_id_private *id_priv;
1500 int ret;
1501
1502 id_priv = container_of(id, struct rdma_id_private, id);
1503 if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ROUTE_QUERY))
1504 return -EINVAL;
1505
1506 atomic_inc(&id_priv->refcount);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001507 switch (rdma_node_get_transport(id->device->node_type)) {
1508 case RDMA_TRANSPORT_IB:
Sean Heftye51060f2006-06-17 20:37:29 -07001509 ret = cma_resolve_ib_route(id_priv, timeout_ms);
1510 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001511 case RDMA_TRANSPORT_IWARP:
1512 ret = cma_resolve_iw_route(id_priv, timeout_ms);
1513 break;
Sean Heftye51060f2006-06-17 20:37:29 -07001514 default:
1515 ret = -ENOSYS;
1516 break;
1517 }
1518 if (ret)
1519 goto err;
1520
1521 return 0;
1522err:
1523 cma_comp_exch(id_priv, CMA_ROUTE_QUERY, CMA_ADDR_RESOLVED);
1524 cma_deref_id(id_priv);
1525 return ret;
1526}
1527EXPORT_SYMBOL(rdma_resolve_route);
1528
1529static int cma_bind_loopback(struct rdma_id_private *id_priv)
1530{
1531 struct cma_device *cma_dev;
1532 struct ib_port_attr port_attr;
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07001533 union ib_gid gid;
Sean Heftye51060f2006-06-17 20:37:29 -07001534 u16 pkey;
1535 int ret;
1536 u8 p;
1537
1538 mutex_lock(&lock);
Krishna Kumare82153b2006-10-16 10:09:01 +05301539 if (list_empty(&dev_list)) {
Sean Heftye51060f2006-06-17 20:37:29 -07001540 ret = -ENODEV;
1541 goto out;
1542 }
Krishna Kumare82153b2006-10-16 10:09:01 +05301543 list_for_each_entry(cma_dev, &dev_list, list)
1544 for (p = 1; p <= cma_dev->device->phys_port_cnt; ++p)
1545 if (!ib_query_port(cma_dev->device, p, &port_attr) &&
1546 port_attr.state == IB_PORT_ACTIVE)
1547 goto port_found;
1548
1549 p = 1;
1550 cma_dev = list_entry(dev_list.next, struct cma_device, list);
Sean Heftye51060f2006-06-17 20:37:29 -07001551
1552port_found:
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07001553 ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07001554 if (ret)
1555 goto out;
1556
1557 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
1558 if (ret)
1559 goto out;
1560
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07001561 ib_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07001562 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
1563 id_priv->id.port_num = p;
1564 cma_attach_to_dev(id_priv, cma_dev);
1565out:
1566 mutex_unlock(&lock);
1567 return ret;
1568}
1569
1570static void addr_handler(int status, struct sockaddr *src_addr,
1571 struct rdma_dev_addr *dev_addr, void *context)
1572{
1573 struct rdma_id_private *id_priv = context;
Sean Heftya1b1b612006-11-30 16:33:14 -08001574 struct rdma_cm_event event;
Sean Heftye51060f2006-06-17 20:37:29 -07001575
Sean Heftya1b1b612006-11-30 16:33:14 -08001576 memset(&event, 0, sizeof event);
Sean Heftye51060f2006-06-17 20:37:29 -07001577 atomic_inc(&id_priv->dev_remove);
Sean Hefty61a73c72006-09-01 15:33:55 -07001578
1579 /*
1580 * Grab mutex to block rdma_destroy_id() from removing the device while
1581 * we're trying to acquire it.
1582 */
1583 mutex_lock(&lock);
1584 if (!cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_RESOLVED)) {
1585 mutex_unlock(&lock);
1586 goto out;
1587 }
1588
1589 if (!status && !id_priv->cma_dev)
Sean Heftye51060f2006-06-17 20:37:29 -07001590 status = cma_acquire_dev(id_priv);
Sean Hefty61a73c72006-09-01 15:33:55 -07001591 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -07001592
1593 if (status) {
Sean Hefty61a73c72006-09-01 15:33:55 -07001594 if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ADDR_BOUND))
Sean Heftye51060f2006-06-17 20:37:29 -07001595 goto out;
Sean Heftya1b1b612006-11-30 16:33:14 -08001596 event.event = RDMA_CM_EVENT_ADDR_ERROR;
1597 event.status = status;
Sean Heftye51060f2006-06-17 20:37:29 -07001598 } else {
Sean Heftye51060f2006-06-17 20:37:29 -07001599 memcpy(&id_priv->id.route.addr.src_addr, src_addr,
1600 ip_addr_size(src_addr));
Sean Heftya1b1b612006-11-30 16:33:14 -08001601 event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07001602 }
1603
Sean Heftya1b1b612006-11-30 16:33:14 -08001604 if (id_priv->id.event_handler(&id_priv->id, &event)) {
Sean Heftye51060f2006-06-17 20:37:29 -07001605 cma_exch(id_priv, CMA_DESTROYING);
1606 cma_release_remove(id_priv);
1607 cma_deref_id(id_priv);
1608 rdma_destroy_id(&id_priv->id);
1609 return;
1610 }
1611out:
1612 cma_release_remove(id_priv);
1613 cma_deref_id(id_priv);
1614}
1615
1616static int cma_resolve_loopback(struct rdma_id_private *id_priv)
1617{
1618 struct cma_work *work;
1619 struct sockaddr_in *src_in, *dst_in;
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07001620 union ib_gid gid;
Sean Heftye51060f2006-06-17 20:37:29 -07001621 int ret;
1622
1623 work = kzalloc(sizeof *work, GFP_KERNEL);
1624 if (!work)
1625 return -ENOMEM;
1626
1627 if (!id_priv->cma_dev) {
1628 ret = cma_bind_loopback(id_priv);
1629 if (ret)
1630 goto err;
1631 }
1632
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07001633 ib_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
1634 ib_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07001635
1636 if (cma_zero_addr(&id_priv->id.route.addr.src_addr)) {
1637 src_in = (struct sockaddr_in *)&id_priv->id.route.addr.src_addr;
1638 dst_in = (struct sockaddr_in *)&id_priv->id.route.addr.dst_addr;
1639 src_in->sin_family = dst_in->sin_family;
1640 src_in->sin_addr.s_addr = dst_in->sin_addr.s_addr;
1641 }
1642
1643 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00001644 INIT_WORK(&work->work, cma_work_handler);
Sean Heftye51060f2006-06-17 20:37:29 -07001645 work->old_state = CMA_ADDR_QUERY;
1646 work->new_state = CMA_ADDR_RESOLVED;
1647 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
1648 queue_work(cma_wq, &work->work);
1649 return 0;
1650err:
1651 kfree(work);
1652 return ret;
1653}
1654
1655static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
1656 struct sockaddr *dst_addr)
1657{
1658 if (src_addr && src_addr->sa_family)
1659 return rdma_bind_addr(id, src_addr);
1660 else
1661 return cma_bind_any(id, dst_addr->sa_family);
1662}
1663
1664int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
1665 struct sockaddr *dst_addr, int timeout_ms)
1666{
1667 struct rdma_id_private *id_priv;
1668 int ret;
1669
1670 id_priv = container_of(id, struct rdma_id_private, id);
1671 if (id_priv->state == CMA_IDLE) {
1672 ret = cma_bind_addr(id, src_addr, dst_addr);
1673 if (ret)
1674 return ret;
1675 }
1676
1677 if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_ADDR_QUERY))
1678 return -EINVAL;
1679
1680 atomic_inc(&id_priv->refcount);
1681 memcpy(&id->route.addr.dst_addr, dst_addr, ip_addr_size(dst_addr));
1682 if (cma_any_addr(dst_addr))
1683 ret = cma_resolve_loopback(id_priv);
1684 else
Sean Hefty7a118df2006-10-31 11:12:59 -08001685 ret = rdma_resolve_ip(&addr_client, &id->route.addr.src_addr,
1686 dst_addr, &id->route.addr.dev_addr,
Sean Heftye51060f2006-06-17 20:37:29 -07001687 timeout_ms, addr_handler, id_priv);
1688 if (ret)
1689 goto err;
1690
1691 return 0;
1692err:
1693 cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_BOUND);
1694 cma_deref_id(id_priv);
1695 return ret;
1696}
1697EXPORT_SYMBOL(rdma_resolve_addr);
1698
1699static void cma_bind_port(struct rdma_bind_list *bind_list,
1700 struct rdma_id_private *id_priv)
1701{
1702 struct sockaddr_in *sin;
1703
1704 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1705 sin->sin_port = htons(bind_list->port);
1706 id_priv->bind_list = bind_list;
1707 hlist_add_head(&id_priv->node, &bind_list->owners);
1708}
1709
1710static int cma_alloc_port(struct idr *ps, struct rdma_id_private *id_priv,
1711 unsigned short snum)
1712{
1713 struct rdma_bind_list *bind_list;
1714 int port, start, ret;
1715
1716 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
1717 if (!bind_list)
1718 return -ENOMEM;
1719
1720 start = snum ? snum : sysctl_local_port_range[0];
1721
1722 do {
1723 ret = idr_get_new_above(ps, bind_list, start, &port);
1724 } while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL));
1725
1726 if (ret)
1727 goto err;
1728
1729 if ((snum && port != snum) ||
1730 (!snum && port > sysctl_local_port_range[1])) {
1731 idr_remove(ps, port);
1732 ret = -EADDRNOTAVAIL;
1733 goto err;
1734 }
1735
1736 bind_list->ps = ps;
1737 bind_list->port = (unsigned short) port;
1738 cma_bind_port(bind_list, id_priv);
1739 return 0;
1740err:
1741 kfree(bind_list);
1742 return ret;
1743}
1744
1745static int cma_use_port(struct idr *ps, struct rdma_id_private *id_priv)
1746{
1747 struct rdma_id_private *cur_id;
1748 struct sockaddr_in *sin, *cur_sin;
1749 struct rdma_bind_list *bind_list;
1750 struct hlist_node *node;
1751 unsigned short snum;
1752
1753 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1754 snum = ntohs(sin->sin_port);
1755 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
1756 return -EACCES;
1757
1758 bind_list = idr_find(ps, snum);
1759 if (!bind_list)
1760 return cma_alloc_port(ps, id_priv, snum);
1761
1762 /*
1763 * We don't support binding to any address if anyone is bound to
1764 * a specific address on the same port.
1765 */
1766 if (cma_any_addr(&id_priv->id.route.addr.src_addr))
1767 return -EADDRNOTAVAIL;
1768
1769 hlist_for_each_entry(cur_id, node, &bind_list->owners, node) {
1770 if (cma_any_addr(&cur_id->id.route.addr.src_addr))
1771 return -EADDRNOTAVAIL;
Roland Dreier3cd96562006-09-22 15:22:46 -07001772
Sean Heftye51060f2006-06-17 20:37:29 -07001773 cur_sin = (struct sockaddr_in *) &cur_id->id.route.addr.src_addr;
1774 if (sin->sin_addr.s_addr == cur_sin->sin_addr.s_addr)
1775 return -EADDRINUSE;
1776 }
1777
1778 cma_bind_port(bind_list, id_priv);
1779 return 0;
1780}
1781
1782static int cma_get_port(struct rdma_id_private *id_priv)
1783{
1784 struct idr *ps;
1785 int ret;
1786
1787 switch (id_priv->id.ps) {
1788 case RDMA_PS_SDP:
1789 ps = &sdp_ps;
1790 break;
1791 case RDMA_PS_TCP:
1792 ps = &tcp_ps;
1793 break;
Sean Hefty628e5f62006-11-30 16:44:16 -08001794 case RDMA_PS_UDP:
1795 ps = &udp_ps;
1796 break;
Sean Heftye51060f2006-06-17 20:37:29 -07001797 default:
1798 return -EPROTONOSUPPORT;
1799 }
1800
1801 mutex_lock(&lock);
1802 if (cma_any_port(&id_priv->id.route.addr.src_addr))
1803 ret = cma_alloc_port(ps, id_priv, 0);
1804 else
1805 ret = cma_use_port(ps, id_priv);
1806 mutex_unlock(&lock);
1807
1808 return ret;
1809}
1810
1811int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
1812{
1813 struct rdma_id_private *id_priv;
1814 int ret;
1815
1816 if (addr->sa_family != AF_INET)
1817 return -EAFNOSUPPORT;
1818
1819 id_priv = container_of(id, struct rdma_id_private, id);
1820 if (!cma_comp_exch(id_priv, CMA_IDLE, CMA_ADDR_BOUND))
1821 return -EINVAL;
1822
1823 if (!cma_any_addr(addr)) {
1824 ret = rdma_translate_ip(addr, &id->route.addr.dev_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07001825 if (ret)
Krishna Kumar255d0c12006-10-24 13:22:28 -07001826 goto err1;
1827
1828 mutex_lock(&lock);
1829 ret = cma_acquire_dev(id_priv);
1830 mutex_unlock(&lock);
1831 if (ret)
1832 goto err1;
Sean Heftye51060f2006-06-17 20:37:29 -07001833 }
1834
1835 memcpy(&id->route.addr.src_addr, addr, ip_addr_size(addr));
1836 ret = cma_get_port(id_priv);
1837 if (ret)
Krishna Kumar255d0c12006-10-24 13:22:28 -07001838 goto err2;
Sean Heftye51060f2006-06-17 20:37:29 -07001839
1840 return 0;
Krishna Kumar255d0c12006-10-24 13:22:28 -07001841err2:
1842 if (!cma_any_addr(addr)) {
1843 mutex_lock(&lock);
1844 cma_detach_from_dev(id_priv);
1845 mutex_unlock(&lock);
1846 }
1847err1:
Sean Heftye51060f2006-06-17 20:37:29 -07001848 cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_IDLE);
1849 return ret;
1850}
1851EXPORT_SYMBOL(rdma_bind_addr);
1852
1853static int cma_format_hdr(void *hdr, enum rdma_port_space ps,
1854 struct rdma_route *route)
1855{
1856 struct sockaddr_in *src4, *dst4;
1857 struct cma_hdr *cma_hdr;
1858 struct sdp_hh *sdp_hdr;
1859
1860 src4 = (struct sockaddr_in *) &route->addr.src_addr;
1861 dst4 = (struct sockaddr_in *) &route->addr.dst_addr;
1862
1863 switch (ps) {
1864 case RDMA_PS_SDP:
1865 sdp_hdr = hdr;
1866 if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION)
1867 return -EINVAL;
1868 sdp_set_ip_ver(sdp_hdr, 4);
1869 sdp_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
1870 sdp_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
1871 sdp_hdr->port = src4->sin_port;
1872 break;
1873 default:
1874 cma_hdr = hdr;
1875 cma_hdr->cma_version = CMA_VERSION;
1876 cma_set_ip_ver(cma_hdr, 4);
1877 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
1878 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
1879 cma_hdr->port = src4->sin_port;
1880 break;
1881 }
1882 return 0;
1883}
1884
Sean Hefty628e5f62006-11-30 16:44:16 -08001885static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
1886 struct ib_cm_event *ib_event)
1887{
1888 struct rdma_id_private *id_priv = cm_id->context;
1889 struct rdma_cm_event event;
1890 struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
1891 int ret = 0;
1892
1893 memset(&event, 0, sizeof event);
1894 atomic_inc(&id_priv->dev_remove);
1895 if (!cma_comp(id_priv, CMA_CONNECT))
1896 goto out;
1897
1898 switch (ib_event->event) {
1899 case IB_CM_SIDR_REQ_ERROR:
1900 event.event = RDMA_CM_EVENT_UNREACHABLE;
1901 event.status = -ETIMEDOUT;
1902 break;
1903 case IB_CM_SIDR_REP_RECEIVED:
1904 event.param.ud.private_data = ib_event->private_data;
1905 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
1906 if (rep->status != IB_SIDR_SUCCESS) {
1907 event.event = RDMA_CM_EVENT_UNREACHABLE;
1908 event.status = ib_event->param.sidr_rep_rcvd.status;
1909 break;
1910 }
1911 if (rep->qkey != RDMA_UD_QKEY) {
1912 event.event = RDMA_CM_EVENT_UNREACHABLE;
1913 event.status = -EINVAL;
1914 break;
1915 }
1916 ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
1917 id_priv->id.route.path_rec,
1918 &event.param.ud.ah_attr);
1919 event.param.ud.qp_num = rep->qpn;
1920 event.param.ud.qkey = rep->qkey;
1921 event.event = RDMA_CM_EVENT_ESTABLISHED;
1922 event.status = 0;
1923 break;
1924 default:
1925 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d",
1926 ib_event->event);
1927 goto out;
1928 }
1929
1930 ret = id_priv->id.event_handler(&id_priv->id, &event);
1931 if (ret) {
1932 /* Destroy the CM ID by returning a non-zero value. */
1933 id_priv->cm_id.ib = NULL;
1934 cma_exch(id_priv, CMA_DESTROYING);
1935 cma_release_remove(id_priv);
1936 rdma_destroy_id(&id_priv->id);
1937 return ret;
1938 }
1939out:
1940 cma_release_remove(id_priv);
1941 return ret;
1942}
1943
1944static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
1945 struct rdma_conn_param *conn_param)
1946{
1947 struct ib_cm_sidr_req_param req;
1948 struct rdma_route *route;
1949 int ret;
1950
1951 req.private_data_len = sizeof(struct cma_hdr) +
1952 conn_param->private_data_len;
1953 req.private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
1954 if (!req.private_data)
1955 return -ENOMEM;
1956
1957 if (conn_param->private_data && conn_param->private_data_len)
1958 memcpy((void *) req.private_data + sizeof(struct cma_hdr),
1959 conn_param->private_data, conn_param->private_data_len);
1960
1961 route = &id_priv->id.route;
1962 ret = cma_format_hdr((void *) req.private_data, id_priv->id.ps, route);
1963 if (ret)
1964 goto out;
1965
1966 id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device,
1967 cma_sidr_rep_handler, id_priv);
1968 if (IS_ERR(id_priv->cm_id.ib)) {
1969 ret = PTR_ERR(id_priv->cm_id.ib);
1970 goto out;
1971 }
1972
1973 req.path = route->path_rec;
1974 req.service_id = cma_get_service_id(id_priv->id.ps,
1975 &route->addr.dst_addr);
1976 req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
1977 req.max_cm_retries = CMA_MAX_CM_RETRIES;
1978
1979 ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
1980 if (ret) {
1981 ib_destroy_cm_id(id_priv->cm_id.ib);
1982 id_priv->cm_id.ib = NULL;
1983 }
1984out:
1985 kfree(req.private_data);
1986 return ret;
1987}
1988
Sean Heftye51060f2006-06-17 20:37:29 -07001989static int cma_connect_ib(struct rdma_id_private *id_priv,
1990 struct rdma_conn_param *conn_param)
1991{
1992 struct ib_cm_req_param req;
1993 struct rdma_route *route;
1994 void *private_data;
1995 int offset, ret;
1996
1997 memset(&req, 0, sizeof req);
1998 offset = cma_user_data_offset(id_priv->id.ps);
1999 req.private_data_len = offset + conn_param->private_data_len;
2000 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2001 if (!private_data)
2002 return -ENOMEM;
2003
2004 if (conn_param->private_data && conn_param->private_data_len)
2005 memcpy(private_data + offset, conn_param->private_data,
2006 conn_param->private_data_len);
2007
2008 id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_ib_handler,
2009 id_priv);
2010 if (IS_ERR(id_priv->cm_id.ib)) {
2011 ret = PTR_ERR(id_priv->cm_id.ib);
2012 goto out;
2013 }
2014
2015 route = &id_priv->id.route;
2016 ret = cma_format_hdr(private_data, id_priv->id.ps, route);
2017 if (ret)
2018 goto out;
2019 req.private_data = private_data;
2020
2021 req.primary_path = &route->path_rec[0];
2022 if (route->num_paths == 2)
2023 req.alternate_path = &route->path_rec[1];
2024
2025 req.service_id = cma_get_service_id(id_priv->id.ps,
2026 &route->addr.dst_addr);
2027 req.qp_num = id_priv->qp_num;
Sean Hefty9b2e9c02006-11-30 16:30:47 -08002028 req.qp_type = IB_QPT_RC;
Sean Heftye51060f2006-06-17 20:37:29 -07002029 req.starting_psn = id_priv->seq_num;
2030 req.responder_resources = conn_param->responder_resources;
2031 req.initiator_depth = conn_param->initiator_depth;
2032 req.flow_control = conn_param->flow_control;
2033 req.retry_count = conn_param->retry_count;
2034 req.rnr_retry_count = conn_param->rnr_retry_count;
2035 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2036 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2037 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2038 req.srq = id_priv->srq ? 1 : 0;
2039
2040 ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
2041out:
Krishna Kumar675a0272006-09-29 11:47:06 -07002042 if (ret && !IS_ERR(id_priv->cm_id.ib)) {
2043 ib_destroy_cm_id(id_priv->cm_id.ib);
2044 id_priv->cm_id.ib = NULL;
2045 }
2046
Sean Heftye51060f2006-06-17 20:37:29 -07002047 kfree(private_data);
2048 return ret;
2049}
2050
Tom Tucker07ebafb2006-08-03 16:02:42 -05002051static int cma_connect_iw(struct rdma_id_private *id_priv,
2052 struct rdma_conn_param *conn_param)
2053{
2054 struct iw_cm_id *cm_id;
2055 struct sockaddr_in* sin;
2056 int ret;
2057 struct iw_cm_conn_param iw_param;
2058
2059 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
2060 if (IS_ERR(cm_id)) {
2061 ret = PTR_ERR(cm_id);
2062 goto out;
2063 }
2064
2065 id_priv->cm_id.iw = cm_id;
2066
2067 sin = (struct sockaddr_in*) &id_priv->id.route.addr.src_addr;
2068 cm_id->local_addr = *sin;
2069
2070 sin = (struct sockaddr_in*) &id_priv->id.route.addr.dst_addr;
2071 cm_id->remote_addr = *sin;
2072
2073 ret = cma_modify_qp_rtr(&id_priv->id);
Krishna Kumar675a0272006-09-29 11:47:06 -07002074 if (ret)
2075 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002076
2077 iw_param.ord = conn_param->initiator_depth;
2078 iw_param.ird = conn_param->responder_resources;
2079 iw_param.private_data = conn_param->private_data;
2080 iw_param.private_data_len = conn_param->private_data_len;
2081 if (id_priv->id.qp)
2082 iw_param.qpn = id_priv->qp_num;
2083 else
2084 iw_param.qpn = conn_param->qp_num;
2085 ret = iw_cm_connect(cm_id, &iw_param);
2086out:
Krishna Kumar675a0272006-09-29 11:47:06 -07002087 if (ret && !IS_ERR(cm_id)) {
2088 iw_destroy_cm_id(cm_id);
2089 id_priv->cm_id.iw = NULL;
2090 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05002091 return ret;
2092}
2093
Sean Heftye51060f2006-06-17 20:37:29 -07002094int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2095{
2096 struct rdma_id_private *id_priv;
2097 int ret;
2098
2099 id_priv = container_of(id, struct rdma_id_private, id);
2100 if (!cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_CONNECT))
2101 return -EINVAL;
2102
2103 if (!id->qp) {
2104 id_priv->qp_num = conn_param->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -07002105 id_priv->srq = conn_param->srq;
2106 }
2107
Tom Tucker07ebafb2006-08-03 16:02:42 -05002108 switch (rdma_node_get_transport(id->device->node_type)) {
2109 case RDMA_TRANSPORT_IB:
Sean Hefty628e5f62006-11-30 16:44:16 -08002110 if (id->ps == RDMA_PS_UDP)
2111 ret = cma_resolve_ib_udp(id_priv, conn_param);
2112 else
2113 ret = cma_connect_ib(id_priv, conn_param);
Sean Heftye51060f2006-06-17 20:37:29 -07002114 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002115 case RDMA_TRANSPORT_IWARP:
2116 ret = cma_connect_iw(id_priv, conn_param);
2117 break;
Sean Heftye51060f2006-06-17 20:37:29 -07002118 default:
2119 ret = -ENOSYS;
2120 break;
2121 }
2122 if (ret)
2123 goto err;
2124
2125 return 0;
2126err:
2127 cma_comp_exch(id_priv, CMA_CONNECT, CMA_ROUTE_RESOLVED);
2128 return ret;
2129}
2130EXPORT_SYMBOL(rdma_connect);
2131
2132static int cma_accept_ib(struct rdma_id_private *id_priv,
2133 struct rdma_conn_param *conn_param)
2134{
2135 struct ib_cm_rep_param rep;
Sean Hefty0fe313b2006-11-30 16:37:15 -08002136 struct ib_qp_attr qp_attr;
2137 int qp_attr_mask, ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002138
Sean Hefty0fe313b2006-11-30 16:37:15 -08002139 if (id_priv->id.qp) {
2140 ret = cma_modify_qp_rtr(&id_priv->id);
2141 if (ret)
2142 goto out;
2143
2144 qp_attr.qp_state = IB_QPS_RTS;
2145 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, &qp_attr,
2146 &qp_attr_mask);
2147 if (ret)
2148 goto out;
2149
2150 qp_attr.max_rd_atomic = conn_param->initiator_depth;
2151 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
2152 if (ret)
2153 goto out;
2154 }
Sean Heftye51060f2006-06-17 20:37:29 -07002155
2156 memset(&rep, 0, sizeof rep);
2157 rep.qp_num = id_priv->qp_num;
2158 rep.starting_psn = id_priv->seq_num;
2159 rep.private_data = conn_param->private_data;
2160 rep.private_data_len = conn_param->private_data_len;
2161 rep.responder_resources = conn_param->responder_resources;
2162 rep.initiator_depth = conn_param->initiator_depth;
2163 rep.target_ack_delay = CMA_CM_RESPONSE_TIMEOUT;
2164 rep.failover_accepted = 0;
2165 rep.flow_control = conn_param->flow_control;
2166 rep.rnr_retry_count = conn_param->rnr_retry_count;
2167 rep.srq = id_priv->srq ? 1 : 0;
2168
Sean Hefty0fe313b2006-11-30 16:37:15 -08002169 ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
2170out:
2171 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002172}
2173
Tom Tucker07ebafb2006-08-03 16:02:42 -05002174static int cma_accept_iw(struct rdma_id_private *id_priv,
2175 struct rdma_conn_param *conn_param)
2176{
2177 struct iw_cm_conn_param iw_param;
2178 int ret;
2179
2180 ret = cma_modify_qp_rtr(&id_priv->id);
2181 if (ret)
2182 return ret;
2183
2184 iw_param.ord = conn_param->initiator_depth;
2185 iw_param.ird = conn_param->responder_resources;
2186 iw_param.private_data = conn_param->private_data;
2187 iw_param.private_data_len = conn_param->private_data_len;
2188 if (id_priv->id.qp) {
2189 iw_param.qpn = id_priv->qp_num;
2190 } else
2191 iw_param.qpn = conn_param->qp_num;
2192
2193 return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
2194}
2195
Sean Hefty628e5f62006-11-30 16:44:16 -08002196static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
2197 enum ib_cm_sidr_status status,
2198 const void *private_data, int private_data_len)
2199{
2200 struct ib_cm_sidr_rep_param rep;
2201
2202 memset(&rep, 0, sizeof rep);
2203 rep.status = status;
2204 if (status == IB_SIDR_SUCCESS) {
2205 rep.qp_num = id_priv->qp_num;
2206 rep.qkey = RDMA_UD_QKEY;
2207 }
2208 rep.private_data = private_data;
2209 rep.private_data_len = private_data_len;
2210
2211 return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
2212}
2213
Sean Heftye51060f2006-06-17 20:37:29 -07002214int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2215{
2216 struct rdma_id_private *id_priv;
2217 int ret;
2218
2219 id_priv = container_of(id, struct rdma_id_private, id);
2220 if (!cma_comp(id_priv, CMA_CONNECT))
2221 return -EINVAL;
2222
2223 if (!id->qp && conn_param) {
2224 id_priv->qp_num = conn_param->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -07002225 id_priv->srq = conn_param->srq;
2226 }
2227
Tom Tucker07ebafb2006-08-03 16:02:42 -05002228 switch (rdma_node_get_transport(id->device->node_type)) {
2229 case RDMA_TRANSPORT_IB:
Sean Hefty628e5f62006-11-30 16:44:16 -08002230 if (id->ps == RDMA_PS_UDP)
2231 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
2232 conn_param->private_data,
2233 conn_param->private_data_len);
2234 else if (conn_param)
Sean Heftye51060f2006-06-17 20:37:29 -07002235 ret = cma_accept_ib(id_priv, conn_param);
2236 else
2237 ret = cma_rep_recv(id_priv);
2238 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002239 case RDMA_TRANSPORT_IWARP:
2240 ret = cma_accept_iw(id_priv, conn_param);
2241 break;
Sean Heftye51060f2006-06-17 20:37:29 -07002242 default:
2243 ret = -ENOSYS;
2244 break;
2245 }
2246
2247 if (ret)
2248 goto reject;
2249
2250 return 0;
2251reject:
2252 cma_modify_qp_err(id);
2253 rdma_reject(id, NULL, 0);
2254 return ret;
2255}
2256EXPORT_SYMBOL(rdma_accept);
2257
Sean Hefty0fe313b2006-11-30 16:37:15 -08002258int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
2259{
2260 struct rdma_id_private *id_priv;
2261 int ret;
2262
2263 id_priv = container_of(id, struct rdma_id_private, id);
2264 if (!cma_comp(id_priv, CMA_CONNECT))
2265 return -EINVAL;
2266
2267 switch (id->device->node_type) {
2268 case RDMA_NODE_IB_CA:
2269 ret = ib_cm_notify(id_priv->cm_id.ib, event);
2270 break;
2271 default:
2272 ret = 0;
2273 break;
2274 }
2275 return ret;
2276}
2277EXPORT_SYMBOL(rdma_notify);
2278
Sean Heftye51060f2006-06-17 20:37:29 -07002279int rdma_reject(struct rdma_cm_id *id, const void *private_data,
2280 u8 private_data_len)
2281{
2282 struct rdma_id_private *id_priv;
2283 int ret;
2284
2285 id_priv = container_of(id, struct rdma_id_private, id);
2286 if (!cma_comp(id_priv, CMA_CONNECT))
2287 return -EINVAL;
2288
Tom Tucker07ebafb2006-08-03 16:02:42 -05002289 switch (rdma_node_get_transport(id->device->node_type)) {
2290 case RDMA_TRANSPORT_IB:
Sean Hefty628e5f62006-11-30 16:44:16 -08002291 if (id->ps == RDMA_PS_UDP)
2292 ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT,
2293 private_data, private_data_len);
2294 else
2295 ret = ib_send_cm_rej(id_priv->cm_id.ib,
2296 IB_CM_REJ_CONSUMER_DEFINED, NULL,
2297 0, private_data, private_data_len);
Sean Heftye51060f2006-06-17 20:37:29 -07002298 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002299 case RDMA_TRANSPORT_IWARP:
2300 ret = iw_cm_reject(id_priv->cm_id.iw,
2301 private_data, private_data_len);
2302 break;
Sean Heftye51060f2006-06-17 20:37:29 -07002303 default:
2304 ret = -ENOSYS;
2305 break;
2306 }
2307 return ret;
2308}
2309EXPORT_SYMBOL(rdma_reject);
2310
2311int rdma_disconnect(struct rdma_cm_id *id)
2312{
2313 struct rdma_id_private *id_priv;
2314 int ret;
2315
2316 id_priv = container_of(id, struct rdma_id_private, id);
2317 if (!cma_comp(id_priv, CMA_CONNECT) &&
2318 !cma_comp(id_priv, CMA_DISCONNECT))
2319 return -EINVAL;
2320
Tom Tucker07ebafb2006-08-03 16:02:42 -05002321 switch (rdma_node_get_transport(id->device->node_type)) {
2322 case RDMA_TRANSPORT_IB:
2323 ret = cma_modify_qp_err(id);
2324 if (ret)
2325 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07002326 /* Initiate or respond to a disconnect. */
2327 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
2328 ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
2329 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002330 case RDMA_TRANSPORT_IWARP:
2331 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
2332 break;
Sean Heftye51060f2006-06-17 20:37:29 -07002333 default:
Tom Tucker07ebafb2006-08-03 16:02:42 -05002334 ret = -EINVAL;
Sean Heftye51060f2006-06-17 20:37:29 -07002335 break;
2336 }
2337out:
2338 return ret;
2339}
2340EXPORT_SYMBOL(rdma_disconnect);
2341
2342static void cma_add_one(struct ib_device *device)
2343{
2344 struct cma_device *cma_dev;
2345 struct rdma_id_private *id_priv;
2346
2347 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
2348 if (!cma_dev)
2349 return;
2350
2351 cma_dev->device = device;
2352 cma_dev->node_guid = device->node_guid;
Sean Heftye51060f2006-06-17 20:37:29 -07002353
2354 init_completion(&cma_dev->comp);
2355 atomic_set(&cma_dev->refcount, 1);
2356 INIT_LIST_HEAD(&cma_dev->id_list);
2357 ib_set_client_data(device, &cma_client, cma_dev);
2358
2359 mutex_lock(&lock);
2360 list_add_tail(&cma_dev->list, &dev_list);
2361 list_for_each_entry(id_priv, &listen_any_list, list)
2362 cma_listen_on_dev(id_priv, cma_dev);
2363 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -07002364}
2365
2366static int cma_remove_id_dev(struct rdma_id_private *id_priv)
2367{
Sean Heftya1b1b612006-11-30 16:33:14 -08002368 struct rdma_cm_event event;
Sean Heftye51060f2006-06-17 20:37:29 -07002369 enum cma_state state;
2370
2371 /* Record that we want to remove the device */
2372 state = cma_exch(id_priv, CMA_DEVICE_REMOVAL);
2373 if (state == CMA_DESTROYING)
2374 return 0;
2375
2376 cma_cancel_operation(id_priv, state);
2377 wait_event(id_priv->wait_remove, !atomic_read(&id_priv->dev_remove));
2378
2379 /* Check for destruction from another callback. */
2380 if (!cma_comp(id_priv, CMA_DEVICE_REMOVAL))
2381 return 0;
2382
Sean Heftya1b1b612006-11-30 16:33:14 -08002383 memset(&event, 0, sizeof event);
2384 event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
2385 return id_priv->id.event_handler(&id_priv->id, &event);
Sean Heftye51060f2006-06-17 20:37:29 -07002386}
2387
2388static void cma_process_remove(struct cma_device *cma_dev)
2389{
Sean Heftye51060f2006-06-17 20:37:29 -07002390 struct rdma_id_private *id_priv;
2391 int ret;
2392
Sean Heftye51060f2006-06-17 20:37:29 -07002393 mutex_lock(&lock);
2394 while (!list_empty(&cma_dev->id_list)) {
2395 id_priv = list_entry(cma_dev->id_list.next,
2396 struct rdma_id_private, list);
2397
2398 if (cma_internal_listen(id_priv)) {
2399 cma_destroy_listen(id_priv);
2400 continue;
2401 }
2402
Krishna Kumar94de1782006-09-29 12:03:35 -07002403 list_del_init(&id_priv->list);
Sean Heftye51060f2006-06-17 20:37:29 -07002404 atomic_inc(&id_priv->refcount);
2405 mutex_unlock(&lock);
2406
2407 ret = cma_remove_id_dev(id_priv);
2408 cma_deref_id(id_priv);
2409 if (ret)
2410 rdma_destroy_id(&id_priv->id);
2411
2412 mutex_lock(&lock);
2413 }
2414 mutex_unlock(&lock);
2415
2416 cma_deref_dev(cma_dev);
2417 wait_for_completion(&cma_dev->comp);
2418}
2419
2420static void cma_remove_one(struct ib_device *device)
2421{
2422 struct cma_device *cma_dev;
2423
2424 cma_dev = ib_get_client_data(device, &cma_client);
2425 if (!cma_dev)
2426 return;
2427
2428 mutex_lock(&lock);
2429 list_del(&cma_dev->list);
2430 mutex_unlock(&lock);
2431
2432 cma_process_remove(cma_dev);
2433 kfree(cma_dev);
2434}
2435
2436static int cma_init(void)
2437{
2438 int ret;
2439
2440 cma_wq = create_singlethread_workqueue("rdma_cm_wq");
2441 if (!cma_wq)
2442 return -ENOMEM;
2443
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002444 ib_sa_register_client(&sa_client);
Sean Hefty7a118df2006-10-31 11:12:59 -08002445 rdma_addr_register_client(&addr_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002446
Sean Heftye51060f2006-06-17 20:37:29 -07002447 ret = ib_register_client(&cma_client);
2448 if (ret)
2449 goto err;
2450 return 0;
2451
2452err:
Sean Hefty7a118df2006-10-31 11:12:59 -08002453 rdma_addr_unregister_client(&addr_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002454 ib_sa_unregister_client(&sa_client);
Sean Heftye51060f2006-06-17 20:37:29 -07002455 destroy_workqueue(cma_wq);
2456 return ret;
2457}
2458
2459static void cma_cleanup(void)
2460{
2461 ib_unregister_client(&cma_client);
Sean Hefty7a118df2006-10-31 11:12:59 -08002462 rdma_addr_unregister_client(&addr_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002463 ib_sa_unregister_client(&sa_client);
Sean Heftye51060f2006-06-17 20:37:29 -07002464 destroy_workqueue(cma_wq);
2465 idr_destroy(&sdp_ps);
2466 idr_destroy(&tcp_ps);
Sean Hefty628e5f62006-11-30 16:44:16 -08002467 idr_destroy(&udp_ps);
Sean Heftye51060f2006-06-17 20:37:29 -07002468}
2469
2470module_init(cma_init);
2471module_exit(cma_cleanup);