Commit | Line | Data |
---|---|---|
75216638 SH |
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> | |
88314e4d | 34 | #include <linux/file.h> |
75216638 SH |
35 | #include <linux/mutex.h> |
36 | #include <linux/poll.h> | |
d43c36dc | 37 | #include <linux/sched.h> |
75216638 SH |
38 | #include <linux/idr.h> |
39 | #include <linux/in.h> | |
40 | #include <linux/in6.h> | |
41 | #include <linux/miscdevice.h> | |
5a0e3ad6 | 42 | #include <linux/slab.h> |
97cb7e40 | 43 | #include <linux/sysctl.h> |
e4dd23d7 | 44 | #include <linux/module.h> |
75216638 SH |
45 | |
46 | #include <rdma/rdma_user_cm.h> | |
47 | #include <rdma/ib_marshall.h> | |
48 | #include <rdma/rdma_cm.h> | |
a7ca1f00 | 49 | #include <rdma/rdma_cm_ib.h> |
ee7aed45 | 50 | #include <rdma/ib_addr.h> |
75216638 SH |
51 | |
52 | MODULE_AUTHOR("Sean Hefty"); | |
53 | MODULE_DESCRIPTION("RDMA Userspace Connection Manager Access"); | |
54 | MODULE_LICENSE("Dual BSD/GPL"); | |
55 | ||
97cb7e40 SW |
56 | static unsigned int max_backlog = 1024; |
57 | ||
58 | static struct ctl_table_header *ucma_ctl_table_hdr; | |
59 | static ctl_table ucma_ctl_table[] = { | |
60 | { | |
61 | .procname = "max_backlog", | |
62 | .data = &max_backlog, | |
63 | .maxlen = sizeof max_backlog, | |
64 | .mode = 0644, | |
65 | .proc_handler = proc_dointvec, | |
66 | }, | |
67 | { } | |
68 | }; | |
69 | ||
75216638 SH |
70 | struct ucma_file { |
71 | struct mutex mut; | |
72 | struct file *filp; | |
73 | struct list_head ctx_list; | |
74 | struct list_head event_list; | |
75 | wait_queue_head_t poll_wait; | |
76 | }; | |
77 | ||
78 | struct ucma_context { | |
79 | int id; | |
80 | struct completion comp; | |
81 | atomic_t ref; | |
82 | int events_reported; | |
83 | int backlog; | |
84 | ||
85 | struct ucma_file *file; | |
86 | struct rdma_cm_id *cm_id; | |
87 | u64 uid; | |
88 | ||
89 | struct list_head list; | |
c8f6a362 SH |
90 | struct list_head mc_list; |
91 | }; | |
92 | ||
93 | struct ucma_multicast { | |
94 | struct ucma_context *ctx; | |
95 | int id; | |
96 | int events_reported; | |
97 | ||
98 | u64 uid; | |
99 | struct list_head list; | |
3f446754 | 100 | struct sockaddr_storage addr; |
75216638 SH |
101 | }; |
102 | ||
103 | struct ucma_event { | |
104 | struct ucma_context *ctx; | |
c8f6a362 | 105 | struct ucma_multicast *mc; |
75216638 SH |
106 | struct list_head list; |
107 | struct rdma_cm_id *cm_id; | |
108 | struct rdma_ucm_event_resp resp; | |
109 | }; | |
110 | ||
111 | static DEFINE_MUTEX(mut); | |
112 | static DEFINE_IDR(ctx_idr); | |
c8f6a362 | 113 | static DEFINE_IDR(multicast_idr); |
75216638 SH |
114 | |
115 | static inline struct ucma_context *_ucma_find_context(int id, | |
116 | struct ucma_file *file) | |
117 | { | |
118 | struct ucma_context *ctx; | |
119 | ||
120 | ctx = idr_find(&ctx_idr, id); | |
121 | if (!ctx) | |
122 | ctx = ERR_PTR(-ENOENT); | |
123 | else if (ctx->file != file) | |
124 | ctx = ERR_PTR(-EINVAL); | |
125 | return ctx; | |
126 | } | |
127 | ||
128 | static struct ucma_context *ucma_get_ctx(struct ucma_file *file, int id) | |
129 | { | |
130 | struct ucma_context *ctx; | |
131 | ||
132 | mutex_lock(&mut); | |
133 | ctx = _ucma_find_context(id, file); | |
134 | if (!IS_ERR(ctx)) | |
135 | atomic_inc(&ctx->ref); | |
136 | mutex_unlock(&mut); | |
137 | return ctx; | |
138 | } | |
139 | ||
140 | static void ucma_put_ctx(struct ucma_context *ctx) | |
141 | { | |
142 | if (atomic_dec_and_test(&ctx->ref)) | |
143 | complete(&ctx->comp); | |
144 | } | |
145 | ||
146 | static struct ucma_context *ucma_alloc_ctx(struct ucma_file *file) | |
147 | { | |
148 | struct ucma_context *ctx; | |
75216638 SH |
149 | |
150 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
151 | if (!ctx) | |
152 | return NULL; | |
153 | ||
154 | atomic_set(&ctx->ref, 1); | |
155 | init_completion(&ctx->comp); | |
c8f6a362 | 156 | INIT_LIST_HEAD(&ctx->mc_list); |
75216638 SH |
157 | ctx->file = file; |
158 | ||
3b069c5d TH |
159 | mutex_lock(&mut); |
160 | ctx->id = idr_alloc(&ctx_idr, ctx, 0, 0, GFP_KERNEL); | |
161 | mutex_unlock(&mut); | |
162 | if (ctx->id < 0) | |
75216638 SH |
163 | goto error; |
164 | ||
165 | list_add_tail(&ctx->list, &file->ctx_list); | |
166 | return ctx; | |
167 | ||
168 | error: | |
169 | kfree(ctx); | |
170 | return NULL; | |
171 | } | |
172 | ||
c8f6a362 SH |
173 | static struct ucma_multicast* ucma_alloc_multicast(struct ucma_context *ctx) |
174 | { | |
175 | struct ucma_multicast *mc; | |
c8f6a362 SH |
176 | |
177 | mc = kzalloc(sizeof(*mc), GFP_KERNEL); | |
178 | if (!mc) | |
179 | return NULL; | |
180 | ||
3b069c5d TH |
181 | mutex_lock(&mut); |
182 | mc->id = idr_alloc(&multicast_idr, mc, 0, 0, GFP_KERNEL); | |
183 | mutex_unlock(&mut); | |
184 | if (mc->id < 0) | |
c8f6a362 SH |
185 | goto error; |
186 | ||
187 | mc->ctx = ctx; | |
188 | list_add_tail(&mc->list, &ctx->mc_list); | |
189 | return mc; | |
190 | ||
191 | error: | |
192 | kfree(mc); | |
193 | return NULL; | |
194 | } | |
195 | ||
75216638 SH |
196 | static void ucma_copy_conn_event(struct rdma_ucm_conn_param *dst, |
197 | struct rdma_conn_param *src) | |
198 | { | |
199 | if (src->private_data_len) | |
200 | memcpy(dst->private_data, src->private_data, | |
201 | src->private_data_len); | |
202 | dst->private_data_len = src->private_data_len; | |
203 | dst->responder_resources =src->responder_resources; | |
204 | dst->initiator_depth = src->initiator_depth; | |
205 | dst->flow_control = src->flow_control; | |
206 | dst->retry_count = src->retry_count; | |
207 | dst->rnr_retry_count = src->rnr_retry_count; | |
208 | dst->srq = src->srq; | |
209 | dst->qp_num = src->qp_num; | |
210 | } | |
211 | ||
212 | static void ucma_copy_ud_event(struct rdma_ucm_ud_param *dst, | |
213 | struct rdma_ud_param *src) | |
214 | { | |
215 | if (src->private_data_len) | |
216 | memcpy(dst->private_data, src->private_data, | |
217 | src->private_data_len); | |
218 | dst->private_data_len = src->private_data_len; | |
219 | ib_copy_ah_attr_to_user(&dst->ah_attr, &src->ah_attr); | |
220 | dst->qp_num = src->qp_num; | |
221 | dst->qkey = src->qkey; | |
222 | } | |
223 | ||
224 | static void ucma_set_event_context(struct ucma_context *ctx, | |
225 | struct rdma_cm_event *event, | |
226 | struct ucma_event *uevent) | |
227 | { | |
228 | uevent->ctx = ctx; | |
c8f6a362 SH |
229 | switch (event->event) { |
230 | case RDMA_CM_EVENT_MULTICAST_JOIN: | |
231 | case RDMA_CM_EVENT_MULTICAST_ERROR: | |
232 | uevent->mc = (struct ucma_multicast *) | |
233 | event->param.ud.private_data; | |
234 | uevent->resp.uid = uevent->mc->uid; | |
235 | uevent->resp.id = uevent->mc->id; | |
236 | break; | |
237 | default: | |
238 | uevent->resp.uid = ctx->uid; | |
239 | uevent->resp.id = ctx->id; | |
240 | break; | |
241 | } | |
75216638 SH |
242 | } |
243 | ||
244 | static int ucma_event_handler(struct rdma_cm_id *cm_id, | |
245 | struct rdma_cm_event *event) | |
246 | { | |
247 | struct ucma_event *uevent; | |
248 | struct ucma_context *ctx = cm_id->context; | |
249 | int ret = 0; | |
250 | ||
251 | uevent = kzalloc(sizeof(*uevent), GFP_KERNEL); | |
252 | if (!uevent) | |
253 | return event->event == RDMA_CM_EVENT_CONNECT_REQUEST; | |
254 | ||
418edaab | 255 | mutex_lock(&ctx->file->mut); |
75216638 SH |
256 | uevent->cm_id = cm_id; |
257 | ucma_set_event_context(ctx, event, uevent); | |
258 | uevent->resp.event = event->event; | |
259 | uevent->resp.status = event->status; | |
638ef7a6 | 260 | if (cm_id->qp_type == IB_QPT_UD) |
75216638 SH |
261 | ucma_copy_ud_event(&uevent->resp.param.ud, &event->param.ud); |
262 | else | |
263 | ucma_copy_conn_event(&uevent->resp.param.conn, | |
264 | &event->param.conn); | |
265 | ||
75216638 SH |
266 | if (event->event == RDMA_CM_EVENT_CONNECT_REQUEST) { |
267 | if (!ctx->backlog) { | |
3492856e | 268 | ret = -ENOMEM; |
30a5ec98 | 269 | kfree(uevent); |
75216638 SH |
270 | goto out; |
271 | } | |
272 | ctx->backlog--; | |
0cefcf0b SH |
273 | } else if (!ctx->uid) { |
274 | /* | |
275 | * We ignore events for new connections until userspace has set | |
276 | * their context. This can only happen if an error occurs on a | |
277 | * new connection before the user accepts it. This is okay, | |
278 | * since the accept will just fail later. | |
279 | */ | |
280 | kfree(uevent); | |
281 | goto out; | |
75216638 | 282 | } |
0cefcf0b | 283 | |
75216638 SH |
284 | list_add_tail(&uevent->list, &ctx->file->event_list); |
285 | wake_up_interruptible(&ctx->file->poll_wait); | |
286 | out: | |
287 | mutex_unlock(&ctx->file->mut); | |
288 | return ret; | |
289 | } | |
290 | ||
291 | static ssize_t ucma_get_event(struct ucma_file *file, const char __user *inbuf, | |
292 | int in_len, int out_len) | |
293 | { | |
294 | struct ucma_context *ctx; | |
295 | struct rdma_ucm_get_event cmd; | |
296 | struct ucma_event *uevent; | |
297 | int ret = 0; | |
75216638 SH |
298 | |
299 | if (out_len < sizeof uevent->resp) | |
300 | return -ENOSPC; | |
301 | ||
302 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
303 | return -EFAULT; | |
304 | ||
305 | mutex_lock(&file->mut); | |
306 | while (list_empty(&file->event_list)) { | |
d92f7644 | 307 | mutex_unlock(&file->mut); |
75216638 | 308 | |
d92f7644 SH |
309 | if (file->filp->f_flags & O_NONBLOCK) |
310 | return -EAGAIN; | |
311 | ||
312 | if (wait_event_interruptible(file->poll_wait, | |
313 | !list_empty(&file->event_list))) | |
314 | return -ERESTARTSYS; | |
75216638 | 315 | |
75216638 | 316 | mutex_lock(&file->mut); |
75216638 SH |
317 | } |
318 | ||
75216638 SH |
319 | uevent = list_entry(file->event_list.next, struct ucma_event, list); |
320 | ||
321 | if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST) { | |
322 | ctx = ucma_alloc_ctx(file); | |
323 | if (!ctx) { | |
324 | ret = -ENOMEM; | |
325 | goto done; | |
326 | } | |
327 | uevent->ctx->backlog++; | |
328 | ctx->cm_id = uevent->cm_id; | |
329 | ctx->cm_id->context = ctx; | |
330 | uevent->resp.id = ctx->id; | |
331 | } | |
332 | ||
333 | if (copy_to_user((void __user *)(unsigned long)cmd.response, | |
334 | &uevent->resp, sizeof uevent->resp)) { | |
335 | ret = -EFAULT; | |
336 | goto done; | |
337 | } | |
338 | ||
339 | list_del(&uevent->list); | |
340 | uevent->ctx->events_reported++; | |
c8f6a362 SH |
341 | if (uevent->mc) |
342 | uevent->mc->events_reported++; | |
75216638 SH |
343 | kfree(uevent); |
344 | done: | |
345 | mutex_unlock(&file->mut); | |
346 | return ret; | |
347 | } | |
348 | ||
b26f9b99 SH |
349 | static int ucma_get_qp_type(struct rdma_ucm_create_id *cmd, enum ib_qp_type *qp_type) |
350 | { | |
351 | switch (cmd->ps) { | |
352 | case RDMA_PS_TCP: | |
353 | *qp_type = IB_QPT_RC; | |
354 | return 0; | |
355 | case RDMA_PS_UDP: | |
356 | case RDMA_PS_IPOIB: | |
357 | *qp_type = IB_QPT_UD; | |
358 | return 0; | |
638ef7a6 SH |
359 | case RDMA_PS_IB: |
360 | *qp_type = cmd->qp_type; | |
361 | return 0; | |
b26f9b99 SH |
362 | default: |
363 | return -EINVAL; | |
364 | } | |
365 | } | |
366 | ||
367 | static ssize_t ucma_create_id(struct ucma_file *file, const char __user *inbuf, | |
368 | int in_len, int out_len) | |
75216638 SH |
369 | { |
370 | struct rdma_ucm_create_id cmd; | |
371 | struct rdma_ucm_create_id_resp resp; | |
372 | struct ucma_context *ctx; | |
b26f9b99 | 373 | enum ib_qp_type qp_type; |
75216638 SH |
374 | int ret; |
375 | ||
376 | if (out_len < sizeof(resp)) | |
377 | return -ENOSPC; | |
378 | ||
379 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
380 | return -EFAULT; | |
381 | ||
b26f9b99 SH |
382 | ret = ucma_get_qp_type(&cmd, &qp_type); |
383 | if (ret) | |
384 | return ret; | |
385 | ||
75216638 SH |
386 | mutex_lock(&file->mut); |
387 | ctx = ucma_alloc_ctx(file); | |
388 | mutex_unlock(&file->mut); | |
389 | if (!ctx) | |
390 | return -ENOMEM; | |
391 | ||
392 | ctx->uid = cmd.uid; | |
b26f9b99 | 393 | ctx->cm_id = rdma_create_id(ucma_event_handler, ctx, cmd.ps, qp_type); |
75216638 SH |
394 | if (IS_ERR(ctx->cm_id)) { |
395 | ret = PTR_ERR(ctx->cm_id); | |
396 | goto err1; | |
397 | } | |
398 | ||
399 | resp.id = ctx->id; | |
400 | if (copy_to_user((void __user *)(unsigned long)cmd.response, | |
401 | &resp, sizeof(resp))) { | |
402 | ret = -EFAULT; | |
403 | goto err2; | |
404 | } | |
405 | return 0; | |
406 | ||
407 | err2: | |
408 | rdma_destroy_id(ctx->cm_id); | |
409 | err1: | |
410 | mutex_lock(&mut); | |
411 | idr_remove(&ctx_idr, ctx->id); | |
412 | mutex_unlock(&mut); | |
413 | kfree(ctx); | |
414 | return ret; | |
415 | } | |
416 | ||
c8f6a362 SH |
417 | static void ucma_cleanup_multicast(struct ucma_context *ctx) |
418 | { | |
419 | struct ucma_multicast *mc, *tmp; | |
420 | ||
421 | mutex_lock(&mut); | |
422 | list_for_each_entry_safe(mc, tmp, &ctx->mc_list, list) { | |
423 | list_del(&mc->list); | |
424 | idr_remove(&multicast_idr, mc->id); | |
425 | kfree(mc); | |
426 | } | |
427 | mutex_unlock(&mut); | |
428 | } | |
429 | ||
c8f6a362 SH |
430 | static void ucma_cleanup_mc_events(struct ucma_multicast *mc) |
431 | { | |
432 | struct ucma_event *uevent, *tmp; | |
433 | ||
434 | list_for_each_entry_safe(uevent, tmp, &mc->ctx->file->event_list, list) { | |
435 | if (uevent->mc != mc) | |
436 | continue; | |
437 | ||
438 | list_del(&uevent->list); | |
439 | kfree(uevent); | |
440 | } | |
441 | } | |
442 | ||
186834b5 HS |
443 | /* |
444 | * We cannot hold file->mut when calling rdma_destroy_id() or we can | |
445 | * deadlock. We also acquire file->mut in ucma_event_handler(), and | |
446 | * rdma_destroy_id() will wait until all callbacks have completed. | |
447 | */ | |
75216638 SH |
448 | static int ucma_free_ctx(struct ucma_context *ctx) |
449 | { | |
450 | int events_reported; | |
186834b5 HS |
451 | struct ucma_event *uevent, *tmp; |
452 | LIST_HEAD(list); | |
75216638 SH |
453 | |
454 | /* No new events will be generated after destroying the id. */ | |
455 | rdma_destroy_id(ctx->cm_id); | |
456 | ||
c8f6a362 SH |
457 | ucma_cleanup_multicast(ctx); |
458 | ||
75216638 SH |
459 | /* Cleanup events not yet reported to the user. */ |
460 | mutex_lock(&ctx->file->mut); | |
186834b5 HS |
461 | list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list) { |
462 | if (uevent->ctx == ctx) | |
463 | list_move_tail(&uevent->list, &list); | |
464 | } | |
75216638 SH |
465 | list_del(&ctx->list); |
466 | mutex_unlock(&ctx->file->mut); | |
467 | ||
186834b5 HS |
468 | list_for_each_entry_safe(uevent, tmp, &list, list) { |
469 | list_del(&uevent->list); | |
470 | if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST) | |
471 | rdma_destroy_id(uevent->cm_id); | |
472 | kfree(uevent); | |
473 | } | |
474 | ||
75216638 SH |
475 | events_reported = ctx->events_reported; |
476 | kfree(ctx); | |
477 | return events_reported; | |
478 | } | |
479 | ||
480 | static ssize_t ucma_destroy_id(struct ucma_file *file, const char __user *inbuf, | |
481 | int in_len, int out_len) | |
482 | { | |
483 | struct rdma_ucm_destroy_id cmd; | |
484 | struct rdma_ucm_destroy_id_resp resp; | |
485 | struct ucma_context *ctx; | |
486 | int ret = 0; | |
487 | ||
488 | if (out_len < sizeof(resp)) | |
489 | return -ENOSPC; | |
490 | ||
491 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
492 | return -EFAULT; | |
493 | ||
494 | mutex_lock(&mut); | |
495 | ctx = _ucma_find_context(cmd.id, file); | |
496 | if (!IS_ERR(ctx)) | |
497 | idr_remove(&ctx_idr, ctx->id); | |
498 | mutex_unlock(&mut); | |
499 | ||
500 | if (IS_ERR(ctx)) | |
501 | return PTR_ERR(ctx); | |
502 | ||
503 | ucma_put_ctx(ctx); | |
504 | wait_for_completion(&ctx->comp); | |
505 | resp.events_reported = ucma_free_ctx(ctx); | |
506 | ||
507 | if (copy_to_user((void __user *)(unsigned long)cmd.response, | |
508 | &resp, sizeof(resp))) | |
509 | ret = -EFAULT; | |
510 | ||
511 | return ret; | |
512 | } | |
513 | ||
514 | static ssize_t ucma_bind_addr(struct ucma_file *file, const char __user *inbuf, | |
515 | int in_len, int out_len) | |
516 | { | |
517 | struct rdma_ucm_bind_addr cmd; | |
518 | struct ucma_context *ctx; | |
519 | int ret; | |
520 | ||
521 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
522 | return -EFAULT; | |
523 | ||
524 | ctx = ucma_get_ctx(file, cmd.id); | |
525 | if (IS_ERR(ctx)) | |
526 | return PTR_ERR(ctx); | |
527 | ||
528 | ret = rdma_bind_addr(ctx->cm_id, (struct sockaddr *) &cmd.addr); | |
529 | ucma_put_ctx(ctx); | |
530 | return ret; | |
531 | } | |
532 | ||
533 | static ssize_t ucma_resolve_addr(struct ucma_file *file, | |
534 | const char __user *inbuf, | |
535 | int in_len, int out_len) | |
536 | { | |
537 | struct rdma_ucm_resolve_addr cmd; | |
538 | struct ucma_context *ctx; | |
539 | int ret; | |
540 | ||
541 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
542 | return -EFAULT; | |
543 | ||
544 | ctx = ucma_get_ctx(file, cmd.id); | |
545 | if (IS_ERR(ctx)) | |
546 | return PTR_ERR(ctx); | |
547 | ||
548 | ret = rdma_resolve_addr(ctx->cm_id, (struct sockaddr *) &cmd.src_addr, | |
549 | (struct sockaddr *) &cmd.dst_addr, | |
550 | cmd.timeout_ms); | |
551 | ucma_put_ctx(ctx); | |
552 | return ret; | |
553 | } | |
554 | ||
555 | static ssize_t ucma_resolve_route(struct ucma_file *file, | |
556 | const char __user *inbuf, | |
557 | int in_len, int out_len) | |
558 | { | |
559 | struct rdma_ucm_resolve_route cmd; | |
560 | struct ucma_context *ctx; | |
561 | int ret; | |
562 | ||
563 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
564 | return -EFAULT; | |
565 | ||
566 | ctx = ucma_get_ctx(file, cmd.id); | |
567 | if (IS_ERR(ctx)) | |
568 | return PTR_ERR(ctx); | |
569 | ||
570 | ret = rdma_resolve_route(ctx->cm_id, cmd.timeout_ms); | |
571 | ucma_put_ctx(ctx); | |
572 | return ret; | |
573 | } | |
574 | ||
575 | static void ucma_copy_ib_route(struct rdma_ucm_query_route_resp *resp, | |
576 | struct rdma_route *route) | |
577 | { | |
578 | struct rdma_dev_addr *dev_addr; | |
579 | ||
580 | resp->num_paths = route->num_paths; | |
581 | switch (route->num_paths) { | |
582 | case 0: | |
583 | dev_addr = &route->addr.dev_addr; | |
6f8372b6 SH |
584 | rdma_addr_get_dgid(dev_addr, |
585 | (union ib_gid *) &resp->ib_route[0].dgid); | |
586 | rdma_addr_get_sgid(dev_addr, | |
587 | (union ib_gid *) &resp->ib_route[0].sgid); | |
75216638 SH |
588 | resp->ib_route[0].pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr)); |
589 | break; | |
590 | case 2: | |
591 | ib_copy_path_rec_to_user(&resp->ib_route[1], | |
592 | &route->path_rec[1]); | |
593 | /* fall through */ | |
594 | case 1: | |
595 | ib_copy_path_rec_to_user(&resp->ib_route[0], | |
596 | &route->path_rec[0]); | |
597 | break; | |
598 | default: | |
599 | break; | |
600 | } | |
601 | } | |
602 | ||
3c86aa70 EC |
603 | static void ucma_copy_iboe_route(struct rdma_ucm_query_route_resp *resp, |
604 | struct rdma_route *route) | |
605 | { | |
606 | struct rdma_dev_addr *dev_addr; | |
af7bd463 EC |
607 | struct net_device *dev; |
608 | u16 vid = 0; | |
3c86aa70 EC |
609 | |
610 | resp->num_paths = route->num_paths; | |
611 | switch (route->num_paths) { | |
612 | case 0: | |
613 | dev_addr = &route->addr.dev_addr; | |
af7bd463 EC |
614 | dev = dev_get_by_index(&init_net, dev_addr->bound_dev_if); |
615 | if (dev) { | |
616 | vid = rdma_vlan_dev_vlan_id(dev); | |
617 | dev_put(dev); | |
618 | } | |
619 | ||
620 | iboe_mac_vlan_to_ll((union ib_gid *) &resp->ib_route[0].dgid, | |
621 | dev_addr->dst_dev_addr, vid); | |
3c86aa70 EC |
622 | iboe_addr_get_sgid(dev_addr, |
623 | (union ib_gid *) &resp->ib_route[0].sgid); | |
624 | resp->ib_route[0].pkey = cpu_to_be16(0xffff); | |
625 | break; | |
626 | case 2: | |
627 | ib_copy_path_rec_to_user(&resp->ib_route[1], | |
628 | &route->path_rec[1]); | |
629 | /* fall through */ | |
630 | case 1: | |
631 | ib_copy_path_rec_to_user(&resp->ib_route[0], | |
632 | &route->path_rec[0]); | |
633 | break; | |
634 | default: | |
635 | break; | |
636 | } | |
637 | } | |
638 | ||
e86f8b06 SW |
639 | static void ucma_copy_iw_route(struct rdma_ucm_query_route_resp *resp, |
640 | struct rdma_route *route) | |
641 | { | |
642 | struct rdma_dev_addr *dev_addr; | |
643 | ||
644 | dev_addr = &route->addr.dev_addr; | |
645 | rdma_addr_get_dgid(dev_addr, (union ib_gid *) &resp->ib_route[0].dgid); | |
646 | rdma_addr_get_sgid(dev_addr, (union ib_gid *) &resp->ib_route[0].sgid); | |
647 | } | |
648 | ||
75216638 SH |
649 | static ssize_t ucma_query_route(struct ucma_file *file, |
650 | const char __user *inbuf, | |
651 | int in_len, int out_len) | |
652 | { | |
ee7aed45 | 653 | struct rdma_ucm_query cmd; |
75216638 SH |
654 | struct rdma_ucm_query_route_resp resp; |
655 | struct ucma_context *ctx; | |
656 | struct sockaddr *addr; | |
657 | int ret = 0; | |
658 | ||
659 | if (out_len < sizeof(resp)) | |
660 | return -ENOSPC; | |
661 | ||
662 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
663 | return -EFAULT; | |
664 | ||
665 | ctx = ucma_get_ctx(file, cmd.id); | |
666 | if (IS_ERR(ctx)) | |
667 | return PTR_ERR(ctx); | |
668 | ||
669 | memset(&resp, 0, sizeof resp); | |
3f446754 | 670 | addr = (struct sockaddr *) &ctx->cm_id->route.addr.src_addr; |
75216638 SH |
671 | memcpy(&resp.src_addr, addr, addr->sa_family == AF_INET ? |
672 | sizeof(struct sockaddr_in) : | |
673 | sizeof(struct sockaddr_in6)); | |
3f446754 | 674 | addr = (struct sockaddr *) &ctx->cm_id->route.addr.dst_addr; |
75216638 SH |
675 | memcpy(&resp.dst_addr, addr, addr->sa_family == AF_INET ? |
676 | sizeof(struct sockaddr_in) : | |
677 | sizeof(struct sockaddr_in6)); | |
678 | if (!ctx->cm_id->device) | |
679 | goto out; | |
680 | ||
9cda779c | 681 | resp.node_guid = (__force __u64) ctx->cm_id->device->node_guid; |
75216638 | 682 | resp.port_num = ctx->cm_id->port_num; |
e86f8b06 SW |
683 | switch (rdma_node_get_transport(ctx->cm_id->device->node_type)) { |
684 | case RDMA_TRANSPORT_IB: | |
685 | switch (rdma_port_get_link_layer(ctx->cm_id->device, | |
686 | ctx->cm_id->port_num)) { | |
3c86aa70 EC |
687 | case IB_LINK_LAYER_INFINIBAND: |
688 | ucma_copy_ib_route(&resp, &ctx->cm_id->route); | |
689 | break; | |
690 | case IB_LINK_LAYER_ETHERNET: | |
691 | ucma_copy_iboe_route(&resp, &ctx->cm_id->route); | |
692 | break; | |
693 | default: | |
694 | break; | |
695 | } | |
e86f8b06 SW |
696 | break; |
697 | case RDMA_TRANSPORT_IWARP: | |
698 | ucma_copy_iw_route(&resp, &ctx->cm_id->route); | |
699 | break; | |
700 | default: | |
701 | break; | |
75216638 SH |
702 | } |
703 | ||
704 | out: | |
705 | if (copy_to_user((void __user *)(unsigned long)cmd.response, | |
706 | &resp, sizeof(resp))) | |
707 | ret = -EFAULT; | |
708 | ||
709 | ucma_put_ctx(ctx); | |
710 | return ret; | |
711 | } | |
712 | ||
ee7aed45 SH |
713 | static void ucma_query_device_addr(struct rdma_cm_id *cm_id, |
714 | struct rdma_ucm_query_addr_resp *resp) | |
715 | { | |
716 | if (!cm_id->device) | |
717 | return; | |
718 | ||
719 | resp->node_guid = (__force __u64) cm_id->device->node_guid; | |
720 | resp->port_num = cm_id->port_num; | |
721 | resp->pkey = (__force __u16) cpu_to_be16( | |
722 | ib_addr_get_pkey(&cm_id->route.addr.dev_addr)); | |
723 | } | |
724 | ||
725 | static ssize_t ucma_query_addr(struct ucma_context *ctx, | |
726 | void __user *response, int out_len) | |
727 | { | |
728 | struct rdma_ucm_query_addr_resp resp; | |
729 | struct sockaddr *addr; | |
730 | int ret = 0; | |
731 | ||
732 | if (out_len < sizeof(resp)) | |
733 | return -ENOSPC; | |
734 | ||
735 | memset(&resp, 0, sizeof resp); | |
736 | ||
737 | addr = (struct sockaddr *) &ctx->cm_id->route.addr.src_addr; | |
738 | resp.src_size = rdma_addr_size(addr); | |
739 | memcpy(&resp.src_addr, addr, resp.src_size); | |
740 | ||
741 | addr = (struct sockaddr *) &ctx->cm_id->route.addr.dst_addr; | |
742 | resp.dst_size = rdma_addr_size(addr); | |
743 | memcpy(&resp.dst_addr, addr, resp.dst_size); | |
744 | ||
745 | ucma_query_device_addr(ctx->cm_id, &resp); | |
746 | ||
747 | if (copy_to_user(response, &resp, sizeof(resp))) | |
748 | ret = -EFAULT; | |
749 | ||
750 | return ret; | |
751 | } | |
752 | ||
753 | static ssize_t ucma_query(struct ucma_file *file, | |
754 | const char __user *inbuf, | |
755 | int in_len, int out_len) | |
756 | { | |
757 | struct rdma_ucm_query cmd; | |
758 | struct ucma_context *ctx; | |
759 | void __user *response; | |
760 | int ret; | |
761 | ||
762 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
763 | return -EFAULT; | |
764 | ||
765 | response = (void __user *)(unsigned long) cmd.response; | |
766 | ctx = ucma_get_ctx(file, cmd.id); | |
767 | if (IS_ERR(ctx)) | |
768 | return PTR_ERR(ctx); | |
769 | ||
770 | switch (cmd.option) { | |
771 | case RDMA_USER_CM_QUERY_ADDR: | |
772 | ret = ucma_query_addr(ctx, response, out_len); | |
773 | break; | |
774 | default: | |
775 | ret = -ENOSYS; | |
776 | break; | |
777 | } | |
778 | ||
779 | ucma_put_ctx(ctx); | |
780 | return ret; | |
781 | } | |
782 | ||
5c438135 SH |
783 | static void ucma_copy_conn_param(struct rdma_cm_id *id, |
784 | struct rdma_conn_param *dst, | |
75216638 SH |
785 | struct rdma_ucm_conn_param *src) |
786 | { | |
787 | dst->private_data = src->private_data; | |
788 | dst->private_data_len = src->private_data_len; | |
789 | dst->responder_resources =src->responder_resources; | |
790 | dst->initiator_depth = src->initiator_depth; | |
791 | dst->flow_control = src->flow_control; | |
792 | dst->retry_count = src->retry_count; | |
793 | dst->rnr_retry_count = src->rnr_retry_count; | |
794 | dst->srq = src->srq; | |
795 | dst->qp_num = src->qp_num; | |
5c438135 | 796 | dst->qkey = (id->route.addr.src_addr.ss_family == AF_IB) ? src->qkey : 0; |
75216638 SH |
797 | } |
798 | ||
799 | static ssize_t ucma_connect(struct ucma_file *file, const char __user *inbuf, | |
800 | int in_len, int out_len) | |
801 | { | |
802 | struct rdma_ucm_connect cmd; | |
803 | struct rdma_conn_param conn_param; | |
804 | struct ucma_context *ctx; | |
805 | int ret; | |
806 | ||
807 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
808 | return -EFAULT; | |
809 | ||
810 | if (!cmd.conn_param.valid) | |
811 | return -EINVAL; | |
812 | ||
813 | ctx = ucma_get_ctx(file, cmd.id); | |
814 | if (IS_ERR(ctx)) | |
815 | return PTR_ERR(ctx); | |
816 | ||
5c438135 | 817 | ucma_copy_conn_param(ctx->cm_id, &conn_param, &cmd.conn_param); |
75216638 SH |
818 | ret = rdma_connect(ctx->cm_id, &conn_param); |
819 | ucma_put_ctx(ctx); | |
820 | return ret; | |
821 | } | |
822 | ||
823 | static ssize_t ucma_listen(struct ucma_file *file, const char __user *inbuf, | |
824 | int in_len, int out_len) | |
825 | { | |
826 | struct rdma_ucm_listen cmd; | |
827 | struct ucma_context *ctx; | |
828 | int ret; | |
829 | ||
830 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
831 | return -EFAULT; | |
832 | ||
833 | ctx = ucma_get_ctx(file, cmd.id); | |
834 | if (IS_ERR(ctx)) | |
835 | return PTR_ERR(ctx); | |
836 | ||
97cb7e40 SW |
837 | ctx->backlog = cmd.backlog > 0 && cmd.backlog < max_backlog ? |
838 | cmd.backlog : max_backlog; | |
75216638 SH |
839 | ret = rdma_listen(ctx->cm_id, ctx->backlog); |
840 | ucma_put_ctx(ctx); | |
841 | return ret; | |
842 | } | |
843 | ||
844 | static ssize_t ucma_accept(struct ucma_file *file, const char __user *inbuf, | |
845 | int in_len, int out_len) | |
846 | { | |
847 | struct rdma_ucm_accept cmd; | |
848 | struct rdma_conn_param conn_param; | |
849 | struct ucma_context *ctx; | |
850 | int ret; | |
851 | ||
852 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
853 | return -EFAULT; | |
854 | ||
855 | ctx = ucma_get_ctx(file, cmd.id); | |
856 | if (IS_ERR(ctx)) | |
857 | return PTR_ERR(ctx); | |
858 | ||
859 | if (cmd.conn_param.valid) { | |
5c438135 | 860 | ucma_copy_conn_param(ctx->cm_id, &conn_param, &cmd.conn_param); |
9ced69ca | 861 | mutex_lock(&file->mut); |
75216638 | 862 | ret = rdma_accept(ctx->cm_id, &conn_param); |
9ced69ca SH |
863 | if (!ret) |
864 | ctx->uid = cmd.uid; | |
865 | mutex_unlock(&file->mut); | |
75216638 SH |
866 | } else |
867 | ret = rdma_accept(ctx->cm_id, NULL); | |
868 | ||
869 | ucma_put_ctx(ctx); | |
870 | return ret; | |
871 | } | |
872 | ||
873 | static ssize_t ucma_reject(struct ucma_file *file, const char __user *inbuf, | |
874 | int in_len, int out_len) | |
875 | { | |
876 | struct rdma_ucm_reject cmd; | |
877 | struct ucma_context *ctx; | |
878 | int ret; | |
879 | ||
880 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
881 | return -EFAULT; | |
882 | ||
883 | ctx = ucma_get_ctx(file, cmd.id); | |
884 | if (IS_ERR(ctx)) | |
885 | return PTR_ERR(ctx); | |
886 | ||
887 | ret = rdma_reject(ctx->cm_id, cmd.private_data, cmd.private_data_len); | |
888 | ucma_put_ctx(ctx); | |
889 | return ret; | |
890 | } | |
891 | ||
892 | static ssize_t ucma_disconnect(struct ucma_file *file, const char __user *inbuf, | |
893 | int in_len, int out_len) | |
894 | { | |
895 | struct rdma_ucm_disconnect cmd; | |
896 | struct ucma_context *ctx; | |
897 | int ret; | |
898 | ||
899 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
900 | return -EFAULT; | |
901 | ||
902 | ctx = ucma_get_ctx(file, cmd.id); | |
903 | if (IS_ERR(ctx)) | |
904 | return PTR_ERR(ctx); | |
905 | ||
906 | ret = rdma_disconnect(ctx->cm_id); | |
907 | ucma_put_ctx(ctx); | |
908 | return ret; | |
909 | } | |
910 | ||
911 | static ssize_t ucma_init_qp_attr(struct ucma_file *file, | |
912 | const char __user *inbuf, | |
913 | int in_len, int out_len) | |
914 | { | |
915 | struct rdma_ucm_init_qp_attr cmd; | |
916 | struct ib_uverbs_qp_attr resp; | |
917 | struct ucma_context *ctx; | |
918 | struct ib_qp_attr qp_attr; | |
919 | int ret; | |
920 | ||
921 | if (out_len < sizeof(resp)) | |
922 | return -ENOSPC; | |
923 | ||
924 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
925 | return -EFAULT; | |
926 | ||
927 | ctx = ucma_get_ctx(file, cmd.id); | |
928 | if (IS_ERR(ctx)) | |
929 | return PTR_ERR(ctx); | |
930 | ||
931 | resp.qp_attr_mask = 0; | |
932 | memset(&qp_attr, 0, sizeof qp_attr); | |
933 | qp_attr.qp_state = cmd.qp_state; | |
934 | ret = rdma_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask); | |
935 | if (ret) | |
936 | goto out; | |
937 | ||
938 | ib_copy_qp_attr_to_user(&resp, &qp_attr); | |
939 | if (copy_to_user((void __user *)(unsigned long)cmd.response, | |
940 | &resp, sizeof(resp))) | |
941 | ret = -EFAULT; | |
942 | ||
943 | out: | |
944 | ucma_put_ctx(ctx); | |
945 | return ret; | |
946 | } | |
947 | ||
7ce86409 SH |
948 | static int ucma_set_option_id(struct ucma_context *ctx, int optname, |
949 | void *optval, size_t optlen) | |
950 | { | |
951 | int ret = 0; | |
952 | ||
953 | switch (optname) { | |
954 | case RDMA_OPTION_ID_TOS: | |
955 | if (optlen != sizeof(u8)) { | |
956 | ret = -EINVAL; | |
957 | break; | |
958 | } | |
959 | rdma_set_service_type(ctx->cm_id, *((u8 *) optval)); | |
960 | break; | |
a9bb7912 HS |
961 | case RDMA_OPTION_ID_REUSEADDR: |
962 | if (optlen != sizeof(int)) { | |
963 | ret = -EINVAL; | |
964 | break; | |
965 | } | |
966 | ret = rdma_set_reuseaddr(ctx->cm_id, *((int *) optval) ? 1 : 0); | |
967 | break; | |
68602120 SH |
968 | case RDMA_OPTION_ID_AFONLY: |
969 | if (optlen != sizeof(int)) { | |
970 | ret = -EINVAL; | |
971 | break; | |
972 | } | |
973 | ret = rdma_set_afonly(ctx->cm_id, *((int *) optval) ? 1 : 0); | |
974 | break; | |
7ce86409 SH |
975 | default: |
976 | ret = -ENOSYS; | |
977 | } | |
978 | ||
979 | return ret; | |
980 | } | |
981 | ||
a7ca1f00 SH |
982 | static int ucma_set_ib_path(struct ucma_context *ctx, |
983 | struct ib_path_rec_data *path_data, size_t optlen) | |
984 | { | |
985 | struct ib_sa_path_rec sa_path; | |
986 | struct rdma_cm_event event; | |
987 | int ret; | |
988 | ||
989 | if (optlen % sizeof(*path_data)) | |
990 | return -EINVAL; | |
991 | ||
992 | for (; optlen; optlen -= sizeof(*path_data), path_data++) { | |
993 | if (path_data->flags == (IB_PATH_GMP | IB_PATH_PRIMARY | | |
994 | IB_PATH_BIDIRECTIONAL)) | |
995 | break; | |
996 | } | |
997 | ||
998 | if (!optlen) | |
999 | return -EINVAL; | |
1000 | ||
1001 | ib_sa_unpack_path(path_data->path_rec, &sa_path); | |
1002 | ret = rdma_set_ib_paths(ctx->cm_id, &sa_path, 1); | |
1003 | if (ret) | |
1004 | return ret; | |
1005 | ||
1006 | memset(&event, 0, sizeof event); | |
1007 | event.event = RDMA_CM_EVENT_ROUTE_RESOLVED; | |
1008 | return ucma_event_handler(ctx->cm_id, &event); | |
1009 | } | |
1010 | ||
1011 | static int ucma_set_option_ib(struct ucma_context *ctx, int optname, | |
1012 | void *optval, size_t optlen) | |
1013 | { | |
1014 | int ret; | |
1015 | ||
1016 | switch (optname) { | |
1017 | case RDMA_OPTION_IB_PATH: | |
1018 | ret = ucma_set_ib_path(ctx, optval, optlen); | |
1019 | break; | |
1020 | default: | |
1021 | ret = -ENOSYS; | |
1022 | } | |
1023 | ||
1024 | return ret; | |
1025 | } | |
1026 | ||
7ce86409 SH |
1027 | static int ucma_set_option_level(struct ucma_context *ctx, int level, |
1028 | int optname, void *optval, size_t optlen) | |
1029 | { | |
1030 | int ret; | |
1031 | ||
1032 | switch (level) { | |
1033 | case RDMA_OPTION_ID: | |
1034 | ret = ucma_set_option_id(ctx, optname, optval, optlen); | |
1035 | break; | |
a7ca1f00 SH |
1036 | case RDMA_OPTION_IB: |
1037 | ret = ucma_set_option_ib(ctx, optname, optval, optlen); | |
1038 | break; | |
7ce86409 SH |
1039 | default: |
1040 | ret = -ENOSYS; | |
1041 | } | |
1042 | ||
1043 | return ret; | |
1044 | } | |
1045 | ||
1046 | static ssize_t ucma_set_option(struct ucma_file *file, const char __user *inbuf, | |
1047 | int in_len, int out_len) | |
1048 | { | |
1049 | struct rdma_ucm_set_option cmd; | |
1050 | struct ucma_context *ctx; | |
1051 | void *optval; | |
1052 | int ret; | |
1053 | ||
1054 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
1055 | return -EFAULT; | |
1056 | ||
1057 | ctx = ucma_get_ctx(file, cmd.id); | |
1058 | if (IS_ERR(ctx)) | |
1059 | return PTR_ERR(ctx); | |
1060 | ||
0764c76e RD |
1061 | optval = memdup_user((void __user *) (unsigned long) cmd.optval, |
1062 | cmd.optlen); | |
1063 | if (IS_ERR(optval)) { | |
1064 | ret = PTR_ERR(optval); | |
1065 | goto out; | |
7ce86409 SH |
1066 | } |
1067 | ||
1068 | ret = ucma_set_option_level(ctx, cmd.level, cmd.optname, optval, | |
1069 | cmd.optlen); | |
7ce86409 | 1070 | kfree(optval); |
0764c76e RD |
1071 | |
1072 | out: | |
7ce86409 SH |
1073 | ucma_put_ctx(ctx); |
1074 | return ret; | |
1075 | } | |
1076 | ||
75216638 SH |
1077 | static ssize_t ucma_notify(struct ucma_file *file, const char __user *inbuf, |
1078 | int in_len, int out_len) | |
1079 | { | |
1080 | struct rdma_ucm_notify cmd; | |
1081 | struct ucma_context *ctx; | |
1082 | int ret; | |
1083 | ||
1084 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
1085 | return -EFAULT; | |
1086 | ||
1087 | ctx = ucma_get_ctx(file, cmd.id); | |
1088 | if (IS_ERR(ctx)) | |
1089 | return PTR_ERR(ctx); | |
1090 | ||
1091 | ret = rdma_notify(ctx->cm_id, (enum ib_event_type) cmd.event); | |
1092 | ucma_put_ctx(ctx); | |
1093 | return ret; | |
1094 | } | |
1095 | ||
c8f6a362 SH |
1096 | static ssize_t ucma_join_multicast(struct ucma_file *file, |
1097 | const char __user *inbuf, | |
1098 | int in_len, int out_len) | |
1099 | { | |
1100 | struct rdma_ucm_join_mcast cmd; | |
1101 | struct rdma_ucm_create_id_resp resp; | |
1102 | struct ucma_context *ctx; | |
1103 | struct ucma_multicast *mc; | |
1104 | int ret; | |
1105 | ||
1106 | if (out_len < sizeof(resp)) | |
1107 | return -ENOSPC; | |
1108 | ||
1109 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
1110 | return -EFAULT; | |
1111 | ||
1112 | ctx = ucma_get_ctx(file, cmd.id); | |
1113 | if (IS_ERR(ctx)) | |
1114 | return PTR_ERR(ctx); | |
1115 | ||
1116 | mutex_lock(&file->mut); | |
1117 | mc = ucma_alloc_multicast(ctx); | |
6aea938f JB |
1118 | if (!mc) { |
1119 | ret = -ENOMEM; | |
c8f6a362 SH |
1120 | goto err1; |
1121 | } | |
1122 | ||
1123 | mc->uid = cmd.uid; | |
1124 | memcpy(&mc->addr, &cmd.addr, sizeof cmd.addr); | |
3f446754 | 1125 | ret = rdma_join_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr, mc); |
c8f6a362 SH |
1126 | if (ret) |
1127 | goto err2; | |
1128 | ||
1129 | resp.id = mc->id; | |
1130 | if (copy_to_user((void __user *)(unsigned long)cmd.response, | |
1131 | &resp, sizeof(resp))) { | |
1132 | ret = -EFAULT; | |
1133 | goto err3; | |
1134 | } | |
1135 | ||
1136 | mutex_unlock(&file->mut); | |
1137 | ucma_put_ctx(ctx); | |
1138 | return 0; | |
1139 | ||
1140 | err3: | |
3f446754 | 1141 | rdma_leave_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr); |
c8f6a362 SH |
1142 | ucma_cleanup_mc_events(mc); |
1143 | err2: | |
1144 | mutex_lock(&mut); | |
1145 | idr_remove(&multicast_idr, mc->id); | |
1146 | mutex_unlock(&mut); | |
1147 | list_del(&mc->list); | |
1148 | kfree(mc); | |
1149 | err1: | |
1150 | mutex_unlock(&file->mut); | |
1151 | ucma_put_ctx(ctx); | |
1152 | return ret; | |
1153 | } | |
1154 | ||
1155 | static ssize_t ucma_leave_multicast(struct ucma_file *file, | |
1156 | const char __user *inbuf, | |
1157 | int in_len, int out_len) | |
1158 | { | |
1159 | struct rdma_ucm_destroy_id cmd; | |
1160 | struct rdma_ucm_destroy_id_resp resp; | |
1161 | struct ucma_multicast *mc; | |
1162 | int ret = 0; | |
1163 | ||
1164 | if (out_len < sizeof(resp)) | |
1165 | return -ENOSPC; | |
1166 | ||
1167 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
1168 | return -EFAULT; | |
1169 | ||
1170 | mutex_lock(&mut); | |
1171 | mc = idr_find(&multicast_idr, cmd.id); | |
1172 | if (!mc) | |
1173 | mc = ERR_PTR(-ENOENT); | |
1174 | else if (mc->ctx->file != file) | |
1175 | mc = ERR_PTR(-EINVAL); | |
1176 | else { | |
1177 | idr_remove(&multicast_idr, mc->id); | |
1178 | atomic_inc(&mc->ctx->ref); | |
1179 | } | |
1180 | mutex_unlock(&mut); | |
1181 | ||
1182 | if (IS_ERR(mc)) { | |
1183 | ret = PTR_ERR(mc); | |
1184 | goto out; | |
1185 | } | |
1186 | ||
3f446754 | 1187 | rdma_leave_multicast(mc->ctx->cm_id, (struct sockaddr *) &mc->addr); |
c8f6a362 SH |
1188 | mutex_lock(&mc->ctx->file->mut); |
1189 | ucma_cleanup_mc_events(mc); | |
1190 | list_del(&mc->list); | |
1191 | mutex_unlock(&mc->ctx->file->mut); | |
1192 | ||
1193 | ucma_put_ctx(mc->ctx); | |
1194 | resp.events_reported = mc->events_reported; | |
1195 | kfree(mc); | |
1196 | ||
1197 | if (copy_to_user((void __user *)(unsigned long)cmd.response, | |
1198 | &resp, sizeof(resp))) | |
1199 | ret = -EFAULT; | |
1200 | out: | |
1201 | return ret; | |
1202 | } | |
1203 | ||
88314e4d SH |
1204 | static void ucma_lock_files(struct ucma_file *file1, struct ucma_file *file2) |
1205 | { | |
1206 | /* Acquire mutex's based on pointer comparison to prevent deadlock. */ | |
1207 | if (file1 < file2) { | |
1208 | mutex_lock(&file1->mut); | |
1209 | mutex_lock(&file2->mut); | |
1210 | } else { | |
1211 | mutex_lock(&file2->mut); | |
1212 | mutex_lock(&file1->mut); | |
1213 | } | |
1214 | } | |
1215 | ||
1216 | static void ucma_unlock_files(struct ucma_file *file1, struct ucma_file *file2) | |
1217 | { | |
1218 | if (file1 < file2) { | |
1219 | mutex_unlock(&file2->mut); | |
1220 | mutex_unlock(&file1->mut); | |
1221 | } else { | |
1222 | mutex_unlock(&file1->mut); | |
1223 | mutex_unlock(&file2->mut); | |
1224 | } | |
1225 | } | |
1226 | ||
1227 | static void ucma_move_events(struct ucma_context *ctx, struct ucma_file *file) | |
1228 | { | |
1229 | struct ucma_event *uevent, *tmp; | |
1230 | ||
1231 | list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list) | |
1232 | if (uevent->ctx == ctx) | |
1233 | list_move_tail(&uevent->list, &file->event_list); | |
1234 | } | |
1235 | ||
1236 | static ssize_t ucma_migrate_id(struct ucma_file *new_file, | |
1237 | const char __user *inbuf, | |
1238 | int in_len, int out_len) | |
1239 | { | |
1240 | struct rdma_ucm_migrate_id cmd; | |
1241 | struct rdma_ucm_migrate_resp resp; | |
1242 | struct ucma_context *ctx; | |
2903ff01 | 1243 | struct fd f; |
88314e4d SH |
1244 | struct ucma_file *cur_file; |
1245 | int ret = 0; | |
1246 | ||
1247 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
1248 | return -EFAULT; | |
1249 | ||
1250 | /* Get current fd to protect against it being closed */ | |
2903ff01 AV |
1251 | f = fdget(cmd.fd); |
1252 | if (!f.file) | |
88314e4d SH |
1253 | return -ENOENT; |
1254 | ||
1255 | /* Validate current fd and prevent destruction of id. */ | |
2903ff01 | 1256 | ctx = ucma_get_ctx(f.file->private_data, cmd.id); |
88314e4d SH |
1257 | if (IS_ERR(ctx)) { |
1258 | ret = PTR_ERR(ctx); | |
1259 | goto file_put; | |
1260 | } | |
1261 | ||
1262 | cur_file = ctx->file; | |
1263 | if (cur_file == new_file) { | |
1264 | resp.events_reported = ctx->events_reported; | |
1265 | goto response; | |
1266 | } | |
1267 | ||
1268 | /* | |
1269 | * Migrate events between fd's, maintaining order, and avoiding new | |
1270 | * events being added before existing events. | |
1271 | */ | |
1272 | ucma_lock_files(cur_file, new_file); | |
1273 | mutex_lock(&mut); | |
1274 | ||
1275 | list_move_tail(&ctx->list, &new_file->ctx_list); | |
1276 | ucma_move_events(ctx, new_file); | |
1277 | ctx->file = new_file; | |
1278 | resp.events_reported = ctx->events_reported; | |
1279 | ||
1280 | mutex_unlock(&mut); | |
1281 | ucma_unlock_files(cur_file, new_file); | |
1282 | ||
1283 | response: | |
1284 | if (copy_to_user((void __user *)(unsigned long)cmd.response, | |
1285 | &resp, sizeof(resp))) | |
1286 | ret = -EFAULT; | |
1287 | ||
1288 | ucma_put_ctx(ctx); | |
1289 | file_put: | |
2903ff01 | 1290 | fdput(f); |
88314e4d SH |
1291 | return ret; |
1292 | } | |
1293 | ||
75216638 SH |
1294 | static ssize_t (*ucma_cmd_table[])(struct ucma_file *file, |
1295 | const char __user *inbuf, | |
1296 | int in_len, int out_len) = { | |
1297 | [RDMA_USER_CM_CMD_CREATE_ID] = ucma_create_id, | |
1298 | [RDMA_USER_CM_CMD_DESTROY_ID] = ucma_destroy_id, | |
1299 | [RDMA_USER_CM_CMD_BIND_ADDR] = ucma_bind_addr, | |
1300 | [RDMA_USER_CM_CMD_RESOLVE_ADDR] = ucma_resolve_addr, | |
1301 | [RDMA_USER_CM_CMD_RESOLVE_ROUTE]= ucma_resolve_route, | |
1302 | [RDMA_USER_CM_CMD_QUERY_ROUTE] = ucma_query_route, | |
1303 | [RDMA_USER_CM_CMD_CONNECT] = ucma_connect, | |
1304 | [RDMA_USER_CM_CMD_LISTEN] = ucma_listen, | |
1305 | [RDMA_USER_CM_CMD_ACCEPT] = ucma_accept, | |
1306 | [RDMA_USER_CM_CMD_REJECT] = ucma_reject, | |
1307 | [RDMA_USER_CM_CMD_DISCONNECT] = ucma_disconnect, | |
1308 | [RDMA_USER_CM_CMD_INIT_QP_ATTR] = ucma_init_qp_attr, | |
1309 | [RDMA_USER_CM_CMD_GET_EVENT] = ucma_get_event, | |
1310 | [RDMA_USER_CM_CMD_GET_OPTION] = NULL, | |
7ce86409 | 1311 | [RDMA_USER_CM_CMD_SET_OPTION] = ucma_set_option, |
75216638 | 1312 | [RDMA_USER_CM_CMD_NOTIFY] = ucma_notify, |
c8f6a362 SH |
1313 | [RDMA_USER_CM_CMD_JOIN_MCAST] = ucma_join_multicast, |
1314 | [RDMA_USER_CM_CMD_LEAVE_MCAST] = ucma_leave_multicast, | |
ee7aed45 SH |
1315 | [RDMA_USER_CM_CMD_MIGRATE_ID] = ucma_migrate_id, |
1316 | [RDMA_USER_CM_CMD_QUERY] = ucma_query | |
75216638 SH |
1317 | }; |
1318 | ||
1319 | static ssize_t ucma_write(struct file *filp, const char __user *buf, | |
1320 | size_t len, loff_t *pos) | |
1321 | { | |
1322 | struct ucma_file *file = filp->private_data; | |
1323 | struct rdma_ucm_cmd_hdr hdr; | |
1324 | ssize_t ret; | |
1325 | ||
1326 | if (len < sizeof(hdr)) | |
1327 | return -EINVAL; | |
1328 | ||
1329 | if (copy_from_user(&hdr, buf, sizeof(hdr))) | |
1330 | return -EFAULT; | |
1331 | ||
caf6e3f2 | 1332 | if (hdr.cmd >= ARRAY_SIZE(ucma_cmd_table)) |
75216638 SH |
1333 | return -EINVAL; |
1334 | ||
1335 | if (hdr.in + sizeof(hdr) > len) | |
1336 | return -EINVAL; | |
1337 | ||
1338 | if (!ucma_cmd_table[hdr.cmd]) | |
1339 | return -ENOSYS; | |
1340 | ||
1341 | ret = ucma_cmd_table[hdr.cmd](file, buf + sizeof(hdr), hdr.in, hdr.out); | |
1342 | if (!ret) | |
1343 | ret = len; | |
1344 | ||
1345 | return ret; | |
1346 | } | |
1347 | ||
1348 | static unsigned int ucma_poll(struct file *filp, struct poll_table_struct *wait) | |
1349 | { | |
1350 | struct ucma_file *file = filp->private_data; | |
1351 | unsigned int mask = 0; | |
1352 | ||
1353 | poll_wait(filp, &file->poll_wait, wait); | |
1354 | ||
1355 | if (!list_empty(&file->event_list)) | |
1356 | mask = POLLIN | POLLRDNORM; | |
1357 | ||
1358 | return mask; | |
1359 | } | |
1360 | ||
f7a6117e RD |
1361 | /* |
1362 | * ucma_open() does not need the BKL: | |
1363 | * | |
1364 | * - no global state is referred to; | |
1365 | * - there is no ioctl method to race against; | |
1366 | * - no further module initialization is required for open to work | |
1367 | * after the device is registered. | |
1368 | */ | |
75216638 SH |
1369 | static int ucma_open(struct inode *inode, struct file *filp) |
1370 | { | |
1371 | struct ucma_file *file; | |
1372 | ||
1373 | file = kmalloc(sizeof *file, GFP_KERNEL); | |
1374 | if (!file) | |
1375 | return -ENOMEM; | |
1376 | ||
1377 | INIT_LIST_HEAD(&file->event_list); | |
1378 | INIT_LIST_HEAD(&file->ctx_list); | |
1379 | init_waitqueue_head(&file->poll_wait); | |
1380 | mutex_init(&file->mut); | |
1381 | ||
1382 | filp->private_data = file; | |
1383 | file->filp = filp; | |
bc1db9af RD |
1384 | |
1385 | return nonseekable_open(inode, filp); | |
75216638 SH |
1386 | } |
1387 | ||
1388 | static int ucma_close(struct inode *inode, struct file *filp) | |
1389 | { | |
1390 | struct ucma_file *file = filp->private_data; | |
1391 | struct ucma_context *ctx, *tmp; | |
1392 | ||
1393 | mutex_lock(&file->mut); | |
1394 | list_for_each_entry_safe(ctx, tmp, &file->ctx_list, list) { | |
1395 | mutex_unlock(&file->mut); | |
1396 | ||
1397 | mutex_lock(&mut); | |
1398 | idr_remove(&ctx_idr, ctx->id); | |
1399 | mutex_unlock(&mut); | |
1400 | ||
1401 | ucma_free_ctx(ctx); | |
1402 | mutex_lock(&file->mut); | |
1403 | } | |
1404 | mutex_unlock(&file->mut); | |
1405 | kfree(file); | |
1406 | return 0; | |
1407 | } | |
1408 | ||
2b8693c0 | 1409 | static const struct file_operations ucma_fops = { |
75216638 SH |
1410 | .owner = THIS_MODULE, |
1411 | .open = ucma_open, | |
1412 | .release = ucma_close, | |
1413 | .write = ucma_write, | |
1414 | .poll = ucma_poll, | |
bc1db9af | 1415 | .llseek = no_llseek, |
75216638 SH |
1416 | }; |
1417 | ||
1418 | static struct miscdevice ucma_misc = { | |
04ea2f81 RD |
1419 | .minor = MISC_DYNAMIC_MINOR, |
1420 | .name = "rdma_cm", | |
1421 | .nodename = "infiniband/rdma_cm", | |
1422 | .mode = 0666, | |
1423 | .fops = &ucma_fops, | |
75216638 SH |
1424 | }; |
1425 | ||
1426 | static ssize_t show_abi_version(struct device *dev, | |
1427 | struct device_attribute *attr, | |
1428 | char *buf) | |
1429 | { | |
1430 | return sprintf(buf, "%d\n", RDMA_USER_CM_ABI_VERSION); | |
1431 | } | |
1432 | static DEVICE_ATTR(abi_version, S_IRUGO, show_abi_version, NULL); | |
1433 | ||
1434 | static int __init ucma_init(void) | |
1435 | { | |
1436 | int ret; | |
1437 | ||
1438 | ret = misc_register(&ucma_misc); | |
1439 | if (ret) | |
1440 | return ret; | |
1441 | ||
1442 | ret = device_create_file(ucma_misc.this_device, &dev_attr_abi_version); | |
1443 | if (ret) { | |
1444 | printk(KERN_ERR "rdma_ucm: couldn't create abi_version attr\n"); | |
97cb7e40 SW |
1445 | goto err1; |
1446 | } | |
1447 | ||
ec8f23ce | 1448 | ucma_ctl_table_hdr = register_net_sysctl(&init_net, "net/rdma_ucm", ucma_ctl_table); |
97cb7e40 SW |
1449 | if (!ucma_ctl_table_hdr) { |
1450 | printk(KERN_ERR "rdma_ucm: couldn't register sysctl paths\n"); | |
1451 | ret = -ENOMEM; | |
1452 | goto err2; | |
75216638 SH |
1453 | } |
1454 | return 0; | |
97cb7e40 SW |
1455 | err2: |
1456 | device_remove_file(ucma_misc.this_device, &dev_attr_abi_version); | |
1457 | err1: | |
75216638 SH |
1458 | misc_deregister(&ucma_misc); |
1459 | return ret; | |
1460 | } | |
1461 | ||
1462 | static void __exit ucma_cleanup(void) | |
1463 | { | |
5dd3df10 | 1464 | unregister_net_sysctl_table(ucma_ctl_table_hdr); |
75216638 SH |
1465 | device_remove_file(ucma_misc.this_device, &dev_attr_abi_version); |
1466 | misc_deregister(&ucma_misc); | |
1467 | idr_destroy(&ctx_idr); | |
1468 | } | |
1469 | ||
1470 | module_init(ucma_init); | |
1471 | module_exit(ucma_cleanup); |