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