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