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