ucm.c 33.4 KB
Newer Older
1 2
/*
 * Copyright (c) 2005 Topspin Communications.  All rights reserved.
3
 * Copyright (c) 2005 Intel Corporation.  All rights reserved.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *	copyright notice, this list of conditions and the following
 *	disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *	copyright notice, this list of conditions and the following
 *	disclaimer in the documentation and/or other materials
 *	provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
S
Sean Hefty 已提交
33 34

#include <linux/completion.h>
35 36 37 38 39 40
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/poll.h>
41
#include <linux/sched.h>
42 43 44
#include <linux/file.h>
#include <linux/mount.h>
#include <linux/cdev.h>
S
Sean Hefty 已提交
45
#include <linux/idr.h>
46
#include <linux/mutex.h>
47
#include <linux/slab.h>
48 49 50

#include <asm/uaccess.h>

51
#include <rdma/ib.h>
S
Sean Hefty 已提交
52 53
#include <rdma/ib_cm.h>
#include <rdma/ib_user_cm.h>
54
#include <rdma/ib_marshall.h>
55 56 57 58 59

MODULE_AUTHOR("Libor Michalek");
MODULE_DESCRIPTION("InfiniBand userspace Connection Manager access");
MODULE_LICENSE("Dual BSD/GPL");

60 61
struct ib_ucm_device {
	int			devnum;
62 63
	struct cdev		cdev;
	struct device		dev;
64 65 66
	struct ib_device	*ib_dev;
};

S
Sean Hefty 已提交
67
struct ib_ucm_file {
S
Sean Hefty 已提交
68
	struct mutex file_mutex;
S
Sean Hefty 已提交
69
	struct file *filp;
70
	struct ib_ucm_device *device;
H
Hal Rosenstock 已提交
71

72 73
	struct list_head  ctxs;
	struct list_head  events;
S
Sean Hefty 已提交
74 75 76 77 78
	wait_queue_head_t poll_wait;
};

struct ib_ucm_context {
	int                 id;
S
Sean Hefty 已提交
79
	struct completion   comp;
S
Sean Hefty 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	atomic_t            ref;
	int		    events_reported;

	struct ib_ucm_file *file;
	struct ib_cm_id    *cm_id;
	__u64		   uid;

	struct list_head    events;    /* list of pending events. */
	struct list_head    file_list; /* member in file ctx list */
};

struct ib_ucm_event {
	struct ib_ucm_context *ctx;
	struct list_head file_list; /* member in file event list */
	struct list_head ctx_list;  /* member in ctx event list */

	struct ib_cm_id *cm_id;
	struct ib_ucm_event_resp resp;
	void *data;
	void *info;
	int data_len;
	int info_len;
};
H
Hal Rosenstock 已提交
103

104 105
enum {
	IB_UCM_MAJOR = 231,
106 107
	IB_UCM_BASE_MINOR = 224,
	IB_UCM_MAX_DEVICES = 32
108 109
};

110
#define IB_UCM_BASE_DEV MKDEV(IB_UCM_MAJOR, IB_UCM_BASE_MINOR)
111

112
static void ib_ucm_add_one(struct ib_device *device);
113
static void ib_ucm_remove_one(struct ib_device *device, void *client_data);
114

115 116 117 118 119 120
static struct ib_client ucm_client = {
	.name   = "ucm",
	.add    = ib_ucm_add_one,
	.remove = ib_ucm_remove_one
};

121
static DEFINE_MUTEX(ctx_id_mutex);
R
Roland Dreier 已提交
122
static DEFINE_IDR(ctx_id_table);
123
static DECLARE_BITMAP(dev_map, IB_UCM_MAX_DEVICES);
S
Sean Hefty 已提交
124

125
static struct ib_ucm_context *ib_ucm_ctx_get(struct ib_ucm_file *file, int id)
126 127 128
{
	struct ib_ucm_context *ctx;

129
	mutex_lock(&ctx_id_mutex);
130
	ctx = idr_find(&ctx_id_table, id);
131 132 133 134 135 136
	if (!ctx)
		ctx = ERR_PTR(-ENOENT);
	else if (ctx->file != file)
		ctx = ERR_PTR(-EINVAL);
	else
		atomic_inc(&ctx->ref);
137
	mutex_unlock(&ctx_id_mutex);
138 139 140 141 142 143

	return ctx;
}

static void ib_ucm_ctx_put(struct ib_ucm_context *ctx)
{
144
	if (atomic_dec_and_test(&ctx->ref))
S
Sean Hefty 已提交
145
		complete(&ctx->comp);
146 147
}

148
static inline int ib_ucm_new_cm_id(int event)
149
{
150 151
	return event == IB_CM_REQ_RECEIVED || event == IB_CM_SIDR_REQ_RECEIVED;
}
152

153 154 155
static void ib_ucm_cleanup_events(struct ib_ucm_context *ctx)
{
	struct ib_ucm_event *uevent;
156

S
Sean Hefty 已提交
157
	mutex_lock(&ctx->file->file_mutex);
158 159 160 161 162 163 164
	list_del(&ctx->file_list);
	while (!list_empty(&ctx->events)) {

		uevent = list_entry(ctx->events.next,
				    struct ib_ucm_event, ctx_list);
		list_del(&uevent->file_list);
		list_del(&uevent->ctx_list);
165
		mutex_unlock(&ctx->file->file_mutex);
166 167

		/* clear incoming connections. */
168
		if (ib_ucm_new_cm_id(uevent->resp.event))
169 170 171
			ib_destroy_cm_id(uevent->cm_id);

		kfree(uevent);
172
		mutex_lock(&ctx->file->file_mutex);
173
	}
S
Sean Hefty 已提交
174
	mutex_unlock(&ctx->file->file_mutex);
175 176 177 178 179 180
}

static struct ib_ucm_context *ib_ucm_ctx_alloc(struct ib_ucm_file *file)
{
	struct ib_ucm_context *ctx;

R
Roland Dreier 已提交
181
	ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
182 183 184
	if (!ctx)
		return NULL;

185
	atomic_set(&ctx->ref, 1);
S
Sean Hefty 已提交
186
	init_completion(&ctx->comp);
187 188 189
	ctx->file = file;
	INIT_LIST_HEAD(&ctx->events);

T
Tejun Heo 已提交
190 191 192 193
	mutex_lock(&ctx_id_mutex);
	ctx->id = idr_alloc(&ctx_id_table, ctx, 0, 0, GFP_KERNEL);
	mutex_unlock(&ctx_id_mutex);
	if (ctx->id < 0)
194 195
		goto error;

196
	list_add_tail(&ctx->file_list, &file->ctxs);
197
	return ctx;
198

199 200 201 202
error:
	kfree(ctx);
	return NULL;
}
203

204
static void ib_ucm_event_req_get(struct ib_ucm_req_event_resp *ureq,
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
				 struct ib_cm_req_event_param *kreq)
{
	ureq->remote_ca_guid             = kreq->remote_ca_guid;
	ureq->remote_qkey                = kreq->remote_qkey;
	ureq->remote_qpn                 = kreq->remote_qpn;
	ureq->qp_type                    = kreq->qp_type;
	ureq->starting_psn               = kreq->starting_psn;
	ureq->responder_resources        = kreq->responder_resources;
	ureq->initiator_depth            = kreq->initiator_depth;
	ureq->local_cm_response_timeout  = kreq->local_cm_response_timeout;
	ureq->flow_control               = kreq->flow_control;
	ureq->remote_cm_response_timeout = kreq->remote_cm_response_timeout;
	ureq->retry_count                = kreq->retry_count;
	ureq->rnr_retry_count            = kreq->rnr_retry_count;
	ureq->srq                        = kreq->srq;
220
	ureq->port			 = kreq->port;
221

222 223 224 225
	ib_copy_path_rec_to_user(&ureq->primary_path, kreq->primary_path);
	if (kreq->alternate_path)
		ib_copy_path_rec_to_user(&ureq->alternate_path,
					 kreq->alternate_path);
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
}

static void ib_ucm_event_rep_get(struct ib_ucm_rep_event_resp *urep,
				 struct ib_cm_rep_event_param *krep)
{
	urep->remote_ca_guid      = krep->remote_ca_guid;
	urep->remote_qkey         = krep->remote_qkey;
	urep->remote_qpn          = krep->remote_qpn;
	urep->starting_psn        = krep->starting_psn;
	urep->responder_resources = krep->responder_resources;
	urep->initiator_depth     = krep->initiator_depth;
	urep->target_ack_delay    = krep->target_ack_delay;
	urep->failover_accepted   = krep->failover_accepted;
	urep->flow_control        = krep->flow_control;
	urep->rnr_retry_count     = krep->rnr_retry_count;
	urep->srq                 = krep->srq;
}

static void ib_ucm_event_sidr_rep_get(struct ib_ucm_sidr_rep_event_resp *urep,
				      struct ib_cm_sidr_rep_event_param *krep)
{
	urep->status = krep->status;
	urep->qkey   = krep->qkey;
	urep->qpn    = krep->qpn;
};

252
static int ib_ucm_event_process(struct ib_cm_event *evt,
253 254 255 256 257 258
				struct ib_ucm_event *uvt)
{
	void *info = NULL;

	switch (evt->event) {
	case IB_CM_REQ_RECEIVED:
259
		ib_ucm_event_req_get(&uvt->resp.u.req_resp,
260 261
				     &evt->param.req_rcvd);
		uvt->data_len      = IB_CM_REQ_PRIVATE_DATA_SIZE;
262
		uvt->resp.present  = IB_UCM_PRES_PRIMARY;
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
		uvt->resp.present |= (evt->param.req_rcvd.alternate_path ?
				      IB_UCM_PRES_ALTERNATE : 0);
		break;
	case IB_CM_REP_RECEIVED:
		ib_ucm_event_rep_get(&uvt->resp.u.rep_resp,
				     &evt->param.rep_rcvd);
		uvt->data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
		break;
	case IB_CM_RTU_RECEIVED:
		uvt->data_len = IB_CM_RTU_PRIVATE_DATA_SIZE;
		uvt->resp.u.send_status = evt->param.send_status;
		break;
	case IB_CM_DREQ_RECEIVED:
		uvt->data_len = IB_CM_DREQ_PRIVATE_DATA_SIZE;
		uvt->resp.u.send_status = evt->param.send_status;
		break;
	case IB_CM_DREP_RECEIVED:
		uvt->data_len = IB_CM_DREP_PRIVATE_DATA_SIZE;
		uvt->resp.u.send_status = evt->param.send_status;
		break;
	case IB_CM_MRA_RECEIVED:
284 285
		uvt->resp.u.mra_resp.timeout =
					evt->param.mra_rcvd.service_timeout;
286 287 288
		uvt->data_len = IB_CM_MRA_PRIVATE_DATA_SIZE;
		break;
	case IB_CM_REJ_RECEIVED:
289
		uvt->resp.u.rej_resp.reason = evt->param.rej_rcvd.reason;
290 291 292 293 294
		uvt->data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
		uvt->info_len = evt->param.rej_rcvd.ari_length;
		info	      = evt->param.rej_rcvd.ari;
		break;
	case IB_CM_LAP_RECEIVED:
295 296
		ib_copy_path_rec_to_user(&uvt->resp.u.lap_resp.path,
					 evt->param.lap_rcvd.alternate_path);
297
		uvt->data_len = IB_CM_LAP_PRIVATE_DATA_SIZE;
298
		uvt->resp.present = IB_UCM_PRES_ALTERNATE;
299 300
		break;
	case IB_CM_APR_RECEIVED:
301
		uvt->resp.u.apr_resp.status = evt->param.apr_rcvd.ap_status;
302 303 304 305 306
		uvt->data_len = IB_CM_APR_PRIVATE_DATA_SIZE;
		uvt->info_len = evt->param.apr_rcvd.info_len;
		info	      = evt->param.apr_rcvd.apr_info;
		break;
	case IB_CM_SIDR_REQ_RECEIVED:
R
Roland Dreier 已提交
307
		uvt->resp.u.sidr_req_resp.pkey =
308
					evt->param.sidr_req_rcvd.pkey;
R
Roland Dreier 已提交
309
		uvt->resp.u.sidr_req_resp.port =
310
					evt->param.sidr_req_rcvd.port;
311 312 313 314 315 316 317 318 319 320 321 322 323 324
		uvt->data_len = IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE;
		break;
	case IB_CM_SIDR_REP_RECEIVED:
		ib_ucm_event_sidr_rep_get(&uvt->resp.u.sidr_rep_resp,
					  &evt->param.sidr_rep_rcvd);
		uvt->data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
		uvt->info_len = evt->param.sidr_rep_rcvd.info_len;
		info	      = evt->param.sidr_rep_rcvd.info;
		break;
	default:
		uvt->resp.u.send_status = evt->param.send_status;
		break;
	}

325
	if (uvt->data_len) {
E
Eric Sesterhenn 已提交
326
		uvt->data = kmemdup(evt->private_data, uvt->data_len, GFP_KERNEL);
327 328
		if (!uvt->data)
			goto err1;
329 330 331 332

		uvt->resp.present |= IB_UCM_PRES_DATA;
	}

333
	if (uvt->info_len) {
E
Eric Sesterhenn 已提交
334
		uvt->info = kmemdup(info, uvt->info_len, GFP_KERNEL);
335 336
		if (!uvt->info)
			goto err2;
337 338 339 340

		uvt->resp.present |= IB_UCM_PRES_INFO;
	}
	return 0;
341 342

err2:
H
Hal Rosenstock 已提交
343
	kfree(uvt->data);
344 345
err1:
	return -ENOMEM;
346 347 348 349 350 351 352 353 354
}

static int ib_ucm_event_handler(struct ib_cm_id *cm_id,
				struct ib_cm_event *event)
{
	struct ib_ucm_event *uevent;
	struct ib_ucm_context *ctx;
	int result = 0;

355
	ctx = cm_id->context;
356

R
Roland Dreier 已提交
357
	uevent = kzalloc(sizeof *uevent, GFP_KERNEL);
358 359
	if (!uevent)
		goto err1;
360

361 362 363 364
	uevent->ctx = ctx;
	uevent->cm_id = cm_id;
	uevent->resp.uid = ctx->uid;
	uevent->resp.id = ctx->id;
365 366
	uevent->resp.event = event->event;

367
	result = ib_ucm_event_process(event, uevent);
368
	if (result)
369
		goto err2;
370

S
Sean Hefty 已提交
371
	mutex_lock(&ctx->file->file_mutex);
372 373 374
	list_add_tail(&uevent->file_list, &ctx->file->events);
	list_add_tail(&uevent->ctx_list, &ctx->events);
	wake_up_interruptible(&ctx->file->poll_wait);
S
Sean Hefty 已提交
375
	mutex_unlock(&ctx->file->file_mutex);
376 377 378 379 380 381
	return 0;

err2:
	kfree(uevent);
err1:
	/* Destroy new cm_id's */
382
	return ib_ucm_new_cm_id(event->event);
383 384 385 386 387 388 389 390
}

static ssize_t ib_ucm_event(struct ib_ucm_file *file,
			    const char __user *inbuf,
			    int in_len, int out_len)
{
	struct ib_ucm_context *ctx;
	struct ib_ucm_event_get cmd;
391
	struct ib_ucm_event *uevent;
392 393 394 395 396 397 398
	int result = 0;

	if (out_len < sizeof(struct ib_ucm_event_resp))
		return -ENOSPC;

	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
		return -EFAULT;
399

S
Sean Hefty 已提交
400
	mutex_lock(&file->file_mutex);
401
	while (list_empty(&file->events)) {
S
Sean Hefty 已提交
402
		mutex_unlock(&file->file_mutex);
403

S
Sean Hefty 已提交
404 405
		if (file->filp->f_flags & O_NONBLOCK)
			return -EAGAIN;
406

S
Sean Hefty 已提交
407 408 409
		if (wait_event_interruptible(file->poll_wait,
					     !list_empty(&file->events)))
			return -ERESTARTSYS;
410

S
Sean Hefty 已提交
411
		mutex_lock(&file->file_mutex);
412 413 414 415
	}

	uevent = list_entry(file->events.next, struct ib_ucm_event, file_list);

416 417 418 419 420 421
	if (ib_ucm_new_cm_id(uevent->resp.event)) {
		ctx = ib_ucm_ctx_alloc(file);
		if (!ctx) {
			result = -ENOMEM;
			goto done;
		}
422

423 424 425
		ctx->cm_id = uevent->cm_id;
		ctx->cm_id->context = ctx;
		uevent->resp.id = ctx->id;
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
	}

	if (copy_to_user((void __user *)(unsigned long)cmd.response,
			 &uevent->resp, sizeof(uevent->resp))) {
		result = -EFAULT;
		goto done;
	}

	if (uevent->data) {
		if (cmd.data_len < uevent->data_len) {
			result = -ENOMEM;
			goto done;
		}
		if (copy_to_user((void __user *)(unsigned long)cmd.data,
				 uevent->data, uevent->data_len)) {
			result = -EFAULT;
			goto done;
		}
	}

	if (uevent->info) {
		if (cmd.info_len < uevent->info_len) {
			result = -ENOMEM;
			goto done;
		}
		if (copy_to_user((void __user *)(unsigned long)cmd.info,
				 uevent->info, uevent->info_len)) {
			result = -EFAULT;
			goto done;
		}
	}

	list_del(&uevent->file_list);
	list_del(&uevent->ctx_list);
460
	uevent->ctx->events_reported++;
461

H
Hal Rosenstock 已提交
462 463
	kfree(uevent->data);
	kfree(uevent->info);
464 465
	kfree(uevent);
done:
S
Sean Hefty 已提交
466
	mutex_unlock(&file->file_mutex);
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
	return result;
}

static ssize_t ib_ucm_create_id(struct ib_ucm_file *file,
				const char __user *inbuf,
				int in_len, int out_len)
{
	struct ib_ucm_create_id cmd;
	struct ib_ucm_create_id_resp resp;
	struct ib_ucm_context *ctx;
	int result;

	if (out_len < sizeof(resp))
		return -ENOSPC;

	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
		return -EFAULT;

S
Sean Hefty 已提交
485
	mutex_lock(&file->file_mutex);
486
	ctx = ib_ucm_ctx_alloc(file);
S
Sean Hefty 已提交
487
	mutex_unlock(&file->file_mutex);
488 489 490
	if (!ctx)
		return -ENOMEM;

491
	ctx->uid = cmd.uid;
492 493
	ctx->cm_id = ib_create_cm_id(file->device->ib_dev,
				     ib_ucm_event_handler, ctx);
494 495
	if (IS_ERR(ctx->cm_id)) {
		result = PTR_ERR(ctx->cm_id);
496
		goto err1;
497 498 499 500 501 502
	}

	resp.id = ctx->id;
	if (copy_to_user((void __user *)(unsigned long)cmd.response,
			 &resp, sizeof(resp))) {
		result = -EFAULT;
503
		goto err2;
504 505 506
	}
	return 0;

507 508 509
err2:
	ib_destroy_cm_id(ctx->cm_id);
err1:
510
	mutex_lock(&ctx_id_mutex);
511
	idr_remove(&ctx_id_table, ctx->id);
512
	mutex_unlock(&ctx_id_mutex);
513
	kfree(ctx);
514 515 516 517 518 519 520 521
	return result;
}

static ssize_t ib_ucm_destroy_id(struct ib_ucm_file *file,
				 const char __user *inbuf,
				 int in_len, int out_len)
{
	struct ib_ucm_destroy_id cmd;
522 523 524 525 526 527
	struct ib_ucm_destroy_id_resp resp;
	struct ib_ucm_context *ctx;
	int result = 0;

	if (out_len < sizeof(resp))
		return -ENOSPC;
528 529 530 531

	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
		return -EFAULT;

532
	mutex_lock(&ctx_id_mutex);
533 534 535 536 537 538 539
	ctx = idr_find(&ctx_id_table, cmd.id);
	if (!ctx)
		ctx = ERR_PTR(-ENOENT);
	else if (ctx->file != file)
		ctx = ERR_PTR(-EINVAL);
	else
		idr_remove(&ctx_id_table, ctx->id);
540
	mutex_unlock(&ctx_id_mutex);
541 542 543 544

	if (IS_ERR(ctx))
		return PTR_ERR(ctx);

S
Sean Hefty 已提交
545 546
	ib_ucm_ctx_put(ctx);
	wait_for_completion(&ctx->comp);
547 548 549 550 551 552 553 554 555 556 557 558 559

	/* No new events will be generated after destroying the cm_id. */
	ib_destroy_cm_id(ctx->cm_id);
	/* Cleanup events not yet reported to the user. */
	ib_ucm_cleanup_events(ctx);

	resp.events_reported = ctx->events_reported;
	if (copy_to_user((void __user *)(unsigned long)cmd.response,
			 &resp, sizeof(resp)))
		result = -EFAULT;

	kfree(ctx);
	return result;
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
}

static ssize_t ib_ucm_attr_id(struct ib_ucm_file *file,
			      const char __user *inbuf,
			      int in_len, int out_len)
{
	struct ib_ucm_attr_id_resp resp;
	struct ib_ucm_attr_id cmd;
	struct ib_ucm_context *ctx;
	int result = 0;

	if (out_len < sizeof(resp))
		return -ENOSPC;

	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
		return -EFAULT;

577 578 579
	ctx = ib_ucm_ctx_get(file, cmd.id);
	if (IS_ERR(ctx))
		return PTR_ERR(ctx);
580 581 582 583 584 585 586 587 588 589

	resp.service_id   = ctx->cm_id->service_id;
	resp.service_mask = ctx->cm_id->service_mask;
	resp.local_id     = ctx->cm_id->local_id;
	resp.remote_id    = ctx->cm_id->remote_id;

	if (copy_to_user((void __user *)(unsigned long)cmd.response,
			 &resp, sizeof(resp)))
		result = -EFAULT;

590
	ib_ucm_ctx_put(ctx);
591 592 593
	return result;
}

594 595 596 597
static ssize_t ib_ucm_init_qp_attr(struct ib_ucm_file *file,
				   const char __user *inbuf,
				   int in_len, int out_len)
{
598
	struct ib_uverbs_qp_attr resp;
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
	struct ib_ucm_init_qp_attr cmd;
	struct ib_ucm_context *ctx;
	struct ib_qp_attr qp_attr;
	int result = 0;

	if (out_len < sizeof(resp))
		return -ENOSPC;

	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
		return -EFAULT;

	ctx = ib_ucm_ctx_get(file, cmd.id);
	if (IS_ERR(ctx))
		return PTR_ERR(ctx);

	resp.qp_attr_mask = 0;
	memset(&qp_attr, 0, sizeof qp_attr);
	qp_attr.qp_state = cmd.qp_state;
	result = ib_cm_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
	if (result)
		goto out;

621
	ib_copy_qp_attr_to_user(&resp, &qp_attr);
622 623 624 625 626 627 628 629 630 631

	if (copy_to_user((void __user *)(unsigned long)cmd.response,
			 &resp, sizeof(resp)))
		result = -EFAULT;

out:
	ib_ucm_ctx_put(ctx);
	return result;
}

632 633 634 635 636 637 638 639 640 641 642
static int ucm_validate_listen(__be64 service_id, __be64 service_mask)
{
	service_id &= service_mask;

	if (((service_id & IB_CMA_SERVICE_ID_MASK) == IB_CMA_SERVICE_ID) ||
	    ((service_id & IB_SDP_SERVICE_ID_MASK) == IB_SDP_SERVICE_ID))
		return -EINVAL;

	return 0;
}

643 644 645 646 647 648 649 650 651 652 653
static ssize_t ib_ucm_listen(struct ib_ucm_file *file,
			     const char __user *inbuf,
			     int in_len, int out_len)
{
	struct ib_ucm_listen cmd;
	struct ib_ucm_context *ctx;
	int result;

	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
		return -EFAULT;

654 655 656
	ctx = ib_ucm_ctx_get(file, cmd.id);
	if (IS_ERR(ctx))
		return PTR_ERR(ctx);
657

658 659 660 661
	result = ucm_validate_listen(cmd.service_id, cmd.service_mask);
	if (result)
		goto out;

H
Haggai Eran 已提交
662
	result = ib_cm_listen(ctx->cm_id, cmd.service_id, cmd.service_mask);
663
out:
664
	ib_ucm_ctx_put(ctx);
665 666 667
	return result;
}

668 669 670
static ssize_t ib_ucm_notify(struct ib_ucm_file *file,
			     const char __user *inbuf,
			     int in_len, int out_len)
671
{
672
	struct ib_ucm_notify cmd;
673 674 675 676 677 678
	struct ib_ucm_context *ctx;
	int result;

	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
		return -EFAULT;

679 680 681
	ctx = ib_ucm_ctx_get(file, cmd.id);
	if (IS_ERR(ctx))
		return PTR_ERR(ctx);
682

683
	result = ib_cm_notify(ctx->cm_id, (enum ib_event_type) cmd.event);
684
	ib_ucm_ctx_put(ctx);
685 686 687 688 689 690 691 692 693 694 695 696
	return result;
}

static int ib_ucm_alloc_data(const void **dest, u64 src, u32 len)
{
	void *data;

	*dest = NULL;

	if (!len)
		return 0;

J
Julia Lawall 已提交
697 698 699
	data = memdup_user((void __user *)(unsigned long)src, len);
	if (IS_ERR(data))
		return PTR_ERR(data);
700 701 702 703 704 705 706

	*dest = data;
	return 0;
}

static int ib_ucm_path_get(struct ib_sa_path_rec **path, u64 src)
{
707
	struct ib_user_path_rec upath;
708 709 710 711 712 713 714 715 716 717 718
	struct ib_sa_path_rec  *sa_path;

	*path = NULL;

	if (!src)
		return 0;

	sa_path = kmalloc(sizeof(*sa_path), GFP_KERNEL);
	if (!sa_path)
		return -ENOMEM;

719 720
	if (copy_from_user(&upath, (void __user *)(unsigned long)src,
			   sizeof(upath))) {
721 722 723 724 725

		kfree(sa_path);
		return -EFAULT;
	}

726
	ib_copy_path_rec_from_user(sa_path, &upath);
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
	*path = sa_path;
	return 0;
}

static ssize_t ib_ucm_send_req(struct ib_ucm_file *file,
			       const char __user *inbuf,
			       int in_len, int out_len)
{
	struct ib_cm_req_param param;
	struct ib_ucm_context *ctx;
	struct ib_ucm_req cmd;
	int result;

	param.private_data   = NULL;
	param.primary_path   = NULL;
	param.alternate_path = NULL;

	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
		return -EFAULT;

	result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
	if (result)
		goto done;

	result = ib_ucm_path_get(&param.primary_path, cmd.primary_path);
	if (result)
		goto done;

	result = ib_ucm_path_get(&param.alternate_path, cmd.alternate_path);
	if (result)
		goto done;

	param.private_data_len           = cmd.len;
	param.service_id                 = cmd.sid;
	param.qp_num                     = cmd.qpn;
	param.qp_type                    = cmd.qp_type;
	param.starting_psn               = cmd.psn;
	param.peer_to_peer               = cmd.peer_to_peer;
	param.responder_resources        = cmd.responder_resources;
	param.initiator_depth            = cmd.initiator_depth;
	param.remote_cm_response_timeout = cmd.remote_cm_response_timeout;
	param.flow_control               = cmd.flow_control;
	param.local_cm_response_timeout  = cmd.local_cm_response_timeout;
	param.retry_count                = cmd.retry_count;
	param.rnr_retry_count            = cmd.rnr_retry_count;
	param.max_cm_retries             = cmd.max_cm_retries;
	param.srq                        = cmd.srq;

775 776
	ctx = ib_ucm_ctx_get(file, cmd.id);
	if (!IS_ERR(ctx)) {
777
		result = ib_send_cm_req(ctx->cm_id, &param);
778 779 780
		ib_ucm_ctx_put(ctx);
	} else
		result = PTR_ERR(ctx);
781 782

done:
H
Hal Rosenstock 已提交
783 784 785
	kfree(param.private_data);
	kfree(param.primary_path);
	kfree(param.alternate_path);
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
	return result;
}

static ssize_t ib_ucm_send_rep(struct ib_ucm_file *file,
			       const char __user *inbuf,
			       int in_len, int out_len)
{
	struct ib_cm_rep_param param;
	struct ib_ucm_context *ctx;
	struct ib_ucm_rep cmd;
	int result;

	param.private_data = NULL;

	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
		return -EFAULT;

	result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
	if (result)
		return result;

	param.qp_num              = cmd.qpn;
	param.starting_psn        = cmd.psn;
	param.private_data_len    = cmd.len;
	param.responder_resources = cmd.responder_resources;
	param.initiator_depth     = cmd.initiator_depth;
	param.failover_accepted   = cmd.failover_accepted;
	param.flow_control        = cmd.flow_control;
	param.rnr_retry_count     = cmd.rnr_retry_count;
	param.srq                 = cmd.srq;

817 818
	ctx = ib_ucm_ctx_get(file, cmd.id);
	if (!IS_ERR(ctx)) {
819
		ctx->uid = cmd.uid;
820
		result = ib_send_cm_rep(ctx->cm_id, &param);
821 822 823
		ib_ucm_ctx_put(ctx);
	} else
		result = PTR_ERR(ctx);
824

H
Hal Rosenstock 已提交
825
	kfree(param.private_data);
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
	return result;
}

static ssize_t ib_ucm_send_private_data(struct ib_ucm_file *file,
					const char __user *inbuf, int in_len,
					int (*func)(struct ib_cm_id *cm_id,
						    const void *private_data,
						    u8 private_data_len))
{
	struct ib_ucm_private_data cmd;
	struct ib_ucm_context *ctx;
	const void *private_data = NULL;
	int result;

	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
		return -EFAULT;

	result = ib_ucm_alloc_data(&private_data, cmd.data, cmd.len);
	if (result)
		return result;

847 848
	ctx = ib_ucm_ctx_get(file, cmd.id);
	if (!IS_ERR(ctx)) {
849
		result = func(ctx->cm_id, private_data, cmd.len);
850 851 852
		ib_ucm_ctx_put(ctx);
	} else
		result = PTR_ERR(ctx);
853

H
Hal Rosenstock 已提交
854
	kfree(private_data);
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
	return result;
}

static ssize_t ib_ucm_send_rtu(struct ib_ucm_file *file,
			       const char __user *inbuf,
			       int in_len, int out_len)
{
	return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_rtu);
}

static ssize_t ib_ucm_send_dreq(struct ib_ucm_file *file,
				const char __user *inbuf,
				int in_len, int out_len)
{
	return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_dreq);
}

static ssize_t ib_ucm_send_drep(struct ib_ucm_file *file,
				const char __user *inbuf,
				int in_len, int out_len)
{
	return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_drep);
}

static ssize_t ib_ucm_send_info(struct ib_ucm_file *file,
				const char __user *inbuf, int in_len,
				int (*func)(struct ib_cm_id *cm_id,
					    int status,
					    const void *info,
					    u8 info_len,
					    const void *data,
					    u8 data_len))
{
	struct ib_ucm_context *ctx;
	struct ib_ucm_info cmd;
	const void *data = NULL;
	const void *info = NULL;
	int result;

	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
		return -EFAULT;

	result = ib_ucm_alloc_data(&data, cmd.data, cmd.data_len);
	if (result)
		goto done;

	result = ib_ucm_alloc_data(&info, cmd.info, cmd.info_len);
	if (result)
		goto done;

905 906 907
	ctx = ib_ucm_ctx_get(file, cmd.id);
	if (!IS_ERR(ctx)) {
		result = func(ctx->cm_id, cmd.status, info, cmd.info_len,
908
			      data, cmd.data_len);
909 910 911
		ib_ucm_ctx_put(ctx);
	} else
		result = PTR_ERR(ctx);
912 913

done:
H
Hal Rosenstock 已提交
914 915
	kfree(data);
	kfree(info);
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
	return result;
}

static ssize_t ib_ucm_send_rej(struct ib_ucm_file *file,
			       const char __user *inbuf,
			       int in_len, int out_len)
{
	return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_rej);
}

static ssize_t ib_ucm_send_apr(struct ib_ucm_file *file,
			       const char __user *inbuf,
			       int in_len, int out_len)
{
	return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_apr);
}

static ssize_t ib_ucm_send_mra(struct ib_ucm_file *file,
			       const char __user *inbuf,
			       int in_len, int out_len)
{
	struct ib_ucm_context *ctx;
	struct ib_ucm_mra cmd;
	const void *data = NULL;
	int result;

	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
		return -EFAULT;

	result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
	if (result)
		return result;

949 950 951 952 953 954
	ctx = ib_ucm_ctx_get(file, cmd.id);
	if (!IS_ERR(ctx)) {
		result = ib_send_cm_mra(ctx->cm_id, cmd.timeout, data, cmd.len);
		ib_ucm_ctx_put(ctx);
	} else
		result = PTR_ERR(ctx);
955

H
Hal Rosenstock 已提交
956
	kfree(data);
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
	return result;
}

static ssize_t ib_ucm_send_lap(struct ib_ucm_file *file,
			       const char __user *inbuf,
			       int in_len, int out_len)
{
	struct ib_ucm_context *ctx;
	struct ib_sa_path_rec *path = NULL;
	struct ib_ucm_lap cmd;
	const void *data = NULL;
	int result;

	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
		return -EFAULT;

	result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
	if (result)
		goto done;

	result = ib_ucm_path_get(&path, cmd.path);
	if (result)
		goto done;

981 982
	ctx = ib_ucm_ctx_get(file, cmd.id);
	if (!IS_ERR(ctx)) {
983
		result = ib_send_cm_lap(ctx->cm_id, path, data, cmd.len);
984 985 986
		ib_ucm_ctx_put(ctx);
	} else
		result = PTR_ERR(ctx);
987 988

done:
H
Hal Rosenstock 已提交
989 990
	kfree(data);
	kfree(path);
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
	return result;
}

static ssize_t ib_ucm_send_sidr_req(struct ib_ucm_file *file,
				    const char __user *inbuf,
				    int in_len, int out_len)
{
	struct ib_cm_sidr_req_param param;
	struct ib_ucm_context *ctx;
	struct ib_ucm_sidr_req cmd;
	int result;

	param.private_data = NULL;
	param.path = NULL;

	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
		return -EFAULT;

	result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
	if (result)
		goto done;

	result = ib_ucm_path_get(&param.path, cmd.path);
	if (result)
		goto done;

	param.private_data_len = cmd.len;
	param.service_id       = cmd.sid;
	param.timeout_ms       = cmd.timeout;
	param.max_cm_retries   = cmd.max_cm_retries;

1022 1023
	ctx = ib_ucm_ctx_get(file, cmd.id);
	if (!IS_ERR(ctx)) {
1024
		result = ib_send_cm_sidr_req(ctx->cm_id, &param);
1025 1026 1027
		ib_ucm_ctx_put(ctx);
	} else
		result = PTR_ERR(ctx);
1028 1029

done:
H
Hal Rosenstock 已提交
1030 1031
	kfree(param.private_data);
	kfree(param.path);
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
	return result;
}

static ssize_t ib_ucm_send_sidr_rep(struct ib_ucm_file *file,
				    const char __user *inbuf,
				    int in_len, int out_len)
{
	struct ib_cm_sidr_rep_param param;
	struct ib_ucm_sidr_rep cmd;
	struct ib_ucm_context *ctx;
	int result;

	param.info = NULL;

	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
		return -EFAULT;

	result = ib_ucm_alloc_data(&param.private_data,
				   cmd.data, cmd.data_len);
	if (result)
		goto done;

	result = ib_ucm_alloc_data(&param.info, cmd.info, cmd.info_len);
	if (result)
		goto done;

1058 1059 1060 1061 1062
	param.qp_num		= cmd.qpn;
	param.qkey		= cmd.qkey;
	param.status		= cmd.status;
	param.info_length	= cmd.info_len;
	param.private_data_len	= cmd.data_len;
1063

1064 1065
	ctx = ib_ucm_ctx_get(file, cmd.id);
	if (!IS_ERR(ctx)) {
1066
		result = ib_send_cm_sidr_rep(ctx->cm_id, &param);
1067 1068 1069
		ib_ucm_ctx_put(ctx);
	} else
		result = PTR_ERR(ctx);
1070 1071

done:
H
Hal Rosenstock 已提交
1072 1073
	kfree(param.private_data);
	kfree(param.info);
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
	return result;
}

static ssize_t (*ucm_cmd_table[])(struct ib_ucm_file *file,
				  const char __user *inbuf,
				  int in_len, int out_len) = {
	[IB_USER_CM_CMD_CREATE_ID]     = ib_ucm_create_id,
	[IB_USER_CM_CMD_DESTROY_ID]    = ib_ucm_destroy_id,
	[IB_USER_CM_CMD_ATTR_ID]       = ib_ucm_attr_id,
	[IB_USER_CM_CMD_LISTEN]        = ib_ucm_listen,
1084
	[IB_USER_CM_CMD_NOTIFY]        = ib_ucm_notify,
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
	[IB_USER_CM_CMD_SEND_REQ]      = ib_ucm_send_req,
	[IB_USER_CM_CMD_SEND_REP]      = ib_ucm_send_rep,
	[IB_USER_CM_CMD_SEND_RTU]      = ib_ucm_send_rtu,
	[IB_USER_CM_CMD_SEND_DREQ]     = ib_ucm_send_dreq,
	[IB_USER_CM_CMD_SEND_DREP]     = ib_ucm_send_drep,
	[IB_USER_CM_CMD_SEND_REJ]      = ib_ucm_send_rej,
	[IB_USER_CM_CMD_SEND_MRA]      = ib_ucm_send_mra,
	[IB_USER_CM_CMD_SEND_LAP]      = ib_ucm_send_lap,
	[IB_USER_CM_CMD_SEND_APR]      = ib_ucm_send_apr,
	[IB_USER_CM_CMD_SEND_SIDR_REQ] = ib_ucm_send_sidr_req,
	[IB_USER_CM_CMD_SEND_SIDR_REP] = ib_ucm_send_sidr_rep,
	[IB_USER_CM_CMD_EVENT]	       = ib_ucm_event,
1097
	[IB_USER_CM_CMD_INIT_QP_ATTR]  = ib_ucm_init_qp_attr,
1098 1099 1100 1101 1102 1103 1104 1105 1106
};

static ssize_t ib_ucm_write(struct file *filp, const char __user *buf,
			    size_t len, loff_t *pos)
{
	struct ib_ucm_file *file = filp->private_data;
	struct ib_ucm_cmd_hdr hdr;
	ssize_t result;

1107 1108 1109
	if (WARN_ON_ONCE(!ib_safe_file_access(filp)))
		return -EACCES;

1110 1111 1112 1113 1114 1115
	if (len < sizeof(hdr))
		return -EINVAL;

	if (copy_from_user(&hdr, buf, sizeof(hdr)))
		return -EFAULT;

1116
	if (hdr.cmd >= ARRAY_SIZE(ucm_cmd_table))
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
		return -EINVAL;

	if (hdr.in + sizeof(hdr) > len)
		return -EINVAL;

	result = ucm_cmd_table[hdr.cmd](file, buf + sizeof(hdr),
					hdr.in, hdr.out);
	if (!result)
		result = len;

	return result;
}

static unsigned int ib_ucm_poll(struct file *filp,
				struct poll_table_struct *wait)
{
	struct ib_ucm_file *file = filp->private_data;
	unsigned int mask = 0;

	poll_wait(filp, &file->poll_wait, wait);

	if (!list_empty(&file->events))
		mask = POLLIN | POLLRDNORM;

	return mask;
}

1144 1145 1146 1147 1148 1149 1150 1151
/*
 * ib_ucm_open() does not need the BKL:
 *
 *  - no global state is referred to;
 *  - there is no ioctl method to race against;
 *  - no further module initialization is required for open to work
 *    after the device is registered.
 */
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
static int ib_ucm_open(struct inode *inode, struct file *filp)
{
	struct ib_ucm_file *file;

	file = kmalloc(sizeof(*file), GFP_KERNEL);
	if (!file)
		return -ENOMEM;

	INIT_LIST_HEAD(&file->events);
	INIT_LIST_HEAD(&file->ctxs);
	init_waitqueue_head(&file->poll_wait);

S
Sean Hefty 已提交
1164
	mutex_init(&file->file_mutex);
1165 1166 1167

	filp->private_data = file;
	file->filp = filp;
1168
	file->device = container_of(inode->i_cdev, struct ib_ucm_device, cdev);
1169

1170
	return nonseekable_open(inode, filp);
1171 1172 1173 1174 1175 1176 1177
}

static int ib_ucm_close(struct inode *inode, struct file *filp)
{
	struct ib_ucm_file *file = filp->private_data;
	struct ib_ucm_context *ctx;

S
Sean Hefty 已提交
1178
	mutex_lock(&file->file_mutex);
1179 1180 1181
	while (!list_empty(&file->ctxs)) {
		ctx = list_entry(file->ctxs.next,
				 struct ib_ucm_context, file_list);
S
Sean Hefty 已提交
1182
		mutex_unlock(&file->file_mutex);
1183

1184
		mutex_lock(&ctx_id_mutex);
1185
		idr_remove(&ctx_id_table, ctx->id);
1186
		mutex_unlock(&ctx_id_mutex);
1187 1188 1189 1190 1191

		ib_destroy_cm_id(ctx->cm_id);
		ib_ucm_cleanup_events(ctx);
		kfree(ctx);

S
Sean Hefty 已提交
1192
		mutex_lock(&file->file_mutex);
1193
	}
S
Sean Hefty 已提交
1194
	mutex_unlock(&file->file_mutex);
1195 1196 1197 1198
	kfree(file);
	return 0;
}

1199
static DECLARE_BITMAP(overflow_map, IB_UCM_MAX_DEVICES);
1200
static void ib_ucm_release_dev(struct device *dev)
1201
{
1202
	struct ib_ucm_device *ucm_dev;
1203

1204 1205
	ucm_dev = container_of(dev, struct ib_ucm_device, dev);
	cdev_del(&ucm_dev->cdev);
1206 1207 1208
	if (ucm_dev->devnum < IB_UCM_MAX_DEVICES)
		clear_bit(ucm_dev->devnum, dev_map);
	else
1209
		clear_bit(ucm_dev->devnum - IB_UCM_MAX_DEVICES, overflow_map);
1210
	kfree(ucm_dev);
1211 1212
}

1213
static const struct file_operations ucm_fops = {
1214 1215
	.owner	 = THIS_MODULE,
	.open	 = ib_ucm_open,
1216
	.release = ib_ucm_close,
1217
	.write	 = ib_ucm_write,
1218
	.poll    = ib_ucm_poll,
1219
	.llseek	 = no_llseek,
1220 1221
};

1222 1223
static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
			  char *buf)
1224
{
1225
	struct ib_ucm_device *ucm_dev;
R
Roland Dreier 已提交
1226

1227 1228
	ucm_dev = container_of(dev, struct ib_ucm_device, dev);
	return sprintf(buf, "%s\n", ucm_dev->ib_dev->name);
1229
}
1230
static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1231

1232 1233 1234 1235 1236 1237 1238 1239 1240
static dev_t overflow_maj;
static int find_overflow_devnum(void)
{
	int ret;

	if (!overflow_maj) {
		ret = alloc_chrdev_region(&overflow_maj, 0, IB_UCM_MAX_DEVICES,
					  "infiniband_cm");
		if (ret) {
P
Parav Pandit 已提交
1241
			pr_err("ucm: couldn't register dynamic device number\n");
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
			return ret;
		}
	}

	ret = find_first_zero_bit(overflow_map, IB_UCM_MAX_DEVICES);
	if (ret >= IB_UCM_MAX_DEVICES)
		return -1;

	return ret;
}

1253
static void ib_ucm_add_one(struct ib_device *device)
1254
{
1255
	int devnum;
1256
	dev_t base;
1257
	struct ib_ucm_device *ucm_dev;
1258

1259
	if (!device->alloc_ucontext || !rdma_cap_ib_cm(device, 1))
1260 1261
		return;

R
Roland Dreier 已提交
1262
	ucm_dev = kzalloc(sizeof *ucm_dev, GFP_KERNEL);
1263 1264
	if (!ucm_dev)
		return;
1265

1266
	ucm_dev->ib_dev = device;
1267

1268
	devnum = find_first_zero_bit(dev_map, IB_UCM_MAX_DEVICES);
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
	if (devnum >= IB_UCM_MAX_DEVICES) {
		devnum = find_overflow_devnum();
		if (devnum < 0)
			goto err;

		ucm_dev->devnum = devnum + IB_UCM_MAX_DEVICES;
		base = devnum + overflow_maj;
		set_bit(devnum, overflow_map);
	} else {
		ucm_dev->devnum = devnum;
		base = devnum + IB_UCM_BASE_DEV;
		set_bit(devnum, dev_map);
	}
1282

1283 1284 1285
	cdev_init(&ucm_dev->cdev, &ucm_fops);
	ucm_dev->cdev.owner = THIS_MODULE;
	kobject_set_name(&ucm_dev->cdev.kobj, "ucm%d", ucm_dev->devnum);
1286
	if (cdev_add(&ucm_dev->cdev, base, 1))
1287 1288
		goto err;

1289 1290 1291 1292
	ucm_dev->dev.class = &cm_class;
	ucm_dev->dev.parent = device->dma_device;
	ucm_dev->dev.devt = ucm_dev->cdev.dev;
	ucm_dev->dev.release = ib_ucm_release_dev;
1293
	dev_set_name(&ucm_dev->dev, "ucm%d", ucm_dev->devnum);
1294
	if (device_register(&ucm_dev->dev))
1295 1296
		goto err_cdev;

1297 1298
	if (device_create_file(&ucm_dev->dev, &dev_attr_ibdev))
		goto err_dev;
1299 1300 1301 1302

	ib_set_client_data(device, &ucm_client, ucm_dev);
	return;

1303 1304
err_dev:
	device_unregister(&ucm_dev->dev);
1305
err_cdev:
1306
	cdev_del(&ucm_dev->cdev);
1307 1308 1309 1310
	if (ucm_dev->devnum < IB_UCM_MAX_DEVICES)
		clear_bit(devnum, dev_map);
	else
		clear_bit(devnum, overflow_map);
1311 1312 1313 1314 1315
err:
	kfree(ucm_dev);
	return;
}

1316
static void ib_ucm_remove_one(struct ib_device *device, void *client_data)
1317
{
1318
	struct ib_ucm_device *ucm_dev = client_data;
1319 1320 1321 1322

	if (!ucm_dev)
		return;

1323
	device_unregister(&ucm_dev->dev);
1324 1325
}

1326 1327
static CLASS_ATTR_STRING(abi_version, S_IRUGO,
			 __stringify(IB_USER_CM_ABI_VERSION));
1328 1329 1330 1331 1332 1333 1334 1335

static int __init ib_ucm_init(void)
{
	int ret;

	ret = register_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES,
				     "infiniband_cm");
	if (ret) {
P
Parav Pandit 已提交
1336
		pr_err("ucm: couldn't register device number\n");
1337
		goto error1;
1338 1339
	}

1340
	ret = class_create_file(&cm_class, &class_attr_abi_version.attr);
1341
	if (ret) {
P
Parav Pandit 已提交
1342
		pr_err("ucm: couldn't create abi_version attribute\n");
1343
		goto error2;
1344
	}
1345

1346 1347
	ret = ib_register_client(&ucm_client);
	if (ret) {
P
Parav Pandit 已提交
1348
		pr_err("ucm: couldn't register client\n");
1349
		goto error3;
1350
	}
1351
	return 0;
1352

1353
error3:
1354
	class_remove_file(&cm_class, &class_attr_abi_version.attr);
1355
error2:
1356
	unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
1357
error1:
1358
	return ret;
1359 1360 1361 1362
}

static void __exit ib_ucm_cleanup(void)
{
1363
	ib_unregister_client(&ucm_client);
1364
	class_remove_file(&cm_class, &class_attr_abi_version.attr);
1365
	unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
1366 1367
	if (overflow_maj)
		unregister_chrdev_region(overflow_maj, IB_UCM_MAX_DEVICES);
1368
	idr_destroy(&ctx_id_table);
1369 1370 1371 1372
}

module_init(ib_ucm_init);
module_exit(ib_ucm_cleanup);