]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/ofed/drivers/infiniband/core/cma.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / ofed / 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 available to you under a choice of one of two
8  * licenses.  You may choose to be licensed under the terms of the GNU
9  * General Public License (GPL) Version 2, available from the file
10  * COPYING in the main directory of this source tree, or the
11  * OpenIB.org BSD license below:
12  *
13  *     Redistribution and use in source and binary forms, with or
14  *     without modification, are permitted provided that the following
15  *     conditions are met:
16  *
17  *      - Redistributions of source code must retain the above
18  *        copyright notice, this list of conditions and the following
19  *        disclaimer.
20  *
21  *      - Redistributions in binary form must reproduce the above
22  *        copyright notice, this list of conditions and the following
23  *        disclaimer in the documentation and/or other materials
24  *        provided with the distribution.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33  * SOFTWARE.
34  */
35
36 #include <linux/completion.h>
37 #include <linux/in.h>
38 #include <linux/in6.h>
39 #include <linux/mutex.h>
40 #include <linux/random.h>
41 #include <linux/idr.h>
42 #include <linux/inetdevice.h>
43
44 #include <net/tcp.h>
45 #include <net/ipv6.h>
46
47 #include <rdma/rdma_cm.h>
48 #include <rdma/rdma_cm_ib.h>
49 #include <rdma/ib_cache.h>
50 #include <rdma/ib_cm.h>
51 #include <rdma/ib_sa.h>
52 #include <rdma/iw_cm.h>
53
54 MODULE_AUTHOR("Sean Hefty");
55 MODULE_DESCRIPTION("Generic RDMA CM Agent");
56 MODULE_LICENSE("Dual BSD/GPL");
57
58 static int tavor_quirk = 0;
59 module_param_named(tavor_quirk, tavor_quirk, int, 0644);
60 MODULE_PARM_DESC(tavor_quirk, "Tavor performance quirk: limit MTU to 1K if > 0");
61
62 int unify_tcp_port_space = 0;
63 module_param(unify_tcp_port_space, int, 0644);
64 MODULE_PARM_DESC(unify_tcp_port_space, "Unify the host TCP and RDMA port "
65                  "space allocation (default=0)");
66
67 #define CMA_CM_RESPONSE_TIMEOUT 20
68 #define CMA_MAX_CM_RETRIES 15
69 #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
70 #define IBOE_PACKET_LIFETIME 18
71
72 static int cma_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
73 module_param_named(cma_response_timeout, cma_response_timeout, int, 0644);
74 MODULE_PARM_DESC(cma_response_timeout, "CMA_CM_RESPONSE_TIMEOUT default=20");
75
76 static int def_prec2sl = 3;
77 module_param_named(def_prec2sl, def_prec2sl, int, 0644);
78 MODULE_PARM_DESC(def_prec2sl, "Default value for SL priority with RoCE. Valid values 0 - 7");
79
80 static void cma_add_one(struct ib_device *device);
81 static void cma_remove_one(struct ib_device *device);
82
83 static struct ib_client cma_client = {
84         .name   = "cma",
85         .add    = cma_add_one,
86         .remove = cma_remove_one
87 };
88
89 static struct ib_sa_client sa_client;
90 static struct rdma_addr_client addr_client;
91 static LIST_HEAD(dev_list);
92 static LIST_HEAD(listen_any_list);
93 static DEFINE_MUTEX(lock);
94 static struct workqueue_struct *cma_wq;
95 static DEFINE_IDR(sdp_ps);
96 static DEFINE_IDR(tcp_ps);
97 static DEFINE_IDR(udp_ps);
98 static DEFINE_IDR(ipoib_ps);
99 static int next_port;
100
101 struct cma_device {
102         struct list_head        list;
103         struct ib_device        *device;
104         struct completion       comp;
105         atomic_t                refcount;
106         struct list_head        id_list;
107 };
108
109 enum cma_state {
110         CMA_IDLE,
111         CMA_ADDR_QUERY,
112         CMA_ADDR_RESOLVED,
113         CMA_ROUTE_QUERY,
114         CMA_ROUTE_RESOLVED,
115         CMA_CONNECT,
116         CMA_DISCONNECT,
117         CMA_ADDR_BOUND,
118         CMA_LISTEN,
119         CMA_DEVICE_REMOVAL,
120         CMA_DESTROYING
121 };
122
123 struct rdma_bind_list {
124         struct idr              *ps;
125         struct hlist_head       owners;
126         unsigned short          port;
127 };
128
129 /*
130  * Device removal can occur at anytime, so we need extra handling to
131  * serialize notifying the user of device removal with other callbacks.
132  * We do this by disabling removal notification while a callback is in process,
133  * and reporting it after the callback completes.
134  */
135 struct rdma_id_private {
136         struct rdma_cm_id       id;
137
138         struct rdma_bind_list   *bind_list;
139         struct socket           *sock;
140         struct hlist_node       node;
141         struct list_head        list; /* listen_any_list or cma_device.list */
142         struct list_head        listen_list; /* per device listens */
143         struct cma_device       *cma_dev;
144         struct list_head        mc_list;
145
146         int                     internal_id;
147         enum cma_state          state;
148         spinlock_t              lock;
149         struct mutex            qp_mutex;
150
151         struct completion       comp;
152         atomic_t                refcount;
153         struct mutex            handler_mutex;
154
155         int                     backlog;
156         int                     timeout_ms;
157         struct ib_sa_query      *query;
158         int                     query_id;
159         union {
160                 struct ib_cm_id *ib;
161                 struct iw_cm_id *iw;
162         } cm_id;
163
164         u32                     seq_num;
165         u32                     qkey;
166         u32                     qp_num;
167         u8                      srq;
168         u8                      tos;
169 };
170
171 struct cma_multicast {
172         struct rdma_id_private *id_priv;
173         union {
174                 struct ib_sa_multicast *ib;
175         } multicast;
176         struct list_head        list;
177         void                    *context;
178         struct sockaddr_storage addr;
179         struct kref             mcref;
180 };
181
182 struct cma_work {
183         struct work_struct      work;
184         struct rdma_id_private  *id;
185         enum cma_state          old_state;
186         enum cma_state          new_state;
187         struct rdma_cm_event    event;
188 };
189
190 struct cma_ndev_work {
191         struct work_struct      work;
192         struct rdma_id_private  *id;
193         struct rdma_cm_event    event;
194 };
195
196 struct iboe_mcast_work {
197         struct work_struct       work;
198         struct rdma_id_private  *id;
199         struct cma_multicast    *mc;
200 };
201
202 union cma_ip_addr {
203         struct in6_addr ip6;
204         struct {
205                 __be32 pad[3];
206                 __be32 addr;
207         } ip4;
208 };
209
210 struct cma_hdr {
211         u8 cma_version;
212         u8 ip_version;  /* IP version: 7:4 */
213         __be16 port;
214         union cma_ip_addr src_addr;
215         union cma_ip_addr dst_addr;
216 };
217
218 struct sdp_hh {
219         u8 bsdh[16];
220         u8 sdp_version; /* Major version: 7:4 */
221         u8 ip_version;  /* IP version: 7:4 */
222         u8 sdp_specific1[10];
223         __be16 port;
224         __be16 sdp_specific2;
225         union cma_ip_addr src_addr;
226         union cma_ip_addr dst_addr;
227 };
228
229 struct sdp_hah {
230         u8 bsdh[16];
231         u8 sdp_version;
232 };
233
234 #define CMA_VERSION 0x00
235 #define SDP_MAJ_VERSION 0x2
236
237 static int cma_comp(struct rdma_id_private *id_priv, enum cma_state comp)
238 {
239         unsigned long flags;
240         int ret;
241
242         spin_lock_irqsave(&id_priv->lock, flags);
243         ret = (id_priv->state == comp);
244         spin_unlock_irqrestore(&id_priv->lock, flags);
245         return ret;
246 }
247
248 static int cma_comp_exch(struct rdma_id_private *id_priv,
249                          enum cma_state comp, enum cma_state exch)
250 {
251         unsigned long flags;
252         int ret;
253
254         spin_lock_irqsave(&id_priv->lock, flags);
255         if ((ret = (id_priv->state == comp)))
256                 id_priv->state = exch;
257         spin_unlock_irqrestore(&id_priv->lock, flags);
258         return ret;
259 }
260
261 static enum cma_state cma_exch(struct rdma_id_private *id_priv,
262                                enum cma_state exch)
263 {
264         unsigned long flags;
265         enum cma_state old;
266
267         spin_lock_irqsave(&id_priv->lock, flags);
268         old = id_priv->state;
269         id_priv->state = exch;
270         spin_unlock_irqrestore(&id_priv->lock, flags);
271         return old;
272 }
273
274 static inline u8 cma_get_ip_ver(struct cma_hdr *hdr)
275 {
276         return hdr->ip_version >> 4;
277 }
278
279 static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
280 {
281         hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
282 }
283
284 static inline u8 sdp_get_majv(u8 sdp_version)
285 {
286         return sdp_version >> 4;
287 }
288
289 static inline u8 sdp_get_ip_ver(struct sdp_hh *hh)
290 {
291         return hh->ip_version >> 4;
292 }
293
294 static inline void sdp_set_ip_ver(struct sdp_hh *hh, u8 ip_ver)
295 {
296         hh->ip_version = (ip_ver << 4) | (hh->ip_version & 0xF);
297 }
298
299 static inline int cma_is_ud_ps(enum rdma_port_space ps)
300 {
301         return (ps == RDMA_PS_UDP || ps == RDMA_PS_IPOIB);
302 }
303
304 static void cma_attach_to_dev(struct rdma_id_private *id_priv,
305                               struct cma_device *cma_dev)
306 {
307         atomic_inc(&cma_dev->refcount);
308         id_priv->cma_dev = cma_dev;
309         id_priv->id.device = cma_dev->device;
310         id_priv->id.route.addr.dev_addr.transport =
311                 rdma_node_get_transport(cma_dev->device->node_type);
312         list_add_tail(&id_priv->list, &cma_dev->id_list);
313 }
314
315 static inline void cma_deref_dev(struct cma_device *cma_dev)
316 {
317         if (atomic_dec_and_test(&cma_dev->refcount))
318                 complete(&cma_dev->comp);
319 }
320
321 static inline void release_mc(struct kref *kref)
322 {
323         struct cma_multicast *mc = container_of(kref, struct cma_multicast, mcref);
324
325         kfree(mc->multicast.ib);
326         kfree(mc);
327 }
328
329 static void cma_detach_from_dev(struct rdma_id_private *id_priv)
330 {
331         list_del(&id_priv->list);
332         cma_deref_dev(id_priv->cma_dev);
333         id_priv->cma_dev = NULL;
334 }
335
336 static int cma_set_qkey(struct rdma_id_private *id_priv)
337 {
338         struct ib_sa_mcmember_rec rec;
339         int ret = 0;
340
341         if (id_priv->qkey)
342                 return 0;
343
344         switch (id_priv->id.ps) {
345         case RDMA_PS_UDP:
346                 id_priv->qkey = RDMA_UDP_QKEY;
347                 break;
348         case RDMA_PS_IPOIB:
349                 ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid);
350                 ret = ib_sa_get_mcmember_rec(id_priv->id.device,
351                                              id_priv->id.port_num, &rec.mgid,
352                                              &rec);
353                 if (!ret)
354                         id_priv->qkey = be32_to_cpu(rec.qkey);
355                 break;
356         default:
357                 break;
358         }
359         return ret;
360 }
361
362 static int cma_acquire_dev(struct rdma_id_private *id_priv)
363 {
364         struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
365         struct cma_device *cma_dev;
366         union ib_gid gid;
367         int ret = -ENODEV;
368
369         if (dev_addr->dev_type != ARPHRD_INFINIBAND) {
370                 iboe_addr_get_sgid(dev_addr, &gid);
371                 list_for_each_entry(cma_dev, &dev_list, list) {
372                         ret = ib_find_cached_gid(cma_dev->device, &gid,
373                                                  &id_priv->id.port_num, NULL);
374                         if (!ret)
375                                 goto out;
376                 }
377         }
378
379         memcpy(&gid, dev_addr->src_dev_addr +
380                rdma_addr_gid_offset(dev_addr), sizeof gid);
381         list_for_each_entry(cma_dev, &dev_list, list) {
382                 ret = ib_find_cached_gid(cma_dev->device, &gid,
383                                          &id_priv->id.port_num, NULL);
384                 if (!ret)
385                         break;
386         }
387
388 out:
389         if (!ret)
390                 cma_attach_to_dev(id_priv, cma_dev);
391
392         return ret;
393 }
394
395 static void cma_deref_id(struct rdma_id_private *id_priv)
396 {
397         if (atomic_dec_and_test(&id_priv->refcount))
398                 complete(&id_priv->comp);
399 }
400
401 static int cma_disable_callback(struct rdma_id_private *id_priv,
402                               enum cma_state state)
403 {
404         mutex_lock(&id_priv->handler_mutex);
405         if (id_priv->state != state) {
406                 mutex_unlock(&id_priv->handler_mutex);
407                 return -EINVAL;
408         }
409         return 0;
410 }
411
412 static int cma_has_cm_dev(struct rdma_id_private *id_priv)
413 {
414         return (id_priv->id.device && id_priv->cm_id.ib);
415 }
416
417 struct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler,
418                                   void *context, enum rdma_port_space ps)
419 {
420         struct rdma_id_private *id_priv;
421
422         id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
423         if (!id_priv)
424                 return ERR_PTR(-ENOMEM);
425
426         id_priv->state = CMA_IDLE;
427         id_priv->id.context = context;
428         id_priv->id.event_handler = event_handler;
429         id_priv->id.ps = ps;
430         spin_lock_init(&id_priv->lock);
431         mutex_init(&id_priv->qp_mutex);
432         init_completion(&id_priv->comp);
433         atomic_set(&id_priv->refcount, 1);
434         mutex_init(&id_priv->handler_mutex);
435         INIT_LIST_HEAD(&id_priv->listen_list);
436         INIT_LIST_HEAD(&id_priv->mc_list);
437         get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
438
439         return &id_priv->id;
440 }
441 EXPORT_SYMBOL(rdma_create_id);
442
443 static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
444 {
445         struct ib_qp_attr qp_attr;
446         int qp_attr_mask, ret;
447
448         qp_attr.qp_state = IB_QPS_INIT;
449         ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
450         if (ret)
451                 return ret;
452
453         ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
454         if (ret)
455                 return ret;
456
457         qp_attr.qp_state = IB_QPS_RTR;
458         ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
459         if (ret)
460                 return ret;
461
462         qp_attr.qp_state = IB_QPS_RTS;
463         qp_attr.sq_psn = 0;
464         ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
465
466         return ret;
467 }
468
469 static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
470 {
471         struct ib_qp_attr qp_attr;
472         int qp_attr_mask, ret;
473
474         qp_attr.qp_state = IB_QPS_INIT;
475         ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
476         if (ret)
477                 return ret;
478
479         return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
480 }
481
482 int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
483                    struct ib_qp_init_attr *qp_init_attr)
484 {
485         struct rdma_id_private *id_priv;
486         struct ib_qp *qp;
487         int ret;
488
489         id_priv = container_of(id, struct rdma_id_private, id);
490         if (id->device != pd->device)
491                 return -EINVAL;
492
493         qp = ib_create_qp(pd, qp_init_attr);
494         if (IS_ERR(qp))
495                 return PTR_ERR(qp);
496
497         if (cma_is_ud_ps(id_priv->id.ps))
498                 ret = cma_init_ud_qp(id_priv, qp);
499         else
500                 ret = cma_init_conn_qp(id_priv, qp);
501         if (ret)
502                 goto err;
503
504         id->qp = qp;
505         id_priv->qp_num = qp->qp_num;
506         id_priv->srq = (qp->srq != NULL);
507         return 0;
508 err:
509         ib_destroy_qp(qp);
510         return ret;
511 }
512 EXPORT_SYMBOL(rdma_create_qp);
513
514 void rdma_destroy_qp(struct rdma_cm_id *id)
515 {
516         struct rdma_id_private *id_priv;
517
518         id_priv = container_of(id, struct rdma_id_private, id);
519         mutex_lock(&id_priv->qp_mutex);
520         ib_destroy_qp(id_priv->id.qp);
521         id_priv->id.qp = NULL;
522         mutex_unlock(&id_priv->qp_mutex);
523 }
524 EXPORT_SYMBOL(rdma_destroy_qp);
525
526 static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
527                              struct rdma_conn_param *conn_param)
528 {
529         struct ib_qp_attr qp_attr;
530         int qp_attr_mask, ret;
531
532         mutex_lock(&id_priv->qp_mutex);
533         if (!id_priv->id.qp) {
534                 ret = 0;
535                 goto out;
536         }
537
538         /* Need to update QP attributes from default values. */
539         qp_attr.qp_state = IB_QPS_INIT;
540         ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
541         if (ret)
542                 goto out;
543
544         ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
545         if (ret)
546                 goto out;
547
548         qp_attr.qp_state = IB_QPS_RTR;
549         ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
550         if (ret)
551                 goto out;
552
553         if (conn_param)
554                 qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
555         ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
556 out:
557         mutex_unlock(&id_priv->qp_mutex);
558         return ret;
559 }
560
561 static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
562                              struct rdma_conn_param *conn_param)
563 {
564         struct ib_qp_attr qp_attr;
565         int qp_attr_mask, ret;
566
567         mutex_lock(&id_priv->qp_mutex);
568         if (!id_priv->id.qp) {
569                 ret = 0;
570                 goto out;
571         }
572
573         qp_attr.qp_state = IB_QPS_RTS;
574         ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
575         if (ret)
576                 goto out;
577
578         if (conn_param)
579                 qp_attr.max_rd_atomic = conn_param->initiator_depth;
580         ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
581 out:
582         mutex_unlock(&id_priv->qp_mutex);
583         return ret;
584 }
585
586 static int cma_modify_qp_err(struct rdma_id_private *id_priv)
587 {
588         struct ib_qp_attr qp_attr;
589         int ret;
590
591         mutex_lock(&id_priv->qp_mutex);
592         if (!id_priv->id.qp) {
593                 ret = 0;
594                 goto out;
595         }
596
597         qp_attr.qp_state = IB_QPS_ERR;
598         ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
599 out:
600         mutex_unlock(&id_priv->qp_mutex);
601         return ret;
602 }
603
604 static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
605                                struct ib_qp_attr *qp_attr, int *qp_attr_mask)
606 {
607         struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
608         int ret;
609         u16 pkey;
610
611         if (rdma_port_get_link_layer(id_priv->id.device, id_priv->id.port_num) ==
612             IB_LINK_LAYER_INFINIBAND)
613                 pkey = ib_addr_get_pkey(dev_addr);
614         else
615                 pkey = 0xffff;
616
617         ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
618                                   pkey, &qp_attr->pkey_index);
619         if (ret)
620                 return ret;
621
622         qp_attr->port_num = id_priv->id.port_num;
623         *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
624
625         if (cma_is_ud_ps(id_priv->id.ps)) {
626                 ret = cma_set_qkey(id_priv);
627                 if (ret)
628                         return ret;
629
630                 qp_attr->qkey = id_priv->qkey;
631                 *qp_attr_mask |= IB_QP_QKEY;
632         } else {
633                 qp_attr->qp_access_flags = 0;
634                 *qp_attr_mask |= IB_QP_ACCESS_FLAGS;
635         }
636         return 0;
637 }
638
639 int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
640                        int *qp_attr_mask)
641 {
642         struct rdma_id_private *id_priv;
643         int ret = 0;
644
645         id_priv = container_of(id, struct rdma_id_private, id);
646         switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
647         case RDMA_TRANSPORT_IB:
648                 if (!id_priv->cm_id.ib || cma_is_ud_ps(id_priv->id.ps))
649                         ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
650                 else
651                         ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
652                                                  qp_attr_mask);
653                 if (qp_attr->qp_state == IB_QPS_RTR)
654                         qp_attr->rq_psn = id_priv->seq_num;
655                 break;
656         case RDMA_TRANSPORT_IWARP:
657                 if (!id_priv->cm_id.iw) {
658                         qp_attr->qp_access_flags = 0;
659                         *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
660                 } else
661                         ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
662                                                  qp_attr_mask);
663                 break;
664         default:
665                 ret = -ENOSYS;
666                 break;
667         }
668
669         return ret;
670 }
671 EXPORT_SYMBOL(rdma_init_qp_attr);
672
673 static inline int cma_zero_addr(struct sockaddr *addr)
674 {
675         struct in6_addr *ip6;
676
677         if (addr->sa_family == AF_INET)
678                 return ipv4_is_zeronet(
679                         ((struct sockaddr_in *)addr)->sin_addr.s_addr);
680         else {
681                 ip6 = &((struct sockaddr_in6 *) addr)->sin6_addr;
682                 return (ip6->s6_addr32[0] | ip6->s6_addr32[1] |
683                         ip6->s6_addr32[2] | ip6->s6_addr32[3]) == 0;
684         }
685 }
686
687 static inline int cma_loopback_addr(struct sockaddr *addr)
688 {
689         if (addr->sa_family == AF_INET)
690                 return ipv4_is_loopback(
691                         ((struct sockaddr_in *) addr)->sin_addr.s_addr);
692         else
693                 return ipv6_addr_loopback(
694                         &((struct sockaddr_in6 *) addr)->sin6_addr);
695 }
696
697 static inline int cma_any_addr(struct sockaddr *addr)
698 {
699         return cma_zero_addr(addr) || cma_loopback_addr(addr);
700 }
701
702 static inline __be16 cma_port(struct sockaddr *addr)
703 {
704         if (addr->sa_family == AF_INET)
705                 return ((struct sockaddr_in *) addr)->sin_port;
706         else
707                 return ((struct sockaddr_in6 *) addr)->sin6_port;
708 }
709
710 static inline int cma_any_port(struct sockaddr *addr)
711 {
712         return !cma_port(addr);
713 }
714
715 static int cma_get_net_info(void *hdr, enum rdma_port_space ps,
716                             u8 *ip_ver, __be16 *port,
717                             union cma_ip_addr **src, union cma_ip_addr **dst)
718 {
719         switch (ps) {
720         case RDMA_PS_SDP:
721                 if (sdp_get_majv(((struct sdp_hh *) hdr)->sdp_version) !=
722                     SDP_MAJ_VERSION)
723                         return -EINVAL;
724
725                 *ip_ver = sdp_get_ip_ver(hdr);
726                 *port   = ((struct sdp_hh *) hdr)->port;
727                 *src    = &((struct sdp_hh *) hdr)->src_addr;
728                 *dst    = &((struct sdp_hh *) hdr)->dst_addr;
729                 break;
730         default:
731                 if (((struct cma_hdr *) hdr)->cma_version != CMA_VERSION)
732                         return -EINVAL;
733
734                 *ip_ver = cma_get_ip_ver(hdr);
735                 *port   = ((struct cma_hdr *) hdr)->port;
736                 *src    = &((struct cma_hdr *) hdr)->src_addr;
737                 *dst    = &((struct cma_hdr *) hdr)->dst_addr;
738                 break;
739         }
740
741         if (*ip_ver != 4 && *ip_ver != 6)
742                 return -EINVAL;
743         return 0;
744 }
745
746 static void cma_save_net_info(struct rdma_addr *addr,
747                               struct rdma_addr *listen_addr,
748                               u8 ip_ver, __be16 port,
749                               union cma_ip_addr *src, union cma_ip_addr *dst)
750 {
751         struct sockaddr_in *listen4, *ip4;
752         struct sockaddr_in6 *listen6, *ip6;
753
754         switch (ip_ver) {
755         case 4:
756                 listen4 = (struct sockaddr_in *) &listen_addr->src_addr;
757                 ip4 = (struct sockaddr_in *) &addr->src_addr;
758                 ip4->sin_family = listen4->sin_family;
759                 ip4->sin_addr.s_addr = dst->ip4.addr;
760                 ip4->sin_port = listen4->sin_port;
761
762                 ip4 = (struct sockaddr_in *) &addr->dst_addr;
763                 ip4->sin_family = listen4->sin_family;
764                 ip4->sin_addr.s_addr = src->ip4.addr;
765                 ip4->sin_port = port;
766                 break;
767         case 6:
768                 listen6 = (struct sockaddr_in6 *) &listen_addr->src_addr;
769                 ip6 = (struct sockaddr_in6 *) &addr->src_addr;
770                 ip6->sin6_family = listen6->sin6_family;
771                 ip6->sin6_addr = dst->ip6;
772                 ip6->sin6_port = listen6->sin6_port;
773
774                 ip6 = (struct sockaddr_in6 *) &addr->dst_addr;
775                 ip6->sin6_family = listen6->sin6_family;
776                 ip6->sin6_addr = src->ip6;
777                 ip6->sin6_port = port;
778                 break;
779         default:
780                 break;
781         }
782 }
783
784 static inline int cma_user_data_offset(enum rdma_port_space ps)
785 {
786         switch (ps) {
787         case RDMA_PS_SDP:
788                 return 0;
789         default:
790                 return sizeof(struct cma_hdr);
791         }
792 }
793
794 static void cma_cancel_route(struct rdma_id_private *id_priv)
795 {
796         switch (rdma_port_get_link_layer(id_priv->id.device, id_priv->id.port_num)) {
797         case IB_LINK_LAYER_INFINIBAND:
798                 if (id_priv->query)
799                         ib_sa_cancel_query(id_priv->query_id, id_priv->query);
800                 break;
801         default:
802                 break;
803         }
804 }
805
806 static void cma_cancel_listens(struct rdma_id_private *id_priv)
807 {
808         struct rdma_id_private *dev_id_priv;
809
810         /*
811          * Remove from listen_any_list to prevent added devices from spawning
812          * additional listen requests.
813          */
814         mutex_lock(&lock);
815         list_del(&id_priv->list);
816
817         while (!list_empty(&id_priv->listen_list)) {
818                 dev_id_priv = list_entry(id_priv->listen_list.next,
819                                          struct rdma_id_private, listen_list);
820                 /* sync with device removal to avoid duplicate destruction */
821                 list_del_init(&dev_id_priv->list);
822                 list_del(&dev_id_priv->listen_list);
823                 mutex_unlock(&lock);
824
825                 rdma_destroy_id(&dev_id_priv->id);
826                 mutex_lock(&lock);
827         }
828         mutex_unlock(&lock);
829 }
830
831 static void cma_cancel_operation(struct rdma_id_private *id_priv,
832                                  enum cma_state state)
833 {
834         switch (state) {
835         case CMA_ADDR_QUERY:
836                 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
837                 break;
838         case CMA_ROUTE_QUERY:
839                 cma_cancel_route(id_priv);
840                 break;
841         case CMA_LISTEN:
842                 if (cma_any_addr((struct sockaddr *) &id_priv->id.route.addr.src_addr)
843                                 && !id_priv->cma_dev)
844                         cma_cancel_listens(id_priv);
845                 break;
846         default:
847                 break;
848         }
849 }
850
851 static void cma_release_port(struct rdma_id_private *id_priv)
852 {
853         struct rdma_bind_list *bind_list = id_priv->bind_list;
854
855         if (!bind_list)
856                 return;
857
858         mutex_lock(&lock);
859         hlist_del(&id_priv->node);
860         if (hlist_empty(&bind_list->owners)) {
861                 idr_remove(bind_list->ps, bind_list->port);
862                 kfree(bind_list);
863         }
864         mutex_unlock(&lock);
865         if (id_priv->sock)
866                 sock_release(id_priv->sock);
867 }
868
869 static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
870 {
871         struct cma_multicast *mc;
872
873         while (!list_empty(&id_priv->mc_list)) {
874                 mc = container_of(id_priv->mc_list.next,
875                                   struct cma_multicast, list);
876                 list_del(&mc->list);
877                 switch (rdma_port_get_link_layer(id_priv->cma_dev->device, id_priv->id.port_num)) {
878                 case IB_LINK_LAYER_INFINIBAND:
879                         ib_sa_free_multicast(mc->multicast.ib);
880                         kfree(mc);
881                         break;
882                 case IB_LINK_LAYER_ETHERNET:
883                         kref_put(&mc->mcref, release_mc);
884                         break;
885                 default:
886                         break;
887                 }
888         }
889 }
890
891 void rdma_destroy_id(struct rdma_cm_id *id)
892 {
893         struct rdma_id_private *id_priv;
894         enum cma_state state;
895
896         id_priv = container_of(id, struct rdma_id_private, id);
897         state = cma_exch(id_priv, CMA_DESTROYING);
898         cma_cancel_operation(id_priv, state);
899
900         mutex_lock(&lock);
901         if (id_priv->cma_dev) {
902                 mutex_unlock(&lock);
903                 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
904                 case RDMA_TRANSPORT_IB:
905                         if (id_priv->cm_id.ib && !IS_ERR(id_priv->cm_id.ib))
906                                 ib_destroy_cm_id(id_priv->cm_id.ib);
907                         break;
908                 case RDMA_TRANSPORT_IWARP:
909                         if (id_priv->cm_id.iw && !IS_ERR(id_priv->cm_id.iw))
910                                 iw_destroy_cm_id(id_priv->cm_id.iw);
911                         break;
912                 default:
913                         break;
914                 }
915                 cma_leave_mc_groups(id_priv);
916                 mutex_lock(&lock);
917                 cma_detach_from_dev(id_priv);
918         }
919         mutex_unlock(&lock);
920
921         cma_release_port(id_priv);
922         cma_deref_id(id_priv);
923         wait_for_completion(&id_priv->comp);
924
925         if (id_priv->internal_id)
926                 cma_deref_id(id_priv->id.context);
927
928         kfree(id_priv->id.route.path_rec);
929         kfree(id_priv);
930 }
931 EXPORT_SYMBOL(rdma_destroy_id);
932
933 static int cma_rep_recv(struct rdma_id_private *id_priv)
934 {
935         int ret;
936
937         ret = cma_modify_qp_rtr(id_priv, NULL);
938         if (ret)
939                 goto reject;
940
941         ret = cma_modify_qp_rts(id_priv, NULL);
942         if (ret)
943                 goto reject;
944
945         ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
946         if (ret)
947                 goto reject;
948
949         return 0;
950 reject:
951         cma_modify_qp_err(id_priv);
952         ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
953                        NULL, 0, NULL, 0);
954         return ret;
955 }
956
957 static int cma_verify_rep(struct rdma_id_private *id_priv, void *data)
958 {
959         if (id_priv->id.ps == RDMA_PS_SDP &&
960             sdp_get_majv(((struct sdp_hah *) data)->sdp_version) !=
961             SDP_MAJ_VERSION)
962                 return -EINVAL;
963
964         return 0;
965 }
966
967 static void cma_set_rep_event_data(struct rdma_cm_event *event,
968                                    struct ib_cm_rep_event_param *rep_data,
969                                    void *private_data)
970 {
971         event->param.conn.private_data = private_data;
972         event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
973         event->param.conn.responder_resources = rep_data->responder_resources;
974         event->param.conn.initiator_depth = rep_data->initiator_depth;
975         event->param.conn.flow_control = rep_data->flow_control;
976         event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
977         event->param.conn.srq = rep_data->srq;
978         event->param.conn.qp_num = rep_data->remote_qpn;
979 }
980
981 static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
982 {
983         struct rdma_id_private *id_priv = cm_id->context;
984         struct rdma_cm_event event;
985         int ret = 0;
986
987         if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
988                 cma_disable_callback(id_priv, CMA_CONNECT)) ||
989             (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
990                 cma_disable_callback(id_priv, CMA_DISCONNECT)))
991                 return 0;
992
993         memset(&event, 0, sizeof event);
994         switch (ib_event->event) {
995         case IB_CM_REQ_ERROR:
996         case IB_CM_REP_ERROR:
997                 event.event = RDMA_CM_EVENT_UNREACHABLE;
998                 event.status = -ETIMEDOUT;
999                 break;
1000         case IB_CM_REP_RECEIVED:
1001                 event.status = cma_verify_rep(id_priv, ib_event->private_data);
1002                 if (event.status)
1003                         event.event = RDMA_CM_EVENT_CONNECT_ERROR;
1004                 else if (id_priv->id.qp && id_priv->id.ps != RDMA_PS_SDP) {
1005                         event.status = cma_rep_recv(id_priv);
1006                         event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
1007                                                      RDMA_CM_EVENT_ESTABLISHED;
1008                 } else
1009                         event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
1010                 cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
1011                                        ib_event->private_data);
1012                 break;
1013         case IB_CM_RTU_RECEIVED:
1014         case IB_CM_USER_ESTABLISHED:
1015                 event.event = RDMA_CM_EVENT_ESTABLISHED;
1016                 break;
1017         case IB_CM_DREQ_ERROR:
1018                 event.status = -ETIMEDOUT; /* fall through */
1019         case IB_CM_DREQ_RECEIVED:
1020         case IB_CM_DREP_RECEIVED:
1021                 if (!cma_comp_exch(id_priv, CMA_CONNECT, CMA_DISCONNECT))
1022                         goto out;
1023                 event.event = RDMA_CM_EVENT_DISCONNECTED;
1024                 break;
1025         case IB_CM_TIMEWAIT_EXIT:
1026                 event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
1027                 break;
1028         case IB_CM_MRA_RECEIVED:
1029                 /* ignore event */
1030                 goto out;
1031         case IB_CM_REJ_RECEIVED:
1032                 cma_modify_qp_err(id_priv);
1033                 event.status = ib_event->param.rej_rcvd.reason;
1034                 event.event = RDMA_CM_EVENT_REJECTED;
1035                 event.param.conn.private_data = ib_event->private_data;
1036                 event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
1037                 break;
1038         default:
1039                 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
1040                        ib_event->event);
1041                 goto out;
1042         }
1043
1044         ret = id_priv->id.event_handler(&id_priv->id, &event);
1045         if (ret) {
1046                 /* Destroy the CM ID by returning a non-zero value. */
1047                 id_priv->cm_id.ib = NULL;
1048                 cma_exch(id_priv, CMA_DESTROYING);
1049                 mutex_unlock(&id_priv->handler_mutex);
1050                 rdma_destroy_id(&id_priv->id);
1051                 return ret;
1052         }
1053 out:
1054         mutex_unlock(&id_priv->handler_mutex);
1055         return ret;
1056 }
1057
1058 static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
1059                                                struct ib_cm_event *ib_event)
1060 {
1061         struct rdma_id_private *id_priv;
1062         struct rdma_cm_id *id;
1063         struct rdma_route *rt;
1064         union cma_ip_addr *src, *dst;
1065         __be16 port;
1066         u8 ip_ver;
1067         int ret;
1068
1069         if (cma_get_net_info(ib_event->private_data, listen_id->ps,
1070                              &ip_ver, &port, &src, &dst))
1071                 goto err;
1072
1073         id = rdma_create_id(listen_id->event_handler, listen_id->context,
1074                             listen_id->ps);
1075         if (IS_ERR(id))
1076                 goto err;
1077
1078         cma_save_net_info(&id->route.addr, &listen_id->route.addr,
1079                           ip_ver, port, src, dst);
1080
1081         rt = &id->route;
1082         rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
1083         rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
1084                                GFP_KERNEL);
1085         if (!rt->path_rec)
1086                 goto destroy_id;
1087
1088         rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
1089         if (rt->num_paths == 2)
1090                 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
1091
1092         if (cma_any_addr((struct sockaddr *) &rt->addr.src_addr)) {
1093                 rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
1094                 rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
1095                 ib_addr_set_pkey(&rt->addr.dev_addr, rt->path_rec[0].pkey);
1096         } else {
1097                 ret = rdma_translate_ip((struct sockaddr *) &rt->addr.src_addr,
1098                                         &rt->addr.dev_addr);
1099                 if (ret)
1100                         goto destroy_id;
1101         }
1102         rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
1103
1104         id_priv = container_of(id, struct rdma_id_private, id);
1105         id_priv->state = CMA_CONNECT;
1106         return id_priv;
1107
1108 destroy_id:
1109         rdma_destroy_id(id);
1110 err:
1111         return NULL;
1112 }
1113
1114 static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
1115                                               struct ib_cm_event *ib_event)
1116 {
1117         struct rdma_id_private *id_priv;
1118         struct rdma_cm_id *id;
1119         union cma_ip_addr *src, *dst;
1120         __be16 port;
1121         u8 ip_ver;
1122         int ret;
1123
1124         id = rdma_create_id(listen_id->event_handler, listen_id->context,
1125                             listen_id->ps);
1126         if (IS_ERR(id))
1127                 return NULL;
1128
1129
1130         if (cma_get_net_info(ib_event->private_data, listen_id->ps,
1131                              &ip_ver, &port, &src, &dst))
1132                 goto err;
1133
1134         cma_save_net_info(&id->route.addr, &listen_id->route.addr,
1135                           ip_ver, port, src, dst);
1136
1137         if (!cma_any_addr((struct sockaddr *) &id->route.addr.src_addr)) {
1138                 ret = rdma_translate_ip((struct sockaddr *) &id->route.addr.src_addr,
1139                                         &id->route.addr.dev_addr);
1140                 if (ret)
1141                         goto err;
1142         }
1143
1144         id_priv = container_of(id, struct rdma_id_private, id);
1145         id_priv->state = CMA_CONNECT;
1146         return id_priv;
1147 err:
1148         rdma_destroy_id(id);
1149         return NULL;
1150 }
1151
1152 static void cma_set_req_event_data(struct rdma_cm_event *event,
1153                                    struct ib_cm_req_event_param *req_data,
1154                                    void *private_data, int offset)
1155 {
1156         event->param.conn.private_data = private_data + offset;
1157         event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
1158         event->param.conn.responder_resources = req_data->responder_resources;
1159         event->param.conn.initiator_depth = req_data->initiator_depth;
1160         event->param.conn.flow_control = req_data->flow_control;
1161         event->param.conn.retry_count = req_data->retry_count;
1162         event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
1163         event->param.conn.srq = req_data->srq;
1164         event->param.conn.qp_num = req_data->remote_qpn;
1165 }
1166
1167 static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1168 {
1169         struct rdma_id_private *listen_id, *conn_id;
1170         struct rdma_cm_event event;
1171         int offset, ret;
1172
1173         listen_id = cm_id->context;
1174         if (cma_disable_callback(listen_id, CMA_LISTEN))
1175                 return -ECONNABORTED;
1176
1177         memset(&event, 0, sizeof event);
1178         offset = cma_user_data_offset(listen_id->id.ps);
1179         event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1180         if (cma_is_ud_ps(listen_id->id.ps)) {
1181                 conn_id = cma_new_udp_id(&listen_id->id, ib_event);
1182                 event.param.ud.private_data = ib_event->private_data + offset;
1183                 event.param.ud.private_data_len =
1184                                 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
1185         } else {
1186                 conn_id = cma_new_conn_id(&listen_id->id, ib_event);
1187                 cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
1188                                        ib_event->private_data, offset);
1189         }
1190         if (!conn_id) {
1191                 ret = -ENOMEM;
1192                 goto out;
1193         }
1194
1195         mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
1196         mutex_lock(&lock);
1197         ret = cma_acquire_dev(conn_id);
1198         mutex_unlock(&lock);
1199         if (ret)
1200                 goto release_conn_id;
1201
1202         conn_id->cm_id.ib = cm_id;
1203         cm_id->context = conn_id;
1204         cm_id->cm_handler = cma_ib_handler;
1205
1206         ret = conn_id->id.event_handler(&conn_id->id, &event);
1207         if (!ret) {
1208                 /*
1209                  * Acquire mutex to prevent user executing rdma_destroy_id()
1210                  * while we're accessing the cm_id.
1211                  */
1212                 mutex_lock(&lock);
1213                 if (cma_comp(conn_id, CMA_CONNECT) &&
1214                     !cma_is_ud_ps(conn_id->id.ps))
1215                         ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
1216                 mutex_unlock(&lock);
1217                 mutex_unlock(&conn_id->handler_mutex);
1218                 goto out;
1219         }
1220
1221         /* Destroy the CM ID by returning a non-zero value. */
1222         conn_id->cm_id.ib = NULL;
1223
1224 release_conn_id:
1225         cma_exch(conn_id, CMA_DESTROYING);
1226         mutex_unlock(&conn_id->handler_mutex);
1227         rdma_destroy_id(&conn_id->id);
1228
1229 out:
1230         mutex_unlock(&listen_id->handler_mutex);
1231         return ret;
1232 }
1233
1234 static __be64 cma_get_service_id(enum rdma_port_space ps, struct sockaddr *addr)
1235 {
1236         return cpu_to_be64(((u64)ps << 16) + be16_to_cpu(cma_port(addr)));
1237 }
1238
1239 static void cma_set_compare_data(enum rdma_port_space ps, struct sockaddr *addr,
1240                                  struct ib_cm_compare_data *compare)
1241 {
1242         struct cma_hdr *cma_data, *cma_mask;
1243         struct sdp_hh *sdp_data, *sdp_mask;
1244         __be32 ip4_addr;
1245         struct in6_addr ip6_addr;
1246
1247         memset(compare, 0, sizeof *compare);
1248         cma_data = (void *) compare->data;
1249         cma_mask = (void *) compare->mask;
1250         sdp_data = (void *) compare->data;
1251         sdp_mask = (void *) compare->mask;
1252
1253         switch (addr->sa_family) {
1254         case AF_INET:
1255                 ip4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1256                 if (ps == RDMA_PS_SDP) {
1257                         sdp_set_ip_ver(sdp_data, 4);
1258                         sdp_set_ip_ver(sdp_mask, 0xF);
1259                         sdp_data->dst_addr.ip4.addr = ip4_addr;
1260                         sdp_mask->dst_addr.ip4.addr = htonl(~0);
1261                 } else {
1262                         cma_set_ip_ver(cma_data, 4);
1263                         cma_set_ip_ver(cma_mask, 0xF);
1264                         cma_data->dst_addr.ip4.addr = ip4_addr;
1265                         cma_mask->dst_addr.ip4.addr = htonl(~0);
1266                 }
1267                 break;
1268 #ifdef INET6
1269         case AF_INET6:
1270                 ip6_addr = ((struct sockaddr_in6 *) addr)->sin6_addr;
1271                 if (ps == RDMA_PS_SDP) {
1272                         sdp_set_ip_ver(sdp_data, 6);
1273                         sdp_set_ip_ver(sdp_mask, 0xF);
1274                         sdp_data->dst_addr.ip6 = ip6_addr;
1275                         memset(&sdp_mask->dst_addr.ip6, 0xFF,
1276                                sizeof sdp_mask->dst_addr.ip6);
1277                 } else {
1278                         cma_set_ip_ver(cma_data, 6);
1279                         cma_set_ip_ver(cma_mask, 0xF);
1280                         cma_data->dst_addr.ip6 = ip6_addr;
1281                         memset(&cma_mask->dst_addr.ip6, 0xFF,
1282                                sizeof cma_mask->dst_addr.ip6);
1283                 }
1284                 break;
1285 #endif
1286         default:
1287                 break;
1288         }
1289 }
1290
1291 static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
1292 {
1293         struct rdma_id_private *id_priv = iw_id->context;
1294         struct rdma_cm_event event;
1295         struct sockaddr_in *sin;
1296         int ret = 0;
1297
1298         if (cma_disable_callback(id_priv, CMA_CONNECT))
1299                 return 0;
1300
1301         memset(&event, 0, sizeof event);
1302         switch (iw_event->event) {
1303         case IW_CM_EVENT_CLOSE:
1304                 event.event = RDMA_CM_EVENT_DISCONNECTED;
1305                 break;
1306         case IW_CM_EVENT_CONNECT_REPLY:
1307                 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1308                 *sin = iw_event->local_addr;
1309                 sin = (struct sockaddr_in *) &id_priv->id.route.addr.dst_addr;
1310                 *sin = iw_event->remote_addr;
1311                 switch (iw_event->status) {
1312                 case 0:
1313                         event.event = RDMA_CM_EVENT_ESTABLISHED;
1314                         break;
1315                 case -ECONNRESET:
1316                 case -ECONNREFUSED:
1317                         event.event = RDMA_CM_EVENT_REJECTED;
1318                         break;
1319                 case -ETIMEDOUT:
1320                         event.event = RDMA_CM_EVENT_UNREACHABLE;
1321                         break;
1322                 default:
1323                         event.event = RDMA_CM_EVENT_CONNECT_ERROR;
1324                         break;
1325                 }
1326                 break;
1327         case IW_CM_EVENT_ESTABLISHED:
1328                 event.event = RDMA_CM_EVENT_ESTABLISHED;
1329                 break;
1330         default:
1331                 BUG_ON(1);
1332         }
1333
1334         event.status = iw_event->status;
1335         event.param.conn.private_data = iw_event->private_data;
1336         event.param.conn.private_data_len = iw_event->private_data_len;
1337         ret = id_priv->id.event_handler(&id_priv->id, &event);
1338         if (ret) {
1339                 /* Destroy the CM ID by returning a non-zero value. */
1340                 id_priv->cm_id.iw = NULL;
1341                 cma_exch(id_priv, CMA_DESTROYING);
1342                 mutex_unlock(&id_priv->handler_mutex);
1343                 rdma_destroy_id(&id_priv->id);
1344                 return ret;
1345         }
1346
1347         mutex_unlock(&id_priv->handler_mutex);
1348         return ret;
1349 }
1350
1351 static int iw_conn_req_handler(struct iw_cm_id *cm_id,
1352                                struct iw_cm_event *iw_event)
1353 {
1354         struct rdma_cm_id *new_cm_id;
1355         struct rdma_id_private *listen_id, *conn_id;
1356         struct sockaddr_in *sin;
1357         struct net_device *dev = NULL;
1358         struct rdma_cm_event event;
1359         int ret;
1360         struct ib_device_attr attr;
1361
1362         listen_id = cm_id->context;
1363         if (cma_disable_callback(listen_id, CMA_LISTEN))
1364                 return -ECONNABORTED;
1365
1366         /* Create a new RDMA id for the new IW CM ID */
1367         new_cm_id = rdma_create_id(listen_id->id.event_handler,
1368                                    listen_id->id.context,
1369                                    RDMA_PS_TCP);
1370         if (IS_ERR(new_cm_id)) {
1371                 ret = -ENOMEM;
1372                 goto out;
1373         }
1374         conn_id = container_of(new_cm_id, struct rdma_id_private, id);
1375         mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
1376         conn_id->state = CMA_CONNECT;
1377
1378         dev = ip_dev_find(NULL, iw_event->local_addr.sin_addr.s_addr);
1379         if (!dev) {
1380                 ret = -EADDRNOTAVAIL;
1381                 mutex_unlock(&conn_id->handler_mutex);
1382                 rdma_destroy_id(new_cm_id);
1383                 goto out;
1384         }
1385         ret = rdma_copy_addr(&conn_id->id.route.addr.dev_addr, dev, NULL);
1386         if (ret) {
1387                 mutex_unlock(&conn_id->handler_mutex);
1388                 rdma_destroy_id(new_cm_id);
1389                 goto out;
1390         }
1391
1392         mutex_lock(&lock);
1393         ret = cma_acquire_dev(conn_id);
1394         mutex_unlock(&lock);
1395         if (ret) {
1396                 mutex_unlock(&conn_id->handler_mutex);
1397                 rdma_destroy_id(new_cm_id);
1398                 goto out;
1399         }
1400
1401         conn_id->cm_id.iw = cm_id;
1402         cm_id->context = conn_id;
1403         cm_id->cm_handler = cma_iw_handler;
1404
1405         sin = (struct sockaddr_in *) &new_cm_id->route.addr.src_addr;
1406         *sin = iw_event->local_addr;
1407         sin = (struct sockaddr_in *) &new_cm_id->route.addr.dst_addr;
1408         *sin = iw_event->remote_addr;
1409
1410         ret = ib_query_device(conn_id->id.device, &attr);
1411         if (ret) {
1412                 mutex_unlock(&conn_id->handler_mutex);
1413                 rdma_destroy_id(new_cm_id);
1414                 goto out;
1415         }
1416
1417         memset(&event, 0, sizeof event);
1418         event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1419         event.param.conn.private_data = iw_event->private_data;
1420         event.param.conn.private_data_len = iw_event->private_data_len;
1421         event.param.conn.initiator_depth = attr.max_qp_init_rd_atom;
1422         event.param.conn.responder_resources = attr.max_qp_rd_atom;
1423         ret = conn_id->id.event_handler(&conn_id->id, &event);
1424         if (ret) {
1425                 /* User wants to destroy the CM ID */
1426                 conn_id->cm_id.iw = NULL;
1427                 cma_exch(conn_id, CMA_DESTROYING);
1428                 mutex_unlock(&conn_id->handler_mutex);
1429                 rdma_destroy_id(&conn_id->id);
1430                 goto out;
1431         }
1432
1433         mutex_unlock(&conn_id->handler_mutex);
1434
1435 out:
1436         if (dev)
1437                 dev_put(dev);
1438         mutex_unlock(&listen_id->handler_mutex);
1439         return ret;
1440 }
1441
1442 static int cma_ib_listen(struct rdma_id_private *id_priv)
1443 {
1444         struct ib_cm_compare_data compare_data;
1445         struct sockaddr *addr;
1446         __be64 svc_id;
1447         int ret;
1448
1449         id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_req_handler,
1450                                             id_priv);
1451         if (IS_ERR(id_priv->cm_id.ib))
1452                 return PTR_ERR(id_priv->cm_id.ib);
1453
1454         addr = (struct sockaddr *) &id_priv->id.route.addr.src_addr;
1455         svc_id = cma_get_service_id(id_priv->id.ps, addr);
1456         if (cma_any_addr(addr))
1457                 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, NULL);
1458         else {
1459                 cma_set_compare_data(id_priv->id.ps, addr, &compare_data);
1460                 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, &compare_data);
1461         }
1462
1463         if (ret) {
1464                 ib_destroy_cm_id(id_priv->cm_id.ib);
1465                 id_priv->cm_id.ib = NULL;
1466         }
1467
1468         return ret;
1469 }
1470
1471 static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
1472 {
1473         int ret;
1474         struct sockaddr_in *sin;
1475
1476         id_priv->cm_id.iw = iw_create_cm_id(id_priv->id.device,
1477                                             iw_conn_req_handler,
1478                                             id_priv);
1479         if (IS_ERR(id_priv->cm_id.iw))
1480                 return PTR_ERR(id_priv->cm_id.iw);
1481
1482         sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1483         id_priv->cm_id.iw->local_addr = *sin;
1484
1485         ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
1486
1487         if (ret) {
1488                 iw_destroy_cm_id(id_priv->cm_id.iw);
1489                 id_priv->cm_id.iw = NULL;
1490         }
1491
1492         return ret;
1493 }
1494
1495 static int cma_listen_handler(struct rdma_cm_id *id,
1496                               struct rdma_cm_event *event)
1497 {
1498         struct rdma_id_private *id_priv = id->context;
1499
1500         id->context = id_priv->id.context;
1501         id->event_handler = id_priv->id.event_handler;
1502         return id_priv->id.event_handler(id, event);
1503 }
1504
1505 static void cma_listen_on_dev(struct rdma_id_private *id_priv,
1506                               struct cma_device *cma_dev)
1507 {
1508         struct rdma_id_private *dev_id_priv;
1509         struct rdma_cm_id *id;
1510         int ret;
1511
1512         id = rdma_create_id(cma_listen_handler, id_priv, id_priv->id.ps);
1513         if (IS_ERR(id))
1514                 return;
1515
1516         dev_id_priv = container_of(id, struct rdma_id_private, id);
1517
1518         dev_id_priv->state = CMA_ADDR_BOUND;
1519         memcpy(&id->route.addr.src_addr, &id_priv->id.route.addr.src_addr,
1520                ip_addr_size((struct sockaddr *) &id_priv->id.route.addr.src_addr));
1521
1522         cma_attach_to_dev(dev_id_priv, cma_dev);
1523         list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
1524         atomic_inc(&id_priv->refcount);
1525         dev_id_priv->internal_id = 1;
1526
1527         ret = rdma_listen(id, id_priv->backlog);
1528         if (ret)
1529                 printk(KERN_WARNING "RDMA CMA: cma_listen_on_dev, error %d, "
1530                        "listening on device %s\n", ret, cma_dev->device->name);
1531 }
1532
1533 static void cma_listen_on_all(struct rdma_id_private *id_priv)
1534 {
1535         struct cma_device *cma_dev;
1536
1537         mutex_lock(&lock);
1538         list_add_tail(&id_priv->list, &listen_any_list);
1539         list_for_each_entry(cma_dev, &dev_list, list)
1540                 cma_listen_on_dev(id_priv, cma_dev);
1541         mutex_unlock(&lock);
1542 }
1543
1544 int rdma_listen(struct rdma_cm_id *id, int backlog)
1545 {
1546         struct rdma_id_private *id_priv;
1547         int ret;
1548
1549         id_priv = container_of(id, struct rdma_id_private, id);
1550         if (id_priv->state == CMA_IDLE) {
1551                 ((struct sockaddr *) &id->route.addr.src_addr)->sa_family = AF_INET;
1552                 ret = rdma_bind_addr(id, (struct sockaddr *) &id->route.addr.src_addr);
1553                 if (ret)
1554                         return ret;
1555         }
1556
1557         if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_LISTEN))
1558                 return -EINVAL;
1559
1560         id_priv->backlog = backlog;
1561         if (id->device) {
1562                 switch (rdma_node_get_transport(id->device->node_type)) {
1563                 case RDMA_TRANSPORT_IB:
1564                         ret = cma_ib_listen(id_priv);
1565                         if (ret)
1566                                 goto err;
1567                         break;
1568                 case RDMA_TRANSPORT_IWARP:
1569                         ret = cma_iw_listen(id_priv, backlog);
1570                         if (ret)
1571                                 goto err;
1572                         break;
1573                 default:
1574                         ret = -ENOSYS;
1575                         goto err;
1576                 }
1577         } else
1578                 cma_listen_on_all(id_priv);
1579
1580         return 0;
1581 err:
1582         id_priv->backlog = 0;
1583         cma_comp_exch(id_priv, CMA_LISTEN, CMA_ADDR_BOUND);
1584         return ret;
1585 }
1586 EXPORT_SYMBOL(rdma_listen);
1587
1588 void rdma_set_service_type(struct rdma_cm_id *id, int tos)
1589 {
1590         struct rdma_id_private *id_priv;
1591
1592         id_priv = container_of(id, struct rdma_id_private, id);
1593         id_priv->tos = (u8) tos;
1594 }
1595 EXPORT_SYMBOL(rdma_set_service_type);
1596
1597 static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
1598                               void *context)
1599 {
1600         struct cma_work *work = context;
1601         struct rdma_route *route;
1602
1603         route = &work->id->id.route;
1604
1605         if (!status) {
1606                 route->num_paths = 1;
1607                 *route->path_rec = *path_rec;
1608         } else {
1609                 work->old_state = CMA_ROUTE_QUERY;
1610                 work->new_state = CMA_ADDR_RESOLVED;
1611                 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
1612                 work->event.status = status;
1613         }
1614
1615         queue_work(cma_wq, &work->work);
1616 }
1617
1618 static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
1619                               struct cma_work *work)
1620 {
1621         struct rdma_addr *addr = &id_priv->id.route.addr;
1622         struct ib_sa_path_rec path_rec;
1623         ib_sa_comp_mask comp_mask;
1624         struct sockaddr_in6 *sin6;
1625
1626         memset(&path_rec, 0, sizeof path_rec);
1627         rdma_addr_get_sgid(&addr->dev_addr, &path_rec.sgid);
1628         rdma_addr_get_dgid(&addr->dev_addr, &path_rec.dgid);
1629         path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(&addr->dev_addr));
1630         path_rec.numb_path = 1;
1631         path_rec.reversible = 1;
1632         path_rec.service_id = cma_get_service_id(id_priv->id.ps,
1633                                                         (struct sockaddr *) &addr->dst_addr);
1634
1635         comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
1636                     IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
1637                     IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
1638
1639         if (addr->src_addr.ss_family == AF_INET) {
1640                 path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
1641                 comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
1642         } else {
1643                 sin6 = (struct sockaddr_in6 *) &addr->src_addr;
1644                 path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
1645                 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
1646         }
1647
1648         if (tavor_quirk) {
1649                 path_rec.mtu_selector = IB_SA_LT;
1650                 path_rec.mtu = IB_MTU_2048;
1651         }
1652
1653         id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
1654                                                id_priv->id.port_num, &path_rec,
1655                                                comp_mask, timeout_ms,
1656                                                GFP_KERNEL, cma_query_handler,
1657                                                work, &id_priv->query);
1658
1659         return (id_priv->query_id < 0) ? id_priv->query_id : 0;
1660 }
1661
1662 static void cma_work_handler(struct work_struct *_work)
1663 {
1664         struct cma_work *work = container_of(_work, struct cma_work, work);
1665         struct rdma_id_private *id_priv = work->id;
1666         int destroy = 0;
1667
1668         mutex_lock(&id_priv->handler_mutex);
1669         if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
1670                 goto out;
1671
1672         if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
1673                 cma_exch(id_priv, CMA_DESTROYING);
1674                 destroy = 1;
1675         }
1676 out:
1677         mutex_unlock(&id_priv->handler_mutex);
1678         cma_deref_id(id_priv);
1679         if (destroy)
1680                 rdma_destroy_id(&id_priv->id);
1681         kfree(work);
1682 }
1683
1684 static void cma_ndev_work_handler(struct work_struct *_work)
1685 {
1686         struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work);
1687         struct rdma_id_private *id_priv = work->id;
1688         int destroy = 0;
1689
1690         mutex_lock(&id_priv->handler_mutex);
1691         if (id_priv->state == CMA_DESTROYING ||
1692             id_priv->state == CMA_DEVICE_REMOVAL)
1693                 goto out;
1694
1695         if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
1696                 cma_exch(id_priv, CMA_DESTROYING);
1697                 destroy = 1;
1698         }
1699
1700 out:
1701         mutex_unlock(&id_priv->handler_mutex);
1702         cma_deref_id(id_priv);
1703         if (destroy)
1704                 rdma_destroy_id(&id_priv->id);
1705         kfree(work);
1706 }
1707
1708 static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
1709 {
1710         struct rdma_route *route = &id_priv->id.route;
1711         struct cma_work *work;
1712         int ret;
1713
1714         work = kzalloc(sizeof *work, GFP_KERNEL);
1715         if (!work)
1716                 return -ENOMEM;
1717
1718         work->id = id_priv;
1719         INIT_WORK(&work->work, cma_work_handler);
1720         work->old_state = CMA_ROUTE_QUERY;
1721         work->new_state = CMA_ROUTE_RESOLVED;
1722         work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1723
1724         route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
1725         if (!route->path_rec) {
1726                 ret = -ENOMEM;
1727                 goto err1;
1728         }
1729
1730         ret = cma_query_ib_route(id_priv, timeout_ms, work);
1731         if (ret)
1732                 goto err2;
1733
1734         return 0;
1735 err2:
1736         kfree(route->path_rec);
1737         route->path_rec = NULL;
1738 err1:
1739         kfree(work);
1740         return ret;
1741 }
1742
1743 int rdma_set_ib_paths(struct rdma_cm_id *id,
1744                       struct ib_sa_path_rec *path_rec, int num_paths)
1745 {
1746         struct rdma_id_private *id_priv;
1747         int ret;
1748
1749         id_priv = container_of(id, struct rdma_id_private, id);
1750         if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ROUTE_RESOLVED))
1751                 return -EINVAL;
1752
1753         id->route.path_rec = kmalloc(sizeof *path_rec * num_paths, GFP_KERNEL);
1754         if (!id->route.path_rec) {
1755                 ret = -ENOMEM;
1756                 goto err;
1757         }
1758
1759         memcpy(id->route.path_rec, path_rec, sizeof *path_rec * num_paths);
1760         return 0;
1761 err:
1762         cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_ADDR_RESOLVED);
1763         return ret;
1764 }
1765 EXPORT_SYMBOL(rdma_set_ib_paths);
1766
1767 static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
1768 {
1769         struct cma_work *work;
1770
1771         work = kzalloc(sizeof *work, GFP_KERNEL);
1772         if (!work)
1773                 return -ENOMEM;
1774
1775         work->id = id_priv;
1776         INIT_WORK(&work->work, cma_work_handler);
1777         work->old_state = CMA_ROUTE_QUERY;
1778         work->new_state = CMA_ROUTE_RESOLVED;
1779         work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1780         queue_work(cma_wq, &work->work);
1781         return 0;
1782 }
1783
1784 static u8 tos_to_sl(u8 tos)
1785 {
1786         return def_prec2sl & 7;
1787 }
1788
1789 static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
1790 {
1791         struct rdma_route *route = &id_priv->id.route;
1792         struct rdma_addr *addr = &route->addr;
1793         struct cma_work *work;
1794         int ret;
1795         struct sockaddr_in *src_addr = (struct sockaddr_in *)&route->addr.src_addr;
1796         struct sockaddr_in *dst_addr = (struct sockaddr_in *)&route->addr.dst_addr;
1797         struct net_device *ndev = NULL;
1798         u16 vid;
1799
1800         if (src_addr->sin_family != dst_addr->sin_family)
1801                 return -EINVAL;
1802
1803         work = kzalloc(sizeof *work, GFP_KERNEL);
1804         if (!work)
1805                 return -ENOMEM;
1806
1807         work->id = id_priv;
1808         INIT_WORK(&work->work, cma_work_handler);
1809
1810         route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
1811         if (!route->path_rec) {
1812                 ret = -ENOMEM;
1813                 goto err1;
1814         }
1815
1816         route->num_paths = 1;
1817
1818         if (addr->dev_addr.bound_dev_if)
1819                 ndev = dev_get_by_index(&init_net, addr->dev_addr.bound_dev_if);
1820         if (!ndev) {
1821                 ret = -ENODEV;
1822                 goto err2;
1823         }
1824
1825         vid = rdma_vlan_dev_vlan_id(ndev);
1826
1827         iboe_mac_vlan_to_ll(&route->path_rec->sgid, addr->dev_addr.src_dev_addr, vid);
1828         iboe_mac_vlan_to_ll(&route->path_rec->dgid, addr->dev_addr.dst_dev_addr, vid);
1829
1830         route->path_rec->hop_limit = 1;
1831         route->path_rec->reversible = 1;
1832         route->path_rec->pkey = cpu_to_be16(0xffff);
1833         route->path_rec->mtu_selector = IB_SA_EQ;
1834         route->path_rec->sl = tos_to_sl(id_priv->tos);
1835
1836 #ifdef __linux__
1837         route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
1838 #else
1839         route->path_rec->mtu = iboe_get_mtu(ndev->if_mtu);
1840 #endif
1841         route->path_rec->rate_selector = IB_SA_EQ;
1842         route->path_rec->rate = iboe_get_rate(ndev);
1843         dev_put(ndev);
1844         route->path_rec->packet_life_time_selector = IB_SA_EQ;
1845         route->path_rec->packet_life_time = IBOE_PACKET_LIFETIME;
1846         if (!route->path_rec->mtu) {
1847                 ret = -EINVAL;
1848                 goto err2;
1849         }
1850
1851         work->old_state = CMA_ROUTE_QUERY;
1852         work->new_state = CMA_ROUTE_RESOLVED;
1853         work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1854         work->event.status = 0;
1855
1856         queue_work(cma_wq, &work->work);
1857
1858         return 0;
1859
1860 err2:
1861         kfree(route->path_rec);
1862         route->path_rec = NULL;
1863 err1:
1864         kfree(work);
1865         return ret;
1866 }
1867
1868 int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
1869 {
1870         struct rdma_id_private *id_priv;
1871         int ret;
1872
1873         id_priv = container_of(id, struct rdma_id_private, id);
1874         if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ROUTE_QUERY))
1875                 return -EINVAL;
1876
1877         atomic_inc(&id_priv->refcount);
1878         switch (rdma_node_get_transport(id->device->node_type)) {
1879         case RDMA_TRANSPORT_IB:
1880                 switch (rdma_port_get_link_layer(id->device, id->port_num)) {
1881                 case IB_LINK_LAYER_INFINIBAND:
1882                         ret = cma_resolve_ib_route(id_priv, timeout_ms);
1883                         break;
1884                 case IB_LINK_LAYER_ETHERNET:
1885                         ret = cma_resolve_iboe_route(id_priv);
1886                         break;
1887                 default:
1888                         ret = -ENOSYS;
1889                 }
1890                 break;
1891         case RDMA_TRANSPORT_IWARP:
1892                 ret = cma_resolve_iw_route(id_priv, timeout_ms);
1893                 break;
1894         default:
1895                 ret = -ENOSYS;
1896                 break;
1897         }
1898         if (ret)
1899                 goto err;
1900
1901         return 0;
1902 err:
1903         cma_comp_exch(id_priv, CMA_ROUTE_QUERY, CMA_ADDR_RESOLVED);
1904         cma_deref_id(id_priv);
1905         return ret;
1906 }
1907 EXPORT_SYMBOL(rdma_resolve_route);
1908
1909 static int cma_bind_loopback(struct rdma_id_private *id_priv)
1910 {
1911         struct cma_device *cma_dev;
1912         struct ib_port_attr port_attr;
1913         union ib_gid gid;
1914         u16 pkey;
1915         int ret;
1916         u8 p;
1917
1918         mutex_lock(&lock);
1919         if (list_empty(&dev_list)) {
1920                 ret = -ENODEV;
1921                 goto out;
1922         }
1923         list_for_each_entry(cma_dev, &dev_list, list)
1924                 for (p = 1; p <= cma_dev->device->phys_port_cnt; ++p)
1925                         if (!ib_query_port(cma_dev->device, p, &port_attr) &&
1926                             port_attr.state == IB_PORT_ACTIVE)
1927                                 goto port_found;
1928
1929         p = 1;
1930         cma_dev = list_entry(dev_list.next, struct cma_device, list);
1931
1932 port_found:
1933         ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid);
1934         if (ret)
1935                 goto out;
1936
1937         ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
1938         if (ret)
1939                 goto out;
1940
1941         id_priv->id.route.addr.dev_addr.dev_type =
1942                 (rdma_port_get_link_layer(cma_dev->device, p) == IB_LINK_LAYER_INFINIBAND) ?
1943                 ARPHRD_INFINIBAND : ARPHRD_ETHER;
1944
1945         rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
1946         ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
1947         id_priv->id.port_num = p;
1948         cma_attach_to_dev(id_priv, cma_dev);
1949 out:
1950         mutex_unlock(&lock);
1951         return ret;
1952 }
1953
1954 static void addr_handler(int status, struct sockaddr *src_addr,
1955                          struct rdma_dev_addr *dev_addr, void *context)
1956 {
1957         struct rdma_id_private *id_priv = context;
1958         struct rdma_cm_event event;
1959
1960         memset(&event, 0, sizeof event);
1961         mutex_lock(&id_priv->handler_mutex);
1962
1963         /*
1964          * Grab mutex to block rdma_destroy_id() from removing the device while
1965          * we're trying to acquire it.
1966          */
1967         mutex_lock(&lock);
1968         if (!cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_RESOLVED)) {
1969                 mutex_unlock(&lock);
1970                 goto out;
1971         }
1972
1973         if (!status && !id_priv->cma_dev)
1974                 status = cma_acquire_dev(id_priv);
1975         mutex_unlock(&lock);
1976
1977         if (status) {
1978                 if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ADDR_BOUND))
1979                         goto out;
1980                 event.event = RDMA_CM_EVENT_ADDR_ERROR;
1981                 event.status = status;
1982         } else {
1983                 memcpy(&id_priv->id.route.addr.src_addr, src_addr,
1984                        ip_addr_size(src_addr));
1985                 event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
1986         }
1987
1988         if (id_priv->id.event_handler(&id_priv->id, &event)) {
1989                 cma_exch(id_priv, CMA_DESTROYING);
1990                 mutex_unlock(&id_priv->handler_mutex);
1991                 cma_deref_id(id_priv);
1992                 rdma_destroy_id(&id_priv->id);
1993                 return;
1994         }
1995 out:
1996         mutex_unlock(&id_priv->handler_mutex);
1997         cma_deref_id(id_priv);
1998 }
1999
2000 static int cma_resolve_loopback(struct rdma_id_private *id_priv)
2001 {
2002         struct cma_work *work;
2003         struct sockaddr *src, *dst;
2004         union ib_gid gid;
2005         int ret;
2006
2007         work = kzalloc(sizeof *work, GFP_KERNEL);
2008         if (!work)
2009                 return -ENOMEM;
2010
2011         if (!id_priv->cma_dev) {
2012                 ret = cma_bind_loopback(id_priv);
2013                 if (ret)
2014                         goto err;
2015         }
2016
2017         rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
2018         rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
2019
2020         src = (struct sockaddr *) &id_priv->id.route.addr.src_addr;
2021         if (cma_zero_addr(src)) {
2022                 dst = (struct sockaddr *) &id_priv->id.route.addr.dst_addr;
2023                 if ((src->sa_family = dst->sa_family) == AF_INET) {
2024                         ((struct sockaddr_in *) src)->sin_addr.s_addr =
2025                                 ((struct sockaddr_in *) dst)->sin_addr.s_addr;
2026                 } else {
2027                         ipv6_addr_copy(&((struct sockaddr_in6 *) src)->sin6_addr,
2028                                        &((struct sockaddr_in6 *) dst)->sin6_addr);
2029                 }
2030         }
2031
2032         work->id = id_priv;
2033         INIT_WORK(&work->work, cma_work_handler);
2034         work->old_state = CMA_ADDR_QUERY;
2035         work->new_state = CMA_ADDR_RESOLVED;
2036         work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2037         queue_work(cma_wq, &work->work);
2038         return 0;
2039 err:
2040         kfree(work);
2041         return ret;
2042 }
2043
2044 static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2045                          struct sockaddr *dst_addr)
2046 {
2047         if (!src_addr || !src_addr->sa_family) {
2048                 src_addr = (struct sockaddr *) &id->route.addr.src_addr;
2049                 if ((src_addr->sa_family = dst_addr->sa_family) == AF_INET6) {
2050                         ((struct sockaddr_in6 *) src_addr)->sin6_scope_id =
2051                                 ((struct sockaddr_in6 *) dst_addr)->sin6_scope_id;
2052                 }
2053         }
2054         return rdma_bind_addr(id, src_addr);
2055 }
2056
2057 int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2058                       struct sockaddr *dst_addr, int timeout_ms)
2059 {
2060         struct rdma_id_private *id_priv;
2061         int ret;
2062
2063         id_priv = container_of(id, struct rdma_id_private, id);
2064         if (id_priv->state == CMA_IDLE) {
2065                 ret = cma_bind_addr(id, src_addr, dst_addr);
2066                 if (ret)
2067                         return ret;
2068         }
2069
2070         if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_ADDR_QUERY))
2071                 return -EINVAL;
2072
2073         atomic_inc(&id_priv->refcount);
2074         memcpy(&id->route.addr.dst_addr, dst_addr, ip_addr_size(dst_addr));
2075         if (cma_any_addr(dst_addr))
2076                 ret = cma_resolve_loopback(id_priv);
2077         else
2078                 ret = rdma_resolve_ip(&addr_client, (struct sockaddr *) &id->route.addr.src_addr,
2079                                       dst_addr, &id->route.addr.dev_addr,
2080                                       timeout_ms, addr_handler, id_priv);
2081         if (ret)
2082                 goto err;
2083
2084         return 0;
2085 err:
2086         cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_BOUND);
2087         cma_deref_id(id_priv);
2088         return ret;
2089 }
2090 EXPORT_SYMBOL(rdma_resolve_addr);
2091
2092 static void cma_bind_port(struct rdma_bind_list *bind_list,
2093                           struct rdma_id_private *id_priv)
2094 {
2095         struct sockaddr_in *sin;
2096
2097         sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
2098         sin->sin_port = htons(bind_list->port);
2099         id_priv->bind_list = bind_list;
2100         hlist_add_head(&id_priv->node, &bind_list->owners);
2101 }
2102
2103 static int cma_alloc_port(struct idr *ps, struct rdma_id_private *id_priv,
2104                           unsigned short snum)
2105 {
2106         struct rdma_bind_list *bind_list;
2107         int port, ret;
2108
2109         bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
2110         if (!bind_list)
2111                 return -ENOMEM;
2112
2113         do {
2114                 ret = idr_get_new_above(ps, bind_list, snum, &port);
2115         } while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL));
2116
2117         if (ret)
2118                 goto err1;
2119
2120         if (port != snum) {
2121                 ret = -EADDRNOTAVAIL;
2122                 goto err2;
2123         }
2124
2125         bind_list->ps = ps;
2126         bind_list->port = (unsigned short) port;
2127         cma_bind_port(bind_list, id_priv);
2128         return 0;
2129 err2:
2130         idr_remove(ps, port);
2131 err1:
2132         kfree(bind_list);
2133         return ret;
2134 }
2135
2136 static int cma_alloc_any_port(struct idr *ps, struct rdma_id_private *id_priv)
2137 {
2138         struct rdma_bind_list *bind_list;
2139         int port, ret, low, high;
2140
2141         bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
2142         if (!bind_list)
2143                 return -ENOMEM;
2144
2145 retry:
2146         /* FIXME: add proper port randomization per like inet_csk_get_port */
2147         do {
2148                 ret = idr_get_new_above(ps, bind_list, next_port, &port);
2149         } while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL));
2150
2151         if (ret)
2152                 goto err1;
2153
2154         inet_get_local_port_range(&low, &high);
2155         if (port > high) {
2156                 if (next_port != low) {
2157                         idr_remove(ps, port);
2158                         next_port = low;
2159                         goto retry;
2160                 }
2161                 ret = -EADDRNOTAVAIL;
2162                 goto err2;
2163         }
2164
2165         if (port == high)
2166                 next_port = low;
2167         else
2168                 next_port = port + 1;
2169
2170         bind_list->ps = ps;
2171         bind_list->port = (unsigned short) port;
2172         cma_bind_port(bind_list, id_priv);
2173         return 0;
2174 err2:
2175         idr_remove(ps, port);
2176 err1:
2177         kfree(bind_list);
2178         return ret;
2179 }
2180
2181 static int cma_use_port(struct idr *ps, struct rdma_id_private *id_priv)
2182 {
2183         struct rdma_id_private *cur_id;
2184         struct sockaddr_in *sin, *cur_sin;
2185         struct rdma_bind_list *bind_list;
2186         struct hlist_node *node;
2187         unsigned short snum;
2188
2189         sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
2190         snum = ntohs(sin->sin_port);
2191 #ifdef __linux__
2192         if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
2193                 return -EACCES;
2194 #endif
2195
2196         bind_list = idr_find(ps, snum);
2197         if (!bind_list)
2198                 return cma_alloc_port(ps, id_priv, snum);
2199
2200         /*
2201          * We don't support binding to any address if anyone is bound to
2202          * a specific address on the same port.
2203          */
2204         if (cma_any_addr((struct sockaddr *) &id_priv->id.route.addr.src_addr))
2205                 return -EADDRNOTAVAIL;
2206
2207         hlist_for_each_entry(cur_id, node, &bind_list->owners, node) {
2208                 if (cma_any_addr((struct sockaddr *) &cur_id->id.route.addr.src_addr))
2209                         return -EADDRNOTAVAIL;
2210
2211                 cur_sin = (struct sockaddr_in *) &cur_id->id.route.addr.src_addr;
2212                 if (sin->sin_addr.s_addr == cur_sin->sin_addr.s_addr)
2213                         return -EADDRINUSE;
2214         }
2215
2216         cma_bind_port(bind_list, id_priv);
2217         return 0;
2218 }
2219
2220 static int cma_get_tcp_port(struct rdma_id_private *id_priv)
2221 {
2222         int ret;
2223         int size;
2224         struct socket *sock;
2225
2226         ret = sock_create_kern(AF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
2227         if (ret)
2228                 return ret;
2229 #ifdef __linux__
2230         ret = sock->ops->bind(sock,
2231                         (struct sockaddr *) &id_priv->id.route.addr.src_addr,
2232                         ip_addr_size((struct sockaddr *) &id_priv->id.route.addr.src_addr));
2233 #else
2234         ret = -sobind(sock,
2235                         (struct sockaddr *)&id_priv->id.route.addr.src_addr,
2236                         curthread);
2237 #endif
2238         if (ret) {
2239                 sock_release(sock);
2240                 return ret;
2241         }
2242         size = ip_addr_size((struct sockaddr *) &id_priv->id.route.addr.src_addr);
2243         ret = sock_getname(sock,
2244                         (struct sockaddr *) &id_priv->id.route.addr.src_addr,
2245                         &size, 0);
2246         if (ret) {
2247                 sock_release(sock);
2248                 return ret;
2249         }
2250         id_priv->sock = sock;
2251         return 0;
2252 }
2253
2254 static int cma_get_port(struct rdma_id_private *id_priv)
2255 {
2256         struct idr *ps;
2257         int ret;
2258
2259         switch (id_priv->id.ps) {
2260         case RDMA_PS_SDP:
2261                 ps = &sdp_ps;
2262                 break;
2263         case RDMA_PS_TCP:
2264                 ps = &tcp_ps;
2265                 if (unify_tcp_port_space) {
2266                         ret = cma_get_tcp_port(id_priv);
2267                         if (ret)
2268                                 goto out;
2269                 }
2270                 break;
2271         case RDMA_PS_UDP:
2272                 ps = &udp_ps;
2273                 break;
2274         case RDMA_PS_IPOIB:
2275                 ps = &ipoib_ps;
2276                 break;
2277         default:
2278                 return -EPROTONOSUPPORT;
2279         }
2280
2281         mutex_lock(&lock);
2282         if (cma_any_port((struct sockaddr *) &id_priv->id.route.addr.src_addr))
2283                 ret = cma_alloc_any_port(ps, id_priv);
2284         else
2285                 ret = cma_use_port(ps, id_priv);
2286         mutex_unlock(&lock);
2287 out:
2288         return ret;
2289 }
2290
2291 static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
2292                                struct sockaddr *addr)
2293 {
2294 #if defined(INET6)
2295         struct sockaddr_in6 *sin6;
2296
2297         if (addr->sa_family != AF_INET6)
2298                 return 0;
2299
2300         sin6 = (struct sockaddr_in6 *) addr;
2301 #ifdef __linux__
2302         if ((ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) &&
2303 #else
2304         if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) &&
2305 #endif
2306             !sin6->sin6_scope_id)
2307                         return -EINVAL;
2308
2309         dev_addr->bound_dev_if = sin6->sin6_scope_id;
2310 #endif
2311         return 0;
2312 }
2313
2314 int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
2315 {
2316         struct rdma_id_private *id_priv;
2317         int ret;
2318
2319         if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
2320                 return -EAFNOSUPPORT;
2321
2322         id_priv = container_of(id, struct rdma_id_private, id);
2323         if (!cma_comp_exch(id_priv, CMA_IDLE, CMA_ADDR_BOUND))
2324                 return -EINVAL;
2325
2326         ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
2327         if (ret)
2328                 goto err1;
2329
2330         if (!cma_any_addr(addr)) {
2331                 ret = rdma_translate_ip(addr, &id->route.addr.dev_addr);
2332                 if (ret)
2333                         goto err1;
2334
2335                 mutex_lock(&lock);
2336                 ret = cma_acquire_dev(id_priv);
2337                 mutex_unlock(&lock);
2338                 if (ret)
2339                         goto err1;
2340         }
2341
2342         memcpy(&id->route.addr.src_addr, addr, ip_addr_size(addr));
2343         ret = cma_get_port(id_priv);
2344         if (ret)
2345                 goto err2;
2346
2347         return 0;
2348 err2:
2349         if (id_priv->cma_dev) {
2350                 mutex_lock(&lock);
2351                 cma_detach_from_dev(id_priv);
2352                 mutex_unlock(&lock);
2353         }
2354 err1:
2355         cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_IDLE);
2356         return ret;
2357 }
2358 EXPORT_SYMBOL(rdma_bind_addr);
2359
2360 static int cma_format_hdr(void *hdr, enum rdma_port_space ps,
2361                           struct rdma_route *route)
2362 {
2363         struct cma_hdr *cma_hdr;
2364         struct sdp_hh *sdp_hdr;
2365
2366         if (route->addr.src_addr.ss_family == AF_INET) {
2367                 struct sockaddr_in *src4, *dst4;
2368
2369                 src4 = (struct sockaddr_in *) &route->addr.src_addr;
2370                 dst4 = (struct sockaddr_in *) &route->addr.dst_addr;
2371
2372                 switch (ps) {
2373                 case RDMA_PS_SDP:
2374                         sdp_hdr = hdr;
2375                         if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION)
2376                                 return -EINVAL;
2377                         sdp_set_ip_ver(sdp_hdr, 4);
2378                         sdp_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
2379                         sdp_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
2380                         sdp_hdr->port = src4->sin_port;
2381                         break;
2382                 default:
2383                         cma_hdr = hdr;
2384                         cma_hdr->cma_version = CMA_VERSION;
2385                         cma_set_ip_ver(cma_hdr, 4);
2386                         cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
2387                         cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
2388                         cma_hdr->port = src4->sin_port;
2389                         break;
2390                 }
2391         } else {
2392                 struct sockaddr_in6 *src6, *dst6;
2393
2394                 src6 = (struct sockaddr_in6 *) &route->addr.src_addr;
2395                 dst6 = (struct sockaddr_in6 *) &route->addr.dst_addr;
2396
2397                 switch (ps) {
2398                 case RDMA_PS_SDP:
2399                         sdp_hdr = hdr;
2400                         if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION)
2401                                 return -EINVAL;
2402                         sdp_set_ip_ver(sdp_hdr, 6);
2403                         sdp_hdr->src_addr.ip6 = src6->sin6_addr;
2404                         sdp_hdr->dst_addr.ip6 = dst6->sin6_addr;
2405                         sdp_hdr->port = src6->sin6_port;
2406                         break;
2407                 default:
2408                         cma_hdr = hdr;
2409                         cma_hdr->cma_version = CMA_VERSION;
2410                         cma_set_ip_ver(cma_hdr, 6);
2411                         cma_hdr->src_addr.ip6 = src6->sin6_addr;
2412                         cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
2413                         cma_hdr->port = src6->sin6_port;
2414                         break;
2415                 }
2416         }
2417         return 0;
2418 }
2419
2420 static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
2421                                 struct ib_cm_event *ib_event)
2422 {
2423         struct rdma_id_private *id_priv = cm_id->context;
2424         struct rdma_cm_event event;
2425         struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
2426         int ret = 0;
2427
2428         if (cma_disable_callback(id_priv, CMA_CONNECT))
2429                 return 0;
2430
2431         memset(&event, 0, sizeof event);
2432         switch (ib_event->event) {
2433         case IB_CM_SIDR_REQ_ERROR:
2434                 event.event = RDMA_CM_EVENT_UNREACHABLE;
2435                 event.status = -ETIMEDOUT;
2436                 break;
2437         case IB_CM_SIDR_REP_RECEIVED:
2438                 event.param.ud.private_data = ib_event->private_data;
2439                 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
2440                 if (rep->status != IB_SIDR_SUCCESS) {
2441                         event.event = RDMA_CM_EVENT_UNREACHABLE;
2442                         event.status = ib_event->param.sidr_rep_rcvd.status;
2443                         break;
2444                 }
2445                 ret = cma_set_qkey(id_priv);
2446                 if (ret) {
2447                         event.event = RDMA_CM_EVENT_ADDR_ERROR;
2448                         event.status = -EINVAL;
2449                         break;
2450                 }
2451                 if (id_priv->qkey != rep->qkey) {
2452                         event.event = RDMA_CM_EVENT_UNREACHABLE;
2453                         event.status = -EINVAL;
2454                         break;
2455                 }
2456                 ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
2457                                      id_priv->id.route.path_rec,
2458                                      &event.param.ud.ah_attr);
2459                 event.param.ud.qp_num = rep->qpn;
2460                 event.param.ud.qkey = rep->qkey;
2461                 event.event = RDMA_CM_EVENT_ESTABLISHED;
2462                 event.status = 0;
2463                 break;
2464         default:
2465                 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
2466                        ib_event->event);
2467                 goto out;
2468         }
2469
2470         ret = id_priv->id.event_handler(&id_priv->id, &event);
2471         if (ret) {
2472                 /* Destroy the CM ID by returning a non-zero value. */
2473                 id_priv->cm_id.ib = NULL;
2474                 cma_exch(id_priv, CMA_DESTROYING);
2475                 mutex_unlock(&id_priv->handler_mutex);
2476                 rdma_destroy_id(&id_priv->id);
2477                 return ret;
2478         }
2479 out:
2480         mutex_unlock(&id_priv->handler_mutex);
2481         return ret;
2482 }
2483
2484 static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
2485                               struct rdma_conn_param *conn_param)
2486 {
2487         struct ib_cm_sidr_req_param req;
2488         struct rdma_route *route;
2489         int ret;
2490
2491         req.private_data_len = sizeof(struct cma_hdr) +
2492                                conn_param->private_data_len;
2493         req.private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2494         if (!req.private_data)
2495                 return -ENOMEM;
2496
2497         if (conn_param->private_data && conn_param->private_data_len)
2498                 memcpy((void *) req.private_data + sizeof(struct cma_hdr),
2499                        conn_param->private_data, conn_param->private_data_len);
2500
2501         route = &id_priv->id.route;
2502         ret = cma_format_hdr((void *) req.private_data, id_priv->id.ps, route);
2503         if (ret)
2504                 goto out;
2505
2506         id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device,
2507                                             cma_sidr_rep_handler, id_priv);
2508         if (IS_ERR(id_priv->cm_id.ib)) {
2509                 ret = PTR_ERR(id_priv->cm_id.ib);
2510                 goto out;
2511         }
2512
2513         req.path = route->path_rec;
2514         req.service_id = cma_get_service_id(id_priv->id.ps,
2515                                             (struct sockaddr *) &route->addr.dst_addr);
2516         req.timeout_ms = 1 << (cma_response_timeout - 8);
2517         req.max_cm_retries = CMA_MAX_CM_RETRIES;
2518
2519         ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
2520         if (ret) {
2521                 ib_destroy_cm_id(id_priv->cm_id.ib);
2522                 id_priv->cm_id.ib = NULL;
2523         }
2524 out:
2525         kfree(req.private_data);
2526         return ret;
2527 }
2528
2529 static int cma_connect_ib(struct rdma_id_private *id_priv,
2530                           struct rdma_conn_param *conn_param)
2531 {
2532         struct ib_cm_req_param req;
2533         struct rdma_route *route;
2534         void *private_data;
2535         int offset, ret;
2536
2537         memset(&req, 0, sizeof req);
2538         offset = cma_user_data_offset(id_priv->id.ps);
2539         req.private_data_len = offset + conn_param->private_data_len;
2540         private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2541         if (!private_data)
2542                 return -ENOMEM;
2543
2544         if (conn_param->private_data && conn_param->private_data_len)
2545                 memcpy(private_data + offset, conn_param->private_data,
2546                        conn_param->private_data_len);
2547
2548         id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_ib_handler,
2549                                             id_priv);
2550         if (IS_ERR(id_priv->cm_id.ib)) {
2551                 ret = PTR_ERR(id_priv->cm_id.ib);
2552                 goto out;
2553         }
2554
2555         route = &id_priv->id.route;
2556         ret = cma_format_hdr(private_data, id_priv->id.ps, route);
2557         if (ret)
2558                 goto out;
2559         req.private_data = private_data;
2560
2561         req.primary_path = &route->path_rec[0];
2562         if (route->num_paths == 2)
2563                 req.alternate_path = &route->path_rec[1];
2564
2565         req.service_id = cma_get_service_id(id_priv->id.ps,
2566                                             (struct sockaddr *) &route->addr.dst_addr);
2567         req.qp_num = id_priv->qp_num;
2568         req.qp_type = IB_QPT_RC;
2569         req.starting_psn = id_priv->seq_num;
2570         req.responder_resources = conn_param->responder_resources;
2571         req.initiator_depth = conn_param->initiator_depth;
2572         req.flow_control = conn_param->flow_control;
2573         req.retry_count = conn_param->retry_count;
2574         req.rnr_retry_count = conn_param->rnr_retry_count;
2575        req.remote_cm_response_timeout = cma_response_timeout;
2576        req.local_cm_response_timeout = cma_response_timeout;
2577         req.max_cm_retries = CMA_MAX_CM_RETRIES;
2578         req.srq = id_priv->srq ? 1 : 0;
2579
2580         ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
2581 out:
2582         if (ret && !IS_ERR(id_priv->cm_id.ib)) {
2583                 ib_destroy_cm_id(id_priv->cm_id.ib);
2584                 id_priv->cm_id.ib = NULL;
2585         }
2586
2587         kfree(private_data);
2588         return ret;
2589 }
2590
2591 static int cma_connect_iw(struct rdma_id_private *id_priv,
2592                           struct rdma_conn_param *conn_param)
2593 {
2594         struct iw_cm_id *cm_id;
2595         struct sockaddr_in* sin;
2596         int ret;
2597         struct iw_cm_conn_param iw_param;
2598
2599         cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
2600         if (IS_ERR(cm_id)) {
2601                 ret = PTR_ERR(cm_id);
2602                 goto out;
2603         }
2604
2605         id_priv->cm_id.iw = cm_id;
2606
2607         sin = (struct sockaddr_in*) &id_priv->id.route.addr.src_addr;
2608         cm_id->local_addr = *sin;
2609
2610         sin = (struct sockaddr_in*) &id_priv->id.route.addr.dst_addr;
2611         cm_id->remote_addr = *sin;
2612
2613         ret = cma_modify_qp_rtr(id_priv, conn_param);
2614         if (ret)
2615                 goto out;
2616
2617         iw_param.ord = conn_param->initiator_depth;
2618         iw_param.ird = conn_param->responder_resources;
2619         iw_param.private_data = conn_param->private_data;
2620         iw_param.private_data_len = conn_param->private_data_len;
2621         if (id_priv->id.qp)
2622                 iw_param.qpn = id_priv->qp_num;
2623         else
2624                 iw_param.qpn = conn_param->qp_num;
2625         ret = iw_cm_connect(cm_id, &iw_param);
2626 out:
2627         if (ret && !IS_ERR(cm_id)) {
2628                 iw_destroy_cm_id(cm_id);
2629                 id_priv->cm_id.iw = NULL;
2630         }
2631         return ret;
2632 }
2633
2634 int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2635 {
2636         struct rdma_id_private *id_priv;
2637         int ret;
2638
2639         id_priv = container_of(id, struct rdma_id_private, id);
2640         if (!cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_CONNECT))
2641                 return -EINVAL;
2642
2643         if (!id->qp) {
2644                 id_priv->qp_num = conn_param->qp_num;
2645                 id_priv->srq = conn_param->srq;
2646         }
2647
2648         switch (rdma_node_get_transport(id->device->node_type)) {
2649         case RDMA_TRANSPORT_IB:
2650                 if (cma_is_ud_ps(id->ps))
2651                         ret = cma_resolve_ib_udp(id_priv, conn_param);
2652                 else
2653                         ret = cma_connect_ib(id_priv, conn_param);
2654                 break;
2655         case RDMA_TRANSPORT_IWARP:
2656                 ret = cma_connect_iw(id_priv, conn_param);
2657                 break;
2658         default:
2659                 ret = -ENOSYS;
2660                 break;
2661         }
2662         if (ret)
2663                 goto err;
2664
2665         return 0;
2666 err:
2667         cma_comp_exch(id_priv, CMA_CONNECT, CMA_ROUTE_RESOLVED);
2668         return ret;
2669 }
2670 EXPORT_SYMBOL(rdma_connect);
2671
2672 static int cma_accept_ib(struct rdma_id_private *id_priv,
2673                          struct rdma_conn_param *conn_param)
2674 {
2675         struct ib_cm_rep_param rep;
2676         int ret;
2677
2678         ret = cma_modify_qp_rtr(id_priv, conn_param);
2679         if (ret)
2680                 goto out;
2681
2682         ret = cma_modify_qp_rts(id_priv, conn_param);
2683         if (ret)
2684                 goto out;
2685
2686         memset(&rep, 0, sizeof rep);
2687         rep.qp_num = id_priv->qp_num;
2688         rep.starting_psn = id_priv->seq_num;
2689         rep.private_data = conn_param->private_data;
2690         rep.private_data_len = conn_param->private_data_len;
2691         rep.responder_resources = conn_param->responder_resources;
2692         rep.initiator_depth = conn_param->initiator_depth;
2693         rep.failover_accepted = 0;
2694         rep.flow_control = conn_param->flow_control;
2695         rep.rnr_retry_count = conn_param->rnr_retry_count;
2696         rep.srq = id_priv->srq ? 1 : 0;
2697
2698         ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
2699 out:
2700         return ret;
2701 }
2702
2703 static int cma_accept_iw(struct rdma_id_private *id_priv,
2704                   struct rdma_conn_param *conn_param)
2705 {
2706         struct iw_cm_conn_param iw_param;
2707         int ret;
2708
2709         ret = cma_modify_qp_rtr(id_priv, conn_param);
2710         if (ret)
2711                 return ret;
2712
2713         iw_param.ord = conn_param->initiator_depth;
2714         iw_param.ird = conn_param->responder_resources;
2715         iw_param.private_data = conn_param->private_data;
2716         iw_param.private_data_len = conn_param->private_data_len;
2717         if (id_priv->id.qp) {
2718                 iw_param.qpn = id_priv->qp_num;
2719         } else
2720                 iw_param.qpn = conn_param->qp_num;
2721
2722         return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
2723 }
2724
2725 static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
2726                              enum ib_cm_sidr_status status,
2727                              const void *private_data, int private_data_len)
2728 {
2729         struct ib_cm_sidr_rep_param rep;
2730         int ret;
2731
2732         memset(&rep, 0, sizeof rep);
2733         rep.status = status;
2734         if (status == IB_SIDR_SUCCESS) {
2735                 ret = cma_set_qkey(id_priv);
2736                 if (ret)
2737                         return ret;
2738                 rep.qp_num = id_priv->qp_num;
2739                 rep.qkey = id_priv->qkey;
2740         }
2741         rep.private_data = private_data;
2742         rep.private_data_len = private_data_len;
2743
2744         return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
2745 }
2746
2747 int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2748 {
2749         struct rdma_id_private *id_priv;
2750         int ret;
2751
2752         id_priv = container_of(id, struct rdma_id_private, id);
2753         if (!cma_comp(id_priv, CMA_CONNECT))
2754                 return -EINVAL;
2755
2756         if (!id->qp && conn_param) {
2757                 id_priv->qp_num = conn_param->qp_num;
2758                 id_priv->srq = conn_param->srq;
2759         }
2760
2761         switch (rdma_node_get_transport(id->device->node_type)) {
2762         case RDMA_TRANSPORT_IB:
2763                 if (cma_is_ud_ps(id->ps))
2764                         ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
2765                                                 conn_param->private_data,
2766                                                 conn_param->private_data_len);
2767                 else if (conn_param)
2768                         ret = cma_accept_ib(id_priv, conn_param);
2769                 else
2770                         ret = cma_rep_recv(id_priv);
2771                 break;
2772         case RDMA_TRANSPORT_IWARP:
2773                 ret = cma_accept_iw(id_priv, conn_param);
2774                 break;
2775         default:
2776                 ret = -ENOSYS;
2777                 break;
2778         }
2779
2780         if (ret)
2781                 goto reject;
2782
2783         return 0;
2784 reject:
2785         cma_modify_qp_err(id_priv);
2786         rdma_reject(id, NULL, 0);
2787         return ret;
2788 }
2789 EXPORT_SYMBOL(rdma_accept);
2790
2791 int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
2792 {
2793         struct rdma_id_private *id_priv;
2794         int ret;
2795
2796         id_priv = container_of(id, struct rdma_id_private, id);
2797         if (!cma_has_cm_dev(id_priv))
2798                 return -EINVAL;
2799
2800         switch (id->device->node_type) {
2801         case RDMA_NODE_IB_CA:
2802                 ret = ib_cm_notify(id_priv->cm_id.ib, event);
2803                 break;
2804         default:
2805                 ret = 0;
2806                 break;
2807         }
2808         return ret;
2809 }
2810 EXPORT_SYMBOL(rdma_notify);
2811
2812 int rdma_reject(struct rdma_cm_id *id, const void *private_data,
2813                 u8 private_data_len)
2814 {
2815         struct rdma_id_private *id_priv;
2816         int ret;
2817
2818         id_priv = container_of(id, struct rdma_id_private, id);
2819         if (!cma_has_cm_dev(id_priv))
2820                 return -EINVAL;
2821
2822         switch (rdma_node_get_transport(id->device->node_type)) {
2823         case RDMA_TRANSPORT_IB:
2824                 if (cma_is_ud_ps(id->ps))
2825                         ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT,
2826                                                 private_data, private_data_len);
2827                 else
2828                         ret = ib_send_cm_rej(id_priv->cm_id.ib,
2829                                              IB_CM_REJ_CONSUMER_DEFINED, NULL,
2830                                              0, private_data, private_data_len);
2831                 break;
2832         case RDMA_TRANSPORT_IWARP:
2833                 ret = iw_cm_reject(id_priv->cm_id.iw,
2834                                    private_data, private_data_len);
2835                 break;
2836         default:
2837                 ret = -ENOSYS;
2838                 break;
2839         }
2840         return ret;
2841 }
2842 EXPORT_SYMBOL(rdma_reject);
2843
2844 int rdma_disconnect(struct rdma_cm_id *id)
2845 {
2846         struct rdma_id_private *id_priv;
2847         int ret;
2848
2849         id_priv = container_of(id, struct rdma_id_private, id);
2850         if (!cma_has_cm_dev(id_priv))
2851                 return -EINVAL;
2852
2853         switch (rdma_node_get_transport(id->device->node_type)) {
2854         case RDMA_TRANSPORT_IB:
2855                 ret = cma_modify_qp_err(id_priv);
2856                 if (ret)
2857                         goto out;
2858                 /* Initiate or respond to a disconnect. */
2859                 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
2860                         ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
2861                 break;
2862         case RDMA_TRANSPORT_IWARP:
2863                 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
2864                 break;
2865         default:
2866                 ret = -EINVAL;
2867                 break;
2868         }
2869 out:
2870         return ret;
2871 }
2872 EXPORT_SYMBOL(rdma_disconnect);
2873
2874 static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
2875 {
2876         struct rdma_id_private *id_priv;
2877         struct cma_multicast *mc = multicast->context;
2878         struct rdma_cm_event event;
2879         int ret;
2880
2881         id_priv = mc->id_priv;
2882         if (cma_disable_callback(id_priv, CMA_ADDR_BOUND) &&
2883             cma_disable_callback(id_priv, CMA_ADDR_RESOLVED))
2884                 return 0;
2885
2886         mutex_lock(&id_priv->qp_mutex);
2887         if (!status && id_priv->id.qp)
2888                 status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
2889                                          multicast->rec.mlid);
2890         mutex_unlock(&id_priv->qp_mutex);
2891
2892         memset(&event, 0, sizeof event);
2893         event.status = status;
2894         event.param.ud.private_data = mc->context;
2895         if (!status) {
2896                 event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
2897                 ib_init_ah_from_mcmember(id_priv->id.device,
2898                                          id_priv->id.port_num, &multicast->rec,
2899                                          &event.param.ud.ah_attr);
2900                 event.param.ud.qp_num = 0xFFFFFF;
2901                 event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
2902         } else
2903                 event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
2904
2905         ret = id_priv->id.event_handler(&id_priv->id, &event);
2906         if (ret) {
2907                 cma_exch(id_priv, CMA_DESTROYING);
2908                 mutex_unlock(&id_priv->handler_mutex);
2909                 rdma_destroy_id(&id_priv->id);
2910                 return 0;
2911         }
2912
2913         mutex_unlock(&id_priv->handler_mutex);
2914         return 0;
2915 }
2916
2917 static void cma_set_mgid(struct rdma_id_private *id_priv,
2918                          struct sockaddr *addr, union ib_gid *mgid)
2919 {
2920         unsigned char mc_map[MAX_ADDR_LEN];
2921         struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2922         struct sockaddr_in *sin = (struct sockaddr_in *) addr;
2923         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
2924
2925         if (cma_any_addr(addr)) {
2926                 memset(mgid, 0, sizeof *mgid);
2927         } else if ((addr->sa_family == AF_INET6) &&
2928                    ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
2929                                                                  0xFF10A01B)) {
2930                 /* IPv6 address is an SA assigned MGID. */
2931                 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
2932         } else if ((addr->sa_family == AF_INET6)) {
2933                 ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
2934                 if (id_priv->id.ps == RDMA_PS_UDP)
2935                         mc_map[7] = 0x01;       /* Use RDMA CM signature */
2936                 *mgid = *(union ib_gid *) (mc_map + 4);
2937         } else {
2938                 ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
2939                 if (id_priv->id.ps == RDMA_PS_UDP)
2940                         mc_map[7] = 0x01;       /* Use RDMA CM signature */
2941                 *mgid = *(union ib_gid *) (mc_map + 4);
2942         }
2943 }
2944
2945 static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
2946                                  struct cma_multicast *mc)
2947 {
2948         struct ib_sa_mcmember_rec rec;
2949         struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2950         ib_sa_comp_mask comp_mask;
2951         int ret;
2952
2953         ib_addr_get_mgid(dev_addr, &rec.mgid);
2954         ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
2955                                      &rec.mgid, &rec);
2956         if (ret)
2957                 return ret;
2958
2959         cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
2960         if (id_priv->id.ps == RDMA_PS_UDP)
2961                 rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
2962         rdma_addr_get_sgid(dev_addr, &rec.port_gid);
2963         rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
2964         rec.join_state = 1;
2965
2966         comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
2967                     IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
2968                     IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
2969                     IB_SA_MCMEMBER_REC_FLOW_LABEL |
2970                     IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
2971
2972         if (id_priv->id.ps == RDMA_PS_IPOIB)
2973                 comp_mask |= IB_SA_MCMEMBER_REC_RATE |
2974                              IB_SA_MCMEMBER_REC_RATE_SELECTOR;
2975
2976         mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
2977                                                 id_priv->id.port_num, &rec,
2978                                                 comp_mask, GFP_KERNEL,
2979                                                 cma_ib_mc_handler, mc);
2980         if (IS_ERR(mc->multicast.ib))
2981                 return PTR_ERR(mc->multicast.ib);
2982
2983         return 0;
2984 }
2985
2986
2987 static void iboe_mcast_work_handler(struct work_struct *work)
2988 {
2989         struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work);
2990         struct cma_multicast *mc = mw->mc;
2991         struct ib_sa_multicast *m = mc->multicast.ib;
2992
2993         mc->multicast.ib->context = mc;
2994         cma_ib_mc_handler(0, m);
2995         kref_put(&mc->mcref, release_mc);
2996         kfree(mw);
2997 }
2998
2999 static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid)
3000 {
3001         struct sockaddr_in *sin = (struct sockaddr_in *)addr;
3002         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
3003
3004         if (cma_any_addr(addr)) {
3005                 memset(mgid, 0, sizeof *mgid);
3006         } else if (addr->sa_family == AF_INET6)
3007                 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
3008         else {
3009                 mgid->raw[0] = 0xff;
3010                 mgid->raw[1] = 0x0e;
3011                 mgid->raw[2] = 0;
3012                 mgid->raw[3] = 0;
3013                 mgid->raw[4] = 0;
3014                 mgid->raw[5] = 0;
3015                 mgid->raw[6] = 0;
3016                 mgid->raw[7] = 0;
3017                 mgid->raw[8] = 0;
3018                 mgid->raw[9] = 0;
3019                 mgid->raw[10] = 0xff;
3020                 mgid->raw[11] = 0xff;
3021                 *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
3022         }
3023 }
3024
3025 static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
3026                                    struct cma_multicast *mc)
3027 {
3028         struct iboe_mcast_work *work;
3029         struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3030         int err;
3031         struct sockaddr *addr = (struct sockaddr *)&mc->addr;
3032         struct net_device *ndev = NULL;
3033
3034         if (cma_zero_addr((struct sockaddr *)&mc->addr))
3035                 return -EINVAL;
3036
3037         work = kzalloc(sizeof *work, GFP_KERNEL);
3038         if (!work)
3039                 return -ENOMEM;
3040
3041         mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL);
3042         if (!mc->multicast.ib) {
3043                 err = -ENOMEM;
3044                 goto out1;
3045         }
3046
3047         cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid);
3048
3049         mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff);
3050         if (id_priv->id.ps == RDMA_PS_UDP)
3051                 mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
3052
3053         if (dev_addr->bound_dev_if)
3054                 ndev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
3055         if (!ndev) {
3056                 err = -ENODEV;
3057                 goto out2;
3058         }
3059
3060         mc->multicast.ib->rec.rate = iboe_get_rate(ndev);
3061         mc->multicast.ib->rec.hop_limit = 1;
3062 #ifdef __linux__
3063         mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu);
3064 #else
3065         mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->if_mtu);
3066 #endif
3067         dev_put(ndev);
3068         if (!mc->multicast.ib->rec.mtu) {
3069                 err = -EINVAL;
3070                 goto out2;
3071         }
3072         iboe_addr_get_sgid(dev_addr, &mc->multicast.ib->rec.port_gid);
3073         work->id = id_priv;
3074         work->mc = mc;
3075         INIT_WORK(&work->work, iboe_mcast_work_handler);
3076         kref_get(&mc->mcref);
3077         queue_work(cma_wq, &work->work);
3078
3079         return 0;
3080
3081 out2:
3082         kfree(mc->multicast.ib);
3083 out1:
3084         kfree(work);
3085         return err;
3086 }
3087
3088 int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
3089                         void *context)
3090 {
3091         struct rdma_id_private *id_priv;
3092         struct cma_multicast *mc;
3093         int ret;
3094
3095         id_priv = container_of(id, struct rdma_id_private, id);
3096         if (!cma_comp(id_priv, CMA_ADDR_BOUND) &&
3097             !cma_comp(id_priv, CMA_ADDR_RESOLVED))
3098                 return -EINVAL;
3099
3100         mc = kmalloc(sizeof *mc, GFP_KERNEL);
3101         if (!mc)
3102                 return -ENOMEM;
3103
3104         memcpy(&mc->addr, addr, ip_addr_size(addr));
3105         mc->context = context;
3106         mc->id_priv = id_priv;
3107
3108         spin_lock(&id_priv->lock);
3109         list_add(&mc->list, &id_priv->mc_list);
3110         spin_unlock(&id_priv->lock);
3111
3112         switch (rdma_node_get_transport(id->device->node_type)) {
3113         case RDMA_TRANSPORT_IB:
3114                 switch (rdma_port_get_link_layer(id->device, id->port_num)) {
3115                 case IB_LINK_LAYER_INFINIBAND:
3116                         ret = cma_join_ib_multicast(id_priv, mc);
3117                         break;
3118                 case IB_LINK_LAYER_ETHERNET:
3119                         kref_init(&mc->mcref);
3120                         ret = cma_iboe_join_multicast(id_priv, mc);
3121                         break;
3122                 default:
3123                         ret = -EINVAL;
3124                 }
3125                 break;
3126         default:
3127                 ret = -ENOSYS;
3128                 break;
3129         }
3130
3131         if (ret) {
3132                 spin_lock_irq(&id_priv->lock);
3133                 list_del(&mc->list);
3134                 spin_unlock_irq(&id_priv->lock);
3135                 kfree(mc);
3136         }
3137
3138         return ret;
3139 }
3140 EXPORT_SYMBOL(rdma_join_multicast);
3141
3142 void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
3143 {
3144         struct rdma_id_private *id_priv;
3145         struct cma_multicast *mc;
3146
3147         id_priv = container_of(id, struct rdma_id_private, id);
3148         spin_lock_irq(&id_priv->lock);
3149         list_for_each_entry(mc, &id_priv->mc_list, list) {
3150                 if (!memcmp(&mc->addr, addr, ip_addr_size(addr))) {
3151                         list_del(&mc->list);
3152                         spin_unlock_irq(&id_priv->lock);
3153
3154                         if (id->qp)
3155                                 ib_detach_mcast(id->qp,
3156                                                 &mc->multicast.ib->rec.mgid,
3157                                                 mc->multicast.ib->rec.mlid);
3158                         if (rdma_node_get_transport(id_priv->cma_dev->device->node_type) == RDMA_TRANSPORT_IB) {
3159                                 switch (rdma_port_get_link_layer(id->device, id->port_num)) {
3160                                 case IB_LINK_LAYER_INFINIBAND:
3161                                         ib_sa_free_multicast(mc->multicast.ib);
3162                                         kfree(mc);
3163                                         break;
3164                                 case IB_LINK_LAYER_ETHERNET:
3165                                         kref_put(&mc->mcref, release_mc);
3166                                         break;
3167                                 default:
3168                                         break;
3169                                 }
3170                         }
3171                         return;
3172                 }
3173         }
3174         spin_unlock_irq(&id_priv->lock);
3175 }
3176 EXPORT_SYMBOL(rdma_leave_multicast);
3177
3178 static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
3179 {
3180         struct rdma_dev_addr *dev_addr;
3181         struct cma_ndev_work *work;
3182
3183         dev_addr = &id_priv->id.route.addr.dev_addr;
3184
3185 #ifdef __linux__
3186         if ((dev_addr->bound_dev_if == ndev->ifindex) &&
3187             memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
3188                 printk(KERN_INFO "RDMA CM addr change for ndev %s used by id %p\n",
3189                        ndev->name, &id_priv->id);
3190 #else
3191         if ((dev_addr->bound_dev_if == ndev->if_index) &&
3192             memcmp(dev_addr->src_dev_addr, IF_LLADDR(ndev), ndev->if_addrlen)) {
3193                 printk(KERN_INFO "RDMA CM addr change for ndev %s used by id %p\n",
3194                        ndev->if_xname, &id_priv->id);
3195 #endif
3196                 work = kzalloc(sizeof *work, GFP_KERNEL);
3197                 if (!work)
3198                         return -ENOMEM;
3199
3200                 INIT_WORK(&work->work, cma_ndev_work_handler);
3201                 work->id = id_priv;
3202                 work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
3203                 atomic_inc(&id_priv->refcount);
3204                 queue_work(cma_wq, &work->work);
3205         }
3206
3207         return 0;
3208 }
3209
3210 static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
3211                                void *ctx)
3212 {
3213         struct net_device *ndev = (struct net_device *)ctx;
3214         struct cma_device *cma_dev;
3215         struct rdma_id_private *id_priv;
3216         int ret = NOTIFY_DONE;
3217
3218 #ifdef __linux__
3219         if (dev_net(ndev) != &init_net)
3220                 return NOTIFY_DONE;
3221
3222         if (event != NETDEV_BONDING_FAILOVER)
3223                 return NOTIFY_DONE;
3224
3225         if (!(ndev->flags & IFF_MASTER) || !(ndev->priv_flags & IFF_BONDING))
3226                 return NOTIFY_DONE;
3227 #else
3228         if (event != NETDEV_DOWN && event != NETDEV_UNREGISTER)
3229                 return NOTIFY_DONE;
3230 #endif
3231
3232         mutex_lock(&lock);
3233         list_for_each_entry(cma_dev, &dev_list, list)
3234                 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
3235                         ret = cma_netdev_change(ndev, id_priv);
3236                         if (ret)
3237                                 goto out;
3238                 }
3239
3240 out:
3241         mutex_unlock(&lock);
3242         return ret;
3243 }
3244
3245 static struct notifier_block cma_nb = {
3246         .notifier_call = cma_netdev_callback
3247 };
3248
3249 static void cma_add_one(struct ib_device *device)
3250 {
3251         struct cma_device *cma_dev;
3252         struct rdma_id_private *id_priv;
3253
3254         cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
3255         if (!cma_dev)
3256                 return;
3257
3258         cma_dev->device = device;
3259
3260         init_completion(&cma_dev->comp);
3261         atomic_set(&cma_dev->refcount, 1);
3262         INIT_LIST_HEAD(&cma_dev->id_list);
3263         ib_set_client_data(device, &cma_client, cma_dev);
3264
3265         mutex_lock(&lock);
3266         list_add_tail(&cma_dev->list, &dev_list);
3267         list_for_each_entry(id_priv, &listen_any_list, list)
3268                 cma_listen_on_dev(id_priv, cma_dev);
3269         mutex_unlock(&lock);
3270 }
3271
3272 static int cma_remove_id_dev(struct rdma_id_private *id_priv)
3273 {
3274         struct rdma_cm_event event;
3275         enum cma_state state;
3276         int ret = 0;
3277
3278         /* Record that we want to remove the device */
3279         state = cma_exch(id_priv, CMA_DEVICE_REMOVAL);
3280         if (state == CMA_DESTROYING)
3281                 return 0;
3282
3283         cma_cancel_operation(id_priv, state);
3284         mutex_lock(&id_priv->handler_mutex);
3285
3286         /* Check for destruction from another callback. */
3287         if (!cma_comp(id_priv, CMA_DEVICE_REMOVAL))
3288                 goto out;
3289
3290         memset(&event, 0, sizeof event);
3291         event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
3292         ret = id_priv->id.event_handler(&id_priv->id, &event);
3293 out:
3294         mutex_unlock(&id_priv->handler_mutex);
3295         return ret;
3296 }
3297
3298 static void cma_process_remove(struct cma_device *cma_dev)
3299 {
3300         struct rdma_id_private *id_priv;
3301         int ret;
3302
3303         mutex_lock(&lock);
3304         while (!list_empty(&cma_dev->id_list)) {
3305                 id_priv = list_entry(cma_dev->id_list.next,
3306                                      struct rdma_id_private, list);
3307
3308                 list_del(&id_priv->listen_list);
3309                 list_del_init(&id_priv->list);
3310                 atomic_inc(&id_priv->refcount);
3311                 mutex_unlock(&lock);
3312
3313                 ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv);
3314                 cma_deref_id(id_priv);
3315                 if (ret)
3316                         rdma_destroy_id(&id_priv->id);
3317
3318                 mutex_lock(&lock);
3319         }
3320         mutex_unlock(&lock);
3321
3322         cma_deref_dev(cma_dev);
3323         wait_for_completion(&cma_dev->comp);
3324 }
3325
3326 static void cma_remove_one(struct ib_device *device)
3327 {
3328         struct cma_device *cma_dev;
3329
3330         cma_dev = ib_get_client_data(device, &cma_client);
3331         if (!cma_dev)
3332                 return;
3333
3334         mutex_lock(&lock);
3335         list_del(&cma_dev->list);
3336         mutex_unlock(&lock);
3337
3338         cma_process_remove(cma_dev);
3339         kfree(cma_dev);
3340 }
3341
3342 static int cma_init(void)
3343 {
3344         int ret, low, high, remaining;
3345
3346         get_random_bytes(&next_port, sizeof next_port);
3347         inet_get_local_port_range(&low, &high);
3348         remaining = (high - low) + 1;
3349         next_port = ((unsigned int) next_port % remaining) + low;
3350
3351         cma_wq = create_singlethread_workqueue("rdma_cm");
3352         if (!cma_wq)
3353                 return -ENOMEM;
3354
3355         ib_sa_register_client(&sa_client);
3356         rdma_addr_register_client(&addr_client);
3357         register_netdevice_notifier(&cma_nb);
3358
3359         ret = ib_register_client(&cma_client);
3360         if (ret)
3361                 goto err;
3362         return 0;
3363
3364 err:
3365         unregister_netdevice_notifier(&cma_nb);
3366         rdma_addr_unregister_client(&addr_client);
3367         ib_sa_unregister_client(&sa_client);
3368         destroy_workqueue(cma_wq);
3369         return ret;
3370 }
3371
3372 static void cma_cleanup(void)
3373 {
3374         ib_unregister_client(&cma_client);
3375         unregister_netdevice_notifier(&cma_nb);
3376         rdma_addr_unregister_client(&addr_client);
3377         ib_sa_unregister_client(&sa_client);
3378         destroy_workqueue(cma_wq);
3379         idr_destroy(&sdp_ps);
3380         idr_destroy(&tcp_ps);
3381         idr_destroy(&udp_ps);
3382         idr_destroy(&ipoib_ps);
3383 }
3384
3385 module_init(cma_init);
3386 module_exit(cma_cleanup);