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