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. | |
32 | * | |
33 | * $Id: ucm.c 2594 2005-06-13 19:46:02Z libor $ | |
34 | */ | |
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> | |
41 | #include <linux/file.h> | |
42 | #include <linux/mount.h> | |
43 | #include <linux/cdev.h> | |
595e726a | 44 | #include <linux/idr.h> |
95ed644f | 45 | #include <linux/mutex.h> |
a5b74540 HR |
46 | |
47 | #include <asm/uaccess.h> | |
48 | ||
595e726a SH |
49 | #include <rdma/ib_cm.h> |
50 | #include <rdma/ib_user_cm.h> | |
a5b74540 HR |
51 | |
52 | MODULE_AUTHOR("Libor Michalek"); | |
53 | MODULE_DESCRIPTION("InfiniBand userspace Connection Manager access"); | |
54 | MODULE_LICENSE("Dual BSD/GPL"); | |
55 | ||
07d357d0 SH |
56 | struct ib_ucm_device { |
57 | int devnum; | |
58 | struct cdev dev; | |
59 | struct class_device class_dev; | |
60 | struct ib_device *ib_dev; | |
61 | }; | |
62 | ||
595e726a SH |
63 | struct ib_ucm_file { |
64 | struct semaphore mutex; | |
65 | struct file *filp; | |
07d357d0 | 66 | struct ib_ucm_device *device; |
79d81907 | 67 | |
07d357d0 SH |
68 | struct list_head ctxs; |
69 | struct list_head events; | |
595e726a SH |
70 | wait_queue_head_t poll_wait; |
71 | }; | |
72 | ||
73 | struct ib_ucm_context { | |
74 | int id; | |
75 | wait_queue_head_t wait; | |
76 | atomic_t ref; | |
77 | int events_reported; | |
78 | ||
79 | struct ib_ucm_file *file; | |
80 | struct ib_cm_id *cm_id; | |
81 | __u64 uid; | |
82 | ||
83 | struct list_head events; /* list of pending events. */ | |
84 | struct list_head file_list; /* member in file ctx list */ | |
85 | }; | |
86 | ||
87 | struct ib_ucm_event { | |
88 | struct ib_ucm_context *ctx; | |
89 | struct list_head file_list; /* member in file event list */ | |
90 | struct list_head ctx_list; /* member in ctx event list */ | |
91 | ||
92 | struct ib_cm_id *cm_id; | |
93 | struct ib_ucm_event_resp resp; | |
94 | void *data; | |
95 | void *info; | |
96 | int data_len; | |
97 | int info_len; | |
98 | }; | |
79d81907 | 99 | |
a5b74540 HR |
100 | enum { |
101 | IB_UCM_MAJOR = 231, | |
07d357d0 SH |
102 | IB_UCM_BASE_MINOR = 224, |
103 | IB_UCM_MAX_DEVICES = 32 | |
a5b74540 HR |
104 | }; |
105 | ||
07d357d0 | 106 | #define IB_UCM_BASE_DEV MKDEV(IB_UCM_MAJOR, IB_UCM_BASE_MINOR) |
a5b74540 | 107 | |
07d357d0 SH |
108 | static void ib_ucm_add_one(struct ib_device *device); |
109 | static void ib_ucm_remove_one(struct ib_device *device); | |
a5b74540 | 110 | |
07d357d0 SH |
111 | static struct ib_client ucm_client = { |
112 | .name = "ucm", | |
113 | .add = ib_ucm_add_one, | |
114 | .remove = ib_ucm_remove_one | |
115 | }; | |
116 | ||
95ed644f | 117 | static DEFINE_MUTEX(ctx_id_mutex); |
762a03e2 | 118 | static DEFINE_IDR(ctx_id_table); |
07d357d0 | 119 | static DECLARE_BITMAP(dev_map, IB_UCM_MAX_DEVICES); |
595e726a | 120 | |
b9ef520f | 121 | static struct ib_ucm_context *ib_ucm_ctx_get(struct ib_ucm_file *file, int id) |
a5b74540 HR |
122 | { |
123 | struct ib_ucm_context *ctx; | |
124 | ||
95ed644f | 125 | mutex_lock(&ctx_id_mutex); |
a5b74540 | 126 | ctx = idr_find(&ctx_id_table, id); |
b9ef520f SH |
127 | if (!ctx) |
128 | ctx = ERR_PTR(-ENOENT); | |
129 | else if (ctx->file != file) | |
130 | ctx = ERR_PTR(-EINVAL); | |
131 | else | |
132 | atomic_inc(&ctx->ref); | |
95ed644f | 133 | mutex_unlock(&ctx_id_mutex); |
a5b74540 HR |
134 | |
135 | return ctx; | |
136 | } | |
137 | ||
138 | static void ib_ucm_ctx_put(struct ib_ucm_context *ctx) | |
139 | { | |
b9ef520f SH |
140 | if (atomic_dec_and_test(&ctx->ref)) |
141 | wake_up(&ctx->wait); | |
142 | } | |
143 | ||
0b2b35f6 | 144 | static inline int ib_ucm_new_cm_id(int event) |
b9ef520f | 145 | { |
0b2b35f6 SH |
146 | return event == IB_CM_REQ_RECEIVED || event == IB_CM_SIDR_REQ_RECEIVED; |
147 | } | |
b9ef520f | 148 | |
0b2b35f6 SH |
149 | static void ib_ucm_cleanup_events(struct ib_ucm_context *ctx) |
150 | { | |
151 | struct ib_ucm_event *uevent; | |
a5b74540 | 152 | |
0b2b35f6 | 153 | down(&ctx->file->mutex); |
a5b74540 HR |
154 | list_del(&ctx->file_list); |
155 | while (!list_empty(&ctx->events)) { | |
156 | ||
157 | uevent = list_entry(ctx->events.next, | |
158 | struct ib_ucm_event, ctx_list); | |
159 | list_del(&uevent->file_list); | |
160 | list_del(&uevent->ctx_list); | |
161 | ||
162 | /* clear incoming connections. */ | |
0b2b35f6 | 163 | if (ib_ucm_new_cm_id(uevent->resp.event)) |
a5b74540 HR |
164 | ib_destroy_cm_id(uevent->cm_id); |
165 | ||
166 | kfree(uevent); | |
167 | } | |
0b2b35f6 | 168 | up(&ctx->file->mutex); |
a5b74540 HR |
169 | } |
170 | ||
171 | static struct ib_ucm_context *ib_ucm_ctx_alloc(struct ib_ucm_file *file) | |
172 | { | |
173 | struct ib_ucm_context *ctx; | |
174 | int result; | |
175 | ||
de6eb66b | 176 | ctx = kzalloc(sizeof *ctx, GFP_KERNEL); |
a5b74540 HR |
177 | if (!ctx) |
178 | return NULL; | |
179 | ||
b9ef520f SH |
180 | atomic_set(&ctx->ref, 1); |
181 | init_waitqueue_head(&ctx->wait); | |
a5b74540 | 182 | ctx->file = file; |
a5b74540 | 183 | INIT_LIST_HEAD(&ctx->events); |
a5b74540 | 184 | |
0b2b35f6 SH |
185 | do { |
186 | result = idr_pre_get(&ctx_id_table, GFP_KERNEL); | |
187 | if (!result) | |
188 | goto error; | |
a5b74540 | 189 | |
95ed644f | 190 | mutex_lock(&ctx_id_mutex); |
0b2b35f6 | 191 | result = idr_get_new(&ctx_id_table, ctx, &ctx->id); |
95ed644f | 192 | mutex_unlock(&ctx_id_mutex); |
0b2b35f6 | 193 | } while (result == -EAGAIN); |
a5b74540 | 194 | |
a5b74540 HR |
195 | if (result) |
196 | goto error; | |
197 | ||
0b2b35f6 | 198 | list_add_tail(&ctx->file_list, &file->ctxs); |
a5b74540 | 199 | return ctx; |
0b2b35f6 | 200 | |
a5b74540 | 201 | error: |
a5b74540 | 202 | kfree(ctx); |
a5b74540 HR |
203 | return NULL; |
204 | } | |
07d357d0 | 205 | |
a5b74540 HR |
206 | static void ib_ucm_event_path_get(struct ib_ucm_path_rec *upath, |
207 | struct ib_sa_path_rec *kpath) | |
208 | { | |
209 | if (!kpath || !upath) | |
210 | return; | |
211 | ||
b9ef520f SH |
212 | memcpy(upath->dgid, kpath->dgid.raw, sizeof *upath->dgid); |
213 | memcpy(upath->sgid, kpath->sgid.raw, sizeof *upath->sgid); | |
a5b74540 HR |
214 | |
215 | upath->dlid = kpath->dlid; | |
216 | upath->slid = kpath->slid; | |
217 | upath->raw_traffic = kpath->raw_traffic; | |
218 | upath->flow_label = kpath->flow_label; | |
219 | upath->hop_limit = kpath->hop_limit; | |
220 | upath->traffic_class = kpath->traffic_class; | |
221 | upath->reversible = kpath->reversible; | |
222 | upath->numb_path = kpath->numb_path; | |
223 | upath->pkey = kpath->pkey; | |
224 | upath->sl = kpath->sl; | |
225 | upath->mtu_selector = kpath->mtu_selector; | |
226 | upath->mtu = kpath->mtu; | |
227 | upath->rate_selector = kpath->rate_selector; | |
228 | upath->rate = kpath->rate; | |
229 | upath->packet_life_time = kpath->packet_life_time; | |
230 | upath->preference = kpath->preference; | |
231 | ||
232 | upath->packet_life_time_selector = | |
233 | kpath->packet_life_time_selector; | |
234 | } | |
235 | ||
0b2b35f6 | 236 | static void ib_ucm_event_req_get(struct ib_ucm_req_event_resp *ureq, |
a5b74540 HR |
237 | struct ib_cm_req_event_param *kreq) |
238 | { | |
a5b74540 HR |
239 | ureq->remote_ca_guid = kreq->remote_ca_guid; |
240 | ureq->remote_qkey = kreq->remote_qkey; | |
241 | ureq->remote_qpn = kreq->remote_qpn; | |
242 | ureq->qp_type = kreq->qp_type; | |
243 | ureq->starting_psn = kreq->starting_psn; | |
244 | ureq->responder_resources = kreq->responder_resources; | |
245 | ureq->initiator_depth = kreq->initiator_depth; | |
246 | ureq->local_cm_response_timeout = kreq->local_cm_response_timeout; | |
247 | ureq->flow_control = kreq->flow_control; | |
248 | ureq->remote_cm_response_timeout = kreq->remote_cm_response_timeout; | |
249 | ureq->retry_count = kreq->retry_count; | |
250 | ureq->rnr_retry_count = kreq->rnr_retry_count; | |
251 | ureq->srq = kreq->srq; | |
07d357d0 | 252 | ureq->port = kreq->port; |
a5b74540 HR |
253 | |
254 | ib_ucm_event_path_get(&ureq->primary_path, kreq->primary_path); | |
255 | ib_ucm_event_path_get(&ureq->alternate_path, kreq->alternate_path); | |
256 | } | |
257 | ||
258 | static void ib_ucm_event_rep_get(struct ib_ucm_rep_event_resp *urep, | |
259 | struct ib_cm_rep_event_param *krep) | |
260 | { | |
261 | urep->remote_ca_guid = krep->remote_ca_guid; | |
262 | urep->remote_qkey = krep->remote_qkey; | |
263 | urep->remote_qpn = krep->remote_qpn; | |
264 | urep->starting_psn = krep->starting_psn; | |
265 | urep->responder_resources = krep->responder_resources; | |
266 | urep->initiator_depth = krep->initiator_depth; | |
267 | urep->target_ack_delay = krep->target_ack_delay; | |
268 | urep->failover_accepted = krep->failover_accepted; | |
269 | urep->flow_control = krep->flow_control; | |
270 | urep->rnr_retry_count = krep->rnr_retry_count; | |
271 | urep->srq = krep->srq; | |
272 | } | |
273 | ||
a5b74540 HR |
274 | static void ib_ucm_event_sidr_rep_get(struct ib_ucm_sidr_rep_event_resp *urep, |
275 | struct ib_cm_sidr_rep_event_param *krep) | |
276 | { | |
277 | urep->status = krep->status; | |
278 | urep->qkey = krep->qkey; | |
279 | urep->qpn = krep->qpn; | |
280 | }; | |
281 | ||
0b2b35f6 | 282 | static int ib_ucm_event_process(struct ib_cm_event *evt, |
a5b74540 HR |
283 | struct ib_ucm_event *uvt) |
284 | { | |
285 | void *info = NULL; | |
a5b74540 HR |
286 | |
287 | switch (evt->event) { | |
288 | case IB_CM_REQ_RECEIVED: | |
0b2b35f6 | 289 | ib_ucm_event_req_get(&uvt->resp.u.req_resp, |
a5b74540 HR |
290 | &evt->param.req_rcvd); |
291 | uvt->data_len = IB_CM_REQ_PRIVATE_DATA_SIZE; | |
b9ef520f | 292 | uvt->resp.present = IB_UCM_PRES_PRIMARY; |
a5b74540 HR |
293 | uvt->resp.present |= (evt->param.req_rcvd.alternate_path ? |
294 | IB_UCM_PRES_ALTERNATE : 0); | |
295 | break; | |
296 | case IB_CM_REP_RECEIVED: | |
297 | ib_ucm_event_rep_get(&uvt->resp.u.rep_resp, | |
298 | &evt->param.rep_rcvd); | |
299 | uvt->data_len = IB_CM_REP_PRIVATE_DATA_SIZE; | |
a5b74540 HR |
300 | break; |
301 | case IB_CM_RTU_RECEIVED: | |
302 | uvt->data_len = IB_CM_RTU_PRIVATE_DATA_SIZE; | |
303 | uvt->resp.u.send_status = evt->param.send_status; | |
a5b74540 HR |
304 | break; |
305 | case IB_CM_DREQ_RECEIVED: | |
306 | uvt->data_len = IB_CM_DREQ_PRIVATE_DATA_SIZE; | |
307 | uvt->resp.u.send_status = evt->param.send_status; | |
a5b74540 HR |
308 | break; |
309 | case IB_CM_DREP_RECEIVED: | |
310 | uvt->data_len = IB_CM_DREP_PRIVATE_DATA_SIZE; | |
311 | uvt->resp.u.send_status = evt->param.send_status; | |
a5b74540 HR |
312 | break; |
313 | case IB_CM_MRA_RECEIVED: | |
b9ef520f SH |
314 | uvt->resp.u.mra_resp.timeout = |
315 | evt->param.mra_rcvd.service_timeout; | |
a5b74540 | 316 | uvt->data_len = IB_CM_MRA_PRIVATE_DATA_SIZE; |
a5b74540 HR |
317 | break; |
318 | case IB_CM_REJ_RECEIVED: | |
b9ef520f | 319 | uvt->resp.u.rej_resp.reason = evt->param.rej_rcvd.reason; |
a5b74540 HR |
320 | uvt->data_len = IB_CM_REJ_PRIVATE_DATA_SIZE; |
321 | uvt->info_len = evt->param.rej_rcvd.ari_length; | |
322 | info = evt->param.rej_rcvd.ari; | |
a5b74540 HR |
323 | break; |
324 | case IB_CM_LAP_RECEIVED: | |
b9ef520f SH |
325 | ib_ucm_event_path_get(&uvt->resp.u.lap_resp.path, |
326 | evt->param.lap_rcvd.alternate_path); | |
a5b74540 | 327 | uvt->data_len = IB_CM_LAP_PRIVATE_DATA_SIZE; |
b9ef520f | 328 | uvt->resp.present = IB_UCM_PRES_ALTERNATE; |
a5b74540 HR |
329 | break; |
330 | case IB_CM_APR_RECEIVED: | |
b9ef520f | 331 | uvt->resp.u.apr_resp.status = evt->param.apr_rcvd.ap_status; |
a5b74540 HR |
332 | uvt->data_len = IB_CM_APR_PRIVATE_DATA_SIZE; |
333 | uvt->info_len = evt->param.apr_rcvd.info_len; | |
334 | info = evt->param.apr_rcvd.apr_info; | |
a5b74540 HR |
335 | break; |
336 | case IB_CM_SIDR_REQ_RECEIVED: | |
0b2b35f6 SH |
337 | uvt->resp.u.sidr_req_resp.pkey = |
338 | evt->param.sidr_req_rcvd.pkey; | |
07d357d0 SH |
339 | uvt->resp.u.sidr_req_resp.port = |
340 | evt->param.sidr_req_rcvd.port; | |
a5b74540 | 341 | uvt->data_len = IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE; |
a5b74540 HR |
342 | break; |
343 | case IB_CM_SIDR_REP_RECEIVED: | |
344 | ib_ucm_event_sidr_rep_get(&uvt->resp.u.sidr_rep_resp, | |
345 | &evt->param.sidr_rep_rcvd); | |
346 | uvt->data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE; | |
347 | uvt->info_len = evt->param.sidr_rep_rcvd.info_len; | |
348 | info = evt->param.sidr_rep_rcvd.info; | |
a5b74540 HR |
349 | break; |
350 | default: | |
351 | uvt->resp.u.send_status = evt->param.send_status; | |
a5b74540 HR |
352 | break; |
353 | } | |
354 | ||
b9ef520f | 355 | if (uvt->data_len) { |
a5b74540 | 356 | uvt->data = kmalloc(uvt->data_len, GFP_KERNEL); |
b9ef520f SH |
357 | if (!uvt->data) |
358 | goto err1; | |
a5b74540 HR |
359 | |
360 | memcpy(uvt->data, evt->private_data, uvt->data_len); | |
361 | uvt->resp.present |= IB_UCM_PRES_DATA; | |
362 | } | |
363 | ||
b9ef520f | 364 | if (uvt->info_len) { |
a5b74540 | 365 | uvt->info = kmalloc(uvt->info_len, GFP_KERNEL); |
b9ef520f SH |
366 | if (!uvt->info) |
367 | goto err2; | |
a5b74540 HR |
368 | |
369 | memcpy(uvt->info, info, uvt->info_len); | |
370 | uvt->resp.present |= IB_UCM_PRES_INFO; | |
371 | } | |
a5b74540 | 372 | return 0; |
b9ef520f SH |
373 | |
374 | err2: | |
79d81907 | 375 | kfree(uvt->data); |
b9ef520f SH |
376 | err1: |
377 | return -ENOMEM; | |
a5b74540 HR |
378 | } |
379 | ||
380 | static int ib_ucm_event_handler(struct ib_cm_id *cm_id, | |
381 | struct ib_cm_event *event) | |
382 | { | |
383 | struct ib_ucm_event *uevent; | |
384 | struct ib_ucm_context *ctx; | |
385 | int result = 0; | |
a5b74540 | 386 | |
b9ef520f | 387 | ctx = cm_id->context; |
a5b74540 | 388 | |
de6eb66b | 389 | uevent = kzalloc(sizeof *uevent, GFP_KERNEL); |
b9ef520f SH |
390 | if (!uevent) |
391 | goto err1; | |
a5b74540 | 392 | |
0b2b35f6 SH |
393 | uevent->ctx = ctx; |
394 | uevent->cm_id = cm_id; | |
395 | uevent->resp.uid = ctx->uid; | |
396 | uevent->resp.id = ctx->id; | |
a5b74540 HR |
397 | uevent->resp.event = event->event; |
398 | ||
0b2b35f6 | 399 | result = ib_ucm_event_process(event, uevent); |
a5b74540 | 400 | if (result) |
b9ef520f | 401 | goto err2; |
a5b74540 | 402 | |
a5b74540 | 403 | down(&ctx->file->mutex); |
a5b74540 HR |
404 | list_add_tail(&uevent->file_list, &ctx->file->events); |
405 | list_add_tail(&uevent->ctx_list, &ctx->events); | |
a5b74540 | 406 | wake_up_interruptible(&ctx->file->poll_wait); |
a5b74540 | 407 | up(&ctx->file->mutex); |
b9ef520f SH |
408 | return 0; |
409 | ||
410 | err2: | |
411 | kfree(uevent); | |
412 | err1: | |
413 | /* Destroy new cm_id's */ | |
0b2b35f6 | 414 | return ib_ucm_new_cm_id(event->event); |
a5b74540 HR |
415 | } |
416 | ||
417 | static ssize_t ib_ucm_event(struct ib_ucm_file *file, | |
418 | const char __user *inbuf, | |
419 | int in_len, int out_len) | |
420 | { | |
421 | struct ib_ucm_context *ctx; | |
422 | struct ib_ucm_event_get cmd; | |
0b2b35f6 | 423 | struct ib_ucm_event *uevent; |
a5b74540 HR |
424 | int result = 0; |
425 | DEFINE_WAIT(wait); | |
426 | ||
427 | if (out_len < sizeof(struct ib_ucm_event_resp)) | |
428 | return -ENOSPC; | |
429 | ||
430 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
431 | return -EFAULT; | |
07d357d0 | 432 | |
a5b74540 | 433 | down(&file->mutex); |
a5b74540 HR |
434 | while (list_empty(&file->events)) { |
435 | ||
436 | if (file->filp->f_flags & O_NONBLOCK) { | |
437 | result = -EAGAIN; | |
438 | break; | |
439 | } | |
440 | ||
441 | if (signal_pending(current)) { | |
442 | result = -ERESTARTSYS; | |
443 | break; | |
444 | } | |
445 | ||
446 | prepare_to_wait(&file->poll_wait, &wait, TASK_INTERRUPTIBLE); | |
447 | ||
448 | up(&file->mutex); | |
449 | schedule(); | |
450 | down(&file->mutex); | |
451 | ||
452 | finish_wait(&file->poll_wait, &wait); | |
453 | } | |
454 | ||
455 | if (result) | |
456 | goto done; | |
457 | ||
458 | uevent = list_entry(file->events.next, struct ib_ucm_event, file_list); | |
459 | ||
0b2b35f6 SH |
460 | if (ib_ucm_new_cm_id(uevent->resp.event)) { |
461 | ctx = ib_ucm_ctx_alloc(file); | |
462 | if (!ctx) { | |
463 | result = -ENOMEM; | |
464 | goto done; | |
465 | } | |
a5b74540 | 466 | |
0b2b35f6 SH |
467 | ctx->cm_id = uevent->cm_id; |
468 | ctx->cm_id->context = ctx; | |
469 | uevent->resp.id = ctx->id; | |
a5b74540 HR |
470 | } |
471 | ||
a5b74540 HR |
472 | if (copy_to_user((void __user *)(unsigned long)cmd.response, |
473 | &uevent->resp, sizeof(uevent->resp))) { | |
474 | result = -EFAULT; | |
475 | goto done; | |
476 | } | |
477 | ||
478 | if (uevent->data) { | |
a5b74540 HR |
479 | if (cmd.data_len < uevent->data_len) { |
480 | result = -ENOMEM; | |
481 | goto done; | |
482 | } | |
a5b74540 HR |
483 | if (copy_to_user((void __user *)(unsigned long)cmd.data, |
484 | uevent->data, uevent->data_len)) { | |
485 | result = -EFAULT; | |
486 | goto done; | |
487 | } | |
488 | } | |
489 | ||
490 | if (uevent->info) { | |
a5b74540 HR |
491 | if (cmd.info_len < uevent->info_len) { |
492 | result = -ENOMEM; | |
493 | goto done; | |
494 | } | |
a5b74540 HR |
495 | if (copy_to_user((void __user *)(unsigned long)cmd.info, |
496 | uevent->info, uevent->info_len)) { | |
497 | result = -EFAULT; | |
498 | goto done; | |
499 | } | |
500 | } | |
501 | ||
502 | list_del(&uevent->file_list); | |
503 | list_del(&uevent->ctx_list); | |
0b2b35f6 | 504 | uevent->ctx->events_reported++; |
a5b74540 | 505 | |
79d81907 HR |
506 | kfree(uevent->data); |
507 | kfree(uevent->info); | |
a5b74540 HR |
508 | kfree(uevent); |
509 | done: | |
510 | up(&file->mutex); | |
511 | return result; | |
512 | } | |
513 | ||
a5b74540 HR |
514 | static ssize_t ib_ucm_create_id(struct ib_ucm_file *file, |
515 | const char __user *inbuf, | |
516 | int in_len, int out_len) | |
517 | { | |
518 | struct ib_ucm_create_id cmd; | |
519 | struct ib_ucm_create_id_resp resp; | |
520 | struct ib_ucm_context *ctx; | |
521 | int result; | |
522 | ||
523 | if (out_len < sizeof(resp)) | |
524 | return -ENOSPC; | |
525 | ||
526 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
527 | return -EFAULT; | |
528 | ||
b9ef520f | 529 | down(&file->mutex); |
a5b74540 | 530 | ctx = ib_ucm_ctx_alloc(file); |
b9ef520f | 531 | up(&file->mutex); |
a5b74540 HR |
532 | if (!ctx) |
533 | return -ENOMEM; | |
534 | ||
0b2b35f6 | 535 | ctx->uid = cmd.uid; |
07d357d0 SH |
536 | ctx->cm_id = ib_create_cm_id(file->device->ib_dev, |
537 | ib_ucm_event_handler, ctx); | |
b9ef520f SH |
538 | if (IS_ERR(ctx->cm_id)) { |
539 | result = PTR_ERR(ctx->cm_id); | |
07d357d0 | 540 | goto err1; |
a5b74540 HR |
541 | } |
542 | ||
543 | resp.id = ctx->id; | |
544 | if (copy_to_user((void __user *)(unsigned long)cmd.response, | |
545 | &resp, sizeof(resp))) { | |
546 | result = -EFAULT; | |
07d357d0 | 547 | goto err2; |
a5b74540 | 548 | } |
a5b74540 | 549 | return 0; |
a5b74540 | 550 | |
07d357d0 SH |
551 | err2: |
552 | ib_destroy_cm_id(ctx->cm_id); | |
553 | err1: | |
95ed644f | 554 | mutex_lock(&ctx_id_mutex); |
0b2b35f6 | 555 | idr_remove(&ctx_id_table, ctx->id); |
95ed644f | 556 | mutex_unlock(&ctx_id_mutex); |
0b2b35f6 | 557 | kfree(ctx); |
a5b74540 HR |
558 | return result; |
559 | } | |
560 | ||
561 | static ssize_t ib_ucm_destroy_id(struct ib_ucm_file *file, | |
562 | const char __user *inbuf, | |
563 | int in_len, int out_len) | |
564 | { | |
565 | struct ib_ucm_destroy_id cmd; | |
0b2b35f6 SH |
566 | struct ib_ucm_destroy_id_resp resp; |
567 | struct ib_ucm_context *ctx; | |
568 | int result = 0; | |
569 | ||
570 | if (out_len < sizeof(resp)) | |
571 | return -ENOSPC; | |
a5b74540 HR |
572 | |
573 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
574 | return -EFAULT; | |
575 | ||
95ed644f | 576 | mutex_lock(&ctx_id_mutex); |
0b2b35f6 SH |
577 | ctx = idr_find(&ctx_id_table, cmd.id); |
578 | if (!ctx) | |
579 | ctx = ERR_PTR(-ENOENT); | |
580 | else if (ctx->file != file) | |
581 | ctx = ERR_PTR(-EINVAL); | |
582 | else | |
583 | idr_remove(&ctx_id_table, ctx->id); | |
95ed644f | 584 | mutex_unlock(&ctx_id_mutex); |
0b2b35f6 SH |
585 | |
586 | if (IS_ERR(ctx)) | |
587 | return PTR_ERR(ctx); | |
588 | ||
589 | atomic_dec(&ctx->ref); | |
590 | wait_event(ctx->wait, !atomic_read(&ctx->ref)); | |
591 | ||
592 | /* No new events will be generated after destroying the cm_id. */ | |
593 | ib_destroy_cm_id(ctx->cm_id); | |
594 | /* Cleanup events not yet reported to the user. */ | |
595 | ib_ucm_cleanup_events(ctx); | |
596 | ||
597 | resp.events_reported = ctx->events_reported; | |
598 | if (copy_to_user((void __user *)(unsigned long)cmd.response, | |
599 | &resp, sizeof(resp))) | |
600 | result = -EFAULT; | |
601 | ||
602 | kfree(ctx); | |
603 | return result; | |
a5b74540 HR |
604 | } |
605 | ||
606 | static ssize_t ib_ucm_attr_id(struct ib_ucm_file *file, | |
607 | const char __user *inbuf, | |
608 | int in_len, int out_len) | |
609 | { | |
610 | struct ib_ucm_attr_id_resp resp; | |
611 | struct ib_ucm_attr_id cmd; | |
612 | struct ib_ucm_context *ctx; | |
613 | int result = 0; | |
614 | ||
615 | if (out_len < sizeof(resp)) | |
616 | return -ENOSPC; | |
617 | ||
618 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
619 | return -EFAULT; | |
620 | ||
b9ef520f SH |
621 | ctx = ib_ucm_ctx_get(file, cmd.id); |
622 | if (IS_ERR(ctx)) | |
623 | return PTR_ERR(ctx); | |
a5b74540 HR |
624 | |
625 | resp.service_id = ctx->cm_id->service_id; | |
626 | resp.service_mask = ctx->cm_id->service_mask; | |
627 | resp.local_id = ctx->cm_id->local_id; | |
628 | resp.remote_id = ctx->cm_id->remote_id; | |
629 | ||
630 | if (copy_to_user((void __user *)(unsigned long)cmd.response, | |
631 | &resp, sizeof(resp))) | |
632 | result = -EFAULT; | |
633 | ||
b9ef520f | 634 | ib_ucm_ctx_put(ctx); |
a5b74540 HR |
635 | return result; |
636 | } | |
637 | ||
0b2b35f6 SH |
638 | static void ib_ucm_copy_ah_attr(struct ib_ucm_ah_attr *dest_attr, |
639 | struct ib_ah_attr *src_attr) | |
640 | { | |
641 | memcpy(dest_attr->grh_dgid, src_attr->grh.dgid.raw, | |
642 | sizeof src_attr->grh.dgid); | |
643 | dest_attr->grh_flow_label = src_attr->grh.flow_label; | |
644 | dest_attr->grh_sgid_index = src_attr->grh.sgid_index; | |
645 | dest_attr->grh_hop_limit = src_attr->grh.hop_limit; | |
646 | dest_attr->grh_traffic_class = src_attr->grh.traffic_class; | |
647 | ||
648 | dest_attr->dlid = src_attr->dlid; | |
649 | dest_attr->sl = src_attr->sl; | |
650 | dest_attr->src_path_bits = src_attr->src_path_bits; | |
651 | dest_attr->static_rate = src_attr->static_rate; | |
652 | dest_attr->is_global = (src_attr->ah_flags & IB_AH_GRH); | |
653 | dest_attr->port_num = src_attr->port_num; | |
654 | } | |
655 | ||
656 | static void ib_ucm_copy_qp_attr(struct ib_ucm_init_qp_attr_resp *dest_attr, | |
657 | struct ib_qp_attr *src_attr) | |
658 | { | |
659 | dest_attr->cur_qp_state = src_attr->cur_qp_state; | |
660 | dest_attr->path_mtu = src_attr->path_mtu; | |
661 | dest_attr->path_mig_state = src_attr->path_mig_state; | |
662 | dest_attr->qkey = src_attr->qkey; | |
663 | dest_attr->rq_psn = src_attr->rq_psn; | |
664 | dest_attr->sq_psn = src_attr->sq_psn; | |
665 | dest_attr->dest_qp_num = src_attr->dest_qp_num; | |
666 | dest_attr->qp_access_flags = src_attr->qp_access_flags; | |
667 | ||
668 | dest_attr->max_send_wr = src_attr->cap.max_send_wr; | |
669 | dest_attr->max_recv_wr = src_attr->cap.max_recv_wr; | |
670 | dest_attr->max_send_sge = src_attr->cap.max_send_sge; | |
671 | dest_attr->max_recv_sge = src_attr->cap.max_recv_sge; | |
672 | dest_attr->max_inline_data = src_attr->cap.max_inline_data; | |
673 | ||
674 | ib_ucm_copy_ah_attr(&dest_attr->ah_attr, &src_attr->ah_attr); | |
675 | ib_ucm_copy_ah_attr(&dest_attr->alt_ah_attr, &src_attr->alt_ah_attr); | |
676 | ||
677 | dest_attr->pkey_index = src_attr->pkey_index; | |
678 | dest_attr->alt_pkey_index = src_attr->alt_pkey_index; | |
679 | dest_attr->en_sqd_async_notify = src_attr->en_sqd_async_notify; | |
680 | dest_attr->sq_draining = src_attr->sq_draining; | |
681 | dest_attr->max_rd_atomic = src_attr->max_rd_atomic; | |
682 | dest_attr->max_dest_rd_atomic = src_attr->max_dest_rd_atomic; | |
683 | dest_attr->min_rnr_timer = src_attr->min_rnr_timer; | |
684 | dest_attr->port_num = src_attr->port_num; | |
685 | dest_attr->timeout = src_attr->timeout; | |
686 | dest_attr->retry_cnt = src_attr->retry_cnt; | |
687 | dest_attr->rnr_retry = src_attr->rnr_retry; | |
688 | dest_attr->alt_port_num = src_attr->alt_port_num; | |
689 | dest_attr->alt_timeout = src_attr->alt_timeout; | |
690 | } | |
691 | ||
692 | static ssize_t ib_ucm_init_qp_attr(struct ib_ucm_file *file, | |
693 | const char __user *inbuf, | |
694 | int in_len, int out_len) | |
695 | { | |
696 | struct ib_ucm_init_qp_attr_resp resp; | |
697 | struct ib_ucm_init_qp_attr cmd; | |
698 | struct ib_ucm_context *ctx; | |
699 | struct ib_qp_attr qp_attr; | |
700 | int result = 0; | |
701 | ||
702 | if (out_len < sizeof(resp)) | |
703 | return -ENOSPC; | |
704 | ||
705 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
706 | return -EFAULT; | |
707 | ||
708 | ctx = ib_ucm_ctx_get(file, cmd.id); | |
709 | if (IS_ERR(ctx)) | |
710 | return PTR_ERR(ctx); | |
711 | ||
712 | resp.qp_attr_mask = 0; | |
713 | memset(&qp_attr, 0, sizeof qp_attr); | |
714 | qp_attr.qp_state = cmd.qp_state; | |
715 | result = ib_cm_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask); | |
716 | if (result) | |
717 | goto out; | |
718 | ||
719 | ib_ucm_copy_qp_attr(&resp, &qp_attr); | |
720 | ||
721 | if (copy_to_user((void __user *)(unsigned long)cmd.response, | |
722 | &resp, sizeof(resp))) | |
723 | result = -EFAULT; | |
724 | ||
725 | out: | |
726 | ib_ucm_ctx_put(ctx); | |
727 | return result; | |
728 | } | |
729 | ||
a5b74540 HR |
730 | static ssize_t ib_ucm_listen(struct ib_ucm_file *file, |
731 | const char __user *inbuf, | |
732 | int in_len, int out_len) | |
733 | { | |
734 | struct ib_ucm_listen cmd; | |
735 | struct ib_ucm_context *ctx; | |
736 | int result; | |
737 | ||
738 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
739 | return -EFAULT; | |
740 | ||
b9ef520f SH |
741 | ctx = ib_ucm_ctx_get(file, cmd.id); |
742 | if (IS_ERR(ctx)) | |
743 | return PTR_ERR(ctx); | |
a5b74540 | 744 | |
b9ef520f SH |
745 | result = ib_cm_listen(ctx->cm_id, cmd.service_id, cmd.service_mask); |
746 | ib_ucm_ctx_put(ctx); | |
a5b74540 HR |
747 | return result; |
748 | } | |
749 | ||
750 | static ssize_t ib_ucm_establish(struct ib_ucm_file *file, | |
751 | const char __user *inbuf, | |
752 | int in_len, int out_len) | |
753 | { | |
754 | struct ib_ucm_establish cmd; | |
755 | struct ib_ucm_context *ctx; | |
756 | int result; | |
757 | ||
758 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
759 | return -EFAULT; | |
760 | ||
b9ef520f SH |
761 | ctx = ib_ucm_ctx_get(file, cmd.id); |
762 | if (IS_ERR(ctx)) | |
763 | return PTR_ERR(ctx); | |
a5b74540 | 764 | |
b9ef520f SH |
765 | result = ib_cm_establish(ctx->cm_id); |
766 | ib_ucm_ctx_put(ctx); | |
a5b74540 HR |
767 | return result; |
768 | } | |
769 | ||
770 | static int ib_ucm_alloc_data(const void **dest, u64 src, u32 len) | |
771 | { | |
772 | void *data; | |
773 | ||
774 | *dest = NULL; | |
775 | ||
776 | if (!len) | |
777 | return 0; | |
778 | ||
779 | data = kmalloc(len, GFP_KERNEL); | |
780 | if (!data) | |
781 | return -ENOMEM; | |
782 | ||
783 | if (copy_from_user(data, (void __user *)(unsigned long)src, len)) { | |
784 | kfree(data); | |
785 | return -EFAULT; | |
786 | } | |
787 | ||
788 | *dest = data; | |
789 | return 0; | |
790 | } | |
791 | ||
792 | static int ib_ucm_path_get(struct ib_sa_path_rec **path, u64 src) | |
793 | { | |
794 | struct ib_ucm_path_rec ucm_path; | |
795 | struct ib_sa_path_rec *sa_path; | |
796 | ||
797 | *path = NULL; | |
798 | ||
799 | if (!src) | |
800 | return 0; | |
801 | ||
802 | sa_path = kmalloc(sizeof(*sa_path), GFP_KERNEL); | |
803 | if (!sa_path) | |
804 | return -ENOMEM; | |
805 | ||
806 | if (copy_from_user(&ucm_path, (void __user *)(unsigned long)src, | |
807 | sizeof(ucm_path))) { | |
808 | ||
809 | kfree(sa_path); | |
810 | return -EFAULT; | |
811 | } | |
812 | ||
b9ef520f SH |
813 | memcpy(sa_path->dgid.raw, ucm_path.dgid, sizeof sa_path->dgid); |
814 | memcpy(sa_path->sgid.raw, ucm_path.sgid, sizeof sa_path->sgid); | |
a5b74540 HR |
815 | |
816 | sa_path->dlid = ucm_path.dlid; | |
817 | sa_path->slid = ucm_path.slid; | |
818 | sa_path->raw_traffic = ucm_path.raw_traffic; | |
819 | sa_path->flow_label = ucm_path.flow_label; | |
820 | sa_path->hop_limit = ucm_path.hop_limit; | |
821 | sa_path->traffic_class = ucm_path.traffic_class; | |
822 | sa_path->reversible = ucm_path.reversible; | |
823 | sa_path->numb_path = ucm_path.numb_path; | |
824 | sa_path->pkey = ucm_path.pkey; | |
825 | sa_path->sl = ucm_path.sl; | |
826 | sa_path->mtu_selector = ucm_path.mtu_selector; | |
827 | sa_path->mtu = ucm_path.mtu; | |
828 | sa_path->rate_selector = ucm_path.rate_selector; | |
829 | sa_path->rate = ucm_path.rate; | |
830 | sa_path->packet_life_time = ucm_path.packet_life_time; | |
831 | sa_path->preference = ucm_path.preference; | |
832 | ||
833 | sa_path->packet_life_time_selector = | |
834 | ucm_path.packet_life_time_selector; | |
835 | ||
836 | *path = sa_path; | |
837 | return 0; | |
838 | } | |
839 | ||
840 | static ssize_t ib_ucm_send_req(struct ib_ucm_file *file, | |
841 | const char __user *inbuf, | |
842 | int in_len, int out_len) | |
843 | { | |
844 | struct ib_cm_req_param param; | |
845 | struct ib_ucm_context *ctx; | |
846 | struct ib_ucm_req cmd; | |
847 | int result; | |
848 | ||
849 | param.private_data = NULL; | |
850 | param.primary_path = NULL; | |
851 | param.alternate_path = NULL; | |
852 | ||
853 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
854 | return -EFAULT; | |
855 | ||
856 | result = ib_ucm_alloc_data(¶m.private_data, cmd.data, cmd.len); | |
857 | if (result) | |
858 | goto done; | |
859 | ||
860 | result = ib_ucm_path_get(¶m.primary_path, cmd.primary_path); | |
861 | if (result) | |
862 | goto done; | |
863 | ||
864 | result = ib_ucm_path_get(¶m.alternate_path, cmd.alternate_path); | |
865 | if (result) | |
866 | goto done; | |
867 | ||
868 | param.private_data_len = cmd.len; | |
869 | param.service_id = cmd.sid; | |
870 | param.qp_num = cmd.qpn; | |
871 | param.qp_type = cmd.qp_type; | |
872 | param.starting_psn = cmd.psn; | |
873 | param.peer_to_peer = cmd.peer_to_peer; | |
874 | param.responder_resources = cmd.responder_resources; | |
875 | param.initiator_depth = cmd.initiator_depth; | |
876 | param.remote_cm_response_timeout = cmd.remote_cm_response_timeout; | |
877 | param.flow_control = cmd.flow_control; | |
878 | param.local_cm_response_timeout = cmd.local_cm_response_timeout; | |
879 | param.retry_count = cmd.retry_count; | |
880 | param.rnr_retry_count = cmd.rnr_retry_count; | |
881 | param.max_cm_retries = cmd.max_cm_retries; | |
882 | param.srq = cmd.srq; | |
883 | ||
b9ef520f SH |
884 | ctx = ib_ucm_ctx_get(file, cmd.id); |
885 | if (!IS_ERR(ctx)) { | |
a5b74540 | 886 | result = ib_send_cm_req(ctx->cm_id, ¶m); |
b9ef520f SH |
887 | ib_ucm_ctx_put(ctx); |
888 | } else | |
889 | result = PTR_ERR(ctx); | |
a5b74540 | 890 | |
a5b74540 | 891 | done: |
79d81907 HR |
892 | kfree(param.private_data); |
893 | kfree(param.primary_path); | |
894 | kfree(param.alternate_path); | |
a5b74540 HR |
895 | return result; |
896 | } | |
897 | ||
898 | static ssize_t ib_ucm_send_rep(struct ib_ucm_file *file, | |
899 | const char __user *inbuf, | |
900 | int in_len, int out_len) | |
901 | { | |
902 | struct ib_cm_rep_param param; | |
903 | struct ib_ucm_context *ctx; | |
904 | struct ib_ucm_rep cmd; | |
905 | int result; | |
906 | ||
907 | param.private_data = NULL; | |
908 | ||
909 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
910 | return -EFAULT; | |
911 | ||
912 | result = ib_ucm_alloc_data(¶m.private_data, cmd.data, cmd.len); | |
913 | if (result) | |
914 | return result; | |
915 | ||
916 | param.qp_num = cmd.qpn; | |
917 | param.starting_psn = cmd.psn; | |
918 | param.private_data_len = cmd.len; | |
919 | param.responder_resources = cmd.responder_resources; | |
920 | param.initiator_depth = cmd.initiator_depth; | |
921 | param.target_ack_delay = cmd.target_ack_delay; | |
922 | param.failover_accepted = cmd.failover_accepted; | |
923 | param.flow_control = cmd.flow_control; | |
924 | param.rnr_retry_count = cmd.rnr_retry_count; | |
925 | param.srq = cmd.srq; | |
926 | ||
b9ef520f SH |
927 | ctx = ib_ucm_ctx_get(file, cmd.id); |
928 | if (!IS_ERR(ctx)) { | |
0b2b35f6 | 929 | ctx->uid = cmd.uid; |
a5b74540 | 930 | result = ib_send_cm_rep(ctx->cm_id, ¶m); |
b9ef520f SH |
931 | ib_ucm_ctx_put(ctx); |
932 | } else | |
933 | result = PTR_ERR(ctx); | |
a5b74540 | 934 | |
79d81907 | 935 | kfree(param.private_data); |
a5b74540 HR |
936 | return result; |
937 | } | |
938 | ||
939 | static ssize_t ib_ucm_send_private_data(struct ib_ucm_file *file, | |
940 | const char __user *inbuf, int in_len, | |
941 | int (*func)(struct ib_cm_id *cm_id, | |
942 | const void *private_data, | |
943 | u8 private_data_len)) | |
944 | { | |
945 | struct ib_ucm_private_data cmd; | |
946 | struct ib_ucm_context *ctx; | |
947 | const void *private_data = NULL; | |
948 | int result; | |
949 | ||
950 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
951 | return -EFAULT; | |
952 | ||
953 | result = ib_ucm_alloc_data(&private_data, cmd.data, cmd.len); | |
954 | if (result) | |
955 | return result; | |
956 | ||
b9ef520f SH |
957 | ctx = ib_ucm_ctx_get(file, cmd.id); |
958 | if (!IS_ERR(ctx)) { | |
a5b74540 | 959 | result = func(ctx->cm_id, private_data, cmd.len); |
b9ef520f SH |
960 | ib_ucm_ctx_put(ctx); |
961 | } else | |
962 | result = PTR_ERR(ctx); | |
a5b74540 | 963 | |
79d81907 | 964 | kfree(private_data); |
a5b74540 HR |
965 | return result; |
966 | } | |
967 | ||
968 | static ssize_t ib_ucm_send_rtu(struct ib_ucm_file *file, | |
969 | const char __user *inbuf, | |
970 | int in_len, int out_len) | |
971 | { | |
972 | return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_rtu); | |
973 | } | |
974 | ||
975 | static ssize_t ib_ucm_send_dreq(struct ib_ucm_file *file, | |
976 | const char __user *inbuf, | |
977 | int in_len, int out_len) | |
978 | { | |
979 | return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_dreq); | |
980 | } | |
981 | ||
982 | static ssize_t ib_ucm_send_drep(struct ib_ucm_file *file, | |
983 | const char __user *inbuf, | |
984 | int in_len, int out_len) | |
985 | { | |
986 | return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_drep); | |
987 | } | |
988 | ||
989 | static ssize_t ib_ucm_send_info(struct ib_ucm_file *file, | |
990 | const char __user *inbuf, int in_len, | |
991 | int (*func)(struct ib_cm_id *cm_id, | |
992 | int status, | |
993 | const void *info, | |
994 | u8 info_len, | |
995 | const void *data, | |
996 | u8 data_len)) | |
997 | { | |
998 | struct ib_ucm_context *ctx; | |
999 | struct ib_ucm_info cmd; | |
1000 | const void *data = NULL; | |
1001 | const void *info = NULL; | |
1002 | int result; | |
1003 | ||
1004 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
1005 | return -EFAULT; | |
1006 | ||
1007 | result = ib_ucm_alloc_data(&data, cmd.data, cmd.data_len); | |
1008 | if (result) | |
1009 | goto done; | |
1010 | ||
1011 | result = ib_ucm_alloc_data(&info, cmd.info, cmd.info_len); | |
1012 | if (result) | |
1013 | goto done; | |
1014 | ||
b9ef520f SH |
1015 | ctx = ib_ucm_ctx_get(file, cmd.id); |
1016 | if (!IS_ERR(ctx)) { | |
1017 | result = func(ctx->cm_id, cmd.status, info, cmd.info_len, | |
a5b74540 | 1018 | data, cmd.data_len); |
b9ef520f SH |
1019 | ib_ucm_ctx_put(ctx); |
1020 | } else | |
1021 | result = PTR_ERR(ctx); | |
a5b74540 | 1022 | |
a5b74540 | 1023 | done: |
79d81907 HR |
1024 | kfree(data); |
1025 | kfree(info); | |
a5b74540 HR |
1026 | return result; |
1027 | } | |
1028 | ||
1029 | static ssize_t ib_ucm_send_rej(struct ib_ucm_file *file, | |
1030 | const char __user *inbuf, | |
1031 | int in_len, int out_len) | |
1032 | { | |
1033 | return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_rej); | |
1034 | } | |
1035 | ||
1036 | static ssize_t ib_ucm_send_apr(struct ib_ucm_file *file, | |
1037 | const char __user *inbuf, | |
1038 | int in_len, int out_len) | |
1039 | { | |
1040 | return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_apr); | |
1041 | } | |
1042 | ||
1043 | static ssize_t ib_ucm_send_mra(struct ib_ucm_file *file, | |
1044 | const char __user *inbuf, | |
1045 | int in_len, int out_len) | |
1046 | { | |
1047 | struct ib_ucm_context *ctx; | |
1048 | struct ib_ucm_mra cmd; | |
1049 | const void *data = NULL; | |
1050 | int result; | |
1051 | ||
1052 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
1053 | return -EFAULT; | |
1054 | ||
1055 | result = ib_ucm_alloc_data(&data, cmd.data, cmd.len); | |
1056 | if (result) | |
1057 | return result; | |
1058 | ||
b9ef520f SH |
1059 | ctx = ib_ucm_ctx_get(file, cmd.id); |
1060 | if (!IS_ERR(ctx)) { | |
1061 | result = ib_send_cm_mra(ctx->cm_id, cmd.timeout, data, cmd.len); | |
1062 | ib_ucm_ctx_put(ctx); | |
1063 | } else | |
1064 | result = PTR_ERR(ctx); | |
a5b74540 | 1065 | |
79d81907 | 1066 | kfree(data); |
a5b74540 HR |
1067 | return result; |
1068 | } | |
1069 | ||
1070 | static ssize_t ib_ucm_send_lap(struct ib_ucm_file *file, | |
1071 | const char __user *inbuf, | |
1072 | int in_len, int out_len) | |
1073 | { | |
1074 | struct ib_ucm_context *ctx; | |
1075 | struct ib_sa_path_rec *path = NULL; | |
1076 | struct ib_ucm_lap cmd; | |
1077 | const void *data = NULL; | |
1078 | int result; | |
1079 | ||
1080 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
1081 | return -EFAULT; | |
1082 | ||
1083 | result = ib_ucm_alloc_data(&data, cmd.data, cmd.len); | |
1084 | if (result) | |
1085 | goto done; | |
1086 | ||
1087 | result = ib_ucm_path_get(&path, cmd.path); | |
1088 | if (result) | |
1089 | goto done; | |
1090 | ||
b9ef520f SH |
1091 | ctx = ib_ucm_ctx_get(file, cmd.id); |
1092 | if (!IS_ERR(ctx)) { | |
a5b74540 | 1093 | result = ib_send_cm_lap(ctx->cm_id, path, data, cmd.len); |
b9ef520f SH |
1094 | ib_ucm_ctx_put(ctx); |
1095 | } else | |
1096 | result = PTR_ERR(ctx); | |
a5b74540 | 1097 | |
a5b74540 | 1098 | done: |
79d81907 HR |
1099 | kfree(data); |
1100 | kfree(path); | |
a5b74540 HR |
1101 | return result; |
1102 | } | |
1103 | ||
1104 | static ssize_t ib_ucm_send_sidr_req(struct ib_ucm_file *file, | |
1105 | const char __user *inbuf, | |
1106 | int in_len, int out_len) | |
1107 | { | |
1108 | struct ib_cm_sidr_req_param param; | |
1109 | struct ib_ucm_context *ctx; | |
1110 | struct ib_ucm_sidr_req cmd; | |
1111 | int result; | |
1112 | ||
1113 | param.private_data = NULL; | |
1114 | param.path = NULL; | |
1115 | ||
1116 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
1117 | return -EFAULT; | |
1118 | ||
1119 | result = ib_ucm_alloc_data(¶m.private_data, cmd.data, cmd.len); | |
1120 | if (result) | |
1121 | goto done; | |
1122 | ||
1123 | result = ib_ucm_path_get(¶m.path, cmd.path); | |
1124 | if (result) | |
1125 | goto done; | |
1126 | ||
1127 | param.private_data_len = cmd.len; | |
1128 | param.service_id = cmd.sid; | |
1129 | param.timeout_ms = cmd.timeout; | |
1130 | param.max_cm_retries = cmd.max_cm_retries; | |
1131 | param.pkey = cmd.pkey; | |
1132 | ||
b9ef520f SH |
1133 | ctx = ib_ucm_ctx_get(file, cmd.id); |
1134 | if (!IS_ERR(ctx)) { | |
a5b74540 | 1135 | result = ib_send_cm_sidr_req(ctx->cm_id, ¶m); |
b9ef520f SH |
1136 | ib_ucm_ctx_put(ctx); |
1137 | } else | |
1138 | result = PTR_ERR(ctx); | |
a5b74540 | 1139 | |
a5b74540 | 1140 | done: |
79d81907 HR |
1141 | kfree(param.private_data); |
1142 | kfree(param.path); | |
a5b74540 HR |
1143 | return result; |
1144 | } | |
1145 | ||
1146 | static ssize_t ib_ucm_send_sidr_rep(struct ib_ucm_file *file, | |
1147 | const char __user *inbuf, | |
1148 | int in_len, int out_len) | |
1149 | { | |
1150 | struct ib_cm_sidr_rep_param param; | |
1151 | struct ib_ucm_sidr_rep cmd; | |
1152 | struct ib_ucm_context *ctx; | |
1153 | int result; | |
1154 | ||
1155 | param.info = NULL; | |
1156 | ||
1157 | if (copy_from_user(&cmd, inbuf, sizeof(cmd))) | |
1158 | return -EFAULT; | |
1159 | ||
1160 | result = ib_ucm_alloc_data(¶m.private_data, | |
1161 | cmd.data, cmd.data_len); | |
1162 | if (result) | |
1163 | goto done; | |
1164 | ||
1165 | result = ib_ucm_alloc_data(¶m.info, cmd.info, cmd.info_len); | |
1166 | if (result) | |
1167 | goto done; | |
1168 | ||
b9ef520f SH |
1169 | param.qp_num = cmd.qpn; |
1170 | param.qkey = cmd.qkey; | |
1171 | param.status = cmd.status; | |
1172 | param.info_length = cmd.info_len; | |
1173 | param.private_data_len = cmd.data_len; | |
a5b74540 | 1174 | |
b9ef520f SH |
1175 | ctx = ib_ucm_ctx_get(file, cmd.id); |
1176 | if (!IS_ERR(ctx)) { | |
a5b74540 | 1177 | result = ib_send_cm_sidr_rep(ctx->cm_id, ¶m); |
b9ef520f SH |
1178 | ib_ucm_ctx_put(ctx); |
1179 | } else | |
1180 | result = PTR_ERR(ctx); | |
a5b74540 | 1181 | |
a5b74540 | 1182 | done: |
79d81907 HR |
1183 | kfree(param.private_data); |
1184 | kfree(param.info); | |
a5b74540 HR |
1185 | return result; |
1186 | } | |
1187 | ||
1188 | static ssize_t (*ucm_cmd_table[])(struct ib_ucm_file *file, | |
1189 | const char __user *inbuf, | |
1190 | int in_len, int out_len) = { | |
1191 | [IB_USER_CM_CMD_CREATE_ID] = ib_ucm_create_id, | |
1192 | [IB_USER_CM_CMD_DESTROY_ID] = ib_ucm_destroy_id, | |
1193 | [IB_USER_CM_CMD_ATTR_ID] = ib_ucm_attr_id, | |
1194 | [IB_USER_CM_CMD_LISTEN] = ib_ucm_listen, | |
1195 | [IB_USER_CM_CMD_ESTABLISH] = ib_ucm_establish, | |
1196 | [IB_USER_CM_CMD_SEND_REQ] = ib_ucm_send_req, | |
1197 | [IB_USER_CM_CMD_SEND_REP] = ib_ucm_send_rep, | |
1198 | [IB_USER_CM_CMD_SEND_RTU] = ib_ucm_send_rtu, | |
1199 | [IB_USER_CM_CMD_SEND_DREQ] = ib_ucm_send_dreq, | |
1200 | [IB_USER_CM_CMD_SEND_DREP] = ib_ucm_send_drep, | |
1201 | [IB_USER_CM_CMD_SEND_REJ] = ib_ucm_send_rej, | |
1202 | [IB_USER_CM_CMD_SEND_MRA] = ib_ucm_send_mra, | |
1203 | [IB_USER_CM_CMD_SEND_LAP] = ib_ucm_send_lap, | |
1204 | [IB_USER_CM_CMD_SEND_APR] = ib_ucm_send_apr, | |
1205 | [IB_USER_CM_CMD_SEND_SIDR_REQ] = ib_ucm_send_sidr_req, | |
1206 | [IB_USER_CM_CMD_SEND_SIDR_REP] = ib_ucm_send_sidr_rep, | |
1207 | [IB_USER_CM_CMD_EVENT] = ib_ucm_event, | |
0b2b35f6 | 1208 | [IB_USER_CM_CMD_INIT_QP_ATTR] = ib_ucm_init_qp_attr, |
a5b74540 HR |
1209 | }; |
1210 | ||
1211 | static ssize_t ib_ucm_write(struct file *filp, const char __user *buf, | |
1212 | size_t len, loff_t *pos) | |
1213 | { | |
1214 | struct ib_ucm_file *file = filp->private_data; | |
1215 | struct ib_ucm_cmd_hdr hdr; | |
1216 | ssize_t result; | |
1217 | ||
1218 | if (len < sizeof(hdr)) | |
1219 | return -EINVAL; | |
1220 | ||
1221 | if (copy_from_user(&hdr, buf, sizeof(hdr))) | |
1222 | return -EFAULT; | |
1223 | ||
a5b74540 HR |
1224 | if (hdr.cmd < 0 || hdr.cmd >= ARRAY_SIZE(ucm_cmd_table)) |
1225 | return -EINVAL; | |
1226 | ||
1227 | if (hdr.in + sizeof(hdr) > len) | |
1228 | return -EINVAL; | |
1229 | ||
1230 | result = ucm_cmd_table[hdr.cmd](file, buf + sizeof(hdr), | |
1231 | hdr.in, hdr.out); | |
1232 | if (!result) | |
1233 | result = len; | |
1234 | ||
1235 | return result; | |
1236 | } | |
1237 | ||
1238 | static unsigned int ib_ucm_poll(struct file *filp, | |
1239 | struct poll_table_struct *wait) | |
1240 | { | |
1241 | struct ib_ucm_file *file = filp->private_data; | |
1242 | unsigned int mask = 0; | |
1243 | ||
1244 | poll_wait(filp, &file->poll_wait, wait); | |
1245 | ||
1246 | if (!list_empty(&file->events)) | |
1247 | mask = POLLIN | POLLRDNORM; | |
1248 | ||
1249 | return mask; | |
1250 | } | |
1251 | ||
1252 | static int ib_ucm_open(struct inode *inode, struct file *filp) | |
1253 | { | |
1254 | struct ib_ucm_file *file; | |
1255 | ||
1256 | file = kmalloc(sizeof(*file), GFP_KERNEL); | |
1257 | if (!file) | |
1258 | return -ENOMEM; | |
1259 | ||
1260 | INIT_LIST_HEAD(&file->events); | |
1261 | INIT_LIST_HEAD(&file->ctxs); | |
1262 | init_waitqueue_head(&file->poll_wait); | |
1263 | ||
1264 | init_MUTEX(&file->mutex); | |
1265 | ||
1266 | filp->private_data = file; | |
1267 | file->filp = filp; | |
07d357d0 | 1268 | file->device = container_of(inode->i_cdev, struct ib_ucm_device, dev); |
a5b74540 | 1269 | |
a5b74540 HR |
1270 | return 0; |
1271 | } | |
1272 | ||
1273 | static int ib_ucm_close(struct inode *inode, struct file *filp) | |
1274 | { | |
1275 | struct ib_ucm_file *file = filp->private_data; | |
1276 | struct ib_ucm_context *ctx; | |
1277 | ||
1278 | down(&file->mutex); | |
a5b74540 | 1279 | while (!list_empty(&file->ctxs)) { |
a5b74540 HR |
1280 | ctx = list_entry(file->ctxs.next, |
1281 | struct ib_ucm_context, file_list); | |
b9ef520f | 1282 | up(&file->mutex); |
0b2b35f6 | 1283 | |
95ed644f | 1284 | mutex_lock(&ctx_id_mutex); |
0b2b35f6 | 1285 | idr_remove(&ctx_id_table, ctx->id); |
95ed644f | 1286 | mutex_unlock(&ctx_id_mutex); |
0b2b35f6 SH |
1287 | |
1288 | ib_destroy_cm_id(ctx->cm_id); | |
1289 | ib_ucm_cleanup_events(ctx); | |
1290 | kfree(ctx); | |
1291 | ||
a5b74540 HR |
1292 | down(&file->mutex); |
1293 | } | |
a5b74540 | 1294 | up(&file->mutex); |
a5b74540 | 1295 | kfree(file); |
a5b74540 HR |
1296 | return 0; |
1297 | } | |
1298 | ||
07d357d0 SH |
1299 | static void ib_ucm_release_class_dev(struct class_device *class_dev) |
1300 | { | |
1301 | struct ib_ucm_device *dev; | |
1302 | ||
1303 | dev = container_of(class_dev, struct ib_ucm_device, class_dev); | |
1304 | cdev_del(&dev->dev); | |
1305 | clear_bit(dev->devnum, dev_map); | |
1306 | kfree(dev); | |
1307 | } | |
1308 | ||
1309 | static struct file_operations ucm_fops = { | |
a5b74540 HR |
1310 | .owner = THIS_MODULE, |
1311 | .open = ib_ucm_open, | |
1312 | .release = ib_ucm_close, | |
1313 | .write = ib_ucm_write, | |
1314 | .poll = ib_ucm_poll, | |
1315 | }; | |
1316 | ||
07d357d0 SH |
1317 | static struct class ucm_class = { |
1318 | .name = "infiniband_cm", | |
1319 | .release = ib_ucm_release_class_dev | |
1320 | }; | |
1321 | ||
07d357d0 SH |
1322 | static ssize_t show_ibdev(struct class_device *class_dev, char *buf) |
1323 | { | |
1324 | struct ib_ucm_device *dev; | |
1325 | ||
1326 | dev = container_of(class_dev, struct ib_ucm_device, class_dev); | |
1327 | return sprintf(buf, "%s\n", dev->ib_dev->name); | |
1328 | } | |
1329 | static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL); | |
a5b74540 | 1330 | |
07d357d0 | 1331 | static void ib_ucm_add_one(struct ib_device *device) |
a5b74540 | 1332 | { |
07d357d0 | 1333 | struct ib_ucm_device *ucm_dev; |
a5b74540 | 1334 | |
07d357d0 SH |
1335 | if (!device->alloc_ucontext) |
1336 | return; | |
1337 | ||
de6eb66b | 1338 | ucm_dev = kzalloc(sizeof *ucm_dev, GFP_KERNEL); |
07d357d0 SH |
1339 | if (!ucm_dev) |
1340 | return; | |
a5b74540 | 1341 | |
07d357d0 | 1342 | ucm_dev->ib_dev = device; |
a5b74540 | 1343 | |
07d357d0 SH |
1344 | ucm_dev->devnum = find_first_zero_bit(dev_map, IB_UCM_MAX_DEVICES); |
1345 | if (ucm_dev->devnum >= IB_UCM_MAX_DEVICES) | |
1346 | goto err; | |
1347 | ||
1348 | set_bit(ucm_dev->devnum, dev_map); | |
1349 | ||
1350 | cdev_init(&ucm_dev->dev, &ucm_fops); | |
1351 | ucm_dev->dev.owner = THIS_MODULE; | |
1352 | kobject_set_name(&ucm_dev->dev.kobj, "ucm%d", ucm_dev->devnum); | |
1353 | if (cdev_add(&ucm_dev->dev, IB_UCM_BASE_DEV + ucm_dev->devnum, 1)) | |
1354 | goto err; | |
1355 | ||
1356 | ucm_dev->class_dev.class = &ucm_class; | |
1357 | ucm_dev->class_dev.dev = device->dma_device; | |
68f5f996 | 1358 | ucm_dev->class_dev.devt = ucm_dev->dev.dev; |
07d357d0 SH |
1359 | snprintf(ucm_dev->class_dev.class_id, BUS_ID_SIZE, "ucm%d", |
1360 | ucm_dev->devnum); | |
1361 | if (class_device_register(&ucm_dev->class_dev)) | |
a5b74540 | 1362 | goto err_cdev; |
a5b74540 | 1363 | |
07d357d0 SH |
1364 | if (class_device_create_file(&ucm_dev->class_dev, |
1365 | &class_device_attr_ibdev)) | |
a5b74540 | 1366 | goto err_class; |
07d357d0 SH |
1367 | |
1368 | ib_set_client_data(device, &ucm_client, ucm_dev); | |
1369 | return; | |
1370 | ||
1371 | err_class: | |
1372 | class_device_unregister(&ucm_dev->class_dev); | |
1373 | err_cdev: | |
1374 | cdev_del(&ucm_dev->dev); | |
1375 | clear_bit(ucm_dev->devnum, dev_map); | |
1376 | err: | |
1377 | kfree(ucm_dev); | |
1378 | return; | |
1379 | } | |
1380 | ||
1381 | static void ib_ucm_remove_one(struct ib_device *device) | |
1382 | { | |
1383 | struct ib_ucm_device *ucm_dev = ib_get_client_data(device, &ucm_client); | |
1384 | ||
1385 | if (!ucm_dev) | |
1386 | return; | |
1387 | ||
1388 | class_device_unregister(&ucm_dev->class_dev); | |
1389 | } | |
1390 | ||
1391 | static ssize_t show_abi_version(struct class *class, char *buf) | |
1392 | { | |
1393 | return sprintf(buf, "%d\n", IB_USER_CM_ABI_VERSION); | |
1394 | } | |
1395 | static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL); | |
1396 | ||
1397 | static int __init ib_ucm_init(void) | |
1398 | { | |
1399 | int ret; | |
1400 | ||
1401 | ret = register_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES, | |
1402 | "infiniband_cm"); | |
1403 | if (ret) { | |
1404 | printk(KERN_ERR "ucm: couldn't register device number\n"); | |
1405 | goto err; | |
a5b74540 HR |
1406 | } |
1407 | ||
07d357d0 SH |
1408 | ret = class_register(&ucm_class); |
1409 | if (ret) { | |
1410 | printk(KERN_ERR "ucm: couldn't create class infiniband_cm\n"); | |
1411 | goto err_chrdev; | |
1412 | } | |
a5b74540 | 1413 | |
07d357d0 SH |
1414 | ret = class_create_file(&ucm_class, &class_attr_abi_version); |
1415 | if (ret) { | |
1416 | printk(KERN_ERR "ucm: couldn't create abi_version attribute\n"); | |
1417 | goto err_class; | |
1418 | } | |
a5b74540 | 1419 | |
07d357d0 SH |
1420 | ret = ib_register_client(&ucm_client); |
1421 | if (ret) { | |
1422 | printk(KERN_ERR "ucm: couldn't register client\n"); | |
1423 | goto err_class; | |
1424 | } | |
a5b74540 | 1425 | return 0; |
07d357d0 | 1426 | |
a5b74540 | 1427 | err_class: |
07d357d0 SH |
1428 | class_unregister(&ucm_class); |
1429 | err_chrdev: | |
1430 | unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES); | |
1431 | err: | |
1432 | return ret; | |
a5b74540 HR |
1433 | } |
1434 | ||
1435 | static void __exit ib_ucm_cleanup(void) | |
1436 | { | |
07d357d0 SH |
1437 | ib_unregister_client(&ucm_client); |
1438 | class_unregister(&ucm_class); | |
1439 | unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES); | |
5d7edb3c | 1440 | idr_destroy(&ctx_id_table); |
a5b74540 HR |
1441 | } |
1442 | ||
1443 | module_init(ib_ucm_init); | |
1444 | module_exit(ib_ucm_cleanup); |