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