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