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