Commit | Line | Data |
---|---|---|
a5b74540 HR |
1 | /* |
2 | * Copyright (c) 2005 Topspin Communications. All rights reserved. | |
b9ef520f | 3 | * Copyright (c) 2005 Intel Corporation. All rights reserved. |
a5b74540 HR |
4 | * |
5 | * This software is available to you under a choice of one of two | |
6 | * licenses. You may choose to be licensed under the terms of the GNU | |
7 | * General Public License (GPL) Version 2, available from the file | |
8 | * COPYING in the main directory of this source tree, or the | |
9 | * OpenIB.org BSD license below: | |
10 | * | |
11 | * Redistribution and use in source and binary forms, with or | |
12 | * without modification, are permitted provided that the following | |
13 | * conditions are met: | |
14 | * | |
15 | * - Redistributions of source code must retain the above | |
16 | * copyright notice, this list of conditions and the following | |
17 | * disclaimer. | |
18 | * | |
19 | * - Redistributions in binary form must reproduce the above | |
20 | * copyright notice, this list of conditions and the following | |
21 | * disclaimer in the documentation and/or other materials | |
22 | * provided with the distribution. | |
23 | * | |
24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
25 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
26 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
27 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
28 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
29 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
30 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
31 | * SOFTWARE. | |
a5b74540 | 32 | */ |
1b52fa98 SH |
33 | |
34 | #include <linux/completion.h> | |
a5b74540 HR |
35 | #include <linux/init.h> |
36 | #include <linux/fs.h> | |
37 | #include <linux/module.h> | |
38 | #include <linux/device.h> | |
39 | #include <linux/err.h> | |
40 | #include <linux/poll.h> | |
a99bbaf5 | 41 | #include <linux/sched.h> |
a5b74540 HR |
42 | #include <linux/file.h> |
43 | #include <linux/mount.h> | |
44 | #include <linux/cdev.h> | |
595e726a | 45 | #include <linux/idr.h> |
95ed644f | 46 | #include <linux/mutex.h> |
a5b74540 HR |
47 | |
48 | #include <asm/uaccess.h> | |
49 | ||
595e726a SH |
50 | #include <rdma/ib_cm.h> |
51 | #include <rdma/ib_user_cm.h> | |
6a9af2e1 | 52 | #include <rdma/ib_marshall.h> |
a5b74540 HR |
53 | |
54 | MODULE_AUTHOR("Libor Michalek"); | |
55 | MODULE_DESCRIPTION("InfiniBand userspace Connection Manager access"); | |
56 | MODULE_LICENSE("Dual BSD/GPL"); | |
57 | ||
07d357d0 SH |
58 | struct ib_ucm_device { |
59 | int devnum; | |
f4e91eb4 TJ |
60 | struct cdev cdev; |
61 | struct device dev; | |
07d357d0 SH |
62 | struct ib_device *ib_dev; |
63 | }; | |
64 | ||
595e726a | 65 | struct ib_ucm_file { |
4be10c1e | 66 | struct mutex file_mutex; |
595e726a | 67 | struct file *filp; |
07d357d0 | 68 | struct ib_ucm_device *device; |
79d81907 | 69 | |
07d357d0 SH |
70 | struct list_head ctxs; |
71 | struct list_head events; | |
595e726a SH |
72 | wait_queue_head_t poll_wait; |
73 | }; | |
74 | ||
75 | struct ib_ucm_context { | |
76 | int id; | |
1b52fa98 | 77 | struct completion comp; |
595e726a SH |
78 | atomic_t ref; |
79 | int events_reported; | |
80 | ||
81 | struct ib_ucm_file *file; | |
82 | struct ib_cm_id *cm_id; | |
83 | __u64 uid; | |
84 | ||
85 | struct list_head events; /* list of pending events. */ | |
86 | struct list_head file_list; /* member in file ctx list */ | |
87 | }; | |
88 | ||
89 | struct ib_ucm_event { | |
90 | struct ib_ucm_context *ctx; | |
91 | struct list_head file_list; /* member in file event list */ | |
92 | struct list_head ctx_list; /* member in ctx event list */ | |
93 | ||
94 | struct ib_cm_id *cm_id; | |
95 | struct ib_ucm_event_resp resp; | |
96 | void *data; | |
97 | void *info; | |
98 | int data_len; | |
99 | int info_len; | |
100 | }; | |
79d81907 | 101 | |
a5b74540 HR |
102 | enum { |
103 | IB_UCM_MAJOR = 231, | |
07d357d0 SH |
104 | IB_UCM_BASE_MINOR = 224, |
105 | IB_UCM_MAX_DEVICES = 32 | |
a5b74540 HR |
106 | }; |
107 | ||
9af57b7a SH |
108 | /* ib_cm and ib_user_cm modules share /sys/class/infiniband_cm */ |
109 | extern struct class cm_class; | |
110 | ||
07d357d0 | 111 | #define IB_UCM_BASE_DEV MKDEV(IB_UCM_MAJOR, IB_UCM_BASE_MINOR) |
a5b74540 | 112 | |
07d357d0 SH |
113 | static void ib_ucm_add_one(struct ib_device *device); |
114 | static void ib_ucm_remove_one(struct ib_device *device); | |
a5b74540 | 115 | |
07d357d0 SH |
116 | static struct ib_client ucm_client = { |
117 | .name = "ucm", | |
118 | .add = ib_ucm_add_one, | |
119 | .remove = ib_ucm_remove_one | |
120 | }; | |
121 | ||
95ed644f | 122 | static DEFINE_MUTEX(ctx_id_mutex); |
762a03e2 | 123 | static DEFINE_IDR(ctx_id_table); |
07d357d0 | 124 | static DECLARE_BITMAP(dev_map, IB_UCM_MAX_DEVICES); |
595e726a | 125 | |
b9ef520f | 126 | static struct ib_ucm_context *ib_ucm_ctx_get(struct ib_ucm_file *file, int id) |
a5b74540 HR |
127 | { |
128 | struct ib_ucm_context *ctx; | |
129 | ||
95ed644f | 130 | mutex_lock(&ctx_id_mutex); |
a5b74540 | 131 | ctx = idr_find(&ctx_id_table, id); |
b9ef520f SH |
132 | if (!ctx) |
133 | ctx = ERR_PTR(-ENOENT); | |
134 | else if (ctx->file != file) | |
135 | ctx = ERR_PTR(-EINVAL); | |
136 | else | |
137 | atomic_inc(&ctx->ref); | |
95ed644f | 138 | mutex_unlock(&ctx_id_mutex); |
a5b74540 HR |
139 | |
140 | return ctx; | |
141 | } | |
142 | ||
143 | static void ib_ucm_ctx_put(struct ib_ucm_context *ctx) | |
144 | { | |
b9ef520f | 145 | if (atomic_dec_and_test(&ctx->ref)) |
1b52fa98 | 146 | complete(&ctx->comp); |
b9ef520f SH |
147 | } |
148 | ||
0b2b35f6 | 149 | static inline int ib_ucm_new_cm_id(int event) |
b9ef520f | 150 | { |
0b2b35f6 SH |
151 | return event == IB_CM_REQ_RECEIVED || event == IB_CM_SIDR_REQ_RECEIVED; |
152 | } | |
b9ef520f | 153 | |
0b2b35f6 SH |
154 | static void ib_ucm_cleanup_events(struct ib_ucm_context *ctx) |
155 | { | |
156 | struct ib_ucm_event *uevent; | |
a5b74540 | 157 | |
4be10c1e | 158 | mutex_lock(&ctx->file->file_mutex); |
a5b74540 HR |
159 | list_del(&ctx->file_list); |
160 | while (!list_empty(&ctx->events)) { | |
161 | ||
162 | uevent = list_entry(ctx->events.next, | |
163 | struct ib_ucm_event, ctx_list); | |
164 | list_del(&uevent->file_list); | |
165 | list_del(&uevent->ctx_list); | |
f469b262 | 166 | mutex_unlock(&ctx->file->file_mutex); |
a5b74540 HR |
167 | |
168 | /* clear incoming connections. */ | |
0b2b35f6 | 169 | if (ib_ucm_new_cm_id(uevent->resp.event)) |
a5b74540 HR |
170 | ib_destroy_cm_id(uevent->cm_id); |
171 | ||
172 | kfree(uevent); | |
f469b262 | 173 | mutex_lock(&ctx->file->file_mutex); |
a5b74540 | 174 | } |
4be10c1e | 175 | mutex_unlock(&ctx->file->file_mutex); |
a5b74540 HR |
176 | } |
177 | ||
178 | static struct ib_ucm_context *ib_ucm_ctx_alloc(struct ib_ucm_file *file) | |
179 | { | |
180 | struct ib_ucm_context *ctx; | |
181 | int result; | |
182 | ||
de6eb66b | 183 | ctx = kzalloc(sizeof *ctx, GFP_KERNEL); |
a5b74540 HR |
184 | if (!ctx) |
185 | return NULL; | |
186 | ||
b9ef520f | 187 | atomic_set(&ctx->ref, 1); |
1b52fa98 | 188 | init_completion(&ctx->comp); |
a5b74540 | 189 | ctx->file = file; |
a5b74540 | 190 | INIT_LIST_HEAD(&ctx->events); |
a5b74540 | 191 | |
0b2b35f6 SH |
192 | do { |
193 | result = idr_pre_get(&ctx_id_table, GFP_KERNEL); | |
194 | if (!result) | |
195 | goto error; | |
a5b74540 | 196 | |
95ed644f | 197 | mutex_lock(&ctx_id_mutex); |
0b2b35f6 | 198 | result = idr_get_new(&ctx_id_table, ctx, &ctx->id); |
95ed644f | 199 | mutex_unlock(&ctx_id_mutex); |
0b2b35f6 | 200 | } while (result == -EAGAIN); |
a5b74540 | 201 | |
a5b74540 HR |
202 | if (result) |
203 | goto error; | |
204 | ||
0b2b35f6 | 205 | list_add_tail(&ctx->file_list, &file->ctxs); |
a5b74540 | 206 | return ctx; |
0b2b35f6 | 207 | |
a5b74540 | 208 | error: |
a5b74540 | 209 | kfree(ctx); |
a5b74540 HR |
210 | return NULL; |
211 | } | |
07d357d0 | 212 | |
0b2b35f6 | 213 | static void ib_ucm_event_req_get(struct ib_ucm_req_event_resp *ureq, |
a5b74540 HR |
214 | struct ib_cm_req_event_param *kreq) |
215 | { | |
a5b74540 HR |
216 | ureq->remote_ca_guid = kreq->remote_ca_guid; |
217 | ureq->remote_qkey = kreq->remote_qkey; | |
218 | ureq->remote_qpn = kreq->remote_qpn; | |
219 | ureq->qp_type = kreq->qp_type; | |
220 | ureq->starting_psn = kreq->starting_psn; | |
221 | ureq->responder_resources = kreq->responder_resources; | |
222 | ureq->initiator_depth = kreq->initiator_depth; | |
223 | ureq->local_cm_response_timeout = kreq->local_cm_response_timeout; | |
224 | ureq->flow_control = kreq->flow_control; | |
225 | ureq->remote_cm_response_timeout = kreq->remote_cm_response_timeout; | |
226 | ureq->retry_count = kreq->retry_count; | |
227 | ureq->rnr_retry_count = kreq->rnr_retry_count; | |
228 | ureq->srq = kreq->srq; | |
07d357d0 | 229 | ureq->port = kreq->port; |
a5b74540 | 230 | |
6a9af2e1 SH |
231 | ib_copy_path_rec_to_user(&ureq->primary_path, kreq->primary_path); |
232 | if (kreq->alternate_path) | |
233 | ib_copy_path_rec_to_user(&ureq->alternate_path, | |
234 | kreq->alternate_path); | |
a5b74540 HR |
235 | } |
236 | ||
237 | static void ib_ucm_event_rep_get(struct ib_ucm_rep_event_resp *urep, | |
238 | struct ib_cm_rep_event_param *krep) | |
239 | { | |
240 | urep->remote_ca_guid = krep->remote_ca_guid; | |
241 | urep->remote_qkey = krep->remote_qkey; | |
242 | urep->remote_qpn = krep->remote_qpn; | |
243 | urep->starting_psn = krep->starting_psn; | |
244 | urep->responder_resources = krep->responder_resources; | |
245 | urep->initiator_depth = krep->initiator_depth; | |
246 | urep->target_ack_delay = krep->target_ack_delay; | |
247 | urep->failover_accepted = krep->failover_accepted; | |
248 | urep->flow_control = krep->flow_control; | |
249 | urep->rnr_retry_count = krep->rnr_retry_count; | |
250 | urep->srq = krep->srq; | |
251 | } | |
252 | ||
a5b74540 HR |
253 | static void ib_ucm_event_sidr_rep_get(struct ib_ucm_sidr_rep_event_resp *urep, |
254 | struct ib_cm_sidr_rep_event_param *krep) | |
255 | { | |
256 | urep->status = krep->status; | |
257 | urep->qkey = krep->qkey; | |
258 | urep->qpn = krep->qpn; | |
259 | }; | |
260 | ||
0b2b35f6 | 261 | static int ib_ucm_event_process(struct ib_cm_event *evt, |
a5b74540 HR |
262 | struct ib_ucm_event *uvt) |
263 | { | |
264 | void *info = NULL; | |
a5b74540 HR |
265 | |
266 | switch (evt->event) { | |
267 | case IB_CM_REQ_RECEIVED: | |
0b2b35f6 | 268 | ib_ucm_event_req_get(&uvt->resp.u.req_resp, |
a5b74540 HR |
269 | &evt->param.req_rcvd); |
270 | uvt->data_len = IB_CM_REQ_PRIVATE_DATA_SIZE; | |
b9ef520f | 271 | uvt->resp.present = IB_UCM_PRES_PRIMARY; |
a5b74540 HR |
272 | uvt->resp.present |= (evt->param.req_rcvd.alternate_path ? |
273 | IB_UCM_PRES_ALTERNATE : 0); | |
274 | break; | |
275 | case IB_CM_REP_RECEIVED: | |
276 | ib_ucm_event_rep_get(&uvt->resp.u.rep_resp, | |
277 | &evt->param.rep_rcvd); | |
278 | uvt->data_len = IB_CM_REP_PRIVATE_DATA_SIZE; | |
a5b74540 HR |
279 | break; |
280 | case IB_CM_RTU_RECEIVED: | |
281 | uvt->data_len = IB_CM_RTU_PRIVATE_DATA_SIZE; | |
282 | uvt->resp.u.send_status = evt->param.send_status; | |
a5b74540 HR |
283 | break; |
284 | case IB_CM_DREQ_RECEIVED: | |
285 | uvt->data_len = IB_CM_DREQ_PRIVATE_DATA_SIZE; | |
286 | uvt->resp.u.send_status = evt->param.send_status; | |
a5b74540 HR |
287 | break; |
288 | case IB_CM_DREP_RECEIVED: | |
289 | uvt->data_len = IB_CM_DREP_PRIVATE_DATA_SIZE; | |
290 | uvt->resp.u.send_status = evt->param.send_status; | |
a5b74540 HR |
291 | break; |
292 | case IB_CM_MRA_RECEIVED: | |
b9ef520f SH |
293 | uvt->resp.u.mra_resp.timeout = |
294 | evt->param.mra_rcvd.service_timeout; | |
a5b74540 | 295 | uvt->data_len = IB_CM_MRA_PRIVATE_DATA_SIZE; |
a5b74540 HR |
296 | break; |
297 | case IB_CM_REJ_RECEIVED: | |
b9ef520f | 298 | uvt->resp.u.rej_resp.reason = evt->param.rej_rcvd.reason; |
a5b74540 HR |
299 | uvt->data_len = IB_CM_REJ_PRIVATE_DATA_SIZE; |
300 | uvt->info_len = evt->param.rej_rcvd.ari_length; | |
301 | info = evt->param.rej_rcvd.ari; | |
a5b74540 HR |
302 | break; |
303 | case IB_CM_LAP_RECEIVED: | |
6a9af2e1 SH |
304 | ib_copy_path_rec_to_user(&uvt->resp.u.lap_resp.path, |
305 | evt->param.lap_rcvd.alternate_path); | |
a5b74540 | 306 | uvt->data_len = IB_CM_LAP_PRIVATE_DATA_SIZE; |
b9ef520f | 307 | uvt->resp.present = IB_UCM_PRES_ALTERNATE; |
a5b74540 HR |
308 | break; |
309 | case IB_CM_APR_RECEIVED: | |
b9ef520f | 310 | uvt->resp.u.apr_resp.status = evt->param.apr_rcvd.ap_status; |
a5b74540 HR |
311 | uvt->data_len = IB_CM_APR_PRIVATE_DATA_SIZE; |
312 | uvt->info_len = evt->param.apr_rcvd.info_len; | |
313 | info = evt->param.apr_rcvd.apr_info; | |
a5b74540 HR |
314 | break; |
315 | case IB_CM_SIDR_REQ_RECEIVED: | |
3cd96564 | 316 | uvt->resp.u.sidr_req_resp.pkey = |
0b2b35f6 | 317 | evt->param.sidr_req_rcvd.pkey; |
3cd96564 | 318 | uvt->resp.u.sidr_req_resp.port = |
07d357d0 | 319 | evt->param.sidr_req_rcvd.port; |
a5b74540 | 320 | uvt->data_len = IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE; |
a5b74540 HR |
321 | break; |
322 | case IB_CM_SIDR_REP_RECEIVED: | |
323 | ib_ucm_event_sidr_rep_get(&uvt->resp.u.sidr_rep_resp, | |
324 | &evt->param.sidr_rep_rcvd); | |
325 | uvt->data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE; | |
326 | uvt->info_len = evt->param.sidr_rep_rcvd.info_len; | |
327 | info = evt->param.sidr_rep_rcvd.info; | |
a5b74540 HR |
328 | break; |
329 | default: | |
330 | uvt->resp.u.send_status = evt->param.send_status; | |
a5b74540 HR |
331 | break; |
332 | } | |
333 | ||
b9ef520f | 334 | if (uvt->data_len) { |
bed8bdfd | 335 | uvt->data = kmemdup(evt->private_data, uvt->data_len, GFP_KERNEL); |
b9ef520f SH |
336 | if (!uvt->data) |
337 | goto err1; | |
a5b74540 | 338 | |
a5b74540 HR |
339 | uvt->resp.present |= IB_UCM_PRES_DATA; |
340 | } | |
341 | ||
b9ef520f | 342 | if (uvt->info_len) { |
bed8bdfd | 343 | uvt->info = kmemdup(info, uvt->info_len, GFP_KERNEL); |
b9ef520f SH |
344 | if (!uvt->info) |
345 | goto err2; | |
a5b74540 | 346 | |
a5b74540 HR |
347 | uvt->resp.present |= IB_UCM_PRES_INFO; |
348 | } | |
a5b74540 | 349 | return 0; |
b9ef520f SH |
350 | |
351 | err2: | |
79d81907 | 352 | kfree(uvt->data); |
b9ef520f SH |
353 | err1: |
354 | return -ENOMEM; | |
a5b74540 HR |
355 | } |
356 | ||
357 | static int ib_ucm_event_handler(struct ib_cm_id *cm_id, | |
358 | struct ib_cm_event *event) | |
359 | { | |
360 | struct ib_ucm_event *uevent; | |
361 | struct ib_ucm_context *ctx; | |
362 | int result = 0; | |
a5b74540 | 363 | |
b9ef520f | 364 | ctx = cm_id->context; |
a5b74540 | 365 | |
de6eb66b | 366 | uevent = kzalloc(sizeof *uevent, GFP_KERNEL); |
b9ef520f SH |
367 | if (!uevent) |
368 | goto err1; | |
a5b74540 | 369 | |
0b2b35f6 SH |
370 | uevent->ctx = ctx; |
371 | uevent->cm_id = cm_id; | |
372 | uevent->resp.uid = ctx->uid; | |
373 | uevent->resp.id = ctx->id; | |
a5b74540 HR |
374 | uevent->resp.event = event->event; |
375 | ||
0b2b35f6 | 376 | result = ib_ucm_event_process(event, uevent); |
a5b74540 | 377 | if (result) |
b9ef520f | 378 | goto err2; |
a5b74540 | 379 | |
4be10c1e | 380 | mutex_lock(&ctx->file->file_mutex); |
a5b74540 HR |
381 | list_add_tail(&uevent->file_list, &ctx->file->events); |
382 | list_add_tail(&uevent->ctx_list, &ctx->events); | |
a5b74540 | 383 | wake_up_interruptible(&ctx->file->poll_wait); |
4be10c1e | 384 | mutex_unlock(&ctx->file->file_mutex); |
b9ef520f SH |
385 | return 0; |
386 | ||
387 | err2: | |
388 | kfree(uevent); | |
389 | err1: | |
390 | /* Destroy new cm_id's */ | |
0b2b35f6 | 391 | return ib_ucm_new_cm_id(event->event); |
a5b74540 HR |
392 | } |
393 | ||
394 | static ssize_t ib_ucm_event(struct ib_ucm_file *file, | |
395 | const char __user *inbuf, | |
396 | int in_len, int out_len) | |
397 | { | |
398 | struct ib_ucm_context *ctx; | |
399 | struct ib_ucm_event_get cmd; | |
0b2b35f6 | 400 | struct ib_ucm_event *uevent; |
a5b74540 HR |
401 | int result = 0; |
402 | DEFINE_WAIT(wait); | |
403 | ||
404 | if (out_len < sizeof(struct ib_ucm_event_resp)) | |
405 | return -ENOSPC; | |
406 | ||
407 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
408 | return -EFAULT; | |
07d357d0 | 409 | |
4be10c1e | 410 | mutex_lock(&file->file_mutex); |
a5b74540 | 411 | while (list_empty(&file->events)) { |
9d41b7fd | 412 | mutex_unlock(&file->file_mutex); |
a5b74540 | 413 | |
9d41b7fd SH |
414 | if (file->filp->f_flags & O_NONBLOCK) |
415 | return -EAGAIN; | |
a5b74540 | 416 | |
9d41b7fd SH |
417 | if (wait_event_interruptible(file->poll_wait, |
418 | !list_empty(&file->events))) | |
419 | return -ERESTARTSYS; | |
a5b74540 | 420 | |
4be10c1e | 421 | mutex_lock(&file->file_mutex); |
a5b74540 HR |
422 | } |
423 | ||
a5b74540 HR |
424 | uevent = list_entry(file->events.next, struct ib_ucm_event, file_list); |
425 | ||
0b2b35f6 SH |
426 | if (ib_ucm_new_cm_id(uevent->resp.event)) { |
427 | ctx = ib_ucm_ctx_alloc(file); | |
428 | if (!ctx) { | |
429 | result = -ENOMEM; | |
430 | goto done; | |
431 | } | |
a5b74540 | 432 | |
0b2b35f6 SH |
433 | ctx->cm_id = uevent->cm_id; |
434 | ctx->cm_id->context = ctx; | |
435 | uevent->resp.id = ctx->id; | |
a5b74540 HR |
436 | } |
437 | ||
a5b74540 HR |
438 | if (copy_to_user((void __user *)(unsigned long)cmd.response, |
439 | &uevent->resp, sizeof(uevent->resp))) { | |
440 | result = -EFAULT; | |
441 | goto done; | |
442 | } | |
443 | ||
444 | if (uevent->data) { | |
a5b74540 HR |
445 | if (cmd.data_len < uevent->data_len) { |
446 | result = -ENOMEM; | |
447 | goto done; | |
448 | } | |
a5b74540 HR |
449 | if (copy_to_user((void __user *)(unsigned long)cmd.data, |
450 | uevent->data, uevent->data_len)) { | |
451 | result = -EFAULT; | |
452 | goto done; | |
453 | } | |
454 | } | |
455 | ||
456 | if (uevent->info) { | |
a5b74540 HR |
457 | if (cmd.info_len < uevent->info_len) { |
458 | result = -ENOMEM; | |
459 | goto done; | |
460 | } | |
a5b74540 HR |
461 | if (copy_to_user((void __user *)(unsigned long)cmd.info, |
462 | uevent->info, uevent->info_len)) { | |
463 | result = -EFAULT; | |
464 | goto done; | |
465 | } | |
466 | } | |
467 | ||
468 | list_del(&uevent->file_list); | |
469 | list_del(&uevent->ctx_list); | |
0b2b35f6 | 470 | uevent->ctx->events_reported++; |
a5b74540 | 471 | |
79d81907 HR |
472 | kfree(uevent->data); |
473 | kfree(uevent->info); | |
a5b74540 HR |
474 | kfree(uevent); |
475 | done: | |
4be10c1e | 476 | mutex_unlock(&file->file_mutex); |
a5b74540 HR |
477 | return result; |
478 | } | |
479 | ||
a5b74540 HR |
480 | static ssize_t ib_ucm_create_id(struct ib_ucm_file *file, |
481 | const char __user *inbuf, | |
482 | int in_len, int out_len) | |
483 | { | |
484 | struct ib_ucm_create_id cmd; | |
485 | struct ib_ucm_create_id_resp resp; | |
486 | struct ib_ucm_context *ctx; | |
487 | int result; | |
488 | ||
489 | if (out_len < sizeof(resp)) | |
490 | return -ENOSPC; | |
491 | ||
492 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
493 | return -EFAULT; | |
494 | ||
4be10c1e | 495 | mutex_lock(&file->file_mutex); |
a5b74540 | 496 | ctx = ib_ucm_ctx_alloc(file); |
4be10c1e | 497 | mutex_unlock(&file->file_mutex); |
a5b74540 HR |
498 | if (!ctx) |
499 | return -ENOMEM; | |
500 | ||
0b2b35f6 | 501 | ctx->uid = cmd.uid; |
07d357d0 SH |
502 | ctx->cm_id = ib_create_cm_id(file->device->ib_dev, |
503 | ib_ucm_event_handler, ctx); | |
b9ef520f SH |
504 | if (IS_ERR(ctx->cm_id)) { |
505 | result = PTR_ERR(ctx->cm_id); | |
07d357d0 | 506 | goto err1; |
a5b74540 HR |
507 | } |
508 | ||
509 | resp.id = ctx->id; | |
510 | if (copy_to_user((void __user *)(unsigned long)cmd.response, | |
511 | &resp, sizeof(resp))) { | |
512 | result = -EFAULT; | |
07d357d0 | 513 | goto err2; |
a5b74540 | 514 | } |
a5b74540 | 515 | return 0; |
a5b74540 | 516 | |
07d357d0 SH |
517 | err2: |
518 | ib_destroy_cm_id(ctx->cm_id); | |
519 | err1: | |
95ed644f | 520 | mutex_lock(&ctx_id_mutex); |
0b2b35f6 | 521 | idr_remove(&ctx_id_table, ctx->id); |
95ed644f | 522 | mutex_unlock(&ctx_id_mutex); |
0b2b35f6 | 523 | kfree(ctx); |
a5b74540 HR |
524 | return result; |
525 | } | |
526 | ||
527 | static ssize_t ib_ucm_destroy_id(struct ib_ucm_file *file, | |
528 | const char __user *inbuf, | |
529 | int in_len, int out_len) | |
530 | { | |
531 | struct ib_ucm_destroy_id cmd; | |
0b2b35f6 SH |
532 | struct ib_ucm_destroy_id_resp resp; |
533 | struct ib_ucm_context *ctx; | |
534 | int result = 0; | |
535 | ||
536 | if (out_len < sizeof(resp)) | |
537 | return -ENOSPC; | |
a5b74540 HR |
538 | |
539 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
540 | return -EFAULT; | |
541 | ||
95ed644f | 542 | mutex_lock(&ctx_id_mutex); |
0b2b35f6 SH |
543 | ctx = idr_find(&ctx_id_table, cmd.id); |
544 | if (!ctx) | |
545 | ctx = ERR_PTR(-ENOENT); | |
546 | else if (ctx->file != file) | |
547 | ctx = ERR_PTR(-EINVAL); | |
548 | else | |
549 | idr_remove(&ctx_id_table, ctx->id); | |
95ed644f | 550 | mutex_unlock(&ctx_id_mutex); |
0b2b35f6 SH |
551 | |
552 | if (IS_ERR(ctx)) | |
553 | return PTR_ERR(ctx); | |
554 | ||
1b52fa98 SH |
555 | ib_ucm_ctx_put(ctx); |
556 | wait_for_completion(&ctx->comp); | |
0b2b35f6 SH |
557 | |
558 | /* No new events will be generated after destroying the cm_id. */ | |
559 | ib_destroy_cm_id(ctx->cm_id); | |
560 | /* Cleanup events not yet reported to the user. */ | |
561 | ib_ucm_cleanup_events(ctx); | |
562 | ||
563 | resp.events_reported = ctx->events_reported; | |
564 | if (copy_to_user((void __user *)(unsigned long)cmd.response, | |
565 | &resp, sizeof(resp))) | |
566 | result = -EFAULT; | |
567 | ||
568 | kfree(ctx); | |
569 | return result; | |
a5b74540 HR |
570 | } |
571 | ||
572 | static ssize_t ib_ucm_attr_id(struct ib_ucm_file *file, | |
573 | const char __user *inbuf, | |
574 | int in_len, int out_len) | |
575 | { | |
576 | struct ib_ucm_attr_id_resp resp; | |
577 | struct ib_ucm_attr_id cmd; | |
578 | struct ib_ucm_context *ctx; | |
579 | int result = 0; | |
580 | ||
581 | if (out_len < sizeof(resp)) | |
582 | return -ENOSPC; | |
583 | ||
584 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
585 | return -EFAULT; | |
586 | ||
b9ef520f SH |
587 | ctx = ib_ucm_ctx_get(file, cmd.id); |
588 | if (IS_ERR(ctx)) | |
589 | return PTR_ERR(ctx); | |
a5b74540 HR |
590 | |
591 | resp.service_id = ctx->cm_id->service_id; | |
592 | resp.service_mask = ctx->cm_id->service_mask; | |
593 | resp.local_id = ctx->cm_id->local_id; | |
594 | resp.remote_id = ctx->cm_id->remote_id; | |
595 | ||
596 | if (copy_to_user((void __user *)(unsigned long)cmd.response, | |
597 | &resp, sizeof(resp))) | |
598 | result = -EFAULT; | |
599 | ||
b9ef520f | 600 | ib_ucm_ctx_put(ctx); |
a5b74540 HR |
601 | return result; |
602 | } | |
603 | ||
0b2b35f6 SH |
604 | static ssize_t ib_ucm_init_qp_attr(struct ib_ucm_file *file, |
605 | const char __user *inbuf, | |
606 | int in_len, int out_len) | |
607 | { | |
6a9af2e1 | 608 | struct ib_uverbs_qp_attr resp; |
0b2b35f6 SH |
609 | struct ib_ucm_init_qp_attr cmd; |
610 | struct ib_ucm_context *ctx; | |
611 | struct ib_qp_attr qp_attr; | |
612 | int result = 0; | |
613 | ||
614 | if (out_len < sizeof(resp)) | |
615 | return -ENOSPC; | |
616 | ||
617 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
618 | return -EFAULT; | |
619 | ||
620 | ctx = ib_ucm_ctx_get(file, cmd.id); | |
621 | if (IS_ERR(ctx)) | |
622 | return PTR_ERR(ctx); | |
623 | ||
624 | resp.qp_attr_mask = 0; | |
625 | memset(&qp_attr, 0, sizeof qp_attr); | |
626 | qp_attr.qp_state = cmd.qp_state; | |
627 | result = ib_cm_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask); | |
628 | if (result) | |
629 | goto out; | |
630 | ||
6a9af2e1 | 631 | ib_copy_qp_attr_to_user(&resp, &qp_attr); |
0b2b35f6 SH |
632 | |
633 | if (copy_to_user((void __user *)(unsigned long)cmd.response, | |
634 | &resp, sizeof(resp))) | |
635 | result = -EFAULT; | |
636 | ||
637 | out: | |
638 | ib_ucm_ctx_put(ctx); | |
639 | return result; | |
640 | } | |
641 | ||
6e61d04f SH |
642 | static int ucm_validate_listen(__be64 service_id, __be64 service_mask) |
643 | { | |
644 | service_id &= service_mask; | |
645 | ||
646 | if (((service_id & IB_CMA_SERVICE_ID_MASK) == IB_CMA_SERVICE_ID) || | |
647 | ((service_id & IB_SDP_SERVICE_ID_MASK) == IB_SDP_SERVICE_ID)) | |
648 | return -EINVAL; | |
649 | ||
650 | return 0; | |
651 | } | |
652 | ||
a5b74540 HR |
653 | static ssize_t ib_ucm_listen(struct ib_ucm_file *file, |
654 | const char __user *inbuf, | |
655 | int in_len, int out_len) | |
656 | { | |
657 | struct ib_ucm_listen cmd; | |
658 | struct ib_ucm_context *ctx; | |
659 | int result; | |
660 | ||
661 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
662 | return -EFAULT; | |
663 | ||
b9ef520f SH |
664 | ctx = ib_ucm_ctx_get(file, cmd.id); |
665 | if (IS_ERR(ctx)) | |
666 | return PTR_ERR(ctx); | |
a5b74540 | 667 | |
6e61d04f SH |
668 | result = ucm_validate_listen(cmd.service_id, cmd.service_mask); |
669 | if (result) | |
670 | goto out; | |
671 | ||
672 | result = ib_cm_listen(ctx->cm_id, cmd.service_id, cmd.service_mask, | |
673 | NULL); | |
674 | out: | |
b9ef520f | 675 | ib_ucm_ctx_put(ctx); |
a5b74540 HR |
676 | return result; |
677 | } | |
678 | ||
e1444b5a SH |
679 | static ssize_t ib_ucm_notify(struct ib_ucm_file *file, |
680 | const char __user *inbuf, | |
681 | int in_len, int out_len) | |
a5b74540 | 682 | { |
e1444b5a | 683 | struct ib_ucm_notify cmd; |
a5b74540 HR |
684 | struct ib_ucm_context *ctx; |
685 | int result; | |
686 | ||
687 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
688 | return -EFAULT; | |
689 | ||
b9ef520f SH |
690 | ctx = ib_ucm_ctx_get(file, cmd.id); |
691 | if (IS_ERR(ctx)) | |
692 | return PTR_ERR(ctx); | |
a5b74540 | 693 | |
e1444b5a | 694 | result = ib_cm_notify(ctx->cm_id, (enum ib_event_type) cmd.event); |
b9ef520f | 695 | ib_ucm_ctx_put(ctx); |
a5b74540 HR |
696 | return result; |
697 | } | |
698 | ||
699 | static int ib_ucm_alloc_data(const void **dest, u64 src, u32 len) | |
700 | { | |
701 | void *data; | |
702 | ||
703 | *dest = NULL; | |
704 | ||
705 | if (!len) | |
706 | return 0; | |
707 | ||
708 | data = kmalloc(len, GFP_KERNEL); | |
709 | if (!data) | |
710 | return -ENOMEM; | |
711 | ||
712 | if (copy_from_user(data, (void __user *)(unsigned long)src, len)) { | |
713 | kfree(data); | |
714 | return -EFAULT; | |
715 | } | |
716 | ||
717 | *dest = data; | |
718 | return 0; | |
719 | } | |
720 | ||
721 | static int ib_ucm_path_get(struct ib_sa_path_rec **path, u64 src) | |
722 | { | |
6a9af2e1 | 723 | struct ib_user_path_rec upath; |
a5b74540 HR |
724 | struct ib_sa_path_rec *sa_path; |
725 | ||
726 | *path = NULL; | |
727 | ||
728 | if (!src) | |
729 | return 0; | |
730 | ||
731 | sa_path = kmalloc(sizeof(*sa_path), GFP_KERNEL); | |
732 | if (!sa_path) | |
733 | return -ENOMEM; | |
734 | ||
6a9af2e1 SH |
735 | if (copy_from_user(&upath, (void __user *)(unsigned long)src, |
736 | sizeof(upath))) { | |
a5b74540 HR |
737 | |
738 | kfree(sa_path); | |
739 | return -EFAULT; | |
740 | } | |
741 | ||
6a9af2e1 | 742 | ib_copy_path_rec_from_user(sa_path, &upath); |
a5b74540 HR |
743 | *path = sa_path; |
744 | return 0; | |
745 | } | |
746 | ||
747 | static ssize_t ib_ucm_send_req(struct ib_ucm_file *file, | |
748 | const char __user *inbuf, | |
749 | int in_len, int out_len) | |
750 | { | |
751 | struct ib_cm_req_param param; | |
752 | struct ib_ucm_context *ctx; | |
753 | struct ib_ucm_req cmd; | |
754 | int result; | |
755 | ||
756 | param.private_data = NULL; | |
757 | param.primary_path = NULL; | |
758 | param.alternate_path = NULL; | |
759 | ||
760 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
761 | return -EFAULT; | |
762 | ||
763 | result = ib_ucm_alloc_data(¶m.private_data, cmd.data, cmd.len); | |
764 | if (result) | |
765 | goto done; | |
766 | ||
767 | result = ib_ucm_path_get(¶m.primary_path, cmd.primary_path); | |
768 | if (result) | |
769 | goto done; | |
770 | ||
771 | result = ib_ucm_path_get(¶m.alternate_path, cmd.alternate_path); | |
772 | if (result) | |
773 | goto done; | |
774 | ||
775 | param.private_data_len = cmd.len; | |
776 | param.service_id = cmd.sid; | |
777 | param.qp_num = cmd.qpn; | |
778 | param.qp_type = cmd.qp_type; | |
779 | param.starting_psn = cmd.psn; | |
780 | param.peer_to_peer = cmd.peer_to_peer; | |
781 | param.responder_resources = cmd.responder_resources; | |
782 | param.initiator_depth = cmd.initiator_depth; | |
783 | param.remote_cm_response_timeout = cmd.remote_cm_response_timeout; | |
784 | param.flow_control = cmd.flow_control; | |
785 | param.local_cm_response_timeout = cmd.local_cm_response_timeout; | |
786 | param.retry_count = cmd.retry_count; | |
787 | param.rnr_retry_count = cmd.rnr_retry_count; | |
788 | param.max_cm_retries = cmd.max_cm_retries; | |
789 | param.srq = cmd.srq; | |
790 | ||
b9ef520f SH |
791 | ctx = ib_ucm_ctx_get(file, cmd.id); |
792 | if (!IS_ERR(ctx)) { | |
a5b74540 | 793 | result = ib_send_cm_req(ctx->cm_id, ¶m); |
b9ef520f SH |
794 | ib_ucm_ctx_put(ctx); |
795 | } else | |
796 | result = PTR_ERR(ctx); | |
a5b74540 | 797 | |
a5b74540 | 798 | done: |
79d81907 HR |
799 | kfree(param.private_data); |
800 | kfree(param.primary_path); | |
801 | kfree(param.alternate_path); | |
a5b74540 HR |
802 | return result; |
803 | } | |
804 | ||
805 | static ssize_t ib_ucm_send_rep(struct ib_ucm_file *file, | |
806 | const char __user *inbuf, | |
807 | int in_len, int out_len) | |
808 | { | |
809 | struct ib_cm_rep_param param; | |
810 | struct ib_ucm_context *ctx; | |
811 | struct ib_ucm_rep cmd; | |
812 | int result; | |
813 | ||
814 | param.private_data = NULL; | |
815 | ||
816 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
817 | return -EFAULT; | |
818 | ||
819 | result = ib_ucm_alloc_data(¶m.private_data, cmd.data, cmd.len); | |
820 | if (result) | |
821 | return result; | |
822 | ||
823 | param.qp_num = cmd.qpn; | |
824 | param.starting_psn = cmd.psn; | |
825 | param.private_data_len = cmd.len; | |
826 | param.responder_resources = cmd.responder_resources; | |
827 | param.initiator_depth = cmd.initiator_depth; | |
a5b74540 HR |
828 | param.failover_accepted = cmd.failover_accepted; |
829 | param.flow_control = cmd.flow_control; | |
830 | param.rnr_retry_count = cmd.rnr_retry_count; | |
831 | param.srq = cmd.srq; | |
832 | ||
b9ef520f SH |
833 | ctx = ib_ucm_ctx_get(file, cmd.id); |
834 | if (!IS_ERR(ctx)) { | |
0b2b35f6 | 835 | ctx->uid = cmd.uid; |
a5b74540 | 836 | result = ib_send_cm_rep(ctx->cm_id, ¶m); |
b9ef520f SH |
837 | ib_ucm_ctx_put(ctx); |
838 | } else | |
839 | result = PTR_ERR(ctx); | |
a5b74540 | 840 | |
79d81907 | 841 | kfree(param.private_data); |
a5b74540 HR |
842 | return result; |
843 | } | |
844 | ||
845 | static ssize_t ib_ucm_send_private_data(struct ib_ucm_file *file, | |
846 | const char __user *inbuf, int in_len, | |
847 | int (*func)(struct ib_cm_id *cm_id, | |
848 | const void *private_data, | |
849 | u8 private_data_len)) | |
850 | { | |
851 | struct ib_ucm_private_data cmd; | |
852 | struct ib_ucm_context *ctx; | |
853 | const void *private_data = NULL; | |
854 | int result; | |
855 | ||
856 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
857 | return -EFAULT; | |
858 | ||
859 | result = ib_ucm_alloc_data(&private_data, cmd.data, cmd.len); | |
860 | if (result) | |
861 | return result; | |
862 | ||
b9ef520f SH |
863 | ctx = ib_ucm_ctx_get(file, cmd.id); |
864 | if (!IS_ERR(ctx)) { | |
a5b74540 | 865 | result = func(ctx->cm_id, private_data, cmd.len); |
b9ef520f SH |
866 | ib_ucm_ctx_put(ctx); |
867 | } else | |
868 | result = PTR_ERR(ctx); | |
a5b74540 | 869 | |
79d81907 | 870 | kfree(private_data); |
a5b74540 HR |
871 | return result; |
872 | } | |
873 | ||
874 | static ssize_t ib_ucm_send_rtu(struct ib_ucm_file *file, | |
875 | const char __user *inbuf, | |
876 | int in_len, int out_len) | |
877 | { | |
878 | return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_rtu); | |
879 | } | |
880 | ||
881 | static ssize_t ib_ucm_send_dreq(struct ib_ucm_file *file, | |
882 | const char __user *inbuf, | |
883 | int in_len, int out_len) | |
884 | { | |
885 | return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_dreq); | |
886 | } | |
887 | ||
888 | static ssize_t ib_ucm_send_drep(struct ib_ucm_file *file, | |
889 | const char __user *inbuf, | |
890 | int in_len, int out_len) | |
891 | { | |
892 | return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_drep); | |
893 | } | |
894 | ||
895 | static ssize_t ib_ucm_send_info(struct ib_ucm_file *file, | |
896 | const char __user *inbuf, int in_len, | |
897 | int (*func)(struct ib_cm_id *cm_id, | |
898 | int status, | |
899 | const void *info, | |
900 | u8 info_len, | |
901 | const void *data, | |
902 | u8 data_len)) | |
903 | { | |
904 | struct ib_ucm_context *ctx; | |
905 | struct ib_ucm_info cmd; | |
906 | const void *data = NULL; | |
907 | const void *info = NULL; | |
908 | int result; | |
909 | ||
910 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
911 | return -EFAULT; | |
912 | ||
913 | result = ib_ucm_alloc_data(&data, cmd.data, cmd.data_len); | |
914 | if (result) | |
915 | goto done; | |
916 | ||
917 | result = ib_ucm_alloc_data(&info, cmd.info, cmd.info_len); | |
918 | if (result) | |
919 | goto done; | |
920 | ||
b9ef520f SH |
921 | ctx = ib_ucm_ctx_get(file, cmd.id); |
922 | if (!IS_ERR(ctx)) { | |
923 | result = func(ctx->cm_id, cmd.status, info, cmd.info_len, | |
a5b74540 | 924 | data, cmd.data_len); |
b9ef520f SH |
925 | ib_ucm_ctx_put(ctx); |
926 | } else | |
927 | result = PTR_ERR(ctx); | |
a5b74540 | 928 | |
a5b74540 | 929 | done: |
79d81907 HR |
930 | kfree(data); |
931 | kfree(info); | |
a5b74540 HR |
932 | return result; |
933 | } | |
934 | ||
935 | static ssize_t ib_ucm_send_rej(struct ib_ucm_file *file, | |
936 | const char __user *inbuf, | |
937 | int in_len, int out_len) | |
938 | { | |
939 | return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_rej); | |
940 | } | |
941 | ||
942 | static ssize_t ib_ucm_send_apr(struct ib_ucm_file *file, | |
943 | const char __user *inbuf, | |
944 | int in_len, int out_len) | |
945 | { | |
946 | return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_apr); | |
947 | } | |
948 | ||
949 | static ssize_t ib_ucm_send_mra(struct ib_ucm_file *file, | |
950 | const char __user *inbuf, | |
951 | int in_len, int out_len) | |
952 | { | |
953 | struct ib_ucm_context *ctx; | |
954 | struct ib_ucm_mra cmd; | |
955 | const void *data = NULL; | |
956 | int result; | |
957 | ||
958 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
959 | return -EFAULT; | |
960 | ||
961 | result = ib_ucm_alloc_data(&data, cmd.data, cmd.len); | |
962 | if (result) | |
963 | return result; | |
964 | ||
b9ef520f SH |
965 | ctx = ib_ucm_ctx_get(file, cmd.id); |
966 | if (!IS_ERR(ctx)) { | |
967 | result = ib_send_cm_mra(ctx->cm_id, cmd.timeout, data, cmd.len); | |
968 | ib_ucm_ctx_put(ctx); | |
969 | } else | |
970 | result = PTR_ERR(ctx); | |
a5b74540 | 971 | |
79d81907 | 972 | kfree(data); |
a5b74540 HR |
973 | return result; |
974 | } | |
975 | ||
976 | static ssize_t ib_ucm_send_lap(struct ib_ucm_file *file, | |
977 | const char __user *inbuf, | |
978 | int in_len, int out_len) | |
979 | { | |
980 | struct ib_ucm_context *ctx; | |
981 | struct ib_sa_path_rec *path = NULL; | |
982 | struct ib_ucm_lap cmd; | |
983 | const void *data = NULL; | |
984 | int result; | |
985 | ||
986 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
987 | return -EFAULT; | |
988 | ||
989 | result = ib_ucm_alloc_data(&data, cmd.data, cmd.len); | |
990 | if (result) | |
991 | goto done; | |
992 | ||
993 | result = ib_ucm_path_get(&path, cmd.path); | |
994 | if (result) | |
995 | goto done; | |
996 | ||
b9ef520f SH |
997 | ctx = ib_ucm_ctx_get(file, cmd.id); |
998 | if (!IS_ERR(ctx)) { | |
a5b74540 | 999 | result = ib_send_cm_lap(ctx->cm_id, path, data, cmd.len); |
b9ef520f SH |
1000 | ib_ucm_ctx_put(ctx); |
1001 | } else | |
1002 | result = PTR_ERR(ctx); | |
a5b74540 | 1003 | |
a5b74540 | 1004 | done: |
79d81907 HR |
1005 | kfree(data); |
1006 | kfree(path); | |
a5b74540 HR |
1007 | return result; |
1008 | } | |
1009 | ||
1010 | static ssize_t ib_ucm_send_sidr_req(struct ib_ucm_file *file, | |
1011 | const char __user *inbuf, | |
1012 | int in_len, int out_len) | |
1013 | { | |
1014 | struct ib_cm_sidr_req_param param; | |
1015 | struct ib_ucm_context *ctx; | |
1016 | struct ib_ucm_sidr_req cmd; | |
1017 | int result; | |
1018 | ||
1019 | param.private_data = NULL; | |
1020 | param.path = NULL; | |
1021 | ||
1022 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
1023 | return -EFAULT; | |
1024 | ||
1025 | result = ib_ucm_alloc_data(¶m.private_data, cmd.data, cmd.len); | |
1026 | if (result) | |
1027 | goto done; | |
1028 | ||
1029 | result = ib_ucm_path_get(¶m.path, cmd.path); | |
1030 | if (result) | |
1031 | goto done; | |
1032 | ||
1033 | param.private_data_len = cmd.len; | |
1034 | param.service_id = cmd.sid; | |
1035 | param.timeout_ms = cmd.timeout; | |
1036 | param.max_cm_retries = cmd.max_cm_retries; | |
a5b74540 | 1037 | |
b9ef520f SH |
1038 | ctx = ib_ucm_ctx_get(file, cmd.id); |
1039 | if (!IS_ERR(ctx)) { | |
a5b74540 | 1040 | result = ib_send_cm_sidr_req(ctx->cm_id, ¶m); |
b9ef520f SH |
1041 | ib_ucm_ctx_put(ctx); |
1042 | } else | |
1043 | result = PTR_ERR(ctx); | |
a5b74540 | 1044 | |
a5b74540 | 1045 | done: |
79d81907 HR |
1046 | kfree(param.private_data); |
1047 | kfree(param.path); | |
a5b74540 HR |
1048 | return result; |
1049 | } | |
1050 | ||
1051 | static ssize_t ib_ucm_send_sidr_rep(struct ib_ucm_file *file, | |
1052 | const char __user *inbuf, | |
1053 | int in_len, int out_len) | |
1054 | { | |
1055 | struct ib_cm_sidr_rep_param param; | |
1056 | struct ib_ucm_sidr_rep cmd; | |
1057 | struct ib_ucm_context *ctx; | |
1058 | int result; | |
1059 | ||
1060 | param.info = NULL; | |
1061 | ||
1062 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
1063 | return -EFAULT; | |
1064 | ||
1065 | result = ib_ucm_alloc_data(¶m.private_data, | |
1066 | cmd.data, cmd.data_len); | |
1067 | if (result) | |
1068 | goto done; | |
1069 | ||
1070 | result = ib_ucm_alloc_data(¶m.info, cmd.info, cmd.info_len); | |
1071 | if (result) | |
1072 | goto done; | |
1073 | ||
b9ef520f SH |
1074 | param.qp_num = cmd.qpn; |
1075 | param.qkey = cmd.qkey; | |
1076 | param.status = cmd.status; | |
1077 | param.info_length = cmd.info_len; | |
1078 | param.private_data_len = cmd.data_len; | |
a5b74540 | 1079 | |
b9ef520f SH |
1080 | ctx = ib_ucm_ctx_get(file, cmd.id); |
1081 | if (!IS_ERR(ctx)) { | |
a5b74540 | 1082 | result = ib_send_cm_sidr_rep(ctx->cm_id, ¶m); |
b9ef520f SH |
1083 | ib_ucm_ctx_put(ctx); |
1084 | } else | |
1085 | result = PTR_ERR(ctx); | |
a5b74540 | 1086 | |
a5b74540 | 1087 | done: |
79d81907 HR |
1088 | kfree(param.private_data); |
1089 | kfree(param.info); | |
a5b74540 HR |
1090 | return result; |
1091 | } | |
1092 | ||
1093 | static ssize_t (*ucm_cmd_table[])(struct ib_ucm_file *file, | |
1094 | const char __user *inbuf, | |
1095 | int in_len, int out_len) = { | |
1096 | [IB_USER_CM_CMD_CREATE_ID] = ib_ucm_create_id, | |
1097 | [IB_USER_CM_CMD_DESTROY_ID] = ib_ucm_destroy_id, | |
1098 | [IB_USER_CM_CMD_ATTR_ID] = ib_ucm_attr_id, | |
1099 | [IB_USER_CM_CMD_LISTEN] = ib_ucm_listen, | |
e1444b5a | 1100 | [IB_USER_CM_CMD_NOTIFY] = ib_ucm_notify, |
a5b74540 HR |
1101 | [IB_USER_CM_CMD_SEND_REQ] = ib_ucm_send_req, |
1102 | [IB_USER_CM_CMD_SEND_REP] = ib_ucm_send_rep, | |
1103 | [IB_USER_CM_CMD_SEND_RTU] = ib_ucm_send_rtu, | |
1104 | [IB_USER_CM_CMD_SEND_DREQ] = ib_ucm_send_dreq, | |
1105 | [IB_USER_CM_CMD_SEND_DREP] = ib_ucm_send_drep, | |
1106 | [IB_USER_CM_CMD_SEND_REJ] = ib_ucm_send_rej, | |
1107 | [IB_USER_CM_CMD_SEND_MRA] = ib_ucm_send_mra, | |
1108 | [IB_USER_CM_CMD_SEND_LAP] = ib_ucm_send_lap, | |
1109 | [IB_USER_CM_CMD_SEND_APR] = ib_ucm_send_apr, | |
1110 | [IB_USER_CM_CMD_SEND_SIDR_REQ] = ib_ucm_send_sidr_req, | |
1111 | [IB_USER_CM_CMD_SEND_SIDR_REP] = ib_ucm_send_sidr_rep, | |
1112 | [IB_USER_CM_CMD_EVENT] = ib_ucm_event, | |
0b2b35f6 | 1113 | [IB_USER_CM_CMD_INIT_QP_ATTR] = ib_ucm_init_qp_attr, |
a5b74540 HR |
1114 | }; |
1115 | ||
1116 | static ssize_t ib_ucm_write(struct file *filp, const char __user *buf, | |
1117 | size_t len, loff_t *pos) | |
1118 | { | |
1119 | struct ib_ucm_file *file = filp->private_data; | |
1120 | struct ib_ucm_cmd_hdr hdr; | |
1121 | ssize_t result; | |
1122 | ||
1123 | if (len < sizeof(hdr)) | |
1124 | return -EINVAL; | |
1125 | ||
1126 | if (copy_from_user(&hdr, buf, sizeof(hdr))) | |
1127 | return -EFAULT; | |
1128 | ||
a5b74540 HR |
1129 | if (hdr.cmd < 0 || hdr.cmd >= ARRAY_SIZE(ucm_cmd_table)) |
1130 | return -EINVAL; | |
1131 | ||
1132 | if (hdr.in + sizeof(hdr) > len) | |
1133 | return -EINVAL; | |
1134 | ||
1135 | result = ucm_cmd_table[hdr.cmd](file, buf + sizeof(hdr), | |
1136 | hdr.in, hdr.out); | |
1137 | if (!result) | |
1138 | result = len; | |
1139 | ||
1140 | return result; | |
1141 | } | |
1142 | ||
1143 | static unsigned int ib_ucm_poll(struct file *filp, | |
1144 | struct poll_table_struct *wait) | |
1145 | { | |
1146 | struct ib_ucm_file *file = filp->private_data; | |
1147 | unsigned int mask = 0; | |
1148 | ||
1149 | poll_wait(filp, &file->poll_wait, wait); | |
1150 | ||
1151 | if (!list_empty(&file->events)) | |
1152 | mask = POLLIN | POLLRDNORM; | |
1153 | ||
1154 | return mask; | |
1155 | } | |
1156 | ||
5ba18b18 RD |
1157 | /* |
1158 | * ib_ucm_open() does not need the BKL: | |
1159 | * | |
1160 | * - no global state is referred to; | |
1161 | * - there is no ioctl method to race against; | |
1162 | * - no further module initialization is required for open to work | |
1163 | * after the device is registered. | |
1164 | */ | |
a5b74540 HR |
1165 | static int ib_ucm_open(struct inode *inode, struct file *filp) |
1166 | { | |
1167 | struct ib_ucm_file *file; | |
1168 | ||
1169 | file = kmalloc(sizeof(*file), GFP_KERNEL); | |
1170 | if (!file) | |
1171 | return -ENOMEM; | |
1172 | ||
1173 | INIT_LIST_HEAD(&file->events); | |
1174 | INIT_LIST_HEAD(&file->ctxs); | |
1175 | init_waitqueue_head(&file->poll_wait); | |
1176 | ||
4be10c1e | 1177 | mutex_init(&file->file_mutex); |
a5b74540 HR |
1178 | |
1179 | filp->private_data = file; | |
1180 | file->filp = filp; | |
f4e91eb4 | 1181 | file->device = container_of(inode->i_cdev, struct ib_ucm_device, cdev); |
a5b74540 | 1182 | |
a5b74540 HR |
1183 | return 0; |
1184 | } | |
1185 | ||
1186 | static int ib_ucm_close(struct inode *inode, struct file *filp) | |
1187 | { | |
1188 | struct ib_ucm_file *file = filp->private_data; | |
1189 | struct ib_ucm_context *ctx; | |
1190 | ||
4be10c1e | 1191 | mutex_lock(&file->file_mutex); |
a5b74540 | 1192 | while (!list_empty(&file->ctxs)) { |
a5b74540 HR |
1193 | ctx = list_entry(file->ctxs.next, |
1194 | struct ib_ucm_context, file_list); | |
4be10c1e | 1195 | mutex_unlock(&file->file_mutex); |
0b2b35f6 | 1196 | |
95ed644f | 1197 | mutex_lock(&ctx_id_mutex); |
0b2b35f6 | 1198 | idr_remove(&ctx_id_table, ctx->id); |
95ed644f | 1199 | mutex_unlock(&ctx_id_mutex); |
0b2b35f6 SH |
1200 | |
1201 | ib_destroy_cm_id(ctx->cm_id); | |
1202 | ib_ucm_cleanup_events(ctx); | |
1203 | kfree(ctx); | |
1204 | ||
4be10c1e | 1205 | mutex_lock(&file->file_mutex); |
a5b74540 | 1206 | } |
4be10c1e | 1207 | mutex_unlock(&file->file_mutex); |
a5b74540 | 1208 | kfree(file); |
a5b74540 HR |
1209 | return 0; |
1210 | } | |
1211 | ||
f4e91eb4 | 1212 | static void ib_ucm_release_dev(struct device *dev) |
07d357d0 | 1213 | { |
f4e91eb4 | 1214 | struct ib_ucm_device *ucm_dev; |
07d357d0 | 1215 | |
f4e91eb4 TJ |
1216 | ucm_dev = container_of(dev, struct ib_ucm_device, dev); |
1217 | cdev_del(&ucm_dev->cdev); | |
1218 | clear_bit(ucm_dev->devnum, dev_map); | |
1219 | kfree(ucm_dev); | |
07d357d0 SH |
1220 | } |
1221 | ||
2b8693c0 | 1222 | static const struct file_operations ucm_fops = { |
a5b74540 HR |
1223 | .owner = THIS_MODULE, |
1224 | .open = ib_ucm_open, | |
1225 | .release = ib_ucm_close, | |
1226 | .write = ib_ucm_write, | |
1227 | .poll = ib_ucm_poll, | |
1228 | }; | |
1229 | ||
f4e91eb4 TJ |
1230 | static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr, |
1231 | char *buf) | |
07d357d0 | 1232 | { |
f4e91eb4 | 1233 | struct ib_ucm_device *ucm_dev; |
3cd96564 | 1234 | |
f4e91eb4 TJ |
1235 | ucm_dev = container_of(dev, struct ib_ucm_device, dev); |
1236 | return sprintf(buf, "%s\n", ucm_dev->ib_dev->name); | |
07d357d0 | 1237 | } |
f4e91eb4 | 1238 | static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL); |
a5b74540 | 1239 | |
07d357d0 | 1240 | static void ib_ucm_add_one(struct ib_device *device) |
a5b74540 | 1241 | { |
dd08f702 | 1242 | int devnum; |
31d14b6e | 1243 | dev_t base; |
07d357d0 | 1244 | struct ib_ucm_device *ucm_dev; |
a5b74540 | 1245 | |
07ebafba TT |
1246 | if (!device->alloc_ucontext || |
1247 | rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB) | |
07d357d0 SH |
1248 | return; |
1249 | ||
de6eb66b | 1250 | ucm_dev = kzalloc(sizeof *ucm_dev, GFP_KERNEL); |
07d357d0 SH |
1251 | if (!ucm_dev) |
1252 | return; | |
a5b74540 | 1253 | |
07d357d0 | 1254 | ucm_dev->ib_dev = device; |
a5b74540 | 1255 | |
dd08f702 AC |
1256 | devnum = find_first_zero_bit(dev_map, IB_UCM_MAX_DEVICES); |
1257 | if (devnum >= IB_UCM_MAX_DEVICES) | |
07d357d0 SH |
1258 | goto err; |
1259 | ||
dd08f702 | 1260 | ucm_dev->devnum = devnum; |
31d14b6e | 1261 | base = devnum + IB_UCM_BASE_DEV; |
dd08f702 | 1262 | set_bit(devnum, dev_map); |
07d357d0 | 1263 | |
f4e91eb4 TJ |
1264 | cdev_init(&ucm_dev->cdev, &ucm_fops); |
1265 | ucm_dev->cdev.owner = THIS_MODULE; | |
1266 | kobject_set_name(&ucm_dev->cdev.kobj, "ucm%d", ucm_dev->devnum); | |
31d14b6e | 1267 | if (cdev_add(&ucm_dev->cdev, base, 1)) |
07d357d0 SH |
1268 | goto err; |
1269 | ||
f4e91eb4 TJ |
1270 | ucm_dev->dev.class = &cm_class; |
1271 | ucm_dev->dev.parent = device->dma_device; | |
1272 | ucm_dev->dev.devt = ucm_dev->cdev.dev; | |
1273 | ucm_dev->dev.release = ib_ucm_release_dev; | |
d927e38c | 1274 | dev_set_name(&ucm_dev->dev, "ucm%d", ucm_dev->devnum); |
f4e91eb4 | 1275 | if (device_register(&ucm_dev->dev)) |
a5b74540 | 1276 | goto err_cdev; |
a5b74540 | 1277 | |
f4e91eb4 TJ |
1278 | if (device_create_file(&ucm_dev->dev, &dev_attr_ibdev)) |
1279 | goto err_dev; | |
07d357d0 SH |
1280 | |
1281 | ib_set_client_data(device, &ucm_client, ucm_dev); | |
1282 | return; | |
1283 | ||
f4e91eb4 TJ |
1284 | err_dev: |
1285 | device_unregister(&ucm_dev->dev); | |
07d357d0 | 1286 | err_cdev: |
f4e91eb4 | 1287 | cdev_del(&ucm_dev->cdev); |
dd08f702 | 1288 | clear_bit(devnum, dev_map); |
07d357d0 SH |
1289 | err: |
1290 | kfree(ucm_dev); | |
1291 | return; | |
1292 | } | |
1293 | ||
1294 | static void ib_ucm_remove_one(struct ib_device *device) | |
1295 | { | |
1296 | struct ib_ucm_device *ucm_dev = ib_get_client_data(device, &ucm_client); | |
1297 | ||
1298 | if (!ucm_dev) | |
1299 | return; | |
1300 | ||
f4e91eb4 | 1301 | device_unregister(&ucm_dev->dev); |
07d357d0 SH |
1302 | } |
1303 | ||
1304 | static ssize_t show_abi_version(struct class *class, char *buf) | |
1305 | { | |
1306 | return sprintf(buf, "%d\n", IB_USER_CM_ABI_VERSION); | |
1307 | } | |
1308 | static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL); | |
1309 | ||
1310 | static int __init ib_ucm_init(void) | |
1311 | { | |
1312 | int ret; | |
1313 | ||
1314 | ret = register_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES, | |
1315 | "infiniband_cm"); | |
1316 | if (ret) { | |
1317 | printk(KERN_ERR "ucm: couldn't register device number\n"); | |
9af57b7a | 1318 | goto error1; |
a5b74540 HR |
1319 | } |
1320 | ||
9af57b7a | 1321 | ret = class_create_file(&cm_class, &class_attr_abi_version); |
07d357d0 SH |
1322 | if (ret) { |
1323 | printk(KERN_ERR "ucm: couldn't create abi_version attribute\n"); | |
9af57b7a | 1324 | goto error2; |
07d357d0 | 1325 | } |
a5b74540 | 1326 | |
07d357d0 SH |
1327 | ret = ib_register_client(&ucm_client); |
1328 | if (ret) { | |
1329 | printk(KERN_ERR "ucm: couldn't register client\n"); | |
9af57b7a | 1330 | goto error3; |
07d357d0 | 1331 | } |
a5b74540 | 1332 | return 0; |
07d357d0 | 1333 | |
9af57b7a SH |
1334 | error3: |
1335 | class_remove_file(&cm_class, &class_attr_abi_version); | |
1336 | error2: | |
07d357d0 | 1337 | unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES); |
9af57b7a | 1338 | error1: |
07d357d0 | 1339 | return ret; |
a5b74540 HR |
1340 | } |
1341 | ||
1342 | static void __exit ib_ucm_cleanup(void) | |
1343 | { | |
07d357d0 | 1344 | ib_unregister_client(&ucm_client); |
9af57b7a | 1345 | class_remove_file(&cm_class, &class_attr_abi_version); |
07d357d0 | 1346 | unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES); |
5d7edb3c | 1347 | idr_destroy(&ctx_id_table); |
a5b74540 HR |
1348 | } |
1349 | ||
1350 | module_init(ib_ucm_init); | |
1351 | module_exit(ib_ucm_cleanup); |