ucm.c 30.2 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
 *
 * 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.
 *
 * $Id: ucm.c 2594 2005-06-13 19:46:02Z libor $
 */
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/poll.h>
#include <linux/file.h>
#include <linux/mount.h>
#include <linux/cdev.h>

#include <asm/uaccess.h>

#include "ucm.h"

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

H
Hal Rosenstock 已提交
53 54 55 56 57
static int ucm_debug_level;

module_param_named(debug_level, ucm_debug_level, int, 0644);
MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");

58 59 60 61 62 63 64
enum {
	IB_UCM_MAJOR = 231,
	IB_UCM_MINOR = 255
};

#define IB_UCM_DEV MKDEV(IB_UCM_MAJOR, IB_UCM_MINOR)

H
Hal Rosenstock 已提交
65 66 67 68 69 70 71 72
#define PFX "UCM: "

#define ucm_dbg(format, arg...)			\
	do {					\
		if (ucm_debug_level > 0)	\
			printk(KERN_DEBUG PFX format, ## arg); \
	} while (0)

73 74 75 76
static struct semaphore ctx_id_mutex;
static struct idr       ctx_id_table;
static int              ctx_id_rover = 0;

77
static struct ib_ucm_context *ib_ucm_ctx_get(struct ib_ucm_file *file, int id)
78 79 80 81 82
{
	struct ib_ucm_context *ctx;

	down(&ctx_id_mutex);
	ctx = idr_find(&ctx_id_table, id);
83 84 85 86 87 88
	if (!ctx)
		ctx = ERR_PTR(-ENOENT);
	else if (ctx->file != file)
		ctx = ERR_PTR(-EINVAL);
	else
		atomic_inc(&ctx->ref);
89 90 91 92 93 94 95
	up(&ctx_id_mutex);

	return ctx;
}

static void ib_ucm_ctx_put(struct ib_ucm_context *ctx)
{
96 97 98 99 100 101 102
	if (atomic_dec_and_test(&ctx->ref))
		wake_up(&ctx->wait);
}

static ssize_t ib_ucm_destroy_ctx(struct ib_ucm_file *file, int id)
{
	struct ib_ucm_context *ctx;
103 104 105
	struct ib_ucm_event *uevent;

	down(&ctx_id_mutex);
106 107 108 109 110 111
	ctx = idr_find(&ctx_id_table, id);
	if (!ctx)
		ctx = ERR_PTR(-ENOENT);
	else if (ctx->file != file)
		ctx = ERR_PTR(-EINVAL);
	else
112 113 114
		idr_remove(&ctx_id_table, ctx->id);
	up(&ctx_id_mutex);

115 116
	if (IS_ERR(ctx))
		return PTR_ERR(ctx);
117

118 119 120 121 122 123
	atomic_dec(&ctx->ref);
	wait_event(ctx->wait, !atomic_read(&ctx->ref));

	/* No new events will be generated after destroying the cm_id. */
	if (!IS_ERR(ctx->cm_id))
		ib_destroy_cm_id(ctx->cm_id);
124

125 126
	/* Cleanup events not yet reported to the user. */
	down(&file->mutex);
127 128 129 130 131 132 133 134 135 136 137 138 139 140
	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);

		/* clear incoming connections. */
		if (uevent->cm_id)
			ib_destroy_cm_id(uevent->cm_id);

		kfree(uevent);
	}
141
	up(&file->mutex);
142 143

	kfree(ctx);
144
	return 0;
145 146 147 148 149 150 151 152 153 154 155
}

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

	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
	if (!ctx)
		return NULL;

156 157
	atomic_set(&ctx->ref, 1);
	init_waitqueue_head(&ctx->wait);
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
	ctx->file = file;

	INIT_LIST_HEAD(&ctx->events);

	list_add_tail(&ctx->file_list, &file->ctxs);

	ctx_id_rover = (ctx_id_rover + 1) & INT_MAX;
retry:
	result = idr_pre_get(&ctx_id_table, GFP_KERNEL);
	if (!result)
		goto error;

	down(&ctx_id_mutex);
	result = idr_get_new_above(&ctx_id_table, ctx, ctx_id_rover, &ctx->id);
	up(&ctx_id_mutex);

	if (result == -EAGAIN)
		goto retry;
	if (result)
		goto error;

H
Hal Rosenstock 已提交
179
	ucm_dbg("Allocated CM ID <%d>\n", ctx->id);
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

	return ctx;
error:
	list_del(&ctx->file_list);
	kfree(ctx);

	return NULL;
}
/*
 * Event portion of the API, handle CM events
 * and allow event polling.
 */
static void ib_ucm_event_path_get(struct ib_ucm_path_rec *upath,
				  struct ib_sa_path_rec	 *kpath)
{
	if (!kpath || !upath)
		return;

198 199
	memcpy(upath->dgid, kpath->dgid.raw, sizeof *upath->dgid);
	memcpy(upath->sgid, kpath->sgid.raw, sizeof *upath->sgid);
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

	upath->dlid             = kpath->dlid;
	upath->slid             = kpath->slid;
	upath->raw_traffic      = kpath->raw_traffic;
	upath->flow_label       = kpath->flow_label;
	upath->hop_limit        = kpath->hop_limit;
	upath->traffic_class    = kpath->traffic_class;
	upath->reversible       = kpath->reversible;
	upath->numb_path        = kpath->numb_path;
	upath->pkey             = kpath->pkey;
	upath->sl	        = kpath->sl;
	upath->mtu_selector     = kpath->mtu_selector;
	upath->mtu              = kpath->mtu;
	upath->rate_selector    = kpath->rate_selector;
	upath->rate             = kpath->rate;
	upath->packet_life_time = kpath->packet_life_time;
	upath->preference       = kpath->preference;

	upath->packet_life_time_selector =
		kpath->packet_life_time_selector;
}

222 223
static void ib_ucm_event_req_get(struct ib_ucm_context *ctx,
				 struct ib_ucm_req_event_resp *ureq,
224 225
				 struct ib_cm_req_event_param *kreq)
{
226
	ureq->listen_id = ctx->id;
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 252 253 254 255 256 257 258 259 260 261

	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;

	ib_ucm_event_path_get(&ureq->primary_path, kreq->primary_path);
	ib_ucm_event_path_get(&ureq->alternate_path, kreq->alternate_path);
}

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;
}

262 263
static void ib_ucm_event_sidr_req_get(struct ib_ucm_context *ctx,
				      struct ib_ucm_sidr_req_event_resp *ureq,
264 265
				      struct ib_cm_sidr_req_event_param *kreq)
{
266
	ureq->listen_id = ctx->id;
267 268 269 270 271 272 273 274 275 276 277
	ureq->pkey      = kreq->pkey;
}

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;
};

278 279
static int ib_ucm_event_process(struct ib_ucm_context *ctx,
				struct ib_cm_event *evt,
280 281 282 283 284 285
				struct ib_ucm_event *uvt)
{
	void *info = NULL;

	switch (evt->event) {
	case IB_CM_REQ_RECEIVED:
286
		ib_ucm_event_req_get(ctx, &uvt->resp.u.req_resp,
287 288
				     &evt->param.req_rcvd);
		uvt->data_len      = IB_CM_REQ_PRIVATE_DATA_SIZE;
289
		uvt->resp.present  = IB_UCM_PRES_PRIMARY;
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
		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:
311 312
		uvt->resp.u.mra_resp.timeout =
					evt->param.mra_rcvd.service_timeout;
313 314 315
		uvt->data_len = IB_CM_MRA_PRIVATE_DATA_SIZE;
		break;
	case IB_CM_REJ_RECEIVED:
316
		uvt->resp.u.rej_resp.reason = evt->param.rej_rcvd.reason;
317 318 319 320 321
		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:
322 323
		ib_ucm_event_path_get(&uvt->resp.u.lap_resp.path,
				      evt->param.lap_rcvd.alternate_path);
324
		uvt->data_len = IB_CM_LAP_PRIVATE_DATA_SIZE;
325
		uvt->resp.present = IB_UCM_PRES_ALTERNATE;
326 327
		break;
	case IB_CM_APR_RECEIVED:
328
		uvt->resp.u.apr_resp.status = evt->param.apr_rcvd.ap_status;
329 330 331 332 333
		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:
334
		ib_ucm_event_sidr_req_get(ctx, &uvt->resp.u.sidr_req_resp,
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
					  &evt->param.sidr_req_rcvd);
		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;
	}

350
	if (uvt->data_len) {
351
		uvt->data = kmalloc(uvt->data_len, GFP_KERNEL);
352 353
		if (!uvt->data)
			goto err1;
354 355 356 357 358

		memcpy(uvt->data, evt->private_data, uvt->data_len);
		uvt->resp.present |= IB_UCM_PRES_DATA;
	}

359
	if (uvt->info_len) {
360
		uvt->info = kmalloc(uvt->info_len, GFP_KERNEL);
361 362
		if (!uvt->info)
			goto err2;
363 364 365 366 367

		memcpy(uvt->info, info, uvt->info_len);
		uvt->resp.present |= IB_UCM_PRES_INFO;
	}
	return 0;
368 369

err2:
H
Hal Rosenstock 已提交
370
	kfree(uvt->data);
371 372
err1:
	return -ENOMEM;
373 374 375 376 377 378 379 380 381 382
}

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;
	int id;

383
	ctx = cm_id->context;
384 385 386 387

	if (event->event == IB_CM_REQ_RECEIVED ||
	    event->event == IB_CM_SIDR_REQ_RECEIVED)
		id = IB_UCM_CM_ID_INVALID;
388 389
	else
		id = ctx->id;
390 391

	uevent = kmalloc(sizeof(*uevent), GFP_KERNEL);
392 393
	if (!uevent)
		goto err1;
394 395 396 397 398

	memset(uevent, 0, sizeof(*uevent));
	uevent->resp.id    = id;
	uevent->resp.event = event->event;

399
	result = ib_ucm_event_process(ctx, event, uevent);
400
	if (result)
401
		goto err2;
402 403

	uevent->ctx   = ctx;
404
	uevent->cm_id = (id == IB_UCM_CM_ID_INVALID) ? cm_id : NULL;
405 406 407 408 409 410

	down(&ctx->file->mutex);
	list_add_tail(&uevent->file_list, &ctx->file->events);
	list_add_tail(&uevent->ctx_list, &ctx->events);
	wake_up_interruptible(&ctx->file->poll_wait);
	up(&ctx->file->mutex);
411 412 413 414 415 416 417
	return 0;

err2:
	kfree(uevent);
err1:
	/* Destroy new cm_id's */
	return (id == IB_UCM_CM_ID_INVALID);
418 419 420 421 422 423 424 425 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 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
}

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;
	struct ib_ucm_event *uevent = NULL;
	int result = 0;
	DEFINE_WAIT(wait);

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

	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
		return -EFAULT;
	/*
	 * wait
	 */
	down(&file->mutex);

	while (list_empty(&file->events)) {

		if (file->filp->f_flags & O_NONBLOCK) {
			result = -EAGAIN;
			break;
		}

		if (signal_pending(current)) {
			result = -ERESTARTSYS;
			break;
		}

		prepare_to_wait(&file->poll_wait, &wait, TASK_INTERRUPTIBLE);

		up(&file->mutex);
		schedule();
		down(&file->mutex);

		finish_wait(&file->poll_wait, &wait);
	}

	if (result)
		goto done;

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

	if (!uevent->cm_id)
		goto user;

	ctx = ib_ucm_ctx_alloc(file);
	if (!ctx) {
		result = -ENOMEM;
		goto done;
	}

475 476
	ctx->cm_id          = uevent->cm_id;
	ctx->cm_id->context = ctx;
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517

	uevent->resp.id = ctx->id;

user:
	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);

H
Hal Rosenstock 已提交
518 519
	kfree(uevent->data);
	kfree(uevent->info);
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
	kfree(uevent);
done:
	up(&file->mutex);
	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;

542
	down(&file->mutex);
543
	ctx = ib_ucm_ctx_alloc(file);
544
	up(&file->mutex);
545 546 547
	if (!ctx)
		return -ENOMEM;

548 549 550 551
	ctx->cm_id = ib_create_cm_id(ib_ucm_event_handler, ctx);
	if (IS_ERR(ctx->cm_id)) {
		result = PTR_ERR(ctx->cm_id);
		goto err;
552 553 554 555 556 557
	}

	resp.id = ctx->id;
	if (copy_to_user((void __user *)(unsigned long)cmd.response,
			 &resp, sizeof(resp))) {
		result = -EFAULT;
558
		goto err;
559 560 561 562
	}

	return 0;

563 564
err:
	ib_ucm_destroy_ctx(file, ctx->id);
565 566 567 568 569 570 571 572 573 574 575 576
	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;

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

577
	return ib_ucm_destroy_ctx(file, cmd.id);
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
}

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;

595 596 597
	ctx = ib_ucm_ctx_get(file, cmd.id);
	if (IS_ERR(ctx))
		return PTR_ERR(ctx);
598 599 600 601 602 603 604 605 606 607

	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;

608
	ib_ucm_ctx_put(ctx);
609 610 611 612 613 614 615 616 617 618 619 620 621 622
	return result;
}

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;

623 624 625
	ctx = ib_ucm_ctx_get(file, cmd.id);
	if (IS_ERR(ctx))
		return PTR_ERR(ctx);
626

627 628
	result = ib_cm_listen(ctx->cm_id, cmd.service_id, cmd.service_mask);
	ib_ucm_ctx_put(ctx);
629 630 631 632 633 634 635 636 637 638 639 640 641 642
	return result;
}

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

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

643 644 645
	ctx = ib_ucm_ctx_get(file, cmd.id);
	if (IS_ERR(ctx))
		return PTR_ERR(ctx);
646

647 648
	result = ib_cm_establish(ctx->cm_id);
	ib_ucm_ctx_put(ctx);
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
	return result;
}

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

	*dest = NULL;

	if (!len)
		return 0;

	data = kmalloc(len, GFP_KERNEL);
	if (!data)
		return -ENOMEM;

	if (copy_from_user(data, (void __user *)(unsigned long)src, len)) {
		kfree(data);
		return -EFAULT;
	}

	*dest = data;
	return 0;
}

static int ib_ucm_path_get(struct ib_sa_path_rec **path, u64 src)
{
	struct ib_ucm_path_rec ucm_path;
	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;

	if (copy_from_user(&ucm_path, (void __user *)(unsigned long)src,
			   sizeof(ucm_path))) {

		kfree(sa_path);
		return -EFAULT;
	}

695 696
	memcpy(sa_path->dgid.raw, ucm_path.dgid, sizeof sa_path->dgid);
	memcpy(sa_path->sgid.raw, ucm_path.sgid, sizeof sa_path->sgid);
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 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

	sa_path->dlid	          = ucm_path.dlid;
	sa_path->slid	          = ucm_path.slid;
	sa_path->raw_traffic      = ucm_path.raw_traffic;
	sa_path->flow_label       = ucm_path.flow_label;
	sa_path->hop_limit        = ucm_path.hop_limit;
	sa_path->traffic_class    = ucm_path.traffic_class;
	sa_path->reversible       = ucm_path.reversible;
	sa_path->numb_path        = ucm_path.numb_path;
	sa_path->pkey             = ucm_path.pkey;
	sa_path->sl               = ucm_path.sl;
	sa_path->mtu_selector     = ucm_path.mtu_selector;
	sa_path->mtu              = ucm_path.mtu;
	sa_path->rate_selector    = ucm_path.rate_selector;
	sa_path->rate             = ucm_path.rate;
	sa_path->packet_life_time = ucm_path.packet_life_time;
	sa_path->preference       = ucm_path.preference;

	sa_path->packet_life_time_selector =
		ucm_path.packet_life_time_selector;

	*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;

766 767
	ctx = ib_ucm_ctx_get(file, cmd.id);
	if (!IS_ERR(ctx)) {
768
		result = ib_send_cm_req(ctx->cm_id, &param);
769 770 771
		ib_ucm_ctx_put(ctx);
	} else
		result = PTR_ERR(ctx);
772 773

done:
H
Hal Rosenstock 已提交
774 775 776
	kfree(param.private_data);
	kfree(param.primary_path);
	kfree(param.alternate_path);
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
	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.target_ack_delay    = cmd.target_ack_delay;
	param.failover_accepted   = cmd.failover_accepted;
	param.flow_control        = cmd.flow_control;
	param.rnr_retry_count     = cmd.rnr_retry_count;
	param.srq                 = cmd.srq;

809 810
	ctx = ib_ucm_ctx_get(file, cmd.id);
	if (!IS_ERR(ctx)) {
811
		result = ib_send_cm_rep(ctx->cm_id, &param);
812 813 814
		ib_ucm_ctx_put(ctx);
	} else
		result = PTR_ERR(ctx);
815

H
Hal Rosenstock 已提交
816
	kfree(param.private_data);
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
	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;

838 839
	ctx = ib_ucm_ctx_get(file, cmd.id);
	if (!IS_ERR(ctx)) {
840
		result = func(ctx->cm_id, private_data, cmd.len);
841 842 843
		ib_ucm_ctx_put(ctx);
	} else
		result = PTR_ERR(ctx);
844

H
Hal Rosenstock 已提交
845
	kfree(private_data);
846 847 848 849 850 851 852 853 854 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
	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;

896 897 898
	ctx = ib_ucm_ctx_get(file, cmd.id);
	if (!IS_ERR(ctx)) {
		result = func(ctx->cm_id, cmd.status, info, cmd.info_len,
899
			      data, cmd.data_len);
900 901 902
		ib_ucm_ctx_put(ctx);
	} else
		result = PTR_ERR(ctx);
903 904

done:
H
Hal Rosenstock 已提交
905 906
	kfree(data);
	kfree(info);
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
	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;

940 941 942 943 944 945
	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);
946

H
Hal Rosenstock 已提交
947
	kfree(data);
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
	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;

972 973
	ctx = ib_ucm_ctx_get(file, cmd.id);
	if (!IS_ERR(ctx)) {
974
		result = ib_send_cm_lap(ctx->cm_id, path, data, cmd.len);
975 976 977
		ib_ucm_ctx_put(ctx);
	} else
		result = PTR_ERR(ctx);
978 979

done:
H
Hal Rosenstock 已提交
980 981
	kfree(data);
	kfree(path);
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
	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;
	param.pkey             = cmd.pkey;

1014 1015
	ctx = ib_ucm_ctx_get(file, cmd.id);
	if (!IS_ERR(ctx)) {
1016
		result = ib_send_cm_sidr_req(ctx->cm_id, &param);
1017 1018 1019
		ib_ucm_ctx_put(ctx);
	} else
		result = PTR_ERR(ctx);
1020 1021

done:
H
Hal Rosenstock 已提交
1022 1023
	kfree(param.private_data);
	kfree(param.path);
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
	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;

1050 1051 1052 1053 1054
	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;
1055

1056 1057
	ctx = ib_ucm_ctx_get(file, cmd.id);
	if (!IS_ERR(ctx)) {
1058
		result = ib_send_cm_sidr_rep(ctx->cm_id, &param);
1059 1060 1061
		ib_ucm_ctx_put(ctx);
	} else
		result = PTR_ERR(ctx);
1062 1063

done:
H
Hal Rosenstock 已提交
1064 1065
	kfree(param.private_data);
	kfree(param.info);
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
	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,
	[IB_USER_CM_CMD_ESTABLISH]     = ib_ucm_establish,
	[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,
};

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;

	if (len < sizeof(hdr))
		return -EINVAL;

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

H
Hal Rosenstock 已提交
1104 1105
	ucm_dbg("Write. cmd <%d> in <%d> out <%d> len <%Zu>\n",
		hdr.cmd, hdr.in, hdr.out, len);
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 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 1144 1145 1146 1147 1148 1149 1150 1151

	if (hdr.cmd < 0 || hdr.cmd >= ARRAY_SIZE(ucm_cmd_table))
		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;
}

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);

	init_MUTEX(&file->mutex);

	filp->private_data = file;
	file->filp = filp;

H
Hal Rosenstock 已提交
1152
	ucm_dbg("Created struct\n");
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167

	return 0;
}

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

	down(&file->mutex);
	while (!list_empty(&file->ctxs)) {

		ctx = list_entry(file->ctxs.next,
				 struct ib_ucm_context, file_list);

1168 1169
		up(&file->mutex);
		ib_ucm_destroy_ctx(file, ctx->id);
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
		down(&file->mutex);
	}
	up(&file->mutex);
	kfree(file);
	return 0;
}

static struct file_operations ib_ucm_fops = {
	.owner 	 = THIS_MODULE,
	.open 	 = ib_ucm_open,
	.release = ib_ucm_close,
	.write 	 = ib_ucm_write,
	.poll    = ib_ucm_poll,
};


1186
static struct class *ib_ucm_class;
1187 1188 1189 1190 1191 1192 1193 1194
static struct cdev	  ib_ucm_cdev;

static int __init ib_ucm_init(void)
{
	int result;

	result = register_chrdev_region(IB_UCM_DEV, 1, "infiniband_cm");
	if (result) {
H
Hal Rosenstock 已提交
1195
		ucm_dbg("Error <%d> registering dev\n", result);
1196 1197 1198 1199 1200 1201 1202
		goto err_chr;
	}

	cdev_init(&ib_ucm_cdev, &ib_ucm_fops);

	result = cdev_add(&ib_ucm_cdev, IB_UCM_DEV, 1);
	if (result) {
H
Hal Rosenstock 已提交
1203
 		ucm_dbg("Error <%d> adding cdev\n", result);
1204 1205 1206
		goto err_cdev;
	}

1207
	ib_ucm_class = class_create(THIS_MODULE, "infiniband_cm");
1208 1209
	if (IS_ERR(ib_ucm_class)) {
		result = PTR_ERR(ib_ucm_class);
H
Hal Rosenstock 已提交
1210
 		ucm_dbg("Error <%d> creating class\n", result);
1211 1212 1213
		goto err_class;
	}

1214
	class_device_create(ib_ucm_class, IB_UCM_DEV, NULL, "ucm");
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229

	idr_init(&ctx_id_table);
	init_MUTEX(&ctx_id_mutex);

	return 0;
err_class:
	cdev_del(&ib_ucm_cdev);
err_cdev:
	unregister_chrdev_region(IB_UCM_DEV, 1);
err_chr:
	return result;
}

static void __exit ib_ucm_cleanup(void)
{
1230 1231
	class_device_destroy(ib_ucm_class, IB_UCM_DEV);
	class_destroy(ib_ucm_class);
1232 1233 1234 1235 1236 1237
	cdev_del(&ib_ucm_cdev);
	unregister_chrdev_region(IB_UCM_DEV, 1);
}

module_init(ib_ucm_init);
module_exit(ib_ucm_cleanup);