GNU Linux-libre 4.9.337-gnu1
[releases.git] / drivers / infiniband / core / ucma.c
1 /*
2  * Copyright (c) 2005-2006 Intel Corporation.  All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *      copyright notice, this list of conditions and the following
16  *      disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *      copyright notice, this list of conditions and the following
20  *      disclaimer in the documentation and/or other materials
21  *      provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #include <linux/completion.h>
34 #include <linux/file.h>
35 #include <linux/mutex.h>
36 #include <linux/poll.h>
37 #include <linux/sched.h>
38 #include <linux/idr.h>
39 #include <linux/in.h>
40 #include <linux/in6.h>
41 #include <linux/miscdevice.h>
42 #include <linux/slab.h>
43 #include <linux/sysctl.h>
44 #include <linux/module.h>
45 #include <linux/nsproxy.h>
46
47 #include <linux/nospec.h>
48
49 #include <rdma/rdma_user_cm.h>
50 #include <rdma/ib_marshall.h>
51 #include <rdma/rdma_cm.h>
52 #include <rdma/rdma_cm_ib.h>
53 #include <rdma/ib_addr.h>
54 #include <rdma/ib.h>
55
56 MODULE_AUTHOR("Sean Hefty");
57 MODULE_DESCRIPTION("RDMA Userspace Connection Manager Access");
58 MODULE_LICENSE("Dual BSD/GPL");
59
60 static unsigned int max_backlog = 1024;
61
62 static struct ctl_table_header *ucma_ctl_table_hdr;
63 static struct ctl_table ucma_ctl_table[] = {
64         {
65                 .procname       = "max_backlog",
66                 .data           = &max_backlog,
67                 .maxlen         = sizeof max_backlog,
68                 .mode           = 0644,
69                 .proc_handler   = proc_dointvec,
70         },
71         { }
72 };
73
74 struct ucma_file {
75         struct mutex            mut;
76         struct file             *filp;
77         struct list_head        ctx_list;
78         struct list_head        event_list;
79         wait_queue_head_t       poll_wait;
80         struct workqueue_struct *close_wq;
81 };
82
83 struct ucma_context {
84         int                     id;
85         struct completion       comp;
86         atomic_t                ref;
87         int                     events_reported;
88         int                     backlog;
89
90         struct ucma_file        *file;
91         struct rdma_cm_id       *cm_id;
92         u64                     uid;
93
94         struct list_head        list;
95         struct list_head        mc_list;
96         /* mark that device is in process of destroying the internal HW
97          * resources, protected by the global mut
98          */
99         int                     closing;
100         /* sync between removal event and id destroy, protected by file mut */
101         int                     destroying;
102         struct work_struct      close_work;
103 };
104
105 struct ucma_multicast {
106         struct ucma_context     *ctx;
107         int                     id;
108         int                     events_reported;
109
110         u64                     uid;
111         u8                      join_state;
112         struct list_head        list;
113         struct sockaddr_storage addr;
114 };
115
116 struct ucma_event {
117         struct ucma_context     *ctx;
118         struct ucma_multicast   *mc;
119         struct list_head        list;
120         struct rdma_cm_id       *cm_id;
121         struct rdma_ucm_event_resp resp;
122         struct work_struct      close_work;
123 };
124
125 static DEFINE_MUTEX(mut);
126 static DEFINE_IDR(ctx_idr);
127 static DEFINE_IDR(multicast_idr);
128
129 static const struct file_operations ucma_fops;
130
131 static inline struct ucma_context *_ucma_find_context(int id,
132                                                       struct ucma_file *file)
133 {
134         struct ucma_context *ctx;
135
136         ctx = idr_find(&ctx_idr, id);
137         if (!ctx)
138                 ctx = ERR_PTR(-ENOENT);
139         else if (ctx->file != file || !ctx->cm_id)
140                 ctx = ERR_PTR(-EINVAL);
141         return ctx;
142 }
143
144 static struct ucma_context *ucma_get_ctx(struct ucma_file *file, int id)
145 {
146         struct ucma_context *ctx;
147
148         mutex_lock(&mut);
149         ctx = _ucma_find_context(id, file);
150         if (!IS_ERR(ctx)) {
151                 if (ctx->closing)
152                         ctx = ERR_PTR(-EIO);
153                 else
154                         atomic_inc(&ctx->ref);
155         }
156         mutex_unlock(&mut);
157         return ctx;
158 }
159
160 static void ucma_put_ctx(struct ucma_context *ctx)
161 {
162         if (atomic_dec_and_test(&ctx->ref))
163                 complete(&ctx->comp);
164 }
165
166 static void ucma_close_event_id(struct work_struct *work)
167 {
168         struct ucma_event *uevent_close =  container_of(work, struct ucma_event, close_work);
169
170         rdma_destroy_id(uevent_close->cm_id);
171         kfree(uevent_close);
172 }
173
174 static void ucma_close_id(struct work_struct *work)
175 {
176         struct ucma_context *ctx =  container_of(work, struct ucma_context, close_work);
177
178         /* once all inflight tasks are finished, we close all underlying
179          * resources. The context is still alive till its explicit destryoing
180          * by its creator.
181          */
182         ucma_put_ctx(ctx);
183         wait_for_completion(&ctx->comp);
184         /* No new events will be generated after destroying the id. */
185         rdma_destroy_id(ctx->cm_id);
186 }
187
188 static struct ucma_context *ucma_alloc_ctx(struct ucma_file *file)
189 {
190         struct ucma_context *ctx;
191
192         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
193         if (!ctx)
194                 return NULL;
195
196         INIT_WORK(&ctx->close_work, ucma_close_id);
197         atomic_set(&ctx->ref, 1);
198         init_completion(&ctx->comp);
199         INIT_LIST_HEAD(&ctx->mc_list);
200         ctx->file = file;
201
202         mutex_lock(&mut);
203         ctx->id = idr_alloc(&ctx_idr, ctx, 0, 0, GFP_KERNEL);
204         mutex_unlock(&mut);
205         if (ctx->id < 0)
206                 goto error;
207
208         list_add_tail(&ctx->list, &file->ctx_list);
209         return ctx;
210
211 error:
212         kfree(ctx);
213         return NULL;
214 }
215
216 static struct ucma_multicast* ucma_alloc_multicast(struct ucma_context *ctx)
217 {
218         struct ucma_multicast *mc;
219
220         mc = kzalloc(sizeof(*mc), GFP_KERNEL);
221         if (!mc)
222                 return NULL;
223
224         mutex_lock(&mut);
225         mc->id = idr_alloc(&multicast_idr, NULL, 0, 0, GFP_KERNEL);
226         mutex_unlock(&mut);
227         if (mc->id < 0)
228                 goto error;
229
230         mc->ctx = ctx;
231         list_add_tail(&mc->list, &ctx->mc_list);
232         return mc;
233
234 error:
235         kfree(mc);
236         return NULL;
237 }
238
239 static void ucma_copy_conn_event(struct rdma_ucm_conn_param *dst,
240                                  struct rdma_conn_param *src)
241 {
242         if (src->private_data_len)
243                 memcpy(dst->private_data, src->private_data,
244                        src->private_data_len);
245         dst->private_data_len = src->private_data_len;
246         dst->responder_resources =src->responder_resources;
247         dst->initiator_depth = src->initiator_depth;
248         dst->flow_control = src->flow_control;
249         dst->retry_count = src->retry_count;
250         dst->rnr_retry_count = src->rnr_retry_count;
251         dst->srq = src->srq;
252         dst->qp_num = src->qp_num;
253 }
254
255 static void ucma_copy_ud_event(struct rdma_ucm_ud_param *dst,
256                                struct rdma_ud_param *src)
257 {
258         if (src->private_data_len)
259                 memcpy(dst->private_data, src->private_data,
260                        src->private_data_len);
261         dst->private_data_len = src->private_data_len;
262         ib_copy_ah_attr_to_user(&dst->ah_attr, &src->ah_attr);
263         dst->qp_num = src->qp_num;
264         dst->qkey = src->qkey;
265 }
266
267 static void ucma_set_event_context(struct ucma_context *ctx,
268                                    struct rdma_cm_event *event,
269                                    struct ucma_event *uevent)
270 {
271         uevent->ctx = ctx;
272         switch (event->event) {
273         case RDMA_CM_EVENT_MULTICAST_JOIN:
274         case RDMA_CM_EVENT_MULTICAST_ERROR:
275                 uevent->mc = (struct ucma_multicast *)
276                              event->param.ud.private_data;
277                 uevent->resp.uid = uevent->mc->uid;
278                 uevent->resp.id = uevent->mc->id;
279                 break;
280         default:
281                 uevent->resp.uid = ctx->uid;
282                 uevent->resp.id = ctx->id;
283                 break;
284         }
285 }
286
287 /* Called with file->mut locked for the relevant context. */
288 static void ucma_removal_event_handler(struct rdma_cm_id *cm_id)
289 {
290         struct ucma_context *ctx = cm_id->context;
291         struct ucma_event *con_req_eve;
292         int event_found = 0;
293
294         if (ctx->destroying)
295                 return;
296
297         /* only if context is pointing to cm_id that it owns it and can be
298          * queued to be closed, otherwise that cm_id is an inflight one that
299          * is part of that context event list pending to be detached and
300          * reattached to its new context as part of ucma_get_event,
301          * handled separately below.
302          */
303         if (ctx->cm_id == cm_id) {
304                 mutex_lock(&mut);
305                 ctx->closing = 1;
306                 mutex_unlock(&mut);
307                 queue_work(ctx->file->close_wq, &ctx->close_work);
308                 return;
309         }
310
311         list_for_each_entry(con_req_eve, &ctx->file->event_list, list) {
312                 if (con_req_eve->cm_id == cm_id &&
313                     con_req_eve->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST) {
314                         list_del(&con_req_eve->list);
315                         INIT_WORK(&con_req_eve->close_work, ucma_close_event_id);
316                         queue_work(ctx->file->close_wq, &con_req_eve->close_work);
317                         event_found = 1;
318                         break;
319                 }
320         }
321         if (!event_found)
322                 pr_err("ucma_removal_event_handler: warning: connect request event wasn't found\n");
323 }
324
325 static int ucma_event_handler(struct rdma_cm_id *cm_id,
326                               struct rdma_cm_event *event)
327 {
328         struct ucma_event *uevent;
329         struct ucma_context *ctx = cm_id->context;
330         int ret = 0;
331
332         uevent = kzalloc(sizeof(*uevent), GFP_KERNEL);
333         if (!uevent)
334                 return event->event == RDMA_CM_EVENT_CONNECT_REQUEST;
335
336         mutex_lock(&ctx->file->mut);
337         uevent->cm_id = cm_id;
338         ucma_set_event_context(ctx, event, uevent);
339         uevent->resp.event = event->event;
340         uevent->resp.status = event->status;
341         if (cm_id->qp_type == IB_QPT_UD)
342                 ucma_copy_ud_event(&uevent->resp.param.ud, &event->param.ud);
343         else
344                 ucma_copy_conn_event(&uevent->resp.param.conn,
345                                      &event->param.conn);
346
347         if (event->event == RDMA_CM_EVENT_CONNECT_REQUEST) {
348                 if (!ctx->backlog) {
349                         ret = -ENOMEM;
350                         kfree(uevent);
351                         goto out;
352                 }
353                 ctx->backlog--;
354         } else if (!ctx->uid || ctx->cm_id != cm_id) {
355                 /*
356                  * We ignore events for new connections until userspace has set
357                  * their context.  This can only happen if an error occurs on a
358                  * new connection before the user accepts it.  This is okay,
359                  * since the accept will just fail later. However, we do need
360                  * to release the underlying HW resources in case of a device
361                  * removal event.
362                  */
363                 if (event->event == RDMA_CM_EVENT_DEVICE_REMOVAL)
364                         ucma_removal_event_handler(cm_id);
365
366                 kfree(uevent);
367                 goto out;
368         }
369
370         list_add_tail(&uevent->list, &ctx->file->event_list);
371         wake_up_interruptible(&ctx->file->poll_wait);
372         if (event->event == RDMA_CM_EVENT_DEVICE_REMOVAL)
373                 ucma_removal_event_handler(cm_id);
374 out:
375         mutex_unlock(&ctx->file->mut);
376         return ret;
377 }
378
379 static ssize_t ucma_get_event(struct ucma_file *file, const char __user *inbuf,
380                               int in_len, int out_len)
381 {
382         struct ucma_context *ctx;
383         struct rdma_ucm_get_event cmd;
384         struct ucma_event *uevent;
385         int ret = 0;
386
387         if (out_len < sizeof uevent->resp)
388                 return -ENOSPC;
389
390         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
391                 return -EFAULT;
392
393         mutex_lock(&file->mut);
394         while (list_empty(&file->event_list)) {
395                 mutex_unlock(&file->mut);
396
397                 if (file->filp->f_flags & O_NONBLOCK)
398                         return -EAGAIN;
399
400                 if (wait_event_interruptible(file->poll_wait,
401                                              !list_empty(&file->event_list)))
402                         return -ERESTARTSYS;
403
404                 mutex_lock(&file->mut);
405         }
406
407         uevent = list_entry(file->event_list.next, struct ucma_event, list);
408
409         if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST) {
410                 ctx = ucma_alloc_ctx(file);
411                 if (!ctx) {
412                         ret = -ENOMEM;
413                         goto done;
414                 }
415                 uevent->ctx->backlog++;
416                 ctx->cm_id = uevent->cm_id;
417                 ctx->cm_id->context = ctx;
418                 uevent->resp.id = ctx->id;
419         }
420
421         if (copy_to_user((void __user *)(unsigned long)cmd.response,
422                          &uevent->resp, sizeof uevent->resp)) {
423                 ret = -EFAULT;
424                 goto done;
425         }
426
427         list_del(&uevent->list);
428         uevent->ctx->events_reported++;
429         if (uevent->mc)
430                 uevent->mc->events_reported++;
431         kfree(uevent);
432 done:
433         mutex_unlock(&file->mut);
434         return ret;
435 }
436
437 static int ucma_get_qp_type(struct rdma_ucm_create_id *cmd, enum ib_qp_type *qp_type)
438 {
439         switch (cmd->ps) {
440         case RDMA_PS_TCP:
441                 *qp_type = IB_QPT_RC;
442                 return 0;
443         case RDMA_PS_UDP:
444         case RDMA_PS_IPOIB:
445                 *qp_type = IB_QPT_UD;
446                 return 0;
447         case RDMA_PS_IB:
448                 *qp_type = cmd->qp_type;
449                 return 0;
450         default:
451                 return -EINVAL;
452         }
453 }
454
455 static ssize_t ucma_create_id(struct ucma_file *file, const char __user *inbuf,
456                               int in_len, int out_len)
457 {
458         struct rdma_ucm_create_id cmd;
459         struct rdma_ucm_create_id_resp resp;
460         struct ucma_context *ctx;
461         struct rdma_cm_id *cm_id;
462         enum ib_qp_type qp_type;
463         int ret;
464
465         if (out_len < sizeof(resp))
466                 return -ENOSPC;
467
468         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
469                 return -EFAULT;
470
471         ret = ucma_get_qp_type(&cmd, &qp_type);
472         if (ret)
473                 return ret;
474
475         mutex_lock(&file->mut);
476         ctx = ucma_alloc_ctx(file);
477         mutex_unlock(&file->mut);
478         if (!ctx)
479                 return -ENOMEM;
480
481         ctx->uid = cmd.uid;
482         cm_id = rdma_create_id(current->nsproxy->net_ns,
483                                ucma_event_handler, ctx, cmd.ps, qp_type);
484         if (IS_ERR(cm_id)) {
485                 ret = PTR_ERR(cm_id);
486                 goto err1;
487         }
488
489         resp.id = ctx->id;
490         if (copy_to_user((void __user *)(unsigned long)cmd.response,
491                          &resp, sizeof(resp))) {
492                 ret = -EFAULT;
493                 goto err2;
494         }
495
496         ctx->cm_id = cm_id;
497         return 0;
498
499 err2:
500         rdma_destroy_id(cm_id);
501 err1:
502         mutex_lock(&mut);
503         idr_remove(&ctx_idr, ctx->id);
504         mutex_unlock(&mut);
505         mutex_lock(&file->mut);
506         list_del(&ctx->list);
507         mutex_unlock(&file->mut);
508         kfree(ctx);
509         return ret;
510 }
511
512 static void ucma_cleanup_multicast(struct ucma_context *ctx)
513 {
514         struct ucma_multicast *mc, *tmp;
515
516         mutex_lock(&mut);
517         list_for_each_entry_safe(mc, tmp, &ctx->mc_list, list) {
518                 list_del(&mc->list);
519                 idr_remove(&multicast_idr, mc->id);
520                 kfree(mc);
521         }
522         mutex_unlock(&mut);
523 }
524
525 static void ucma_cleanup_mc_events(struct ucma_multicast *mc)
526 {
527         struct ucma_event *uevent, *tmp;
528
529         list_for_each_entry_safe(uevent, tmp, &mc->ctx->file->event_list, list) {
530                 if (uevent->mc != mc)
531                         continue;
532
533                 list_del(&uevent->list);
534                 kfree(uevent);
535         }
536 }
537
538 /*
539  * ucma_free_ctx is called after the underlying rdma CM-ID is destroyed. At
540  * this point, no new events will be reported from the hardware. However, we
541  * still need to cleanup the UCMA context for this ID. Specifically, there
542  * might be events that have not yet been consumed by the user space software.
543  * These might include pending connect requests which we have not completed
544  * processing.  We cannot call rdma_destroy_id while holding the lock of the
545  * context (file->mut), as it might cause a deadlock. We therefore extract all
546  * relevant events from the context pending events list while holding the
547  * mutex. After that we release them as needed.
548  */
549 static int ucma_free_ctx(struct ucma_context *ctx)
550 {
551         int events_reported;
552         struct ucma_event *uevent, *tmp;
553         LIST_HEAD(list);
554
555
556         ucma_cleanup_multicast(ctx);
557
558         /* Cleanup events not yet reported to the user. */
559         mutex_lock(&ctx->file->mut);
560         list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list) {
561                 if (uevent->ctx == ctx)
562                         list_move_tail(&uevent->list, &list);
563         }
564         list_del(&ctx->list);
565         mutex_unlock(&ctx->file->mut);
566
567         list_for_each_entry_safe(uevent, tmp, &list, list) {
568                 list_del(&uevent->list);
569                 if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST)
570                         rdma_destroy_id(uevent->cm_id);
571                 kfree(uevent);
572         }
573
574         events_reported = ctx->events_reported;
575         kfree(ctx);
576         return events_reported;
577 }
578
579 static ssize_t ucma_destroy_id(struct ucma_file *file, const char __user *inbuf,
580                                int in_len, int out_len)
581 {
582         struct rdma_ucm_destroy_id cmd;
583         struct rdma_ucm_destroy_id_resp resp;
584         struct ucma_context *ctx;
585         int ret = 0;
586
587         if (out_len < sizeof(resp))
588                 return -ENOSPC;
589
590         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
591                 return -EFAULT;
592
593         mutex_lock(&mut);
594         ctx = _ucma_find_context(cmd.id, file);
595         if (!IS_ERR(ctx))
596                 idr_remove(&ctx_idr, ctx->id);
597         mutex_unlock(&mut);
598
599         if (IS_ERR(ctx))
600                 return PTR_ERR(ctx);
601
602         mutex_lock(&ctx->file->mut);
603         ctx->destroying = 1;
604         mutex_unlock(&ctx->file->mut);
605
606         flush_workqueue(ctx->file->close_wq);
607         /* At this point it's guaranteed that there is no inflight
608          * closing task */
609         mutex_lock(&mut);
610         if (!ctx->closing) {
611                 mutex_unlock(&mut);
612                 ucma_put_ctx(ctx);
613                 wait_for_completion(&ctx->comp);
614                 rdma_destroy_id(ctx->cm_id);
615         } else {
616                 mutex_unlock(&mut);
617         }
618
619         resp.events_reported = ucma_free_ctx(ctx);
620         if (copy_to_user((void __user *)(unsigned long)cmd.response,
621                          &resp, sizeof(resp)))
622                 ret = -EFAULT;
623
624         return ret;
625 }
626
627 static ssize_t ucma_bind_ip(struct ucma_file *file, const char __user *inbuf,
628                               int in_len, int out_len)
629 {
630         struct rdma_ucm_bind_ip cmd;
631         struct ucma_context *ctx;
632         int ret;
633
634         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
635                 return -EFAULT;
636
637         if (!rdma_addr_size_in6(&cmd.addr))
638                 return -EINVAL;
639
640         ctx = ucma_get_ctx(file, cmd.id);
641         if (IS_ERR(ctx))
642                 return PTR_ERR(ctx);
643
644         ret = rdma_bind_addr(ctx->cm_id, (struct sockaddr *) &cmd.addr);
645         ucma_put_ctx(ctx);
646         return ret;
647 }
648
649 static ssize_t ucma_bind(struct ucma_file *file, const char __user *inbuf,
650                          int in_len, int out_len)
651 {
652         struct rdma_ucm_bind cmd;
653         struct ucma_context *ctx;
654         int ret;
655
656         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
657                 return -EFAULT;
658
659         if (cmd.reserved || !cmd.addr_size ||
660             cmd.addr_size != rdma_addr_size_kss(&cmd.addr))
661                 return -EINVAL;
662
663         ctx = ucma_get_ctx(file, cmd.id);
664         if (IS_ERR(ctx))
665                 return PTR_ERR(ctx);
666
667         ret = rdma_bind_addr(ctx->cm_id, (struct sockaddr *) &cmd.addr);
668         ucma_put_ctx(ctx);
669         return ret;
670 }
671
672 static ssize_t ucma_resolve_ip(struct ucma_file *file,
673                                const char __user *inbuf,
674                                int in_len, int out_len)
675 {
676         struct rdma_ucm_resolve_ip cmd;
677         struct ucma_context *ctx;
678         int ret;
679
680         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
681                 return -EFAULT;
682
683         if ((cmd.src_addr.sin6_family && !rdma_addr_size_in6(&cmd.src_addr)) ||
684             !rdma_addr_size_in6(&cmd.dst_addr))
685                 return -EINVAL;
686
687         ctx = ucma_get_ctx(file, cmd.id);
688         if (IS_ERR(ctx))
689                 return PTR_ERR(ctx);
690
691         ret = rdma_resolve_addr(ctx->cm_id, (struct sockaddr *) &cmd.src_addr,
692                                 (struct sockaddr *) &cmd.dst_addr, cmd.timeout_ms);
693         ucma_put_ctx(ctx);
694         return ret;
695 }
696
697 static ssize_t ucma_resolve_addr(struct ucma_file *file,
698                                  const char __user *inbuf,
699                                  int in_len, int out_len)
700 {
701         struct rdma_ucm_resolve_addr cmd;
702         struct ucma_context *ctx;
703         int ret;
704
705         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
706                 return -EFAULT;
707
708         if (cmd.reserved ||
709             (cmd.src_size && (cmd.src_size != rdma_addr_size_kss(&cmd.src_addr))) ||
710             !cmd.dst_size || (cmd.dst_size != rdma_addr_size_kss(&cmd.dst_addr)))
711                 return -EINVAL;
712
713         ctx = ucma_get_ctx(file, cmd.id);
714         if (IS_ERR(ctx))
715                 return PTR_ERR(ctx);
716
717         ret = rdma_resolve_addr(ctx->cm_id, (struct sockaddr *) &cmd.src_addr,
718                                 (struct sockaddr *) &cmd.dst_addr, cmd.timeout_ms);
719         ucma_put_ctx(ctx);
720         return ret;
721 }
722
723 static ssize_t ucma_resolve_route(struct ucma_file *file,
724                                   const char __user *inbuf,
725                                   int in_len, int out_len)
726 {
727         struct rdma_ucm_resolve_route cmd;
728         struct ucma_context *ctx;
729         int ret;
730
731         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
732                 return -EFAULT;
733
734         ctx = ucma_get_ctx(file, cmd.id);
735         if (IS_ERR(ctx))
736                 return PTR_ERR(ctx);
737
738         ret = rdma_resolve_route(ctx->cm_id, cmd.timeout_ms);
739         ucma_put_ctx(ctx);
740         return ret;
741 }
742
743 static void ucma_copy_ib_route(struct rdma_ucm_query_route_resp *resp,
744                                struct rdma_route *route)
745 {
746         struct rdma_dev_addr *dev_addr;
747
748         resp->num_paths = route->num_paths;
749         switch (route->num_paths) {
750         case 0:
751                 dev_addr = &route->addr.dev_addr;
752                 rdma_addr_get_dgid(dev_addr,
753                                    (union ib_gid *) &resp->ib_route[0].dgid);
754                 rdma_addr_get_sgid(dev_addr,
755                                    (union ib_gid *) &resp->ib_route[0].sgid);
756                 resp->ib_route[0].pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
757                 break;
758         case 2:
759                 ib_copy_path_rec_to_user(&resp->ib_route[1],
760                                          &route->path_rec[1]);
761                 /* fall through */
762         case 1:
763                 ib_copy_path_rec_to_user(&resp->ib_route[0],
764                                          &route->path_rec[0]);
765                 break;
766         default:
767                 break;
768         }
769 }
770
771 static void ucma_copy_iboe_route(struct rdma_ucm_query_route_resp *resp,
772                                  struct rdma_route *route)
773 {
774
775         resp->num_paths = route->num_paths;
776         switch (route->num_paths) {
777         case 0:
778                 rdma_ip2gid((struct sockaddr *)&route->addr.dst_addr,
779                             (union ib_gid *)&resp->ib_route[0].dgid);
780                 rdma_ip2gid((struct sockaddr *)&route->addr.src_addr,
781                             (union ib_gid *)&resp->ib_route[0].sgid);
782                 resp->ib_route[0].pkey = cpu_to_be16(0xffff);
783                 break;
784         case 2:
785                 ib_copy_path_rec_to_user(&resp->ib_route[1],
786                                          &route->path_rec[1]);
787                 /* fall through */
788         case 1:
789                 ib_copy_path_rec_to_user(&resp->ib_route[0],
790                                          &route->path_rec[0]);
791                 break;
792         default:
793                 break;
794         }
795 }
796
797 static void ucma_copy_iw_route(struct rdma_ucm_query_route_resp *resp,
798                                struct rdma_route *route)
799 {
800         struct rdma_dev_addr *dev_addr;
801
802         dev_addr = &route->addr.dev_addr;
803         rdma_addr_get_dgid(dev_addr, (union ib_gid *) &resp->ib_route[0].dgid);
804         rdma_addr_get_sgid(dev_addr, (union ib_gid *) &resp->ib_route[0].sgid);
805 }
806
807 static ssize_t ucma_query_route(struct ucma_file *file,
808                                 const char __user *inbuf,
809                                 int in_len, int out_len)
810 {
811         struct rdma_ucm_query cmd;
812         struct rdma_ucm_query_route_resp resp;
813         struct ucma_context *ctx;
814         struct sockaddr *addr;
815         int ret = 0;
816
817         if (out_len < sizeof(resp))
818                 return -ENOSPC;
819
820         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
821                 return -EFAULT;
822
823         ctx = ucma_get_ctx(file, cmd.id);
824         if (IS_ERR(ctx))
825                 return PTR_ERR(ctx);
826
827         memset(&resp, 0, sizeof resp);
828         addr = (struct sockaddr *) &ctx->cm_id->route.addr.src_addr;
829         memcpy(&resp.src_addr, addr, addr->sa_family == AF_INET ?
830                                      sizeof(struct sockaddr_in) :
831                                      sizeof(struct sockaddr_in6));
832         addr = (struct sockaddr *) &ctx->cm_id->route.addr.dst_addr;
833         memcpy(&resp.dst_addr, addr, addr->sa_family == AF_INET ?
834                                      sizeof(struct sockaddr_in) :
835                                      sizeof(struct sockaddr_in6));
836         if (!ctx->cm_id->device)
837                 goto out;
838
839         resp.node_guid = (__force __u64) ctx->cm_id->device->node_guid;
840         resp.port_num = ctx->cm_id->port_num;
841
842         if (rdma_cap_ib_sa(ctx->cm_id->device, ctx->cm_id->port_num))
843                 ucma_copy_ib_route(&resp, &ctx->cm_id->route);
844         else if (rdma_protocol_roce(ctx->cm_id->device, ctx->cm_id->port_num))
845                 ucma_copy_iboe_route(&resp, &ctx->cm_id->route);
846         else if (rdma_protocol_iwarp(ctx->cm_id->device, ctx->cm_id->port_num))
847                 ucma_copy_iw_route(&resp, &ctx->cm_id->route);
848
849 out:
850         if (copy_to_user((void __user *)(unsigned long)cmd.response,
851                          &resp, sizeof(resp)))
852                 ret = -EFAULT;
853
854         ucma_put_ctx(ctx);
855         return ret;
856 }
857
858 static void ucma_query_device_addr(struct rdma_cm_id *cm_id,
859                                    struct rdma_ucm_query_addr_resp *resp)
860 {
861         if (!cm_id->device)
862                 return;
863
864         resp->node_guid = (__force __u64) cm_id->device->node_guid;
865         resp->port_num = cm_id->port_num;
866         resp->pkey = (__force __u16) cpu_to_be16(
867                      ib_addr_get_pkey(&cm_id->route.addr.dev_addr));
868 }
869
870 static ssize_t ucma_query_addr(struct ucma_context *ctx,
871                                void __user *response, int out_len)
872 {
873         struct rdma_ucm_query_addr_resp resp;
874         struct sockaddr *addr;
875         int ret = 0;
876
877         if (out_len < sizeof(resp))
878                 return -ENOSPC;
879
880         memset(&resp, 0, sizeof resp);
881
882         addr = (struct sockaddr *) &ctx->cm_id->route.addr.src_addr;
883         resp.src_size = rdma_addr_size(addr);
884         memcpy(&resp.src_addr, addr, resp.src_size);
885
886         addr = (struct sockaddr *) &ctx->cm_id->route.addr.dst_addr;
887         resp.dst_size = rdma_addr_size(addr);
888         memcpy(&resp.dst_addr, addr, resp.dst_size);
889
890         ucma_query_device_addr(ctx->cm_id, &resp);
891
892         if (copy_to_user(response, &resp, sizeof(resp)))
893                 ret = -EFAULT;
894
895         return ret;
896 }
897
898 static ssize_t ucma_query_path(struct ucma_context *ctx,
899                                void __user *response, int out_len)
900 {
901         struct rdma_ucm_query_path_resp *resp;
902         int i, ret = 0;
903
904         if (out_len < sizeof(*resp))
905                 return -ENOSPC;
906
907         resp = kzalloc(out_len, GFP_KERNEL);
908         if (!resp)
909                 return -ENOMEM;
910
911         resp->num_paths = ctx->cm_id->route.num_paths;
912         for (i = 0, out_len -= sizeof(*resp);
913              i < resp->num_paths && out_len > sizeof(struct ib_path_rec_data);
914              i++, out_len -= sizeof(struct ib_path_rec_data)) {
915
916                 resp->path_data[i].flags = IB_PATH_GMP | IB_PATH_PRIMARY |
917                                            IB_PATH_BIDIRECTIONAL;
918                 ib_sa_pack_path(&ctx->cm_id->route.path_rec[i],
919                                 &resp->path_data[i].path_rec);
920         }
921
922         if (copy_to_user(response, resp,
923                          sizeof(*resp) + (i * sizeof(struct ib_path_rec_data))))
924                 ret = -EFAULT;
925
926         kfree(resp);
927         return ret;
928 }
929
930 static ssize_t ucma_query_gid(struct ucma_context *ctx,
931                               void __user *response, int out_len)
932 {
933         struct rdma_ucm_query_addr_resp resp;
934         struct sockaddr_ib *addr;
935         int ret = 0;
936
937         if (out_len < sizeof(resp))
938                 return -ENOSPC;
939
940         memset(&resp, 0, sizeof resp);
941
942         ucma_query_device_addr(ctx->cm_id, &resp);
943
944         addr = (struct sockaddr_ib *) &resp.src_addr;
945         resp.src_size = sizeof(*addr);
946         if (ctx->cm_id->route.addr.src_addr.ss_family == AF_IB) {
947                 memcpy(addr, &ctx->cm_id->route.addr.src_addr, resp.src_size);
948         } else {
949                 addr->sib_family = AF_IB;
950                 addr->sib_pkey = (__force __be16) resp.pkey;
951                 rdma_addr_get_sgid(&ctx->cm_id->route.addr.dev_addr,
952                                    (union ib_gid *) &addr->sib_addr);
953                 addr->sib_sid = rdma_get_service_id(ctx->cm_id, (struct sockaddr *)
954                                                     &ctx->cm_id->route.addr.src_addr);
955         }
956
957         addr = (struct sockaddr_ib *) &resp.dst_addr;
958         resp.dst_size = sizeof(*addr);
959         if (ctx->cm_id->route.addr.dst_addr.ss_family == AF_IB) {
960                 memcpy(addr, &ctx->cm_id->route.addr.dst_addr, resp.dst_size);
961         } else {
962                 addr->sib_family = AF_IB;
963                 addr->sib_pkey = (__force __be16) resp.pkey;
964                 rdma_addr_get_dgid(&ctx->cm_id->route.addr.dev_addr,
965                                    (union ib_gid *) &addr->sib_addr);
966                 addr->sib_sid = rdma_get_service_id(ctx->cm_id, (struct sockaddr *)
967                                                     &ctx->cm_id->route.addr.dst_addr);
968         }
969
970         if (copy_to_user(response, &resp, sizeof(resp)))
971                 ret = -EFAULT;
972
973         return ret;
974 }
975
976 static ssize_t ucma_query(struct ucma_file *file,
977                           const char __user *inbuf,
978                           int in_len, int out_len)
979 {
980         struct rdma_ucm_query cmd;
981         struct ucma_context *ctx;
982         void __user *response;
983         int ret;
984
985         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
986                 return -EFAULT;
987
988         response = (void __user *)(unsigned long) cmd.response;
989         ctx = ucma_get_ctx(file, cmd.id);
990         if (IS_ERR(ctx))
991                 return PTR_ERR(ctx);
992
993         switch (cmd.option) {
994         case RDMA_USER_CM_QUERY_ADDR:
995                 ret = ucma_query_addr(ctx, response, out_len);
996                 break;
997         case RDMA_USER_CM_QUERY_PATH:
998                 ret = ucma_query_path(ctx, response, out_len);
999                 break;
1000         case RDMA_USER_CM_QUERY_GID:
1001                 ret = ucma_query_gid(ctx, response, out_len);
1002                 break;
1003         default:
1004                 ret = -ENOSYS;
1005                 break;
1006         }
1007
1008         ucma_put_ctx(ctx);
1009         return ret;
1010 }
1011
1012 static void ucma_copy_conn_param(struct rdma_cm_id *id,
1013                                  struct rdma_conn_param *dst,
1014                                  struct rdma_ucm_conn_param *src)
1015 {
1016         dst->private_data = src->private_data;
1017         dst->private_data_len = src->private_data_len;
1018         dst->responder_resources =src->responder_resources;
1019         dst->initiator_depth = src->initiator_depth;
1020         dst->flow_control = src->flow_control;
1021         dst->retry_count = src->retry_count;
1022         dst->rnr_retry_count = src->rnr_retry_count;
1023         dst->srq = src->srq;
1024         dst->qp_num = src->qp_num;
1025         dst->qkey = (id->route.addr.src_addr.ss_family == AF_IB) ? src->qkey : 0;
1026 }
1027
1028 static ssize_t ucma_connect(struct ucma_file *file, const char __user *inbuf,
1029                             int in_len, int out_len)
1030 {
1031         struct rdma_ucm_connect cmd;
1032         struct rdma_conn_param conn_param;
1033         struct ucma_context *ctx;
1034         int ret;
1035
1036         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1037                 return -EFAULT;
1038
1039         if (!cmd.conn_param.valid)
1040                 return -EINVAL;
1041
1042         ctx = ucma_get_ctx(file, cmd.id);
1043         if (IS_ERR(ctx))
1044                 return PTR_ERR(ctx);
1045
1046         ucma_copy_conn_param(ctx->cm_id, &conn_param, &cmd.conn_param);
1047         ret = rdma_connect(ctx->cm_id, &conn_param);
1048         ucma_put_ctx(ctx);
1049         return ret;
1050 }
1051
1052 static ssize_t ucma_listen(struct ucma_file *file, const char __user *inbuf,
1053                            int in_len, int out_len)
1054 {
1055         struct rdma_ucm_listen cmd;
1056         struct ucma_context *ctx;
1057         int ret;
1058
1059         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1060                 return -EFAULT;
1061
1062         ctx = ucma_get_ctx(file, cmd.id);
1063         if (IS_ERR(ctx))
1064                 return PTR_ERR(ctx);
1065
1066         ctx->backlog = cmd.backlog > 0 && cmd.backlog < max_backlog ?
1067                        cmd.backlog : max_backlog;
1068         ret = rdma_listen(ctx->cm_id, ctx->backlog);
1069         ucma_put_ctx(ctx);
1070         return ret;
1071 }
1072
1073 static ssize_t ucma_accept(struct ucma_file *file, const char __user *inbuf,
1074                            int in_len, int out_len)
1075 {
1076         struct rdma_ucm_accept cmd;
1077         struct rdma_conn_param conn_param;
1078         struct ucma_context *ctx;
1079         int ret;
1080
1081         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1082                 return -EFAULT;
1083
1084         ctx = ucma_get_ctx(file, cmd.id);
1085         if (IS_ERR(ctx))
1086                 return PTR_ERR(ctx);
1087
1088         if (cmd.conn_param.valid) {
1089                 ucma_copy_conn_param(ctx->cm_id, &conn_param, &cmd.conn_param);
1090                 mutex_lock(&file->mut);
1091                 ret = rdma_accept(ctx->cm_id, &conn_param);
1092                 if (!ret)
1093                         ctx->uid = cmd.uid;
1094                 mutex_unlock(&file->mut);
1095         } else
1096                 ret = rdma_accept(ctx->cm_id, NULL);
1097
1098         ucma_put_ctx(ctx);
1099         return ret;
1100 }
1101
1102 static ssize_t ucma_reject(struct ucma_file *file, const char __user *inbuf,
1103                            int in_len, int out_len)
1104 {
1105         struct rdma_ucm_reject cmd;
1106         struct ucma_context *ctx;
1107         int ret;
1108
1109         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1110                 return -EFAULT;
1111
1112         ctx = ucma_get_ctx(file, cmd.id);
1113         if (IS_ERR(ctx))
1114                 return PTR_ERR(ctx);
1115
1116         ret = rdma_reject(ctx->cm_id, cmd.private_data, cmd.private_data_len);
1117         ucma_put_ctx(ctx);
1118         return ret;
1119 }
1120
1121 static ssize_t ucma_disconnect(struct ucma_file *file, const char __user *inbuf,
1122                                int in_len, int out_len)
1123 {
1124         struct rdma_ucm_disconnect cmd;
1125         struct ucma_context *ctx;
1126         int ret;
1127
1128         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1129                 return -EFAULT;
1130
1131         ctx = ucma_get_ctx(file, cmd.id);
1132         if (IS_ERR(ctx))
1133                 return PTR_ERR(ctx);
1134
1135         ret = rdma_disconnect(ctx->cm_id);
1136         ucma_put_ctx(ctx);
1137         return ret;
1138 }
1139
1140 static ssize_t ucma_init_qp_attr(struct ucma_file *file,
1141                                  const char __user *inbuf,
1142                                  int in_len, int out_len)
1143 {
1144         struct rdma_ucm_init_qp_attr cmd;
1145         struct ib_uverbs_qp_attr resp;
1146         struct ucma_context *ctx;
1147         struct ib_qp_attr qp_attr;
1148         int ret;
1149
1150         if (out_len < sizeof(resp))
1151                 return -ENOSPC;
1152
1153         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1154                 return -EFAULT;
1155
1156         if (cmd.qp_state > IB_QPS_ERR)
1157                 return -EINVAL;
1158
1159         ctx = ucma_get_ctx(file, cmd.id);
1160         if (IS_ERR(ctx))
1161                 return PTR_ERR(ctx);
1162
1163         if (!ctx->cm_id->device) {
1164                 ret = -EINVAL;
1165                 goto out;
1166         }
1167
1168         resp.qp_attr_mask = 0;
1169         memset(&qp_attr, 0, sizeof qp_attr);
1170         qp_attr.qp_state = cmd.qp_state;
1171         ret = rdma_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
1172         if (ret)
1173                 goto out;
1174
1175         ib_copy_qp_attr_to_user(&resp, &qp_attr);
1176         if (copy_to_user((void __user *)(unsigned long)cmd.response,
1177                          &resp, sizeof(resp)))
1178                 ret = -EFAULT;
1179
1180 out:
1181         ucma_put_ctx(ctx);
1182         return ret;
1183 }
1184
1185 static int ucma_set_option_id(struct ucma_context *ctx, int optname,
1186                               void *optval, size_t optlen)
1187 {
1188         int ret = 0;
1189
1190         switch (optname) {
1191         case RDMA_OPTION_ID_TOS:
1192                 if (optlen != sizeof(u8)) {
1193                         ret = -EINVAL;
1194                         break;
1195                 }
1196                 rdma_set_service_type(ctx->cm_id, *((u8 *) optval));
1197                 break;
1198         case RDMA_OPTION_ID_REUSEADDR:
1199                 if (optlen != sizeof(int)) {
1200                         ret = -EINVAL;
1201                         break;
1202                 }
1203                 ret = rdma_set_reuseaddr(ctx->cm_id, *((int *) optval) ? 1 : 0);
1204                 break;
1205         case RDMA_OPTION_ID_AFONLY:
1206                 if (optlen != sizeof(int)) {
1207                         ret = -EINVAL;
1208                         break;
1209                 }
1210                 ret = rdma_set_afonly(ctx->cm_id, *((int *) optval) ? 1 : 0);
1211                 break;
1212         default:
1213                 ret = -ENOSYS;
1214         }
1215
1216         return ret;
1217 }
1218
1219 static int ucma_set_ib_path(struct ucma_context *ctx,
1220                             struct ib_path_rec_data *path_data, size_t optlen)
1221 {
1222         struct ib_sa_path_rec sa_path;
1223         struct rdma_cm_event event;
1224         int ret;
1225
1226         if (optlen % sizeof(*path_data))
1227                 return -EINVAL;
1228
1229         for (; optlen; optlen -= sizeof(*path_data), path_data++) {
1230                 if (path_data->flags == (IB_PATH_GMP | IB_PATH_PRIMARY |
1231                                          IB_PATH_BIDIRECTIONAL))
1232                         break;
1233         }
1234
1235         if (!optlen)
1236                 return -EINVAL;
1237
1238         if (!ctx->cm_id->device)
1239                 return -EINVAL;
1240
1241         memset(&sa_path, 0, sizeof(sa_path));
1242
1243         ib_sa_unpack_path(path_data->path_rec, &sa_path);
1244         ret = rdma_set_ib_paths(ctx->cm_id, &sa_path, 1);
1245         if (ret)
1246                 return ret;
1247
1248         memset(&event, 0, sizeof event);
1249         event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1250         return ucma_event_handler(ctx->cm_id, &event);
1251 }
1252
1253 static int ucma_set_option_ib(struct ucma_context *ctx, int optname,
1254                               void *optval, size_t optlen)
1255 {
1256         int ret;
1257
1258         switch (optname) {
1259         case RDMA_OPTION_IB_PATH:
1260                 ret = ucma_set_ib_path(ctx, optval, optlen);
1261                 break;
1262         default:
1263                 ret = -ENOSYS;
1264         }
1265
1266         return ret;
1267 }
1268
1269 static int ucma_set_option_level(struct ucma_context *ctx, int level,
1270                                  int optname, void *optval, size_t optlen)
1271 {
1272         int ret;
1273
1274         switch (level) {
1275         case RDMA_OPTION_ID:
1276                 ret = ucma_set_option_id(ctx, optname, optval, optlen);
1277                 break;
1278         case RDMA_OPTION_IB:
1279                 ret = ucma_set_option_ib(ctx, optname, optval, optlen);
1280                 break;
1281         default:
1282                 ret = -ENOSYS;
1283         }
1284
1285         return ret;
1286 }
1287
1288 static ssize_t ucma_set_option(struct ucma_file *file, const char __user *inbuf,
1289                                int in_len, int out_len)
1290 {
1291         struct rdma_ucm_set_option cmd;
1292         struct ucma_context *ctx;
1293         void *optval;
1294         int ret;
1295
1296         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1297                 return -EFAULT;
1298
1299         if (unlikely(cmd.optlen > KMALLOC_MAX_SIZE))
1300                 return -EINVAL;
1301
1302         ctx = ucma_get_ctx(file, cmd.id);
1303         if (IS_ERR(ctx))
1304                 return PTR_ERR(ctx);
1305
1306         optval = memdup_user((void __user *) (unsigned long) cmd.optval,
1307                              cmd.optlen);
1308         if (IS_ERR(optval)) {
1309                 ret = PTR_ERR(optval);
1310                 goto out;
1311         }
1312
1313         ret = ucma_set_option_level(ctx, cmd.level, cmd.optname, optval,
1314                                     cmd.optlen);
1315         kfree(optval);
1316
1317 out:
1318         ucma_put_ctx(ctx);
1319         return ret;
1320 }
1321
1322 static ssize_t ucma_notify(struct ucma_file *file, const char __user *inbuf,
1323                            int in_len, int out_len)
1324 {
1325         struct rdma_ucm_notify cmd;
1326         struct ucma_context *ctx;
1327         int ret = -EINVAL;
1328
1329         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1330                 return -EFAULT;
1331
1332         ctx = ucma_get_ctx(file, cmd.id);
1333         if (IS_ERR(ctx))
1334                 return PTR_ERR(ctx);
1335
1336         if (ctx->cm_id->device)
1337                 ret = rdma_notify(ctx->cm_id, (enum ib_event_type)cmd.event);
1338
1339         ucma_put_ctx(ctx);
1340         return ret;
1341 }
1342
1343 static ssize_t ucma_process_join(struct ucma_file *file,
1344                                  struct rdma_ucm_join_mcast *cmd,  int out_len)
1345 {
1346         struct rdma_ucm_create_id_resp resp;
1347         struct ucma_context *ctx;
1348         struct ucma_multicast *mc;
1349         struct sockaddr *addr;
1350         int ret;
1351         u8 join_state;
1352
1353         if (out_len < sizeof(resp))
1354                 return -ENOSPC;
1355
1356         addr = (struct sockaddr *) &cmd->addr;
1357         if (cmd->addr_size != rdma_addr_size(addr))
1358                 return -EINVAL;
1359
1360         if (cmd->join_flags == RDMA_MC_JOIN_FLAG_FULLMEMBER)
1361                 join_state = BIT(FULLMEMBER_JOIN);
1362         else if (cmd->join_flags == RDMA_MC_JOIN_FLAG_SENDONLY_FULLMEMBER)
1363                 join_state = BIT(SENDONLY_FULLMEMBER_JOIN);
1364         else
1365                 return -EINVAL;
1366
1367         ctx = ucma_get_ctx(file, cmd->id);
1368         if (IS_ERR(ctx))
1369                 return PTR_ERR(ctx);
1370
1371         mutex_lock(&file->mut);
1372         mc = ucma_alloc_multicast(ctx);
1373         if (!mc) {
1374                 ret = -ENOMEM;
1375                 goto err1;
1376         }
1377         mc->join_state = join_state;
1378         mc->uid = cmd->uid;
1379         memcpy(&mc->addr, addr, cmd->addr_size);
1380         ret = rdma_join_multicast(ctx->cm_id, (struct sockaddr *)&mc->addr,
1381                                   join_state, mc);
1382         if (ret)
1383                 goto err2;
1384
1385         resp.id = mc->id;
1386         if (copy_to_user((void __user *)(unsigned long) cmd->response,
1387                          &resp, sizeof(resp))) {
1388                 ret = -EFAULT;
1389                 goto err3;
1390         }
1391
1392         mutex_lock(&mut);
1393         idr_replace(&multicast_idr, mc, mc->id);
1394         mutex_unlock(&mut);
1395
1396         mutex_unlock(&file->mut);
1397         ucma_put_ctx(ctx);
1398         return 0;
1399
1400 err3:
1401         rdma_leave_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr);
1402         ucma_cleanup_mc_events(mc);
1403 err2:
1404         mutex_lock(&mut);
1405         idr_remove(&multicast_idr, mc->id);
1406         mutex_unlock(&mut);
1407         list_del(&mc->list);
1408         kfree(mc);
1409 err1:
1410         mutex_unlock(&file->mut);
1411         ucma_put_ctx(ctx);
1412         return ret;
1413 }
1414
1415 static ssize_t ucma_join_ip_multicast(struct ucma_file *file,
1416                                       const char __user *inbuf,
1417                                       int in_len, int out_len)
1418 {
1419         struct rdma_ucm_join_ip_mcast cmd;
1420         struct rdma_ucm_join_mcast join_cmd;
1421
1422         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1423                 return -EFAULT;
1424
1425         join_cmd.response = cmd.response;
1426         join_cmd.uid = cmd.uid;
1427         join_cmd.id = cmd.id;
1428         join_cmd.addr_size = rdma_addr_size_in6(&cmd.addr);
1429         if (!join_cmd.addr_size)
1430                 return -EINVAL;
1431
1432         join_cmd.join_flags = RDMA_MC_JOIN_FLAG_FULLMEMBER;
1433         memcpy(&join_cmd.addr, &cmd.addr, join_cmd.addr_size);
1434
1435         return ucma_process_join(file, &join_cmd, out_len);
1436 }
1437
1438 static ssize_t ucma_join_multicast(struct ucma_file *file,
1439                                    const char __user *inbuf,
1440                                    int in_len, int out_len)
1441 {
1442         struct rdma_ucm_join_mcast cmd;
1443
1444         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1445                 return -EFAULT;
1446
1447         if (!rdma_addr_size_kss(&cmd.addr))
1448                 return -EINVAL;
1449
1450         return ucma_process_join(file, &cmd, out_len);
1451 }
1452
1453 static ssize_t ucma_leave_multicast(struct ucma_file *file,
1454                                     const char __user *inbuf,
1455                                     int in_len, int out_len)
1456 {
1457         struct rdma_ucm_destroy_id cmd;
1458         struct rdma_ucm_destroy_id_resp resp;
1459         struct ucma_multicast *mc;
1460         int ret = 0;
1461
1462         if (out_len < sizeof(resp))
1463                 return -ENOSPC;
1464
1465         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1466                 return -EFAULT;
1467
1468         mutex_lock(&mut);
1469         mc = idr_find(&multicast_idr, cmd.id);
1470         if (!mc)
1471                 mc = ERR_PTR(-ENOENT);
1472         else if (mc->ctx->file != file)
1473                 mc = ERR_PTR(-EINVAL);
1474         else if (!atomic_inc_not_zero(&mc->ctx->ref))
1475                 mc = ERR_PTR(-ENXIO);
1476         else
1477                 idr_remove(&multicast_idr, mc->id);
1478         mutex_unlock(&mut);
1479
1480         if (IS_ERR(mc)) {
1481                 ret = PTR_ERR(mc);
1482                 goto out;
1483         }
1484
1485         rdma_leave_multicast(mc->ctx->cm_id, (struct sockaddr *) &mc->addr);
1486         mutex_lock(&mc->ctx->file->mut);
1487         ucma_cleanup_mc_events(mc);
1488         list_del(&mc->list);
1489         mutex_unlock(&mc->ctx->file->mut);
1490
1491         ucma_put_ctx(mc->ctx);
1492         resp.events_reported = mc->events_reported;
1493         kfree(mc);
1494
1495         if (copy_to_user((void __user *)(unsigned long)cmd.response,
1496                          &resp, sizeof(resp)))
1497                 ret = -EFAULT;
1498 out:
1499         return ret;
1500 }
1501
1502 static void ucma_lock_files(struct ucma_file *file1, struct ucma_file *file2)
1503 {
1504         /* Acquire mutex's based on pointer comparison to prevent deadlock. */
1505         if (file1 < file2) {
1506                 mutex_lock(&file1->mut);
1507                 mutex_lock_nested(&file2->mut, SINGLE_DEPTH_NESTING);
1508         } else {
1509                 mutex_lock(&file2->mut);
1510                 mutex_lock_nested(&file1->mut, SINGLE_DEPTH_NESTING);
1511         }
1512 }
1513
1514 static void ucma_unlock_files(struct ucma_file *file1, struct ucma_file *file2)
1515 {
1516         if (file1 < file2) {
1517                 mutex_unlock(&file2->mut);
1518                 mutex_unlock(&file1->mut);
1519         } else {
1520                 mutex_unlock(&file1->mut);
1521                 mutex_unlock(&file2->mut);
1522         }
1523 }
1524
1525 static void ucma_move_events(struct ucma_context *ctx, struct ucma_file *file)
1526 {
1527         struct ucma_event *uevent, *tmp;
1528
1529         list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list)
1530                 if (uevent->ctx == ctx)
1531                         list_move_tail(&uevent->list, &file->event_list);
1532 }
1533
1534 static ssize_t ucma_migrate_id(struct ucma_file *new_file,
1535                                const char __user *inbuf,
1536                                int in_len, int out_len)
1537 {
1538         struct rdma_ucm_migrate_id cmd;
1539         struct rdma_ucm_migrate_resp resp;
1540         struct ucma_context *ctx;
1541         struct fd f;
1542         struct ucma_file *cur_file;
1543         int ret = 0;
1544
1545         if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1546                 return -EFAULT;
1547
1548         /* Get current fd to protect against it being closed */
1549         f = fdget(cmd.fd);
1550         if (!f.file)
1551                 return -ENOENT;
1552         if (f.file->f_op != &ucma_fops) {
1553                 ret = -EINVAL;
1554                 goto file_put;
1555         }
1556
1557         /* Validate current fd and prevent destruction of id. */
1558         ctx = ucma_get_ctx(f.file->private_data, cmd.id);
1559         if (IS_ERR(ctx)) {
1560                 ret = PTR_ERR(ctx);
1561                 goto file_put;
1562         }
1563
1564         cur_file = ctx->file;
1565         if (cur_file == new_file) {
1566                 resp.events_reported = ctx->events_reported;
1567                 goto response;
1568         }
1569
1570         /*
1571          * Migrate events between fd's, maintaining order, and avoiding new
1572          * events being added before existing events.
1573          */
1574         ucma_lock_files(cur_file, new_file);
1575         mutex_lock(&mut);
1576
1577         list_move_tail(&ctx->list, &new_file->ctx_list);
1578         ucma_move_events(ctx, new_file);
1579         ctx->file = new_file;
1580         resp.events_reported = ctx->events_reported;
1581
1582         mutex_unlock(&mut);
1583         ucma_unlock_files(cur_file, new_file);
1584
1585 response:
1586         if (copy_to_user((void __user *)(unsigned long)cmd.response,
1587                          &resp, sizeof(resp)))
1588                 ret = -EFAULT;
1589
1590         ucma_put_ctx(ctx);
1591 file_put:
1592         fdput(f);
1593         return ret;
1594 }
1595
1596 static ssize_t (*ucma_cmd_table[])(struct ucma_file *file,
1597                                    const char __user *inbuf,
1598                                    int in_len, int out_len) = {
1599         [RDMA_USER_CM_CMD_CREATE_ID]     = ucma_create_id,
1600         [RDMA_USER_CM_CMD_DESTROY_ID]    = ucma_destroy_id,
1601         [RDMA_USER_CM_CMD_BIND_IP]       = ucma_bind_ip,
1602         [RDMA_USER_CM_CMD_RESOLVE_IP]    = ucma_resolve_ip,
1603         [RDMA_USER_CM_CMD_RESOLVE_ROUTE] = ucma_resolve_route,
1604         [RDMA_USER_CM_CMD_QUERY_ROUTE]   = ucma_query_route,
1605         [RDMA_USER_CM_CMD_CONNECT]       = ucma_connect,
1606         [RDMA_USER_CM_CMD_LISTEN]        = ucma_listen,
1607         [RDMA_USER_CM_CMD_ACCEPT]        = ucma_accept,
1608         [RDMA_USER_CM_CMD_REJECT]        = ucma_reject,
1609         [RDMA_USER_CM_CMD_DISCONNECT]    = ucma_disconnect,
1610         [RDMA_USER_CM_CMD_INIT_QP_ATTR]  = ucma_init_qp_attr,
1611         [RDMA_USER_CM_CMD_GET_EVENT]     = ucma_get_event,
1612         [RDMA_USER_CM_CMD_GET_OPTION]    = NULL,
1613         [RDMA_USER_CM_CMD_SET_OPTION]    = ucma_set_option,
1614         [RDMA_USER_CM_CMD_NOTIFY]        = ucma_notify,
1615         [RDMA_USER_CM_CMD_JOIN_IP_MCAST] = ucma_join_ip_multicast,
1616         [RDMA_USER_CM_CMD_LEAVE_MCAST]   = ucma_leave_multicast,
1617         [RDMA_USER_CM_CMD_MIGRATE_ID]    = ucma_migrate_id,
1618         [RDMA_USER_CM_CMD_QUERY]         = ucma_query,
1619         [RDMA_USER_CM_CMD_BIND]          = ucma_bind,
1620         [RDMA_USER_CM_CMD_RESOLVE_ADDR]  = ucma_resolve_addr,
1621         [RDMA_USER_CM_CMD_JOIN_MCAST]    = ucma_join_multicast
1622 };
1623
1624 static ssize_t ucma_write(struct file *filp, const char __user *buf,
1625                           size_t len, loff_t *pos)
1626 {
1627         struct ucma_file *file = filp->private_data;
1628         struct rdma_ucm_cmd_hdr hdr;
1629         ssize_t ret;
1630
1631         if (WARN_ON_ONCE(!ib_safe_file_access(filp)))
1632                 return -EACCES;
1633
1634         if (len < sizeof(hdr))
1635                 return -EINVAL;
1636
1637         if (copy_from_user(&hdr, buf, sizeof(hdr)))
1638                 return -EFAULT;
1639
1640         if (hdr.cmd >= ARRAY_SIZE(ucma_cmd_table))
1641                 return -EINVAL;
1642         hdr.cmd = array_index_nospec(hdr.cmd, ARRAY_SIZE(ucma_cmd_table));
1643
1644         if (hdr.in + sizeof(hdr) > len)
1645                 return -EINVAL;
1646
1647         if (!ucma_cmd_table[hdr.cmd])
1648                 return -ENOSYS;
1649
1650         ret = ucma_cmd_table[hdr.cmd](file, buf + sizeof(hdr), hdr.in, hdr.out);
1651         if (!ret)
1652                 ret = len;
1653
1654         return ret;
1655 }
1656
1657 static unsigned int ucma_poll(struct file *filp, struct poll_table_struct *wait)
1658 {
1659         struct ucma_file *file = filp->private_data;
1660         unsigned int mask = 0;
1661
1662         poll_wait(filp, &file->poll_wait, wait);
1663
1664         if (!list_empty(&file->event_list))
1665                 mask = POLLIN | POLLRDNORM;
1666
1667         return mask;
1668 }
1669
1670 /*
1671  * ucma_open() does not need the BKL:
1672  *
1673  *  - no global state is referred to;
1674  *  - there is no ioctl method to race against;
1675  *  - no further module initialization is required for open to work
1676  *    after the device is registered.
1677  */
1678 static int ucma_open(struct inode *inode, struct file *filp)
1679 {
1680         struct ucma_file *file;
1681
1682         file = kmalloc(sizeof *file, GFP_KERNEL);
1683         if (!file)
1684                 return -ENOMEM;
1685
1686         file->close_wq = alloc_ordered_workqueue("ucma_close_id",
1687                                                  WQ_MEM_RECLAIM);
1688         if (!file->close_wq) {
1689                 kfree(file);
1690                 return -ENOMEM;
1691         }
1692
1693         INIT_LIST_HEAD(&file->event_list);
1694         INIT_LIST_HEAD(&file->ctx_list);
1695         init_waitqueue_head(&file->poll_wait);
1696         mutex_init(&file->mut);
1697
1698         filp->private_data = file;
1699         file->filp = filp;
1700
1701         return nonseekable_open(inode, filp);
1702 }
1703
1704 static int ucma_close(struct inode *inode, struct file *filp)
1705 {
1706         struct ucma_file *file = filp->private_data;
1707         struct ucma_context *ctx, *tmp;
1708
1709         mutex_lock(&file->mut);
1710         list_for_each_entry_safe(ctx, tmp, &file->ctx_list, list) {
1711                 ctx->destroying = 1;
1712                 mutex_unlock(&file->mut);
1713
1714                 mutex_lock(&mut);
1715                 idr_remove(&ctx_idr, ctx->id);
1716                 mutex_unlock(&mut);
1717
1718                 flush_workqueue(file->close_wq);
1719                 /* At that step once ctx was marked as destroying and workqueue
1720                  * was flushed we are safe from any inflights handlers that
1721                  * might put other closing task.
1722                  */
1723                 mutex_lock(&mut);
1724                 if (!ctx->closing) {
1725                         mutex_unlock(&mut);
1726                         ucma_put_ctx(ctx);
1727                         wait_for_completion(&ctx->comp);
1728                         /* rdma_destroy_id ensures that no event handlers are
1729                          * inflight for that id before releasing it.
1730                          */
1731                         rdma_destroy_id(ctx->cm_id);
1732                 } else {
1733                         mutex_unlock(&mut);
1734                 }
1735
1736                 ucma_free_ctx(ctx);
1737                 mutex_lock(&file->mut);
1738         }
1739         mutex_unlock(&file->mut);
1740         destroy_workqueue(file->close_wq);
1741         kfree(file);
1742         return 0;
1743 }
1744
1745 static const struct file_operations ucma_fops = {
1746         .owner   = THIS_MODULE,
1747         .open    = ucma_open,
1748         .release = ucma_close,
1749         .write   = ucma_write,
1750         .poll    = ucma_poll,
1751         .llseek  = no_llseek,
1752 };
1753
1754 static struct miscdevice ucma_misc = {
1755         .minor          = MISC_DYNAMIC_MINOR,
1756         .name           = "rdma_cm",
1757         .nodename       = "infiniband/rdma_cm",
1758         .mode           = 0666,
1759         .fops           = &ucma_fops,
1760 };
1761
1762 static ssize_t show_abi_version(struct device *dev,
1763                                 struct device_attribute *attr,
1764                                 char *buf)
1765 {
1766         return sprintf(buf, "%d\n", RDMA_USER_CM_ABI_VERSION);
1767 }
1768 static DEVICE_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
1769
1770 static int __init ucma_init(void)
1771 {
1772         int ret;
1773
1774         ret = misc_register(&ucma_misc);
1775         if (ret)
1776                 return ret;
1777
1778         ret = device_create_file(ucma_misc.this_device, &dev_attr_abi_version);
1779         if (ret) {
1780                 pr_err("rdma_ucm: couldn't create abi_version attr\n");
1781                 goto err1;
1782         }
1783
1784         ucma_ctl_table_hdr = register_net_sysctl(&init_net, "net/rdma_ucm", ucma_ctl_table);
1785         if (!ucma_ctl_table_hdr) {
1786                 pr_err("rdma_ucm: couldn't register sysctl paths\n");
1787                 ret = -ENOMEM;
1788                 goto err2;
1789         }
1790         return 0;
1791 err2:
1792         device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
1793 err1:
1794         misc_deregister(&ucma_misc);
1795         return ret;
1796 }
1797
1798 static void __exit ucma_cleanup(void)
1799 {
1800         unregister_net_sysctl_table(ucma_ctl_table_hdr);
1801         device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
1802         misc_deregister(&ucma_misc);
1803         idr_destroy(&ctx_idr);
1804         idr_destroy(&multicast_idr);
1805 }
1806
1807 module_init(ucma_init);
1808 module_exit(ucma_cleanup);