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