Pull sony into release branch
[linux-2.6] / drivers / infiniband / core / cma.c
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  *
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>
38 #include <linux/inetdevice.h>
39
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>
47 #include <rdma/iw_cm.h>
48
49 MODULE_AUTHOR("Sean Hefty");
50 MODULE_DESCRIPTION("Generic RDMA CM Agent");
51 MODULE_LICENSE("Dual BSD/GPL");
52
53 #define CMA_CM_RESPONSE_TIMEOUT 20
54 #define CMA_MAX_CM_RETRIES 15
55
56 static void cma_add_one(struct ib_device *device);
57 static void cma_remove_one(struct ib_device *device);
58
59 static struct ib_client cma_client = {
60         .name   = "cma",
61         .add    = cma_add_one,
62         .remove = cma_remove_one
63 };
64
65 static struct ib_sa_client sa_client;
66 static struct rdma_addr_client addr_client;
67 static LIST_HEAD(dev_list);
68 static LIST_HEAD(listen_any_list);
69 static DEFINE_MUTEX(lock);
70 static struct workqueue_struct *cma_wq;
71 static DEFINE_IDR(sdp_ps);
72 static DEFINE_IDR(tcp_ps);
73 static DEFINE_IDR(udp_ps);
74 static int next_port;
75
76 struct cma_device {
77         struct list_head        list;
78         struct ib_device        *device;
79         __be64                  node_guid;
80         struct completion       comp;
81         atomic_t                refcount;
82         struct list_head        id_list;
83 };
84
85 enum cma_state {
86         CMA_IDLE,
87         CMA_ADDR_QUERY,
88         CMA_ADDR_RESOLVED,
89         CMA_ROUTE_QUERY,
90         CMA_ROUTE_RESOLVED,
91         CMA_CONNECT,
92         CMA_DISCONNECT,
93         CMA_ADDR_BOUND,
94         CMA_LISTEN,
95         CMA_DEVICE_REMOVAL,
96         CMA_DESTROYING
97 };
98
99 struct rdma_bind_list {
100         struct idr              *ps;
101         struct hlist_head       owners;
102         unsigned short          port;
103 };
104
105 /*
106  * Device removal can occur at anytime, so we need extra handling to
107  * serialize notifying the user of device removal with other callbacks.
108  * We do this by disabling removal notification while a callback is in process,
109  * and reporting it after the callback completes.
110  */
111 struct rdma_id_private {
112         struct rdma_cm_id       id;
113
114         struct rdma_bind_list   *bind_list;
115         struct hlist_node       node;
116         struct list_head        list;
117         struct list_head        listen_list;
118         struct cma_device       *cma_dev;
119
120         enum cma_state          state;
121         spinlock_t              lock;
122         struct completion       comp;
123         atomic_t                refcount;
124         wait_queue_head_t       wait_remove;
125         atomic_t                dev_remove;
126
127         int                     backlog;
128         int                     timeout_ms;
129         struct ib_sa_query      *query;
130         int                     query_id;
131         union {
132                 struct ib_cm_id *ib;
133                 struct iw_cm_id *iw;
134         } cm_id;
135
136         u32                     seq_num;
137         u32                     qp_num;
138         u8                      srq;
139 };
140
141 struct cma_work {
142         struct work_struct      work;
143         struct rdma_id_private  *id;
144         enum cma_state          old_state;
145         enum cma_state          new_state;
146         struct rdma_cm_event    event;
147 };
148
149 union cma_ip_addr {
150         struct in6_addr ip6;
151         struct {
152                 __u32 pad[3];
153                 __u32 addr;
154         } ip4;
155 };
156
157 struct cma_hdr {
158         u8 cma_version;
159         u8 ip_version;  /* IP version: 7:4 */
160         __u16 port;
161         union cma_ip_addr src_addr;
162         union cma_ip_addr dst_addr;
163 };
164
165 struct sdp_hh {
166         u8 bsdh[16];
167         u8 sdp_version; /* Major version: 7:4 */
168         u8 ip_version;  /* IP version: 7:4 */
169         u8 sdp_specific1[10];
170         __u16 port;
171         __u16 sdp_specific2;
172         union cma_ip_addr src_addr;
173         union cma_ip_addr dst_addr;
174 };
175
176 struct sdp_hah {
177         u8 bsdh[16];
178         u8 sdp_version;
179 };
180
181 #define CMA_VERSION 0x00
182 #define SDP_MAJ_VERSION 0x2
183
184 static int cma_comp(struct rdma_id_private *id_priv, enum cma_state comp)
185 {
186         unsigned long flags;
187         int ret;
188
189         spin_lock_irqsave(&id_priv->lock, flags);
190         ret = (id_priv->state == comp);
191         spin_unlock_irqrestore(&id_priv->lock, flags);
192         return ret;
193 }
194
195 static int cma_comp_exch(struct rdma_id_private *id_priv,
196                          enum cma_state comp, enum cma_state exch)
197 {
198         unsigned long flags;
199         int ret;
200
201         spin_lock_irqsave(&id_priv->lock, flags);
202         if ((ret = (id_priv->state == comp)))
203                 id_priv->state = exch;
204         spin_unlock_irqrestore(&id_priv->lock, flags);
205         return ret;
206 }
207
208 static enum cma_state cma_exch(struct rdma_id_private *id_priv,
209                                enum cma_state exch)
210 {
211         unsigned long flags;
212         enum cma_state old;
213
214         spin_lock_irqsave(&id_priv->lock, flags);
215         old = id_priv->state;
216         id_priv->state = exch;
217         spin_unlock_irqrestore(&id_priv->lock, flags);
218         return old;
219 }
220
221 static inline u8 cma_get_ip_ver(struct cma_hdr *hdr)
222 {
223         return hdr->ip_version >> 4;
224 }
225
226 static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
227 {
228         hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
229 }
230
231 static inline u8 sdp_get_majv(u8 sdp_version)
232 {
233         return sdp_version >> 4;
234 }
235
236 static inline u8 sdp_get_ip_ver(struct sdp_hh *hh)
237 {
238         return hh->ip_version >> 4;
239 }
240
241 static inline void sdp_set_ip_ver(struct sdp_hh *hh, u8 ip_ver)
242 {
243         hh->ip_version = (ip_ver << 4) | (hh->ip_version & 0xF);
244 }
245
246 static void cma_attach_to_dev(struct rdma_id_private *id_priv,
247                               struct cma_device *cma_dev)
248 {
249         atomic_inc(&cma_dev->refcount);
250         id_priv->cma_dev = cma_dev;
251         id_priv->id.device = cma_dev->device;
252         list_add_tail(&id_priv->list, &cma_dev->id_list);
253 }
254
255 static inline void cma_deref_dev(struct cma_device *cma_dev)
256 {
257         if (atomic_dec_and_test(&cma_dev->refcount))
258                 complete(&cma_dev->comp);
259 }
260
261 static void cma_detach_from_dev(struct rdma_id_private *id_priv)
262 {
263         list_del(&id_priv->list);
264         cma_deref_dev(id_priv->cma_dev);
265         id_priv->cma_dev = NULL;
266 }
267
268 static int cma_acquire_dev(struct rdma_id_private *id_priv)
269 {
270         enum rdma_node_type dev_type = id_priv->id.route.addr.dev_addr.dev_type;
271         struct cma_device *cma_dev;
272         union ib_gid gid;
273         int ret = -ENODEV;
274
275         switch (rdma_node_get_transport(dev_type)) {
276         case RDMA_TRANSPORT_IB:
277                 ib_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
278                 break;
279         case RDMA_TRANSPORT_IWARP:
280                 iw_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
281                 break;
282         default:
283                 return -ENODEV;
284         }
285
286         list_for_each_entry(cma_dev, &dev_list, list) {
287                 ret = ib_find_cached_gid(cma_dev->device, &gid,
288                                          &id_priv->id.port_num, NULL);
289                 if (!ret) {
290                         cma_attach_to_dev(id_priv, cma_dev);
291                         break;
292                 }
293         }
294         return ret;
295 }
296
297 static void cma_deref_id(struct rdma_id_private *id_priv)
298 {
299         if (atomic_dec_and_test(&id_priv->refcount))
300                 complete(&id_priv->comp);
301 }
302
303 static void cma_release_remove(struct rdma_id_private *id_priv)
304 {
305         if (atomic_dec_and_test(&id_priv->dev_remove))
306                 wake_up(&id_priv->wait_remove);
307 }
308
309 struct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler,
310                                   void *context, enum rdma_port_space ps)
311 {
312         struct rdma_id_private *id_priv;
313
314         id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
315         if (!id_priv)
316                 return ERR_PTR(-ENOMEM);
317
318         id_priv->state = CMA_IDLE;
319         id_priv->id.context = context;
320         id_priv->id.event_handler = event_handler;
321         id_priv->id.ps = ps;
322         spin_lock_init(&id_priv->lock);
323         init_completion(&id_priv->comp);
324         atomic_set(&id_priv->refcount, 1);
325         init_waitqueue_head(&id_priv->wait_remove);
326         atomic_set(&id_priv->dev_remove, 0);
327         INIT_LIST_HEAD(&id_priv->listen_list);
328         get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
329
330         return &id_priv->id;
331 }
332 EXPORT_SYMBOL(rdma_create_id);
333
334 static int cma_init_ib_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
335 {
336         struct ib_qp_attr qp_attr;
337         struct rdma_dev_addr *dev_addr;
338         int ret;
339
340         dev_addr = &id_priv->id.route.addr.dev_addr;
341         ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
342                                   ib_addr_get_pkey(dev_addr),
343                                   &qp_attr.pkey_index);
344         if (ret)
345                 return ret;
346
347         qp_attr.qp_state = IB_QPS_INIT;
348         qp_attr.qp_access_flags = 0;
349         qp_attr.port_num = id_priv->id.port_num;
350         return ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_ACCESS_FLAGS |
351                                           IB_QP_PKEY_INDEX | IB_QP_PORT);
352 }
353
354 static int cma_init_iw_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
355 {
356         struct ib_qp_attr qp_attr;
357
358         qp_attr.qp_state = IB_QPS_INIT;
359         qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE;
360
361         return ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_ACCESS_FLAGS);
362 }
363
364 int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
365                    struct ib_qp_init_attr *qp_init_attr)
366 {
367         struct rdma_id_private *id_priv;
368         struct ib_qp *qp;
369         int ret;
370
371         id_priv = container_of(id, struct rdma_id_private, id);
372         if (id->device != pd->device)
373                 return -EINVAL;
374
375         qp = ib_create_qp(pd, qp_init_attr);
376         if (IS_ERR(qp))
377                 return PTR_ERR(qp);
378
379         switch (rdma_node_get_transport(id->device->node_type)) {
380         case RDMA_TRANSPORT_IB:
381                 ret = cma_init_ib_qp(id_priv, qp);
382                 break;
383         case RDMA_TRANSPORT_IWARP:
384                 ret = cma_init_iw_qp(id_priv, qp);
385                 break;
386         default:
387                 ret = -ENOSYS;
388                 break;
389         }
390
391         if (ret)
392                 goto err;
393
394         id->qp = qp;
395         id_priv->qp_num = qp->qp_num;
396         id_priv->srq = (qp->srq != NULL);
397         return 0;
398 err:
399         ib_destroy_qp(qp);
400         return ret;
401 }
402 EXPORT_SYMBOL(rdma_create_qp);
403
404 void rdma_destroy_qp(struct rdma_cm_id *id)
405 {
406         ib_destroy_qp(id->qp);
407 }
408 EXPORT_SYMBOL(rdma_destroy_qp);
409
410 static int cma_modify_qp_rtr(struct rdma_cm_id *id)
411 {
412         struct ib_qp_attr qp_attr;
413         int qp_attr_mask, ret;
414
415         if (!id->qp)
416                 return 0;
417
418         /* Need to update QP attributes from default values. */
419         qp_attr.qp_state = IB_QPS_INIT;
420         ret = rdma_init_qp_attr(id, &qp_attr, &qp_attr_mask);
421         if (ret)
422                 return ret;
423
424         ret = ib_modify_qp(id->qp, &qp_attr, qp_attr_mask);
425         if (ret)
426                 return ret;
427
428         qp_attr.qp_state = IB_QPS_RTR;
429         ret = rdma_init_qp_attr(id, &qp_attr, &qp_attr_mask);
430         if (ret)
431                 return ret;
432
433         return ib_modify_qp(id->qp, &qp_attr, qp_attr_mask);
434 }
435
436 static int cma_modify_qp_rts(struct rdma_cm_id *id)
437 {
438         struct ib_qp_attr qp_attr;
439         int qp_attr_mask, ret;
440
441         if (!id->qp)
442                 return 0;
443
444         qp_attr.qp_state = IB_QPS_RTS;
445         ret = rdma_init_qp_attr(id, &qp_attr, &qp_attr_mask);
446         if (ret)
447                 return ret;
448
449         return ib_modify_qp(id->qp, &qp_attr, qp_attr_mask);
450 }
451
452 static int cma_modify_qp_err(struct rdma_cm_id *id)
453 {
454         struct ib_qp_attr qp_attr;
455
456         if (!id->qp)
457                 return 0;
458
459         qp_attr.qp_state = IB_QPS_ERR;
460         return ib_modify_qp(id->qp, &qp_attr, IB_QP_STATE);
461 }
462
463 int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
464                        int *qp_attr_mask)
465 {
466         struct rdma_id_private *id_priv;
467         int ret;
468
469         id_priv = container_of(id, struct rdma_id_private, id);
470         switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
471         case RDMA_TRANSPORT_IB:
472                 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
473                                          qp_attr_mask);
474                 if (qp_attr->qp_state == IB_QPS_RTR)
475                         qp_attr->rq_psn = id_priv->seq_num;
476                 break;
477         case RDMA_TRANSPORT_IWARP:
478                 ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
479                                         qp_attr_mask);
480                 break;
481         default:
482                 ret = -ENOSYS;
483                 break;
484         }
485
486         return ret;
487 }
488 EXPORT_SYMBOL(rdma_init_qp_attr);
489
490 static inline int cma_zero_addr(struct sockaddr *addr)
491 {
492         struct in6_addr *ip6;
493
494         if (addr->sa_family == AF_INET)
495                 return ZERONET(((struct sockaddr_in *) addr)->sin_addr.s_addr);
496         else {
497                 ip6 = &((struct sockaddr_in6 *) addr)->sin6_addr;
498                 return (ip6->s6_addr32[0] | ip6->s6_addr32[1] |
499                         ip6->s6_addr32[2] | ip6->s6_addr32[3]) == 0;
500         }
501 }
502
503 static inline int cma_loopback_addr(struct sockaddr *addr)
504 {
505         return LOOPBACK(((struct sockaddr_in *) addr)->sin_addr.s_addr);
506 }
507
508 static inline int cma_any_addr(struct sockaddr *addr)
509 {
510         return cma_zero_addr(addr) || cma_loopback_addr(addr);
511 }
512
513 static inline __be16 cma_port(struct sockaddr *addr)
514 {
515         if (addr->sa_family == AF_INET)
516                 return ((struct sockaddr_in *) addr)->sin_port;
517         else
518                 return ((struct sockaddr_in6 *) addr)->sin6_port;
519 }
520
521 static inline int cma_any_port(struct sockaddr *addr)
522 {
523         return !cma_port(addr);
524 }
525
526 static int cma_get_net_info(void *hdr, enum rdma_port_space ps,
527                             u8 *ip_ver, __u16 *port,
528                             union cma_ip_addr **src, union cma_ip_addr **dst)
529 {
530         switch (ps) {
531         case RDMA_PS_SDP:
532                 if (sdp_get_majv(((struct sdp_hh *) hdr)->sdp_version) !=
533                     SDP_MAJ_VERSION)
534                         return -EINVAL;
535
536                 *ip_ver = sdp_get_ip_ver(hdr);
537                 *port   = ((struct sdp_hh *) hdr)->port;
538                 *src    = &((struct sdp_hh *) hdr)->src_addr;
539                 *dst    = &((struct sdp_hh *) hdr)->dst_addr;
540                 break;
541         default:
542                 if (((struct cma_hdr *) hdr)->cma_version != CMA_VERSION)
543                         return -EINVAL;
544
545                 *ip_ver = cma_get_ip_ver(hdr);
546                 *port   = ((struct cma_hdr *) hdr)->port;
547                 *src    = &((struct cma_hdr *) hdr)->src_addr;
548                 *dst    = &((struct cma_hdr *) hdr)->dst_addr;
549                 break;
550         }
551
552         if (*ip_ver != 4 && *ip_ver != 6)
553                 return -EINVAL;
554         return 0;
555 }
556
557 static void cma_save_net_info(struct rdma_addr *addr,
558                               struct rdma_addr *listen_addr,
559                               u8 ip_ver, __u16 port,
560                               union cma_ip_addr *src, union cma_ip_addr *dst)
561 {
562         struct sockaddr_in *listen4, *ip4;
563         struct sockaddr_in6 *listen6, *ip6;
564
565         switch (ip_ver) {
566         case 4:
567                 listen4 = (struct sockaddr_in *) &listen_addr->src_addr;
568                 ip4 = (struct sockaddr_in *) &addr->src_addr;
569                 ip4->sin_family = listen4->sin_family;
570                 ip4->sin_addr.s_addr = dst->ip4.addr;
571                 ip4->sin_port = listen4->sin_port;
572
573                 ip4 = (struct sockaddr_in *) &addr->dst_addr;
574                 ip4->sin_family = listen4->sin_family;
575                 ip4->sin_addr.s_addr = src->ip4.addr;
576                 ip4->sin_port = port;
577                 break;
578         case 6:
579                 listen6 = (struct sockaddr_in6 *) &listen_addr->src_addr;
580                 ip6 = (struct sockaddr_in6 *) &addr->src_addr;
581                 ip6->sin6_family = listen6->sin6_family;
582                 ip6->sin6_addr = dst->ip6;
583                 ip6->sin6_port = listen6->sin6_port;
584
585                 ip6 = (struct sockaddr_in6 *) &addr->dst_addr;
586                 ip6->sin6_family = listen6->sin6_family;
587                 ip6->sin6_addr = src->ip6;
588                 ip6->sin6_port = port;
589                 break;
590         default:
591                 break;
592         }
593 }
594
595 static inline int cma_user_data_offset(enum rdma_port_space ps)
596 {
597         switch (ps) {
598         case RDMA_PS_SDP:
599                 return 0;
600         default:
601                 return sizeof(struct cma_hdr);
602         }
603 }
604
605 static void cma_cancel_route(struct rdma_id_private *id_priv)
606 {
607         switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
608         case RDMA_TRANSPORT_IB:
609                 if (id_priv->query)
610                         ib_sa_cancel_query(id_priv->query_id, id_priv->query);
611                 break;
612         default:
613                 break;
614         }
615 }
616
617 static inline int cma_internal_listen(struct rdma_id_private *id_priv)
618 {
619         return (id_priv->state == CMA_LISTEN) && id_priv->cma_dev &&
620                cma_any_addr(&id_priv->id.route.addr.src_addr);
621 }
622
623 static void cma_destroy_listen(struct rdma_id_private *id_priv)
624 {
625         cma_exch(id_priv, CMA_DESTROYING);
626
627         if (id_priv->cma_dev) {
628                 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
629                 case RDMA_TRANSPORT_IB:
630                         if (id_priv->cm_id.ib && !IS_ERR(id_priv->cm_id.ib))
631                                 ib_destroy_cm_id(id_priv->cm_id.ib);
632                         break;
633                 case RDMA_TRANSPORT_IWARP:
634                         if (id_priv->cm_id.iw && !IS_ERR(id_priv->cm_id.iw))
635                                 iw_destroy_cm_id(id_priv->cm_id.iw);
636                         break;
637                 default:
638                         break;
639                 }
640                 cma_detach_from_dev(id_priv);
641         }
642         list_del(&id_priv->listen_list);
643
644         cma_deref_id(id_priv);
645         wait_for_completion(&id_priv->comp);
646
647         kfree(id_priv);
648 }
649
650 static void cma_cancel_listens(struct rdma_id_private *id_priv)
651 {
652         struct rdma_id_private *dev_id_priv;
653
654         mutex_lock(&lock);
655         list_del(&id_priv->list);
656
657         while (!list_empty(&id_priv->listen_list)) {
658                 dev_id_priv = list_entry(id_priv->listen_list.next,
659                                          struct rdma_id_private, listen_list);
660                 cma_destroy_listen(dev_id_priv);
661         }
662         mutex_unlock(&lock);
663 }
664
665 static void cma_cancel_operation(struct rdma_id_private *id_priv,
666                                  enum cma_state state)
667 {
668         switch (state) {
669         case CMA_ADDR_QUERY:
670                 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
671                 break;
672         case CMA_ROUTE_QUERY:
673                 cma_cancel_route(id_priv);
674                 break;
675         case CMA_LISTEN:
676                 if (cma_any_addr(&id_priv->id.route.addr.src_addr) &&
677                     !id_priv->cma_dev)
678                         cma_cancel_listens(id_priv);
679                 break;
680         default:
681                 break;
682         }
683 }
684
685 static void cma_release_port(struct rdma_id_private *id_priv)
686 {
687         struct rdma_bind_list *bind_list = id_priv->bind_list;
688
689         if (!bind_list)
690                 return;
691
692         mutex_lock(&lock);
693         hlist_del(&id_priv->node);
694         if (hlist_empty(&bind_list->owners)) {
695                 idr_remove(bind_list->ps, bind_list->port);
696                 kfree(bind_list);
697         }
698         mutex_unlock(&lock);
699 }
700
701 void rdma_destroy_id(struct rdma_cm_id *id)
702 {
703         struct rdma_id_private *id_priv;
704         enum cma_state state;
705
706         id_priv = container_of(id, struct rdma_id_private, id);
707         state = cma_exch(id_priv, CMA_DESTROYING);
708         cma_cancel_operation(id_priv, state);
709
710         mutex_lock(&lock);
711         if (id_priv->cma_dev) {
712                 mutex_unlock(&lock);
713                 switch (rdma_node_get_transport(id->device->node_type)) {
714                 case RDMA_TRANSPORT_IB:
715                         if (id_priv->cm_id.ib && !IS_ERR(id_priv->cm_id.ib))
716                                 ib_destroy_cm_id(id_priv->cm_id.ib);
717                         break;
718                 case RDMA_TRANSPORT_IWARP:
719                         if (id_priv->cm_id.iw && !IS_ERR(id_priv->cm_id.iw))
720                                 iw_destroy_cm_id(id_priv->cm_id.iw);
721                         break;
722                 default:
723                         break;
724                 }
725                 mutex_lock(&lock);
726                 cma_detach_from_dev(id_priv);
727         }
728         mutex_unlock(&lock);
729
730         cma_release_port(id_priv);
731         cma_deref_id(id_priv);
732         wait_for_completion(&id_priv->comp);
733
734         kfree(id_priv->id.route.path_rec);
735         kfree(id_priv);
736 }
737 EXPORT_SYMBOL(rdma_destroy_id);
738
739 static int cma_rep_recv(struct rdma_id_private *id_priv)
740 {
741         int ret;
742
743         ret = cma_modify_qp_rtr(&id_priv->id);
744         if (ret)
745                 goto reject;
746
747         ret = cma_modify_qp_rts(&id_priv->id);
748         if (ret)
749                 goto reject;
750
751         ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
752         if (ret)
753                 goto reject;
754
755         return 0;
756 reject:
757         cma_modify_qp_err(&id_priv->id);
758         ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
759                        NULL, 0, NULL, 0);
760         return ret;
761 }
762
763 static int cma_verify_rep(struct rdma_id_private *id_priv, void *data)
764 {
765         if (id_priv->id.ps == RDMA_PS_SDP &&
766             sdp_get_majv(((struct sdp_hah *) data)->sdp_version) !=
767             SDP_MAJ_VERSION)
768                 return -EINVAL;
769
770         return 0;
771 }
772
773 static void cma_set_rep_event_data(struct rdma_cm_event *event,
774                                    struct ib_cm_rep_event_param *rep_data,
775                                    void *private_data)
776 {
777         event->param.conn.private_data = private_data;
778         event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
779         event->param.conn.responder_resources = rep_data->responder_resources;
780         event->param.conn.initiator_depth = rep_data->initiator_depth;
781         event->param.conn.flow_control = rep_data->flow_control;
782         event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
783         event->param.conn.srq = rep_data->srq;
784         event->param.conn.qp_num = rep_data->remote_qpn;
785 }
786
787 static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
788 {
789         struct rdma_id_private *id_priv = cm_id->context;
790         struct rdma_cm_event event;
791         int ret = 0;
792
793         atomic_inc(&id_priv->dev_remove);
794         if (!cma_comp(id_priv, CMA_CONNECT))
795                 goto out;
796
797         memset(&event, 0, sizeof event);
798         switch (ib_event->event) {
799         case IB_CM_REQ_ERROR:
800         case IB_CM_REP_ERROR:
801                 event.event = RDMA_CM_EVENT_UNREACHABLE;
802                 event.status = -ETIMEDOUT;
803                 break;
804         case IB_CM_REP_RECEIVED:
805                 event.status = cma_verify_rep(id_priv, ib_event->private_data);
806                 if (event.status)
807                         event.event = RDMA_CM_EVENT_CONNECT_ERROR;
808                 else if (id_priv->id.qp && id_priv->id.ps != RDMA_PS_SDP) {
809                         event.status = cma_rep_recv(id_priv);
810                         event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
811                                                      RDMA_CM_EVENT_ESTABLISHED;
812                 } else
813                         event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
814                 cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
815                                        ib_event->private_data);
816                 break;
817         case IB_CM_RTU_RECEIVED:
818         case IB_CM_USER_ESTABLISHED:
819                 event.event = RDMA_CM_EVENT_ESTABLISHED;
820                 break;
821         case IB_CM_DREQ_ERROR:
822                 event.status = -ETIMEDOUT; /* fall through */
823         case IB_CM_DREQ_RECEIVED:
824         case IB_CM_DREP_RECEIVED:
825                 if (!cma_comp_exch(id_priv, CMA_CONNECT, CMA_DISCONNECT))
826                         goto out;
827                 event.event = RDMA_CM_EVENT_DISCONNECTED;
828                 break;
829         case IB_CM_TIMEWAIT_EXIT:
830         case IB_CM_MRA_RECEIVED:
831                 /* ignore event */
832                 goto out;
833         case IB_CM_REJ_RECEIVED:
834                 cma_modify_qp_err(&id_priv->id);
835                 event.status = ib_event->param.rej_rcvd.reason;
836                 event.event = RDMA_CM_EVENT_REJECTED;
837                 event.param.conn.private_data = ib_event->private_data;
838                 event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
839                 break;
840         default:
841                 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d",
842                        ib_event->event);
843                 goto out;
844         }
845
846         ret = id_priv->id.event_handler(&id_priv->id, &event);
847         if (ret) {
848                 /* Destroy the CM ID by returning a non-zero value. */
849                 id_priv->cm_id.ib = NULL;
850                 cma_exch(id_priv, CMA_DESTROYING);
851                 cma_release_remove(id_priv);
852                 rdma_destroy_id(&id_priv->id);
853                 return ret;
854         }
855 out:
856         cma_release_remove(id_priv);
857         return ret;
858 }
859
860 static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
861                                                struct ib_cm_event *ib_event)
862 {
863         struct rdma_id_private *id_priv;
864         struct rdma_cm_id *id;
865         struct rdma_route *rt;
866         union cma_ip_addr *src, *dst;
867         __u16 port;
868         u8 ip_ver;
869
870         if (cma_get_net_info(ib_event->private_data, listen_id->ps,
871                              &ip_ver, &port, &src, &dst))
872                 goto err;
873
874         id = rdma_create_id(listen_id->event_handler, listen_id->context,
875                             listen_id->ps);
876         if (IS_ERR(id))
877                 goto err;
878
879         cma_save_net_info(&id->route.addr, &listen_id->route.addr,
880                           ip_ver, port, src, dst);
881
882         rt = &id->route;
883         rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
884         rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
885                                GFP_KERNEL);
886         if (!rt->path_rec)
887                 goto destroy_id;
888
889         rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
890         if (rt->num_paths == 2)
891                 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
892
893         ib_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
894         ib_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
895         ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
896         rt->addr.dev_addr.dev_type = RDMA_NODE_IB_CA;
897
898         id_priv = container_of(id, struct rdma_id_private, id);
899         id_priv->state = CMA_CONNECT;
900         return id_priv;
901
902 destroy_id:
903         rdma_destroy_id(id);
904 err:
905         return NULL;
906 }
907
908 static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
909                                               struct ib_cm_event *ib_event)
910 {
911         struct rdma_id_private *id_priv;
912         struct rdma_cm_id *id;
913         union cma_ip_addr *src, *dst;
914         __u16 port;
915         u8 ip_ver;
916         int ret;
917
918         id = rdma_create_id(listen_id->event_handler, listen_id->context,
919                             listen_id->ps);
920         if (IS_ERR(id))
921                 return NULL;
922
923
924         if (cma_get_net_info(ib_event->private_data, listen_id->ps,
925                              &ip_ver, &port, &src, &dst))
926                 goto err;
927
928         cma_save_net_info(&id->route.addr, &listen_id->route.addr,
929                           ip_ver, port, src, dst);
930
931         ret = rdma_translate_ip(&id->route.addr.src_addr,
932                                 &id->route.addr.dev_addr);
933         if (ret)
934                 goto err;
935
936         id_priv = container_of(id, struct rdma_id_private, id);
937         id_priv->state = CMA_CONNECT;
938         return id_priv;
939 err:
940         rdma_destroy_id(id);
941         return NULL;
942 }
943
944 static void cma_set_req_event_data(struct rdma_cm_event *event,
945                                    struct ib_cm_req_event_param *req_data,
946                                    void *private_data, int offset)
947 {
948         event->param.conn.private_data = private_data + offset;
949         event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
950         event->param.conn.responder_resources = req_data->responder_resources;
951         event->param.conn.initiator_depth = req_data->initiator_depth;
952         event->param.conn.flow_control = req_data->flow_control;
953         event->param.conn.retry_count = req_data->retry_count;
954         event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
955         event->param.conn.srq = req_data->srq;
956         event->param.conn.qp_num = req_data->remote_qpn;
957 }
958
959 static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
960 {
961         struct rdma_id_private *listen_id, *conn_id;
962         struct rdma_cm_event event;
963         int offset, ret;
964
965         listen_id = cm_id->context;
966         atomic_inc(&listen_id->dev_remove);
967         if (!cma_comp(listen_id, CMA_LISTEN)) {
968                 ret = -ECONNABORTED;
969                 goto out;
970         }
971
972         memset(&event, 0, sizeof event);
973         offset = cma_user_data_offset(listen_id->id.ps);
974         event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
975         if (listen_id->id.ps == RDMA_PS_UDP) {
976                 conn_id = cma_new_udp_id(&listen_id->id, ib_event);
977                 event.param.ud.private_data = ib_event->private_data + offset;
978                 event.param.ud.private_data_len =
979                                 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
980         } else {
981                 conn_id = cma_new_conn_id(&listen_id->id, ib_event);
982                 cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
983                                        ib_event->private_data, offset);
984         }
985         if (!conn_id) {
986                 ret = -ENOMEM;
987                 goto out;
988         }
989
990         atomic_inc(&conn_id->dev_remove);
991         mutex_lock(&lock);
992         ret = cma_acquire_dev(conn_id);
993         mutex_unlock(&lock);
994         if (ret)
995                 goto release_conn_id;
996
997         conn_id->cm_id.ib = cm_id;
998         cm_id->context = conn_id;
999         cm_id->cm_handler = cma_ib_handler;
1000
1001         ret = conn_id->id.event_handler(&conn_id->id, &event);
1002         if (!ret)
1003                 goto out;
1004
1005         /* Destroy the CM ID by returning a non-zero value. */
1006         conn_id->cm_id.ib = NULL;
1007
1008 release_conn_id:
1009         cma_exch(conn_id, CMA_DESTROYING);
1010         cma_release_remove(conn_id);
1011         rdma_destroy_id(&conn_id->id);
1012
1013 out:
1014         cma_release_remove(listen_id);
1015         return ret;
1016 }
1017
1018 static __be64 cma_get_service_id(enum rdma_port_space ps, struct sockaddr *addr)
1019 {
1020         return cpu_to_be64(((u64)ps << 16) + be16_to_cpu(cma_port(addr)));
1021 }
1022
1023 static void cma_set_compare_data(enum rdma_port_space ps, struct sockaddr *addr,
1024                                  struct ib_cm_compare_data *compare)
1025 {
1026         struct cma_hdr *cma_data, *cma_mask;
1027         struct sdp_hh *sdp_data, *sdp_mask;
1028         __u32 ip4_addr;
1029         struct in6_addr ip6_addr;
1030
1031         memset(compare, 0, sizeof *compare);
1032         cma_data = (void *) compare->data;
1033         cma_mask = (void *) compare->mask;
1034         sdp_data = (void *) compare->data;
1035         sdp_mask = (void *) compare->mask;
1036
1037         switch (addr->sa_family) {
1038         case AF_INET:
1039                 ip4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1040                 if (ps == RDMA_PS_SDP) {
1041                         sdp_set_ip_ver(sdp_data, 4);
1042                         sdp_set_ip_ver(sdp_mask, 0xF);
1043                         sdp_data->dst_addr.ip4.addr = ip4_addr;
1044                         sdp_mask->dst_addr.ip4.addr = ~0;
1045                 } else {
1046                         cma_set_ip_ver(cma_data, 4);
1047                         cma_set_ip_ver(cma_mask, 0xF);
1048                         cma_data->dst_addr.ip4.addr = ip4_addr;
1049                         cma_mask->dst_addr.ip4.addr = ~0;
1050                 }
1051                 break;
1052         case AF_INET6:
1053                 ip6_addr = ((struct sockaddr_in6 *) addr)->sin6_addr;
1054                 if (ps == RDMA_PS_SDP) {
1055                         sdp_set_ip_ver(sdp_data, 6);
1056                         sdp_set_ip_ver(sdp_mask, 0xF);
1057                         sdp_data->dst_addr.ip6 = ip6_addr;
1058                         memset(&sdp_mask->dst_addr.ip6, 0xFF,
1059                                sizeof sdp_mask->dst_addr.ip6);
1060                 } else {
1061                         cma_set_ip_ver(cma_data, 6);
1062                         cma_set_ip_ver(cma_mask, 0xF);
1063                         cma_data->dst_addr.ip6 = ip6_addr;
1064                         memset(&cma_mask->dst_addr.ip6, 0xFF,
1065                                sizeof cma_mask->dst_addr.ip6);
1066                 }
1067                 break;
1068         default:
1069                 break;
1070         }
1071 }
1072
1073 static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
1074 {
1075         struct rdma_id_private *id_priv = iw_id->context;
1076         struct rdma_cm_event event;
1077         struct sockaddr_in *sin;
1078         int ret = 0;
1079
1080         memset(&event, 0, sizeof event);
1081         atomic_inc(&id_priv->dev_remove);
1082
1083         switch (iw_event->event) {
1084         case IW_CM_EVENT_CLOSE:
1085                 event.event = RDMA_CM_EVENT_DISCONNECTED;
1086                 break;
1087         case IW_CM_EVENT_CONNECT_REPLY:
1088                 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1089                 *sin = iw_event->local_addr;
1090                 sin = (struct sockaddr_in *) &id_priv->id.route.addr.dst_addr;
1091                 *sin = iw_event->remote_addr;
1092                 switch (iw_event->status) {
1093                 case 0:
1094                         event.event = RDMA_CM_EVENT_ESTABLISHED;
1095                         break;
1096                 case -ECONNRESET:
1097                 case -ECONNREFUSED:
1098                         event.event = RDMA_CM_EVENT_REJECTED;
1099                         break;
1100                 case -ETIMEDOUT:
1101                         event.event = RDMA_CM_EVENT_UNREACHABLE;
1102                         break;
1103                 default:
1104                         event.event = RDMA_CM_EVENT_CONNECT_ERROR;
1105                         break;
1106                 }
1107                 break;
1108         case IW_CM_EVENT_ESTABLISHED:
1109                 event.event = RDMA_CM_EVENT_ESTABLISHED;
1110                 break;
1111         default:
1112                 BUG_ON(1);
1113         }
1114
1115         event.status = iw_event->status;
1116         event.param.conn.private_data = iw_event->private_data;
1117         event.param.conn.private_data_len = iw_event->private_data_len;
1118         ret = id_priv->id.event_handler(&id_priv->id, &event);
1119         if (ret) {
1120                 /* Destroy the CM ID by returning a non-zero value. */
1121                 id_priv->cm_id.iw = NULL;
1122                 cma_exch(id_priv, CMA_DESTROYING);
1123                 cma_release_remove(id_priv);
1124                 rdma_destroy_id(&id_priv->id);
1125                 return ret;
1126         }
1127
1128         cma_release_remove(id_priv);
1129         return ret;
1130 }
1131
1132 static int iw_conn_req_handler(struct iw_cm_id *cm_id,
1133                                struct iw_cm_event *iw_event)
1134 {
1135         struct rdma_cm_id *new_cm_id;
1136         struct rdma_id_private *listen_id, *conn_id;
1137         struct sockaddr_in *sin;
1138         struct net_device *dev = NULL;
1139         struct rdma_cm_event event;
1140         int ret;
1141
1142         listen_id = cm_id->context;
1143         atomic_inc(&listen_id->dev_remove);
1144         if (!cma_comp(listen_id, CMA_LISTEN)) {
1145                 ret = -ECONNABORTED;
1146                 goto out;
1147         }
1148
1149         /* Create a new RDMA id for the new IW CM ID */
1150         new_cm_id = rdma_create_id(listen_id->id.event_handler,
1151                                    listen_id->id.context,
1152                                    RDMA_PS_TCP);
1153         if (!new_cm_id) {
1154                 ret = -ENOMEM;
1155                 goto out;
1156         }
1157         conn_id = container_of(new_cm_id, struct rdma_id_private, id);
1158         atomic_inc(&conn_id->dev_remove);
1159         conn_id->state = CMA_CONNECT;
1160
1161         dev = ip_dev_find(iw_event->local_addr.sin_addr.s_addr);
1162         if (!dev) {
1163                 ret = -EADDRNOTAVAIL;
1164                 cma_release_remove(conn_id);
1165                 rdma_destroy_id(new_cm_id);
1166                 goto out;
1167         }
1168         ret = rdma_copy_addr(&conn_id->id.route.addr.dev_addr, dev, NULL);
1169         if (ret) {
1170                 cma_release_remove(conn_id);
1171                 rdma_destroy_id(new_cm_id);
1172                 goto out;
1173         }
1174
1175         mutex_lock(&lock);
1176         ret = cma_acquire_dev(conn_id);
1177         mutex_unlock(&lock);
1178         if (ret) {
1179                 cma_release_remove(conn_id);
1180                 rdma_destroy_id(new_cm_id);
1181                 goto out;
1182         }
1183
1184         conn_id->cm_id.iw = cm_id;
1185         cm_id->context = conn_id;
1186         cm_id->cm_handler = cma_iw_handler;
1187
1188         sin = (struct sockaddr_in *) &new_cm_id->route.addr.src_addr;
1189         *sin = iw_event->local_addr;
1190         sin = (struct sockaddr_in *) &new_cm_id->route.addr.dst_addr;
1191         *sin = iw_event->remote_addr;
1192
1193         memset(&event, 0, sizeof event);
1194         event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1195         event.param.conn.private_data = iw_event->private_data;
1196         event.param.conn.private_data_len = iw_event->private_data_len;
1197         ret = conn_id->id.event_handler(&conn_id->id, &event);
1198         if (ret) {
1199                 /* User wants to destroy the CM ID */
1200                 conn_id->cm_id.iw = NULL;
1201                 cma_exch(conn_id, CMA_DESTROYING);
1202                 cma_release_remove(conn_id);
1203                 rdma_destroy_id(&conn_id->id);
1204         }
1205
1206 out:
1207         if (dev)
1208                 dev_put(dev);
1209         cma_release_remove(listen_id);
1210         return ret;
1211 }
1212
1213 static int cma_ib_listen(struct rdma_id_private *id_priv)
1214 {
1215         struct ib_cm_compare_data compare_data;
1216         struct sockaddr *addr;
1217         __be64 svc_id;
1218         int ret;
1219
1220         id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_req_handler,
1221                                             id_priv);
1222         if (IS_ERR(id_priv->cm_id.ib))
1223                 return PTR_ERR(id_priv->cm_id.ib);
1224
1225         addr = &id_priv->id.route.addr.src_addr;
1226         svc_id = cma_get_service_id(id_priv->id.ps, addr);
1227         if (cma_any_addr(addr))
1228                 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, NULL);
1229         else {
1230                 cma_set_compare_data(id_priv->id.ps, addr, &compare_data);
1231                 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, &compare_data);
1232         }
1233
1234         if (ret) {
1235                 ib_destroy_cm_id(id_priv->cm_id.ib);
1236                 id_priv->cm_id.ib = NULL;
1237         }
1238
1239         return ret;
1240 }
1241
1242 static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
1243 {
1244         int ret;
1245         struct sockaddr_in *sin;
1246
1247         id_priv->cm_id.iw = iw_create_cm_id(id_priv->id.device,
1248                                             iw_conn_req_handler,
1249                                             id_priv);
1250         if (IS_ERR(id_priv->cm_id.iw))
1251                 return PTR_ERR(id_priv->cm_id.iw);
1252
1253         sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1254         id_priv->cm_id.iw->local_addr = *sin;
1255
1256         ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
1257
1258         if (ret) {
1259                 iw_destroy_cm_id(id_priv->cm_id.iw);
1260                 id_priv->cm_id.iw = NULL;
1261         }
1262
1263         return ret;
1264 }
1265
1266 static int cma_listen_handler(struct rdma_cm_id *id,
1267                               struct rdma_cm_event *event)
1268 {
1269         struct rdma_id_private *id_priv = id->context;
1270
1271         id->context = id_priv->id.context;
1272         id->event_handler = id_priv->id.event_handler;
1273         return id_priv->id.event_handler(id, event);
1274 }
1275
1276 static void cma_listen_on_dev(struct rdma_id_private *id_priv,
1277                               struct cma_device *cma_dev)
1278 {
1279         struct rdma_id_private *dev_id_priv;
1280         struct rdma_cm_id *id;
1281         int ret;
1282
1283         id = rdma_create_id(cma_listen_handler, id_priv, id_priv->id.ps);
1284         if (IS_ERR(id))
1285                 return;
1286
1287         dev_id_priv = container_of(id, struct rdma_id_private, id);
1288
1289         dev_id_priv->state = CMA_ADDR_BOUND;
1290         memcpy(&id->route.addr.src_addr, &id_priv->id.route.addr.src_addr,
1291                ip_addr_size(&id_priv->id.route.addr.src_addr));
1292
1293         cma_attach_to_dev(dev_id_priv, cma_dev);
1294         list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
1295
1296         ret = rdma_listen(id, id_priv->backlog);
1297         if (ret)
1298                 goto err;
1299
1300         return;
1301 err:
1302         cma_destroy_listen(dev_id_priv);
1303 }
1304
1305 static void cma_listen_on_all(struct rdma_id_private *id_priv)
1306 {
1307         struct cma_device *cma_dev;
1308
1309         mutex_lock(&lock);
1310         list_add_tail(&id_priv->list, &listen_any_list);
1311         list_for_each_entry(cma_dev, &dev_list, list)
1312                 cma_listen_on_dev(id_priv, cma_dev);
1313         mutex_unlock(&lock);
1314 }
1315
1316 static int cma_bind_any(struct rdma_cm_id *id, sa_family_t af)
1317 {
1318         struct sockaddr_in addr_in;
1319
1320         memset(&addr_in, 0, sizeof addr_in);
1321         addr_in.sin_family = af;
1322         return rdma_bind_addr(id, (struct sockaddr *) &addr_in);
1323 }
1324
1325 int rdma_listen(struct rdma_cm_id *id, int backlog)
1326 {
1327         struct rdma_id_private *id_priv;
1328         int ret;
1329
1330         id_priv = container_of(id, struct rdma_id_private, id);
1331         if (id_priv->state == CMA_IDLE) {
1332                 ret = cma_bind_any(id, AF_INET);
1333                 if (ret)
1334                         return ret;
1335         }
1336
1337         if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_LISTEN))
1338                 return -EINVAL;
1339
1340         id_priv->backlog = backlog;
1341         if (id->device) {
1342                 switch (rdma_node_get_transport(id->device->node_type)) {
1343                 case RDMA_TRANSPORT_IB:
1344                         ret = cma_ib_listen(id_priv);
1345                         if (ret)
1346                                 goto err;
1347                         break;
1348                 case RDMA_TRANSPORT_IWARP:
1349                         ret = cma_iw_listen(id_priv, backlog);
1350                         if (ret)
1351                                 goto err;
1352                         break;
1353                 default:
1354                         ret = -ENOSYS;
1355                         goto err;
1356                 }
1357         } else
1358                 cma_listen_on_all(id_priv);
1359
1360         return 0;
1361 err:
1362         id_priv->backlog = 0;
1363         cma_comp_exch(id_priv, CMA_LISTEN, CMA_ADDR_BOUND);
1364         return ret;
1365 }
1366 EXPORT_SYMBOL(rdma_listen);
1367
1368 static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
1369                               void *context)
1370 {
1371         struct cma_work *work = context;
1372         struct rdma_route *route;
1373
1374         route = &work->id->id.route;
1375
1376         if (!status) {
1377                 route->num_paths = 1;
1378                 *route->path_rec = *path_rec;
1379         } else {
1380                 work->old_state = CMA_ROUTE_QUERY;
1381                 work->new_state = CMA_ADDR_RESOLVED;
1382                 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
1383                 work->event.status = status;
1384         }
1385
1386         queue_work(cma_wq, &work->work);
1387 }
1388
1389 static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
1390                               struct cma_work *work)
1391 {
1392         struct rdma_dev_addr *addr = &id_priv->id.route.addr.dev_addr;
1393         struct ib_sa_path_rec path_rec;
1394
1395         memset(&path_rec, 0, sizeof path_rec);
1396         ib_addr_get_sgid(addr, &path_rec.sgid);
1397         ib_addr_get_dgid(addr, &path_rec.dgid);
1398         path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(addr));
1399         path_rec.numb_path = 1;
1400
1401         id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
1402                                 id_priv->id.port_num, &path_rec,
1403                                 IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
1404                                 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH,
1405                                 timeout_ms, GFP_KERNEL,
1406                                 cma_query_handler, work, &id_priv->query);
1407
1408         return (id_priv->query_id < 0) ? id_priv->query_id : 0;
1409 }
1410
1411 static void cma_work_handler(struct work_struct *_work)
1412 {
1413         struct cma_work *work = container_of(_work, struct cma_work, work);
1414         struct rdma_id_private *id_priv = work->id;
1415         int destroy = 0;
1416
1417         atomic_inc(&id_priv->dev_remove);
1418         if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
1419                 goto out;
1420
1421         if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
1422                 cma_exch(id_priv, CMA_DESTROYING);
1423                 destroy = 1;
1424         }
1425 out:
1426         cma_release_remove(id_priv);
1427         cma_deref_id(id_priv);
1428         if (destroy)
1429                 rdma_destroy_id(&id_priv->id);
1430         kfree(work);
1431 }
1432
1433 static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
1434 {
1435         struct rdma_route *route = &id_priv->id.route;
1436         struct cma_work *work;
1437         int ret;
1438
1439         work = kzalloc(sizeof *work, GFP_KERNEL);
1440         if (!work)
1441                 return -ENOMEM;
1442
1443         work->id = id_priv;
1444         INIT_WORK(&work->work, cma_work_handler);
1445         work->old_state = CMA_ROUTE_QUERY;
1446         work->new_state = CMA_ROUTE_RESOLVED;
1447         work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1448
1449         route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
1450         if (!route->path_rec) {
1451                 ret = -ENOMEM;
1452                 goto err1;
1453         }
1454
1455         ret = cma_query_ib_route(id_priv, timeout_ms, work);
1456         if (ret)
1457                 goto err2;
1458
1459         return 0;
1460 err2:
1461         kfree(route->path_rec);
1462         route->path_rec = NULL;
1463 err1:
1464         kfree(work);
1465         return ret;
1466 }
1467
1468 int rdma_set_ib_paths(struct rdma_cm_id *id,
1469                       struct ib_sa_path_rec *path_rec, int num_paths)
1470 {
1471         struct rdma_id_private *id_priv;
1472         int ret;
1473
1474         id_priv = container_of(id, struct rdma_id_private, id);
1475         if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ROUTE_RESOLVED))
1476                 return -EINVAL;
1477
1478         id->route.path_rec = kmalloc(sizeof *path_rec * num_paths, GFP_KERNEL);
1479         if (!id->route.path_rec) {
1480                 ret = -ENOMEM;
1481                 goto err;
1482         }
1483
1484         memcpy(id->route.path_rec, path_rec, sizeof *path_rec * num_paths);
1485         return 0;
1486 err:
1487         cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_ADDR_RESOLVED);
1488         return ret;
1489 }
1490 EXPORT_SYMBOL(rdma_set_ib_paths);
1491
1492 static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
1493 {
1494         struct cma_work *work;
1495
1496         work = kzalloc(sizeof *work, GFP_KERNEL);
1497         if (!work)
1498                 return -ENOMEM;
1499
1500         work->id = id_priv;
1501         INIT_WORK(&work->work, cma_work_handler);
1502         work->old_state = CMA_ROUTE_QUERY;
1503         work->new_state = CMA_ROUTE_RESOLVED;
1504         work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1505         queue_work(cma_wq, &work->work);
1506         return 0;
1507 }
1508
1509 int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
1510 {
1511         struct rdma_id_private *id_priv;
1512         int ret;
1513
1514         id_priv = container_of(id, struct rdma_id_private, id);
1515         if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ROUTE_QUERY))
1516                 return -EINVAL;
1517
1518         atomic_inc(&id_priv->refcount);
1519         switch (rdma_node_get_transport(id->device->node_type)) {
1520         case RDMA_TRANSPORT_IB:
1521                 ret = cma_resolve_ib_route(id_priv, timeout_ms);
1522                 break;
1523         case RDMA_TRANSPORT_IWARP:
1524                 ret = cma_resolve_iw_route(id_priv, timeout_ms);
1525                 break;
1526         default:
1527                 ret = -ENOSYS;
1528                 break;
1529         }
1530         if (ret)
1531                 goto err;
1532
1533         return 0;
1534 err:
1535         cma_comp_exch(id_priv, CMA_ROUTE_QUERY, CMA_ADDR_RESOLVED);
1536         cma_deref_id(id_priv);
1537         return ret;
1538 }
1539 EXPORT_SYMBOL(rdma_resolve_route);
1540
1541 static int cma_bind_loopback(struct rdma_id_private *id_priv)
1542 {
1543         struct cma_device *cma_dev;
1544         struct ib_port_attr port_attr;
1545         union ib_gid gid;
1546         u16 pkey;
1547         int ret;
1548         u8 p;
1549
1550         mutex_lock(&lock);
1551         if (list_empty(&dev_list)) {
1552                 ret = -ENODEV;
1553                 goto out;
1554         }
1555         list_for_each_entry(cma_dev, &dev_list, list)
1556                 for (p = 1; p <= cma_dev->device->phys_port_cnt; ++p)
1557                         if (!ib_query_port(cma_dev->device, p, &port_attr) &&
1558                             port_attr.state == IB_PORT_ACTIVE)
1559                                 goto port_found;
1560
1561         p = 1;
1562         cma_dev = list_entry(dev_list.next, struct cma_device, list);
1563
1564 port_found:
1565         ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid);
1566         if (ret)
1567                 goto out;
1568
1569         ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
1570         if (ret)
1571                 goto out;
1572
1573         ib_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
1574         ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
1575         id_priv->id.port_num = p;
1576         cma_attach_to_dev(id_priv, cma_dev);
1577 out:
1578         mutex_unlock(&lock);
1579         return ret;
1580 }
1581
1582 static void addr_handler(int status, struct sockaddr *src_addr,
1583                          struct rdma_dev_addr *dev_addr, void *context)
1584 {
1585         struct rdma_id_private *id_priv = context;
1586         struct rdma_cm_event event;
1587
1588         memset(&event, 0, sizeof event);
1589         atomic_inc(&id_priv->dev_remove);
1590
1591         /*
1592          * Grab mutex to block rdma_destroy_id() from removing the device while
1593          * we're trying to acquire it.
1594          */
1595         mutex_lock(&lock);
1596         if (!cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_RESOLVED)) {
1597                 mutex_unlock(&lock);
1598                 goto out;
1599         }
1600
1601         if (!status && !id_priv->cma_dev)
1602                 status = cma_acquire_dev(id_priv);
1603         mutex_unlock(&lock);
1604
1605         if (status) {
1606                 if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ADDR_BOUND))
1607                         goto out;
1608                 event.event = RDMA_CM_EVENT_ADDR_ERROR;
1609                 event.status = status;
1610         } else {
1611                 memcpy(&id_priv->id.route.addr.src_addr, src_addr,
1612                        ip_addr_size(src_addr));
1613                 event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
1614         }
1615
1616         if (id_priv->id.event_handler(&id_priv->id, &event)) {
1617                 cma_exch(id_priv, CMA_DESTROYING);
1618                 cma_release_remove(id_priv);
1619                 cma_deref_id(id_priv);
1620                 rdma_destroy_id(&id_priv->id);
1621                 return;
1622         }
1623 out:
1624         cma_release_remove(id_priv);
1625         cma_deref_id(id_priv);
1626 }
1627
1628 static int cma_resolve_loopback(struct rdma_id_private *id_priv)
1629 {
1630         struct cma_work *work;
1631         struct sockaddr_in *src_in, *dst_in;
1632         union ib_gid gid;
1633         int ret;
1634
1635         work = kzalloc(sizeof *work, GFP_KERNEL);
1636         if (!work)
1637                 return -ENOMEM;
1638
1639         if (!id_priv->cma_dev) {
1640                 ret = cma_bind_loopback(id_priv);
1641                 if (ret)
1642                         goto err;
1643         }
1644
1645         ib_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
1646         ib_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
1647
1648         if (cma_zero_addr(&id_priv->id.route.addr.src_addr)) {
1649                 src_in = (struct sockaddr_in *)&id_priv->id.route.addr.src_addr;
1650                 dst_in = (struct sockaddr_in *)&id_priv->id.route.addr.dst_addr;
1651                 src_in->sin_family = dst_in->sin_family;
1652                 src_in->sin_addr.s_addr = dst_in->sin_addr.s_addr;
1653         }
1654
1655         work->id = id_priv;
1656         INIT_WORK(&work->work, cma_work_handler);
1657         work->old_state = CMA_ADDR_QUERY;
1658         work->new_state = CMA_ADDR_RESOLVED;
1659         work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
1660         queue_work(cma_wq, &work->work);
1661         return 0;
1662 err:
1663         kfree(work);
1664         return ret;
1665 }
1666
1667 static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
1668                          struct sockaddr *dst_addr)
1669 {
1670         if (src_addr && src_addr->sa_family)
1671                 return rdma_bind_addr(id, src_addr);
1672         else
1673                 return cma_bind_any(id, dst_addr->sa_family);
1674 }
1675
1676 int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
1677                       struct sockaddr *dst_addr, int timeout_ms)
1678 {
1679         struct rdma_id_private *id_priv;
1680         int ret;
1681
1682         id_priv = container_of(id, struct rdma_id_private, id);
1683         if (id_priv->state == CMA_IDLE) {
1684                 ret = cma_bind_addr(id, src_addr, dst_addr);
1685                 if (ret)
1686                         return ret;
1687         }
1688
1689         if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_ADDR_QUERY))
1690                 return -EINVAL;
1691
1692         atomic_inc(&id_priv->refcount);
1693         memcpy(&id->route.addr.dst_addr, dst_addr, ip_addr_size(dst_addr));
1694         if (cma_any_addr(dst_addr))
1695                 ret = cma_resolve_loopback(id_priv);
1696         else
1697                 ret = rdma_resolve_ip(&addr_client, &id->route.addr.src_addr,
1698                                       dst_addr, &id->route.addr.dev_addr,
1699                                       timeout_ms, addr_handler, id_priv);
1700         if (ret)
1701                 goto err;
1702
1703         return 0;
1704 err:
1705         cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_BOUND);
1706         cma_deref_id(id_priv);
1707         return ret;
1708 }
1709 EXPORT_SYMBOL(rdma_resolve_addr);
1710
1711 static void cma_bind_port(struct rdma_bind_list *bind_list,
1712                           struct rdma_id_private *id_priv)
1713 {
1714         struct sockaddr_in *sin;
1715
1716         sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1717         sin->sin_port = htons(bind_list->port);
1718         id_priv->bind_list = bind_list;
1719         hlist_add_head(&id_priv->node, &bind_list->owners);
1720 }
1721
1722 static int cma_alloc_port(struct idr *ps, struct rdma_id_private *id_priv,
1723                           unsigned short snum)
1724 {
1725         struct rdma_bind_list *bind_list;
1726         int port, ret;
1727
1728         bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
1729         if (!bind_list)
1730                 return -ENOMEM;
1731
1732         do {
1733                 ret = idr_get_new_above(ps, bind_list, snum, &port);
1734         } while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL));
1735
1736         if (ret)
1737                 goto err1;
1738
1739         if (port != snum) {
1740                 ret = -EADDRNOTAVAIL;
1741                 goto err2;
1742         }
1743
1744         bind_list->ps = ps;
1745         bind_list->port = (unsigned short) port;
1746         cma_bind_port(bind_list, id_priv);
1747         return 0;
1748 err2:
1749         idr_remove(ps, port);
1750 err1:
1751         kfree(bind_list);
1752         return ret;
1753 }
1754
1755 static int cma_alloc_any_port(struct idr *ps, struct rdma_id_private *id_priv)
1756 {
1757         struct rdma_bind_list *bind_list;
1758         int port, ret;
1759
1760         bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
1761         if (!bind_list)
1762                 return -ENOMEM;
1763
1764 retry:
1765         do {
1766                 ret = idr_get_new_above(ps, bind_list, next_port, &port);
1767         } while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL));
1768
1769         if (ret)
1770                 goto err1;
1771
1772         if (port > sysctl_local_port_range[1]) {
1773                 if (next_port != sysctl_local_port_range[0]) {
1774                         idr_remove(ps, port);
1775                         next_port = sysctl_local_port_range[0];
1776                         goto retry;
1777                 }
1778                 ret = -EADDRNOTAVAIL;
1779                 goto err2;
1780         }
1781
1782         if (port == sysctl_local_port_range[1])
1783                 next_port = sysctl_local_port_range[0];
1784         else
1785                 next_port = port + 1;
1786
1787         bind_list->ps = ps;
1788         bind_list->port = (unsigned short) port;
1789         cma_bind_port(bind_list, id_priv);
1790         return 0;
1791 err2:
1792         idr_remove(ps, port);
1793 err1:
1794         kfree(bind_list);
1795         return ret;
1796 }
1797
1798 static int cma_use_port(struct idr *ps, struct rdma_id_private *id_priv)
1799 {
1800         struct rdma_id_private *cur_id;
1801         struct sockaddr_in *sin, *cur_sin;
1802         struct rdma_bind_list *bind_list;
1803         struct hlist_node *node;
1804         unsigned short snum;
1805
1806         sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1807         snum = ntohs(sin->sin_port);
1808         if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
1809                 return -EACCES;
1810
1811         bind_list = idr_find(ps, snum);
1812         if (!bind_list)
1813                 return cma_alloc_port(ps, id_priv, snum);
1814
1815         /*
1816          * We don't support binding to any address if anyone is bound to
1817          * a specific address on the same port.
1818          */
1819         if (cma_any_addr(&id_priv->id.route.addr.src_addr))
1820                 return -EADDRNOTAVAIL;
1821
1822         hlist_for_each_entry(cur_id, node, &bind_list->owners, node) {
1823                 if (cma_any_addr(&cur_id->id.route.addr.src_addr))
1824                         return -EADDRNOTAVAIL;
1825
1826                 cur_sin = (struct sockaddr_in *) &cur_id->id.route.addr.src_addr;
1827                 if (sin->sin_addr.s_addr == cur_sin->sin_addr.s_addr)
1828                         return -EADDRINUSE;
1829         }
1830
1831         cma_bind_port(bind_list, id_priv);
1832         return 0;
1833 }
1834
1835 static int cma_get_port(struct rdma_id_private *id_priv)
1836 {
1837         struct idr *ps;
1838         int ret;
1839
1840         switch (id_priv->id.ps) {
1841         case RDMA_PS_SDP:
1842                 ps = &sdp_ps;
1843                 break;
1844         case RDMA_PS_TCP:
1845                 ps = &tcp_ps;
1846                 break;
1847         case RDMA_PS_UDP:
1848                 ps = &udp_ps;
1849                 break;
1850         default:
1851                 return -EPROTONOSUPPORT;
1852         }
1853
1854         mutex_lock(&lock);
1855         if (cma_any_port(&id_priv->id.route.addr.src_addr))
1856                 ret = cma_alloc_any_port(ps, id_priv);
1857         else
1858                 ret = cma_use_port(ps, id_priv);
1859         mutex_unlock(&lock);
1860
1861         return ret;
1862 }
1863
1864 int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
1865 {
1866         struct rdma_id_private *id_priv;
1867         int ret;
1868
1869         if (addr->sa_family != AF_INET)
1870                 return -EAFNOSUPPORT;
1871
1872         id_priv = container_of(id, struct rdma_id_private, id);
1873         if (!cma_comp_exch(id_priv, CMA_IDLE, CMA_ADDR_BOUND))
1874                 return -EINVAL;
1875
1876         if (!cma_any_addr(addr)) {
1877                 ret = rdma_translate_ip(addr, &id->route.addr.dev_addr);
1878                 if (ret)
1879                         goto err1;
1880
1881                 mutex_lock(&lock);
1882                 ret = cma_acquire_dev(id_priv);
1883                 mutex_unlock(&lock);
1884                 if (ret)
1885                         goto err1;
1886         }
1887
1888         memcpy(&id->route.addr.src_addr, addr, ip_addr_size(addr));
1889         ret = cma_get_port(id_priv);
1890         if (ret)
1891                 goto err2;
1892
1893         return 0;
1894 err2:
1895         if (!cma_any_addr(addr)) {
1896                 mutex_lock(&lock);
1897                 cma_detach_from_dev(id_priv);
1898                 mutex_unlock(&lock);
1899         }
1900 err1:
1901         cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_IDLE);
1902         return ret;
1903 }
1904 EXPORT_SYMBOL(rdma_bind_addr);
1905
1906 static int cma_format_hdr(void *hdr, enum rdma_port_space ps,
1907                           struct rdma_route *route)
1908 {
1909         struct sockaddr_in *src4, *dst4;
1910         struct cma_hdr *cma_hdr;
1911         struct sdp_hh *sdp_hdr;
1912
1913         src4 = (struct sockaddr_in *) &route->addr.src_addr;
1914         dst4 = (struct sockaddr_in *) &route->addr.dst_addr;
1915
1916         switch (ps) {
1917         case RDMA_PS_SDP:
1918                 sdp_hdr = hdr;
1919                 if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION)
1920                         return -EINVAL;
1921                 sdp_set_ip_ver(sdp_hdr, 4);
1922                 sdp_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
1923                 sdp_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
1924                 sdp_hdr->port = src4->sin_port;
1925                 break;
1926         default:
1927                 cma_hdr = hdr;
1928                 cma_hdr->cma_version = CMA_VERSION;
1929                 cma_set_ip_ver(cma_hdr, 4);
1930                 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
1931                 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
1932                 cma_hdr->port = src4->sin_port;
1933                 break;
1934         }
1935         return 0;
1936 }
1937
1938 static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
1939                                 struct ib_cm_event *ib_event)
1940 {
1941         struct rdma_id_private *id_priv = cm_id->context;
1942         struct rdma_cm_event event;
1943         struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
1944         int ret = 0;
1945
1946         memset(&event, 0, sizeof event);
1947         atomic_inc(&id_priv->dev_remove);
1948         if (!cma_comp(id_priv, CMA_CONNECT))
1949                 goto out;
1950
1951         switch (ib_event->event) {
1952         case IB_CM_SIDR_REQ_ERROR:
1953                 event.event = RDMA_CM_EVENT_UNREACHABLE;
1954                 event.status = -ETIMEDOUT;
1955                 break;
1956         case IB_CM_SIDR_REP_RECEIVED:
1957                 event.param.ud.private_data = ib_event->private_data;
1958                 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
1959                 if (rep->status != IB_SIDR_SUCCESS) {
1960                         event.event = RDMA_CM_EVENT_UNREACHABLE;
1961                         event.status = ib_event->param.sidr_rep_rcvd.status;
1962                         break;
1963                 }
1964                 if (rep->qkey != RDMA_UD_QKEY) {
1965                         event.event = RDMA_CM_EVENT_UNREACHABLE;
1966                         event.status = -EINVAL;
1967                         break;
1968                 }
1969                 ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
1970                                      id_priv->id.route.path_rec,
1971                                      &event.param.ud.ah_attr);
1972                 event.param.ud.qp_num = rep->qpn;
1973                 event.param.ud.qkey = rep->qkey;
1974                 event.event = RDMA_CM_EVENT_ESTABLISHED;
1975                 event.status = 0;
1976                 break;
1977         default:
1978                 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d",
1979                        ib_event->event);
1980                 goto out;
1981         }
1982
1983         ret = id_priv->id.event_handler(&id_priv->id, &event);
1984         if (ret) {
1985                 /* Destroy the CM ID by returning a non-zero value. */
1986                 id_priv->cm_id.ib = NULL;
1987                 cma_exch(id_priv, CMA_DESTROYING);
1988                 cma_release_remove(id_priv);
1989                 rdma_destroy_id(&id_priv->id);
1990                 return ret;
1991         }
1992 out:
1993         cma_release_remove(id_priv);
1994         return ret;
1995 }
1996
1997 static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
1998                               struct rdma_conn_param *conn_param)
1999 {
2000         struct ib_cm_sidr_req_param req;
2001         struct rdma_route *route;
2002         int ret;
2003
2004         req.private_data_len = sizeof(struct cma_hdr) +
2005                                conn_param->private_data_len;
2006         req.private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2007         if (!req.private_data)
2008                 return -ENOMEM;
2009
2010         if (conn_param->private_data && conn_param->private_data_len)
2011                 memcpy((void *) req.private_data + sizeof(struct cma_hdr),
2012                        conn_param->private_data, conn_param->private_data_len);
2013
2014         route = &id_priv->id.route;
2015         ret = cma_format_hdr((void *) req.private_data, id_priv->id.ps, route);
2016         if (ret)
2017                 goto out;
2018
2019         id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device,
2020                                             cma_sidr_rep_handler, id_priv);
2021         if (IS_ERR(id_priv->cm_id.ib)) {
2022                 ret = PTR_ERR(id_priv->cm_id.ib);
2023                 goto out;
2024         }
2025
2026         req.path = route->path_rec;
2027         req.service_id = cma_get_service_id(id_priv->id.ps,
2028                                             &route->addr.dst_addr);
2029         req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
2030         req.max_cm_retries = CMA_MAX_CM_RETRIES;
2031
2032         ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
2033         if (ret) {
2034                 ib_destroy_cm_id(id_priv->cm_id.ib);
2035                 id_priv->cm_id.ib = NULL;
2036         }
2037 out:
2038         kfree(req.private_data);
2039         return ret;
2040 }
2041
2042 static int cma_connect_ib(struct rdma_id_private *id_priv,
2043                           struct rdma_conn_param *conn_param)
2044 {
2045         struct ib_cm_req_param req;
2046         struct rdma_route *route;
2047         void *private_data;
2048         int offset, ret;
2049
2050         memset(&req, 0, sizeof req);
2051         offset = cma_user_data_offset(id_priv->id.ps);
2052         req.private_data_len = offset + conn_param->private_data_len;
2053         private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2054         if (!private_data)
2055                 return -ENOMEM;
2056
2057         if (conn_param->private_data && conn_param->private_data_len)
2058                 memcpy(private_data + offset, conn_param->private_data,
2059                        conn_param->private_data_len);
2060
2061         id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_ib_handler,
2062                                             id_priv);
2063         if (IS_ERR(id_priv->cm_id.ib)) {
2064                 ret = PTR_ERR(id_priv->cm_id.ib);
2065                 goto out;
2066         }
2067
2068         route = &id_priv->id.route;
2069         ret = cma_format_hdr(private_data, id_priv->id.ps, route);
2070         if (ret)
2071                 goto out;
2072         req.private_data = private_data;
2073
2074         req.primary_path = &route->path_rec[0];
2075         if (route->num_paths == 2)
2076                 req.alternate_path = &route->path_rec[1];
2077
2078         req.service_id = cma_get_service_id(id_priv->id.ps,
2079                                             &route->addr.dst_addr);
2080         req.qp_num = id_priv->qp_num;
2081         req.qp_type = IB_QPT_RC;
2082         req.starting_psn = id_priv->seq_num;
2083         req.responder_resources = conn_param->responder_resources;
2084         req.initiator_depth = conn_param->initiator_depth;
2085         req.flow_control = conn_param->flow_control;
2086         req.retry_count = conn_param->retry_count;
2087         req.rnr_retry_count = conn_param->rnr_retry_count;
2088         req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2089         req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2090         req.max_cm_retries = CMA_MAX_CM_RETRIES;
2091         req.srq = id_priv->srq ? 1 : 0;
2092
2093         ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
2094 out:
2095         if (ret && !IS_ERR(id_priv->cm_id.ib)) {
2096                 ib_destroy_cm_id(id_priv->cm_id.ib);
2097                 id_priv->cm_id.ib = NULL;
2098         }
2099
2100         kfree(private_data);
2101         return ret;
2102 }
2103
2104 static int cma_connect_iw(struct rdma_id_private *id_priv,
2105                           struct rdma_conn_param *conn_param)
2106 {
2107         struct iw_cm_id *cm_id;
2108         struct sockaddr_in* sin;
2109         int ret;
2110         struct iw_cm_conn_param iw_param;
2111
2112         cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
2113         if (IS_ERR(cm_id)) {
2114                 ret = PTR_ERR(cm_id);
2115                 goto out;
2116         }
2117
2118         id_priv->cm_id.iw = cm_id;
2119
2120         sin = (struct sockaddr_in*) &id_priv->id.route.addr.src_addr;
2121         cm_id->local_addr = *sin;
2122
2123         sin = (struct sockaddr_in*) &id_priv->id.route.addr.dst_addr;
2124         cm_id->remote_addr = *sin;
2125
2126         ret = cma_modify_qp_rtr(&id_priv->id);
2127         if (ret)
2128                 goto out;
2129
2130         iw_param.ord = conn_param->initiator_depth;
2131         iw_param.ird = conn_param->responder_resources;
2132         iw_param.private_data = conn_param->private_data;
2133         iw_param.private_data_len = conn_param->private_data_len;
2134         if (id_priv->id.qp)
2135                 iw_param.qpn = id_priv->qp_num;
2136         else
2137                 iw_param.qpn = conn_param->qp_num;
2138         ret = iw_cm_connect(cm_id, &iw_param);
2139 out:
2140         if (ret && !IS_ERR(cm_id)) {
2141                 iw_destroy_cm_id(cm_id);
2142                 id_priv->cm_id.iw = NULL;
2143         }
2144         return ret;
2145 }
2146
2147 int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2148 {
2149         struct rdma_id_private *id_priv;
2150         int ret;
2151
2152         id_priv = container_of(id, struct rdma_id_private, id);
2153         if (!cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_CONNECT))
2154                 return -EINVAL;
2155
2156         if (!id->qp) {
2157                 id_priv->qp_num = conn_param->qp_num;
2158                 id_priv->srq = conn_param->srq;
2159         }
2160
2161         switch (rdma_node_get_transport(id->device->node_type)) {
2162         case RDMA_TRANSPORT_IB:
2163                 if (id->ps == RDMA_PS_UDP)
2164                         ret = cma_resolve_ib_udp(id_priv, conn_param);
2165                 else
2166                         ret = cma_connect_ib(id_priv, conn_param);
2167                 break;
2168         case RDMA_TRANSPORT_IWARP:
2169                 ret = cma_connect_iw(id_priv, conn_param);
2170                 break;
2171         default:
2172                 ret = -ENOSYS;
2173                 break;
2174         }
2175         if (ret)
2176                 goto err;
2177
2178         return 0;
2179 err:
2180         cma_comp_exch(id_priv, CMA_CONNECT, CMA_ROUTE_RESOLVED);
2181         return ret;
2182 }
2183 EXPORT_SYMBOL(rdma_connect);
2184
2185 static int cma_accept_ib(struct rdma_id_private *id_priv,
2186                          struct rdma_conn_param *conn_param)
2187 {
2188         struct ib_cm_rep_param rep;
2189         struct ib_qp_attr qp_attr;
2190         int qp_attr_mask, ret;
2191
2192         if (id_priv->id.qp) {
2193                 ret = cma_modify_qp_rtr(&id_priv->id);
2194                 if (ret)
2195                         goto out;
2196
2197                 qp_attr.qp_state = IB_QPS_RTS;
2198                 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, &qp_attr,
2199                                          &qp_attr_mask);
2200                 if (ret)
2201                         goto out;
2202
2203                 qp_attr.max_rd_atomic = conn_param->initiator_depth;
2204                 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
2205                 if (ret)
2206                         goto out;
2207         }
2208
2209         memset(&rep, 0, sizeof rep);
2210         rep.qp_num = id_priv->qp_num;
2211         rep.starting_psn = id_priv->seq_num;
2212         rep.private_data = conn_param->private_data;
2213         rep.private_data_len = conn_param->private_data_len;
2214         rep.responder_resources = conn_param->responder_resources;
2215         rep.initiator_depth = conn_param->initiator_depth;
2216         rep.target_ack_delay = CMA_CM_RESPONSE_TIMEOUT;
2217         rep.failover_accepted = 0;
2218         rep.flow_control = conn_param->flow_control;
2219         rep.rnr_retry_count = conn_param->rnr_retry_count;
2220         rep.srq = id_priv->srq ? 1 : 0;
2221
2222         ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
2223 out:
2224         return ret;
2225 }
2226
2227 static int cma_accept_iw(struct rdma_id_private *id_priv,
2228                   struct rdma_conn_param *conn_param)
2229 {
2230         struct iw_cm_conn_param iw_param;
2231         int ret;
2232
2233         ret = cma_modify_qp_rtr(&id_priv->id);
2234         if (ret)
2235                 return ret;
2236
2237         iw_param.ord = conn_param->initiator_depth;
2238         iw_param.ird = conn_param->responder_resources;
2239         iw_param.private_data = conn_param->private_data;
2240         iw_param.private_data_len = conn_param->private_data_len;
2241         if (id_priv->id.qp) {
2242                 iw_param.qpn = id_priv->qp_num;
2243         } else
2244                 iw_param.qpn = conn_param->qp_num;
2245
2246         return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
2247 }
2248
2249 static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
2250                              enum ib_cm_sidr_status status,
2251                              const void *private_data, int private_data_len)
2252 {
2253         struct ib_cm_sidr_rep_param rep;
2254
2255         memset(&rep, 0, sizeof rep);
2256         rep.status = status;
2257         if (status == IB_SIDR_SUCCESS) {
2258                 rep.qp_num = id_priv->qp_num;
2259                 rep.qkey = RDMA_UD_QKEY;
2260         }
2261         rep.private_data = private_data;
2262         rep.private_data_len = private_data_len;
2263
2264         return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
2265 }
2266
2267 int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2268 {
2269         struct rdma_id_private *id_priv;
2270         int ret;
2271
2272         id_priv = container_of(id, struct rdma_id_private, id);
2273         if (!cma_comp(id_priv, CMA_CONNECT))
2274                 return -EINVAL;
2275
2276         if (!id->qp && conn_param) {
2277                 id_priv->qp_num = conn_param->qp_num;
2278                 id_priv->srq = conn_param->srq;
2279         }
2280
2281         switch (rdma_node_get_transport(id->device->node_type)) {
2282         case RDMA_TRANSPORT_IB:
2283                 if (id->ps == RDMA_PS_UDP)
2284                         ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
2285                                                 conn_param->private_data,
2286                                                 conn_param->private_data_len);
2287                 else if (conn_param)
2288                         ret = cma_accept_ib(id_priv, conn_param);
2289                 else
2290                         ret = cma_rep_recv(id_priv);
2291                 break;
2292         case RDMA_TRANSPORT_IWARP:
2293                 ret = cma_accept_iw(id_priv, conn_param);
2294                 break;
2295         default:
2296                 ret = -ENOSYS;
2297                 break;
2298         }
2299
2300         if (ret)
2301                 goto reject;
2302
2303         return 0;
2304 reject:
2305         cma_modify_qp_err(id);
2306         rdma_reject(id, NULL, 0);
2307         return ret;
2308 }
2309 EXPORT_SYMBOL(rdma_accept);
2310
2311 int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
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                 return -EINVAL;
2319
2320         switch (id->device->node_type) {
2321         case RDMA_NODE_IB_CA:
2322                 ret = ib_cm_notify(id_priv->cm_id.ib, event);
2323                 break;
2324         default:
2325                 ret = 0;
2326                 break;
2327         }
2328         return ret;
2329 }
2330 EXPORT_SYMBOL(rdma_notify);
2331
2332 int rdma_reject(struct rdma_cm_id *id, const void *private_data,
2333                 u8 private_data_len)
2334 {
2335         struct rdma_id_private *id_priv;
2336         int ret;
2337
2338         id_priv = container_of(id, struct rdma_id_private, id);
2339         if (!cma_comp(id_priv, CMA_CONNECT))
2340                 return -EINVAL;
2341
2342         switch (rdma_node_get_transport(id->device->node_type)) {
2343         case RDMA_TRANSPORT_IB:
2344                 if (id->ps == RDMA_PS_UDP)
2345                         ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT,
2346                                                 private_data, private_data_len);
2347                 else
2348                         ret = ib_send_cm_rej(id_priv->cm_id.ib,
2349                                              IB_CM_REJ_CONSUMER_DEFINED, NULL,
2350                                              0, private_data, private_data_len);
2351                 break;
2352         case RDMA_TRANSPORT_IWARP:
2353                 ret = iw_cm_reject(id_priv->cm_id.iw,
2354                                    private_data, private_data_len);
2355                 break;
2356         default:
2357                 ret = -ENOSYS;
2358                 break;
2359         }
2360         return ret;
2361 }
2362 EXPORT_SYMBOL(rdma_reject);
2363
2364 int rdma_disconnect(struct rdma_cm_id *id)
2365 {
2366         struct rdma_id_private *id_priv;
2367         int ret;
2368
2369         id_priv = container_of(id, struct rdma_id_private, id);
2370         if (!cma_comp(id_priv, CMA_CONNECT) &&
2371             !cma_comp(id_priv, CMA_DISCONNECT))
2372                 return -EINVAL;
2373
2374         switch (rdma_node_get_transport(id->device->node_type)) {
2375         case RDMA_TRANSPORT_IB:
2376                 ret = cma_modify_qp_err(id);
2377                 if (ret)
2378                         goto out;
2379                 /* Initiate or respond to a disconnect. */
2380                 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
2381                         ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
2382                 break;
2383         case RDMA_TRANSPORT_IWARP:
2384                 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
2385                 break;
2386         default:
2387                 ret = -EINVAL;
2388                 break;
2389         }
2390 out:
2391         return ret;
2392 }
2393 EXPORT_SYMBOL(rdma_disconnect);
2394
2395 static void cma_add_one(struct ib_device *device)
2396 {
2397         struct cma_device *cma_dev;
2398         struct rdma_id_private *id_priv;
2399
2400         cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
2401         if (!cma_dev)
2402                 return;
2403
2404         cma_dev->device = device;
2405         cma_dev->node_guid = device->node_guid;
2406
2407         init_completion(&cma_dev->comp);
2408         atomic_set(&cma_dev->refcount, 1);
2409         INIT_LIST_HEAD(&cma_dev->id_list);
2410         ib_set_client_data(device, &cma_client, cma_dev);
2411
2412         mutex_lock(&lock);
2413         list_add_tail(&cma_dev->list, &dev_list);
2414         list_for_each_entry(id_priv, &listen_any_list, list)
2415                 cma_listen_on_dev(id_priv, cma_dev);
2416         mutex_unlock(&lock);
2417 }
2418
2419 static int cma_remove_id_dev(struct rdma_id_private *id_priv)
2420 {
2421         struct rdma_cm_event event;
2422         enum cma_state state;
2423
2424         /* Record that we want to remove the device */
2425         state = cma_exch(id_priv, CMA_DEVICE_REMOVAL);
2426         if (state == CMA_DESTROYING)
2427                 return 0;
2428
2429         cma_cancel_operation(id_priv, state);
2430         wait_event(id_priv->wait_remove, !atomic_read(&id_priv->dev_remove));
2431
2432         /* Check for destruction from another callback. */
2433         if (!cma_comp(id_priv, CMA_DEVICE_REMOVAL))
2434                 return 0;
2435
2436         memset(&event, 0, sizeof event);
2437         event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
2438         return id_priv->id.event_handler(&id_priv->id, &event);
2439 }
2440
2441 static void cma_process_remove(struct cma_device *cma_dev)
2442 {
2443         struct rdma_id_private *id_priv;
2444         int ret;
2445
2446         mutex_lock(&lock);
2447         while (!list_empty(&cma_dev->id_list)) {
2448                 id_priv = list_entry(cma_dev->id_list.next,
2449                                      struct rdma_id_private, list);
2450
2451                 if (cma_internal_listen(id_priv)) {
2452                         cma_destroy_listen(id_priv);
2453                         continue;
2454                 }
2455
2456                 list_del_init(&id_priv->list);
2457                 atomic_inc(&id_priv->refcount);
2458                 mutex_unlock(&lock);
2459
2460                 ret = cma_remove_id_dev(id_priv);
2461                 cma_deref_id(id_priv);
2462                 if (ret)
2463                         rdma_destroy_id(&id_priv->id);
2464
2465                 mutex_lock(&lock);
2466         }
2467         mutex_unlock(&lock);
2468
2469         cma_deref_dev(cma_dev);
2470         wait_for_completion(&cma_dev->comp);
2471 }
2472
2473 static void cma_remove_one(struct ib_device *device)
2474 {
2475         struct cma_device *cma_dev;
2476
2477         cma_dev = ib_get_client_data(device, &cma_client);
2478         if (!cma_dev)
2479                 return;
2480
2481         mutex_lock(&lock);
2482         list_del(&cma_dev->list);
2483         mutex_unlock(&lock);
2484
2485         cma_process_remove(cma_dev);
2486         kfree(cma_dev);
2487 }
2488
2489 static int cma_init(void)
2490 {
2491         int ret;
2492
2493         get_random_bytes(&next_port, sizeof next_port);
2494         next_port = (next_port % (sysctl_local_port_range[1] -
2495                                   sysctl_local_port_range[0])) +
2496                     sysctl_local_port_range[0];
2497         cma_wq = create_singlethread_workqueue("rdma_cm");
2498         if (!cma_wq)
2499                 return -ENOMEM;
2500
2501         ib_sa_register_client(&sa_client);
2502         rdma_addr_register_client(&addr_client);
2503
2504         ret = ib_register_client(&cma_client);
2505         if (ret)
2506                 goto err;
2507         return 0;
2508
2509 err:
2510         rdma_addr_unregister_client(&addr_client);
2511         ib_sa_unregister_client(&sa_client);
2512         destroy_workqueue(cma_wq);
2513         return ret;
2514 }
2515
2516 static void cma_cleanup(void)
2517 {
2518         ib_unregister_client(&cma_client);
2519         rdma_addr_unregister_client(&addr_client);
2520         ib_sa_unregister_client(&sa_client);
2521         destroy_workqueue(cma_wq);
2522         idr_destroy(&sdp_ps);
2523         idr_destroy(&tcp_ps);
2524         idr_destroy(&udp_ps);
2525 }
2526
2527 module_init(cma_init);
2528 module_exit(cma_cleanup);