Linux Audio

Check our new training course

Loading...
v3.1
   1/*
   2 * Copyright (c) 2005 Topspin Communications.  All rights reserved.
   3 * Copyright (c) 2005, 2006, 2007 Cisco Systems.  All rights reserved.
   4 * Copyright (c) 2005 PathScale, Inc.  All rights reserved.
   5 * Copyright (c) 2006 Mellanox Technologies.  All rights reserved.
   6 *
   7 * This software is available to you under a choice of one of two
   8 * licenses.  You may choose to be licensed under the terms of the GNU
   9 * General Public License (GPL) Version 2, available from the file
  10 * COPYING in the main directory of this source tree, or the
  11 * OpenIB.org BSD license below:
  12 *
  13 *     Redistribution and use in source and binary forms, with or
  14 *     without modification, are permitted provided that the following
  15 *     conditions are met:
  16 *
  17 *      - Redistributions of source code must retain the above
  18 *        copyright notice, this list of conditions and the following
  19 *        disclaimer.
  20 *
  21 *      - Redistributions in binary form must reproduce the above
  22 *        copyright notice, this list of conditions and the following
  23 *        disclaimer in the documentation and/or other materials
  24 *        provided with the distribution.
  25 *
  26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33 * SOFTWARE.
  34 */
  35
  36#include <linux/file.h>
  37#include <linux/fs.h>
  38#include <linux/slab.h>
 
  39
  40#include <asm/uaccess.h>
  41
  42#include "uverbs.h"
 
 
  43
  44static struct lock_class_key pd_lock_key;
  45static struct lock_class_key mr_lock_key;
  46static struct lock_class_key cq_lock_key;
  47static struct lock_class_key qp_lock_key;
  48static struct lock_class_key ah_lock_key;
  49static struct lock_class_key srq_lock_key;
  50
  51#define INIT_UDATA(udata, ibuf, obuf, ilen, olen)			\
  52	do {								\
  53		(udata)->inbuf  = (void __user *) (ibuf);		\
  54		(udata)->outbuf = (void __user *) (obuf);		\
  55		(udata)->inlen  = (ilen);				\
  56		(udata)->outlen = (olen);				\
  57	} while (0)
  58
  59/*
  60 * The ib_uobject locking scheme is as follows:
  61 *
  62 * - ib_uverbs_idr_lock protects the uverbs idrs themselves, so it
  63 *   needs to be held during all idr operations.  When an object is
  64 *   looked up, a reference must be taken on the object's kref before
  65 *   dropping this lock.
  66 *
  67 * - Each object also has an rwsem.  This rwsem must be held for
  68 *   reading while an operation that uses the object is performed.
  69 *   For example, while registering an MR, the associated PD's
  70 *   uobject.mutex must be held for reading.  The rwsem must be held
  71 *   for writing while initializing or destroying an object.
  72 *
  73 * - In addition, each object has a "live" flag.  If this flag is not
  74 *   set, then lookups of the object will fail even if it is found in
  75 *   the idr.  This handles a reader that blocks and does not acquire
  76 *   the rwsem until after the object is destroyed.  The destroy
  77 *   operation will set the live flag to 0 and then drop the rwsem;
  78 *   this will allow the reader to acquire the rwsem, see that the
  79 *   live flag is 0, and then drop the rwsem and its reference to
  80 *   object.  The underlying storage will not be freed until the last
  81 *   reference to the object is dropped.
  82 */
  83
  84static void init_uobj(struct ib_uobject *uobj, u64 user_handle,
  85		      struct ib_ucontext *context, struct lock_class_key *key)
  86{
  87	uobj->user_handle = user_handle;
  88	uobj->context     = context;
  89	kref_init(&uobj->ref);
  90	init_rwsem(&uobj->mutex);
  91	lockdep_set_class(&uobj->mutex, key);
  92	uobj->live        = 0;
  93}
  94
  95static void release_uobj(struct kref *kref)
  96{
  97	kfree(container_of(kref, struct ib_uobject, ref));
  98}
  99
 100static void put_uobj(struct ib_uobject *uobj)
 101{
 102	kref_put(&uobj->ref, release_uobj);
 103}
 104
 105static void put_uobj_read(struct ib_uobject *uobj)
 106{
 107	up_read(&uobj->mutex);
 108	put_uobj(uobj);
 109}
 110
 111static void put_uobj_write(struct ib_uobject *uobj)
 112{
 113	up_write(&uobj->mutex);
 114	put_uobj(uobj);
 115}
 116
 117static int idr_add_uobj(struct idr *idr, struct ib_uobject *uobj)
 118{
 119	int ret;
 120
 121retry:
 122	if (!idr_pre_get(idr, GFP_KERNEL))
 123		return -ENOMEM;
 124
 125	spin_lock(&ib_uverbs_idr_lock);
 126	ret = idr_get_new(idr, uobj, &uobj->id);
 127	spin_unlock(&ib_uverbs_idr_lock);
 128
 129	if (ret == -EAGAIN)
 130		goto retry;
 131
 132	return ret;
 133}
 
 
 
 
 
 
 
 
 134
 135void idr_remove_uobj(struct idr *idr, struct ib_uobject *uobj)
 136{
 137	spin_lock(&ib_uverbs_idr_lock);
 138	idr_remove(idr, uobj->id);
 139	spin_unlock(&ib_uverbs_idr_lock);
 140}
 141
 142static struct ib_uobject *__idr_get_uobj(struct idr *idr, int id,
 143					 struct ib_ucontext *context)
 
 
 
 
 
 
 144{
 145	struct ib_uobject *uobj;
 
 
 146
 147	spin_lock(&ib_uverbs_idr_lock);
 148	uobj = idr_find(idr, id);
 149	if (uobj) {
 150		if (uobj->context == context)
 151			kref_get(&uobj->ref);
 152		else
 153			uobj = NULL;
 154	}
 155	spin_unlock(&ib_uverbs_idr_lock);
 156
 157	return uobj;
 158}
 159
 160static struct ib_uobject *idr_read_uobj(struct idr *idr, int id,
 161					struct ib_ucontext *context, int nested)
 
 
 
 
 
 
 162{
 163	struct ib_uobject *uobj;
 
 164
 165	uobj = __idr_get_uobj(idr, id, context);
 166	if (!uobj)
 167		return NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 168
 169	if (nested)
 170		down_read_nested(&uobj->mutex, SINGLE_DEPTH_NESTING);
 171	else
 172		down_read(&uobj->mutex);
 173	if (!uobj->live) {
 174		put_uobj_read(uobj);
 175		return NULL;
 176	}
 177
 178	return uobj;
 
 
 179}
 180
 181static struct ib_uobject *idr_write_uobj(struct idr *idr, int id,
 182					 struct ib_ucontext *context)
 183{
 184	struct ib_uobject *uobj;
 185
 186	uobj = __idr_get_uobj(idr, id, context);
 187	if (!uobj)
 188		return NULL;
 189
 190	down_write(&uobj->mutex);
 191	if (!uobj->live) {
 192		put_uobj_write(uobj);
 193		return NULL;
 194	}
 195
 196	return uobj;
 
 197}
 198
 199static void *idr_read_obj(struct idr *idr, int id, struct ib_ucontext *context,
 200			  int nested)
 201{
 202	struct ib_uobject *uobj;
 203
 204	uobj = idr_read_uobj(idr, id, context, nested);
 205	return uobj ? uobj->object : NULL;
 
 
 206}
 207
 208static struct ib_pd *idr_read_pd(int pd_handle, struct ib_ucontext *context)
 209{
 210	return idr_read_obj(&ib_uverbs_pd_idr, pd_handle, context, 0);
 
 
 211}
 212
 213static void put_pd_read(struct ib_pd *pd)
 
 
 
 
 
 214{
 215	put_uobj_read(pd->uobject);
 
 216}
 217
 218static struct ib_cq *idr_read_cq(int cq_handle, struct ib_ucontext *context, int nested)
 
 219{
 220	return idr_read_obj(&ib_uverbs_cq_idr, cq_handle, context, nested);
 221}
 222
 223static void put_cq_read(struct ib_cq *cq)
 224{
 225	put_uobj_read(cq->uobject);
 226}
 227
 228static struct ib_ah *idr_read_ah(int ah_handle, struct ib_ucontext *context)
 229{
 230	return idr_read_obj(&ib_uverbs_ah_idr, ah_handle, context, 0);
 231}
 232
 233static void put_ah_read(struct ib_ah *ah)
 234{
 235	put_uobj_read(ah->uobject);
 236}
 
 
 237
 238static struct ib_qp *idr_read_qp(int qp_handle, struct ib_ucontext *context)
 239{
 240	return idr_read_obj(&ib_uverbs_qp_idr, qp_handle, context, 0);
 241}
 
 242
 243static void put_qp_read(struct ib_qp *qp)
 244{
 245	put_uobj_read(qp->uobject);
 246}
 247
 248static struct ib_srq *idr_read_srq(int srq_handle, struct ib_ucontext *context)
 249{
 250	return idr_read_obj(&ib_uverbs_srq_idr, srq_handle, context, 0);
 251}
 252
 253static void put_srq_read(struct ib_srq *srq)
 254{
 255	put_uobj_read(srq->uobject);
 
 
 
 
 
 256}
 257
 258ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
 259			      const char __user *buf,
 260			      int in_len, int out_len)
 261{
 262	struct ib_uverbs_get_context      cmd;
 263	struct ib_uverbs_get_context_resp resp;
 264	struct ib_udata                   udata;
 265	struct ib_device                 *ibdev = file->device->ib_dev;
 266	struct ib_ucontext		 *ucontext;
 267	struct file			 *filp;
 268	int ret;
 269
 270	if (out_len < sizeof resp)
 271		return -ENOSPC;
 272
 273	if (copy_from_user(&cmd, buf, sizeof cmd))
 274		return -EFAULT;
 275
 276	mutex_lock(&file->mutex);
 277
 278	if (file->ucontext) {
 279		ret = -EINVAL;
 280		goto err;
 281	}
 282
 283	INIT_UDATA(&udata, buf + sizeof cmd,
 284		   (unsigned long) cmd.response + sizeof resp,
 285		   in_len - sizeof cmd, out_len - sizeof resp);
 286
 287	ucontext = ibdev->alloc_ucontext(ibdev, &udata);
 288	if (IS_ERR(ucontext)) {
 289		ret = PTR_ERR(ucontext);
 290		goto err;
 291	}
 292
 293	ucontext->device = ibdev;
 294	INIT_LIST_HEAD(&ucontext->pd_list);
 295	INIT_LIST_HEAD(&ucontext->mr_list);
 296	INIT_LIST_HEAD(&ucontext->mw_list);
 297	INIT_LIST_HEAD(&ucontext->cq_list);
 298	INIT_LIST_HEAD(&ucontext->qp_list);
 299	INIT_LIST_HEAD(&ucontext->srq_list);
 300	INIT_LIST_HEAD(&ucontext->ah_list);
 301	ucontext->closing = 0;
 302
 303	resp.num_comp_vectors = file->device->num_comp_vectors;
 304
 305	ret = get_unused_fd();
 306	if (ret < 0)
 307		goto err_free;
 308	resp.async_fd = ret;
 
 309
 310	filp = ib_uverbs_alloc_event_file(file, 1);
 311	if (IS_ERR(filp)) {
 312		ret = PTR_ERR(filp);
 313		goto err_fd;
 314	}
 315
 316	if (copy_to_user((void __user *) (unsigned long) cmd.response,
 317			 &resp, sizeof resp)) {
 318		ret = -EFAULT;
 319		goto err_file;
 320	}
 
 
 
 321
 322	file->async_file = filp->private_data;
 
 
 
 
 
 
 323
 324	INIT_IB_EVENT_HANDLER(&file->event_handler, file->device->ib_dev,
 325			      ib_uverbs_event_handler);
 326	ret = ib_register_event_handler(&file->event_handler);
 327	if (ret)
 328		goto err_file;
 329
 330	kref_get(&file->async_file->ref);
 331	kref_get(&file->ref);
 332	file->ucontext = ucontext;
 333
 334	fd_install(resp.async_fd, filp);
 335
 336	mutex_unlock(&file->mutex);
 
 
 337
 338	return in_len;
 
 
 
 
 339
 340err_file:
 341	fput(filp);
 
 
 
 
 
 342
 343err_fd:
 344	put_unused_fd(resp.async_fd);
 
 345
 346err_free:
 347	ibdev->dealloc_ucontext(ucontext);
 
 
 348
 349err:
 350	mutex_unlock(&file->mutex);
 
 
 
 
 351	return ret;
 352}
 353
 354ssize_t ib_uverbs_query_device(struct ib_uverbs_file *file,
 355			       const char __user *buf,
 356			       int in_len, int out_len)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 357{
 358	struct ib_uverbs_query_device      cmd;
 359	struct ib_uverbs_query_device_resp resp;
 360	struct ib_device_attr              attr;
 361	int                                ret;
 362
 363	if (out_len < sizeof resp)
 364		return -ENOSPC;
 365
 366	if (copy_from_user(&cmd, buf, sizeof cmd))
 367		return -EFAULT;
 
 368
 369	ret = ib_query_device(file->device->ib_dev, &attr);
 370	if (ret)
 371		return ret;
 372
 373	memset(&resp, 0, sizeof resp);
 
 374
 375	resp.fw_ver 		       = attr.fw_ver;
 376	resp.node_guid 		       = file->device->ib_dev->node_guid;
 377	resp.sys_image_guid 	       = attr.sys_image_guid;
 378	resp.max_mr_size 	       = attr.max_mr_size;
 379	resp.page_size_cap 	       = attr.page_size_cap;
 380	resp.vendor_id 		       = attr.vendor_id;
 381	resp.vendor_part_id 	       = attr.vendor_part_id;
 382	resp.hw_ver 		       = attr.hw_ver;
 383	resp.max_qp 		       = attr.max_qp;
 384	resp.max_qp_wr 		       = attr.max_qp_wr;
 385	resp.device_cap_flags 	       = attr.device_cap_flags;
 386	resp.max_sge 		       = attr.max_sge;
 387	resp.max_sge_rd 	       = attr.max_sge_rd;
 388	resp.max_cq 		       = attr.max_cq;
 389	resp.max_cqe 		       = attr.max_cqe;
 390	resp.max_mr 		       = attr.max_mr;
 391	resp.max_pd 		       = attr.max_pd;
 392	resp.max_qp_rd_atom 	       = attr.max_qp_rd_atom;
 393	resp.max_ee_rd_atom 	       = attr.max_ee_rd_atom;
 394	resp.max_res_rd_atom 	       = attr.max_res_rd_atom;
 395	resp.max_qp_init_rd_atom       = attr.max_qp_init_rd_atom;
 396	resp.max_ee_init_rd_atom       = attr.max_ee_init_rd_atom;
 397	resp.atomic_cap 	       = attr.atomic_cap;
 398	resp.max_ee 		       = attr.max_ee;
 399	resp.max_rdd 		       = attr.max_rdd;
 400	resp.max_mw 		       = attr.max_mw;
 401	resp.max_raw_ipv6_qp 	       = attr.max_raw_ipv6_qp;
 402	resp.max_raw_ethy_qp 	       = attr.max_raw_ethy_qp;
 403	resp.max_mcast_grp 	       = attr.max_mcast_grp;
 404	resp.max_mcast_qp_attach       = attr.max_mcast_qp_attach;
 405	resp.max_total_mcast_qp_attach = attr.max_total_mcast_qp_attach;
 406	resp.max_ah 		       = attr.max_ah;
 407	resp.max_fmr 		       = attr.max_fmr;
 408	resp.max_map_per_fmr 	       = attr.max_map_per_fmr;
 409	resp.max_srq 		       = attr.max_srq;
 410	resp.max_srq_wr 	       = attr.max_srq_wr;
 411	resp.max_srq_sge 	       = attr.max_srq_sge;
 412	resp.max_pkeys 		       = attr.max_pkeys;
 413	resp.local_ca_ack_delay        = attr.local_ca_ack_delay;
 414	resp.phys_port_cnt	       = file->device->ib_dev->phys_port_cnt;
 415
 416	if (copy_to_user((void __user *) (unsigned long) cmd.response,
 417			 &resp, sizeof resp))
 418		return -EFAULT;
 419
 420	return in_len;
 421}
 422
 423ssize_t ib_uverbs_query_port(struct ib_uverbs_file *file,
 424			     const char __user *buf,
 425			     int in_len, int out_len)
 426{
 427	struct ib_uverbs_query_port      cmd;
 428	struct ib_uverbs_query_port_resp resp;
 429	struct ib_port_attr              attr;
 430	int                              ret;
 
 
 431
 432	if (out_len < sizeof resp)
 433		return -ENOSPC;
 
 
 434
 435	if (copy_from_user(&cmd, buf, sizeof cmd))
 436		return -EFAULT;
 
 437
 438	ret = ib_query_port(file->device->ib_dev, cmd.port_num, &attr);
 439	if (ret)
 440		return ret;
 441
 442	memset(&resp, 0, sizeof resp);
 
 443
 444	resp.state 	     = attr.state;
 445	resp.max_mtu 	     = attr.max_mtu;
 446	resp.active_mtu      = attr.active_mtu;
 447	resp.gid_tbl_len     = attr.gid_tbl_len;
 448	resp.port_cap_flags  = attr.port_cap_flags;
 449	resp.max_msg_sz      = attr.max_msg_sz;
 450	resp.bad_pkey_cntr   = attr.bad_pkey_cntr;
 451	resp.qkey_viol_cntr  = attr.qkey_viol_cntr;
 452	resp.pkey_tbl_len    = attr.pkey_tbl_len;
 453	resp.lid 	     = attr.lid;
 454	resp.sm_lid 	     = attr.sm_lid;
 455	resp.lmc 	     = attr.lmc;
 456	resp.max_vl_num      = attr.max_vl_num;
 457	resp.sm_sl 	     = attr.sm_sl;
 458	resp.subnet_timeout  = attr.subnet_timeout;
 459	resp.init_type_reply = attr.init_type_reply;
 460	resp.active_width    = attr.active_width;
 461	resp.active_speed    = attr.active_speed;
 462	resp.phys_state      = attr.phys_state;
 463	resp.link_layer      = rdma_port_get_link_layer(file->device->ib_dev,
 464							cmd.port_num);
 465
 466	if (copy_to_user((void __user *) (unsigned long) cmd.response,
 467			 &resp, sizeof resp))
 468		return -EFAULT;
 469
 470	return in_len;
 471}
 472
 473ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
 474			   const char __user *buf,
 475			   int in_len, int out_len)
 476{
 
 477	struct ib_uverbs_alloc_pd      cmd;
 478	struct ib_uverbs_alloc_pd_resp resp;
 479	struct ib_udata                udata;
 480	struct ib_uobject             *uobj;
 481	struct ib_pd                  *pd;
 482	int                            ret;
 
 483
 484	if (out_len < sizeof resp)
 485		return -ENOSPC;
 486
 487	if (copy_from_user(&cmd, buf, sizeof cmd))
 488		return -EFAULT;
 489
 490	INIT_UDATA(&udata, buf + sizeof cmd,
 491		   (unsigned long) cmd.response + sizeof resp,
 492		   in_len - sizeof cmd, out_len - sizeof resp);
 493
 494	uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
 495	if (!uobj)
 496		return -ENOMEM;
 497
 498	init_uobj(uobj, 0, file->ucontext, &pd_lock_key);
 499	down_write(&uobj->mutex);
 
 500
 501	pd = file->device->ib_dev->alloc_pd(file->device->ib_dev,
 502					    file->ucontext, &udata);
 503	if (IS_ERR(pd)) {
 504		ret = PTR_ERR(pd);
 505		goto err;
 506	}
 507
 508	pd->device  = file->device->ib_dev;
 509	pd->uobject = uobj;
 510	atomic_set(&pd->usecnt, 0);
 511
 512	uobj->object = pd;
 513	ret = idr_add_uobj(&ib_uverbs_pd_idr, uobj);
 
 
 514	if (ret)
 515		goto err_idr;
 
 
 
 
 516
 517	memset(&resp, 0, sizeof resp);
 518	resp.pd_handle = uobj->id;
 
 519
 520	if (copy_to_user((void __user *) (unsigned long) cmd.response,
 521			 &resp, sizeof resp)) {
 522		ret = -EFAULT;
 523		goto err_copy;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 524	}
 525
 526	mutex_lock(&file->mutex);
 527	list_add_tail(&uobj->list, &file->ucontext->pd_list);
 528	mutex_unlock(&file->mutex);
 
 
 529
 530	uobj->live = 1;
 
 
 
 
 531
 532	up_write(&uobj->mutex);
 
 533
 534	return in_len;
 
 
 
 
 
 
 535
 536err_copy:
 537	idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
 538
 539err_idr:
 540	ib_dealloc_pd(pd);
 
 541
 542err:
 543	put_uobj_write(uobj);
 544	return ret;
 
 
 545}
 546
 547ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,
 548			     const char __user *buf,
 549			     int in_len, int out_len)
 550{
 551	struct ib_uverbs_dealloc_pd cmd;
 552	struct ib_uobject          *uobj;
 553	int                         ret;
 554
 555	if (copy_from_user(&cmd, buf, sizeof cmd))
 556		return -EFAULT;
 
 
 
 
 
 557
 558	uobj = idr_write_uobj(&ib_uverbs_pd_idr, cmd.pd_handle, file->ucontext);
 559	if (!uobj)
 560		return -EINVAL;
 
 
 
 
 
 
 
 
 
 561
 562	ret = ib_dealloc_pd(uobj->object);
 563	if (!ret)
 564		uobj->live = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 565
 566	put_uobj_write(uobj);
 
 
 
 567
 
 568	if (ret)
 569		return ret;
 570
 571	idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
 
 
 
 
 
 
 
 
 
 572
 573	mutex_lock(&file->mutex);
 574	list_del(&uobj->list);
 575	mutex_unlock(&file->mutex);
 
 
 
 
 
 
 576
 577	put_uobj(uobj);
 
 578
 579	return in_len;
 580}
 581
 582ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
 583			 const char __user *buf, int in_len,
 584			 int out_len)
 585{
 
 586	struct ib_uverbs_reg_mr      cmd;
 587	struct ib_uverbs_reg_mr_resp resp;
 588	struct ib_udata              udata;
 589	struct ib_uobject           *uobj;
 590	struct ib_pd                *pd;
 591	struct ib_mr                *mr;
 592	int                          ret;
 
 593
 594	if (out_len < sizeof resp)
 595		return -ENOSPC;
 596
 597	if (copy_from_user(&cmd, buf, sizeof cmd))
 598		return -EFAULT;
 599
 600	INIT_UDATA(&udata, buf + sizeof cmd,
 601		   (unsigned long) cmd.response + sizeof resp,
 602		   in_len - sizeof cmd, out_len - sizeof resp);
 603
 604	if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
 605		return -EINVAL;
 606
 607	/*
 608	 * Local write permission is required if remote write or
 609	 * remote atomic permission is also requested.
 610	 */
 611	if (cmd.access_flags & (IB_ACCESS_REMOTE_ATOMIC | IB_ACCESS_REMOTE_WRITE) &&
 612	    !(cmd.access_flags & IB_ACCESS_LOCAL_WRITE))
 613		return -EINVAL;
 614
 615	uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
 616	if (!uobj)
 617		return -ENOMEM;
 618
 619	init_uobj(uobj, 0, file->ucontext, &mr_lock_key);
 620	down_write(&uobj->mutex);
 
 621
 622	pd = idr_read_pd(cmd.pd_handle, file->ucontext);
 623	if (!pd) {
 624		ret = -EINVAL;
 625		goto err_free;
 626	}
 627
 628	mr = pd->device->reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,
 629				     cmd.access_flags, &udata);
 
 630	if (IS_ERR(mr)) {
 631		ret = PTR_ERR(mr);
 632		goto err_put;
 633	}
 634
 635	mr->device  = pd->device;
 636	mr->pd      = pd;
 
 
 
 637	mr->uobject = uobj;
 638	atomic_inc(&pd->usecnt);
 639	atomic_set(&mr->usecnt, 0);
 
 
 
 
 640
 641	uobj->object = mr;
 642	ret = idr_add_uobj(&ib_uverbs_mr_idr, uobj);
 643	if (ret)
 644		goto err_unreg;
 645
 646	memset(&resp, 0, sizeof resp);
 647	resp.lkey      = mr->lkey;
 648	resp.rkey      = mr->rkey;
 649	resp.mr_handle = uobj->id;
 
 650
 651	if (copy_to_user((void __user *) (unsigned long) cmd.response,
 652			 &resp, sizeof resp)) {
 653		ret = -EFAULT;
 654		goto err_copy;
 655	}
 
 656
 657	put_pd_read(pd);
 
 
 
 
 
 
 
 
 
 
 
 658
 659	mutex_lock(&file->mutex);
 660	list_add_tail(&uobj->list, &file->ucontext->mr_list);
 661	mutex_unlock(&file->mutex);
 662
 663	uobj->live = 1;
 
 664
 665	up_write(&uobj->mutex);
 
 666
 667	return in_len;
 
 
 668
 669err_copy:
 670	idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
 
 671
 672err_unreg:
 673	ib_dereg_mr(mr);
 674
 675err_put:
 676	put_pd_read(pd);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 677
 678err_free:
 679	put_uobj_write(uobj);
 680	return ret;
 681}
 682
 683ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
 684			   const char __user *buf, int in_len,
 685			   int out_len)
 686{
 687	struct ib_uverbs_dereg_mr cmd;
 688	struct ib_mr             *mr;
 689	struct ib_uobject	 *uobj;
 690	int                       ret = -EINVAL;
 691
 692	if (copy_from_user(&cmd, buf, sizeof cmd))
 693		return -EFAULT;
 
 694
 695	uobj = idr_write_uobj(&ib_uverbs_mr_idr, cmd.mr_handle, file->ucontext);
 696	if (!uobj)
 697		return -EINVAL;
 698
 699	mr = uobj->object;
 
 
 
 
 
 
 
 
 700
 701	ret = ib_dereg_mr(mr);
 702	if (!ret)
 703		uobj->live = 0;
 704
 705	put_uobj_write(uobj);
 
 
 706
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 707	if (ret)
 708		return ret;
 709
 710	idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 711
 712	mutex_lock(&file->mutex);
 713	list_del(&uobj->list);
 714	mutex_unlock(&file->mutex);
 
 715
 716	put_uobj(uobj);
 
 
 717
 718	return in_len;
 719}
 720
 721ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
 722				      const char __user *buf, int in_len,
 723				      int out_len)
 724{
 725	struct ib_uverbs_create_comp_channel	   cmd;
 726	struct ib_uverbs_create_comp_channel_resp  resp;
 727	struct file				  *filp;
 
 
 728	int ret;
 729
 730	if (out_len < sizeof resp)
 731		return -ENOSPC;
 732
 733	if (copy_from_user(&cmd, buf, sizeof cmd))
 734		return -EFAULT;
 735
 736	ret = get_unused_fd();
 737	if (ret < 0)
 738		return ret;
 739	resp.fd = ret;
 740
 741	filp = ib_uverbs_alloc_event_file(file, 0);
 742	if (IS_ERR(filp)) {
 743		put_unused_fd(resp.fd);
 744		return PTR_ERR(filp);
 745	}
 746
 747	if (copy_to_user((void __user *) (unsigned long) cmd.response,
 748			 &resp, sizeof resp)) {
 749		put_unused_fd(resp.fd);
 750		fput(filp);
 751		return -EFAULT;
 752	}
 753
 754	fd_install(resp.fd, filp);
 755	return in_len;
 756}
 757
 758ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
 759			    const char __user *buf, int in_len,
 760			    int out_len)
 761{
 762	struct ib_uverbs_create_cq      cmd;
 763	struct ib_uverbs_create_cq_resp resp;
 764	struct ib_udata                 udata;
 765	struct ib_ucq_object           *obj;
 766	struct ib_uverbs_event_file    *ev_file = NULL;
 767	struct ib_cq                   *cq;
 768	int                             ret;
 
 
 
 769
 770	if (out_len < sizeof resp)
 771		return -ENOSPC;
 772
 773	if (copy_from_user(&cmd, buf, sizeof cmd))
 774		return -EFAULT;
 775
 776	INIT_UDATA(&udata, buf + sizeof cmd,
 777		   (unsigned long) cmd.response + sizeof resp,
 778		   in_len - sizeof cmd, out_len - sizeof resp);
 779
 780	if (cmd.comp_vector >= file->device->num_comp_vectors)
 781		return -EINVAL;
 782
 783	obj = kmalloc(sizeof *obj, GFP_KERNEL);
 784	if (!obj)
 785		return -ENOMEM;
 786
 787	init_uobj(&obj->uobject, cmd.user_handle, file->ucontext, &cq_lock_key);
 788	down_write(&obj->uobject.mutex);
 789
 790	if (cmd.comp_channel >= 0) {
 791		ev_file = ib_uverbs_lookup_comp_file(cmd.comp_channel);
 792		if (!ev_file) {
 793			ret = -EINVAL;
 794			goto err;
 795		}
 796	}
 797
 798	obj->uverbs_file	   = file;
 799	obj->comp_events_reported  = 0;
 800	obj->async_events_reported = 0;
 801	INIT_LIST_HEAD(&obj->comp_list);
 802	INIT_LIST_HEAD(&obj->async_list);
 
 
 
 
 803
 804	cq = file->device->ib_dev->create_cq(file->device->ib_dev, cmd.cqe,
 805					     cmd.comp_vector,
 806					     file->ucontext, &udata);
 807	if (IS_ERR(cq)) {
 808		ret = PTR_ERR(cq);
 809		goto err_file;
 810	}
 811
 812	cq->device        = file->device->ib_dev;
 813	cq->uobject       = &obj->uobject;
 814	cq->comp_handler  = ib_uverbs_comp_handler;
 815	cq->event_handler = ib_uverbs_cq_event_handler;
 816	cq->cq_context    = ev_file;
 817	atomic_set(&cq->usecnt, 0);
 818
 819	obj->uobject.object = cq;
 820	ret = idr_add_uobj(&ib_uverbs_cq_idr, &obj->uobject);
 
 
 821	if (ret)
 822		goto err_free;
 
 823
 824	memset(&resp, 0, sizeof resp);
 825	resp.cq_handle = obj->uobject.id;
 826	resp.cqe       = cq->cqe;
 
 
 
 
 
 
 
 827
 828	if (copy_to_user((void __user *) (unsigned long) cmd.response,
 829			 &resp, sizeof resp)) {
 830		ret = -EFAULT;
 831		goto err_copy;
 832	}
 
 
 
 
 
 833
 834	mutex_lock(&file->mutex);
 835	list_add_tail(&obj->uobject.list, &file->ucontext->cq_list);
 836	mutex_unlock(&file->mutex);
 
 
 837
 838	obj->uobject.live = 1;
 
 
 839
 840	up_write(&obj->uobject.mutex);
 
 
 
 
 841
 842	return in_len;
 
 843
 844err_copy:
 845	idr_remove_uobj(&ib_uverbs_cq_idr, &obj->uobject);
 
 
 846
 847err_free:
 848	ib_destroy_cq(cq);
 
 849
 850err_file:
 851	if (ev_file)
 852		ib_uverbs_release_ucq(file, ev_file, obj);
 853
 854err:
 855	put_uobj_write(&obj->uobject);
 856	return ret;
 
 857}
 858
 859ssize_t ib_uverbs_resize_cq(struct ib_uverbs_file *file,
 860			    const char __user *buf, int in_len,
 861			    int out_len)
 862{
 863	struct ib_uverbs_resize_cq	cmd;
 864	struct ib_uverbs_resize_cq_resp	resp;
 865	struct ib_udata                 udata;
 866	struct ib_cq			*cq;
 867	int				ret = -EINVAL;
 868
 869	if (copy_from_user(&cmd, buf, sizeof cmd))
 870		return -EFAULT;
 871
 872	INIT_UDATA(&udata, buf + sizeof cmd,
 873		   (unsigned long) cmd.response + sizeof resp,
 874		   in_len - sizeof cmd, out_len - sizeof resp);
 875
 876	cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
 877	if (!cq)
 878		return -EINVAL;
 879
 880	ret = cq->device->resize_cq(cq, cmd.cqe, &udata);
 881	if (ret)
 882		goto out;
 883
 884	resp.cqe = cq->cqe;
 885
 886	if (copy_to_user((void __user *) (unsigned long) cmd.response,
 887			 &resp, sizeof resp.cqe))
 888		ret = -EFAULT;
 889
 890out:
 891	put_cq_read(cq);
 
 892
 893	return ret ? ret : in_len;
 894}
 895
 896static int copy_wc_to_user(void __user *dest, struct ib_wc *wc)
 
 897{
 898	struct ib_uverbs_wc tmp;
 899
 900	tmp.wr_id		= wc->wr_id;
 901	tmp.status		= wc->status;
 902	tmp.opcode		= wc->opcode;
 903	tmp.vendor_err		= wc->vendor_err;
 904	tmp.byte_len		= wc->byte_len;
 905	tmp.ex.imm_data		= (__u32 __force) wc->ex.imm_data;
 906	tmp.qp_num		= wc->qp->qp_num;
 907	tmp.src_qp		= wc->src_qp;
 908	tmp.wc_flags		= wc->wc_flags;
 909	tmp.pkey_index		= wc->pkey_index;
 910	tmp.slid		= wc->slid;
 
 
 
 911	tmp.sl			= wc->sl;
 912	tmp.dlid_path_bits	= wc->dlid_path_bits;
 913	tmp.port_num		= wc->port_num;
 914	tmp.reserved		= 0;
 915
 916	if (copy_to_user(dest, &tmp, sizeof tmp))
 917		return -EFAULT;
 918
 919	return 0;
 920}
 921
 922ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
 923			  const char __user *buf, int in_len,
 924			  int out_len)
 925{
 926	struct ib_uverbs_poll_cq       cmd;
 927	struct ib_uverbs_poll_cq_resp  resp;
 928	u8 __user                     *header_ptr;
 929	u8 __user                     *data_ptr;
 930	struct ib_cq                  *cq;
 931	struct ib_wc                   wc;
 932	int                            ret;
 933
 934	if (copy_from_user(&cmd, buf, sizeof cmd))
 935		return -EFAULT;
 
 936
 937	cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
 938	if (!cq)
 939		return -EINVAL;
 940
 941	/* we copy a struct ib_uverbs_poll_cq_resp to user space */
 942	header_ptr = (void __user *)(unsigned long) cmd.response;
 943	data_ptr = header_ptr + sizeof resp;
 944
 945	memset(&resp, 0, sizeof resp);
 946	while (resp.count < cmd.ne) {
 947		ret = ib_poll_cq(cq, 1, &wc);
 948		if (ret < 0)
 949			goto out_put;
 950		if (!ret)
 951			break;
 952
 953		ret = copy_wc_to_user(data_ptr, &wc);
 954		if (ret)
 955			goto out_put;
 956
 957		data_ptr += sizeof(struct ib_uverbs_wc);
 958		++resp.count;
 959	}
 960
 961	if (copy_to_user(header_ptr, &resp, sizeof resp)) {
 962		ret = -EFAULT;
 963		goto out_put;
 964	}
 
 965
 966	ret = in_len;
 
 967
 968out_put:
 969	put_cq_read(cq);
 
 970	return ret;
 971}
 972
 973ssize_t ib_uverbs_req_notify_cq(struct ib_uverbs_file *file,
 974				const char __user *buf, int in_len,
 975				int out_len)
 976{
 977	struct ib_uverbs_req_notify_cq cmd;
 978	struct ib_cq                  *cq;
 
 979
 980	if (copy_from_user(&cmd, buf, sizeof cmd))
 981		return -EFAULT;
 
 982
 983	cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
 984	if (!cq)
 985		return -EINVAL;
 986
 987	ib_req_notify_cq(cq, cmd.solicited_only ?
 988			 IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
 989
 990	put_cq_read(cq);
 991
 992	return in_len;
 993}
 994
 995ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
 996			     const char __user *buf, int in_len,
 997			     int out_len)
 998{
 999	struct ib_uverbs_destroy_cq      cmd;
1000	struct ib_uverbs_destroy_cq_resp resp;
1001	struct ib_uobject		*uobj;
1002	struct ib_cq               	*cq;
1003	struct ib_ucq_object        	*obj;
1004	struct ib_uverbs_event_file	*ev_file;
1005	int                        	 ret = -EINVAL;
1006
1007	if (copy_from_user(&cmd, buf, sizeof cmd))
1008		return -EFAULT;
1009
1010	uobj = idr_write_uobj(&ib_uverbs_cq_idr, cmd.cq_handle, file->ucontext);
1011	if (!uobj)
1012		return -EINVAL;
1013	cq      = uobj->object;
1014	ev_file = cq->cq_context;
1015	obj     = container_of(cq->uobject, struct ib_ucq_object, uobject);
1016
1017	ret = ib_destroy_cq(cq);
1018	if (!ret)
1019		uobj->live = 0;
1020
1021	put_uobj_write(uobj);
1022
 
1023	if (ret)
1024		return ret;
1025
1026	idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
 
 
1027
1028	mutex_lock(&file->mutex);
1029	list_del(&uobj->list);
1030	mutex_unlock(&file->mutex);
 
1031
1032	ib_uverbs_release_ucq(file, ev_file, obj);
1033
1034	memset(&resp, 0, sizeof resp);
1035	resp.comp_events_reported  = obj->comp_events_reported;
1036	resp.async_events_reported = obj->async_events_reported;
1037
1038	put_uobj(uobj);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1039
1040	if (copy_to_user((void __user *) (unsigned long) cmd.response,
1041			 &resp, sizeof resp))
1042		return -EFAULT;
 
 
 
 
 
 
 
 
 
 
 
 
 
1043
1044	return in_len;
1045}
1046
1047ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
1048			    const char __user *buf, int in_len,
1049			    int out_len)
1050{
1051	struct ib_uverbs_create_qp      cmd;
1052	struct ib_uverbs_create_qp_resp resp;
1053	struct ib_udata                 udata;
1054	struct ib_uqp_object           *obj;
1055	struct ib_pd                   *pd;
1056	struct ib_cq                   *scq, *rcq;
1057	struct ib_srq                  *srq;
1058	struct ib_qp                   *qp;
1059	struct ib_qp_init_attr          attr;
1060	int ret;
1061
1062	if (out_len < sizeof resp)
1063		return -ENOSPC;
1064
1065	if (copy_from_user(&cmd, buf, sizeof cmd))
1066		return -EFAULT;
 
1067
1068	INIT_UDATA(&udata, buf + sizeof cmd,
1069		   (unsigned long) cmd.response + sizeof resp,
1070		   in_len - sizeof cmd, out_len - sizeof resp);
 
1071
1072	obj = kmalloc(sizeof *obj, GFP_KERNEL);
1073	if (!obj)
1074		return -ENOMEM;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1075
1076	init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_key);
1077	down_write(&obj->uevent.uobject.mutex);
 
 
 
 
 
 
 
 
 
 
1078
1079	srq = cmd.is_srq ? idr_read_srq(cmd.srq_handle, file->ucontext) : NULL;
1080	pd  = idr_read_pd(cmd.pd_handle, file->ucontext);
1081	scq = idr_read_cq(cmd.send_cq_handle, file->ucontext, 0);
1082	rcq = cmd.recv_cq_handle == cmd.send_cq_handle ?
1083		scq : idr_read_cq(cmd.recv_cq_handle, file->ucontext, 1);
 
 
 
 
 
 
1084
1085	if (!pd || !scq || !rcq || (cmd.is_srq && !srq)) {
1086		ret = -EINVAL;
1087		goto err_put;
1088	}
1089
1090	attr.event_handler = ib_uverbs_qp_event_handler;
1091	attr.qp_context    = file;
1092	attr.send_cq       = scq;
1093	attr.recv_cq       = rcq;
1094	attr.srq           = srq;
1095	attr.sq_sig_type   = cmd.sq_sig_all ? IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
1096	attr.qp_type       = cmd.qp_type;
 
 
1097	attr.create_flags  = 0;
1098
1099	attr.cap.max_send_wr     = cmd.max_send_wr;
1100	attr.cap.max_recv_wr     = cmd.max_recv_wr;
1101	attr.cap.max_send_sge    = cmd.max_send_sge;
1102	attr.cap.max_recv_sge    = cmd.max_recv_sge;
1103	attr.cap.max_inline_data = cmd.max_inline_data;
1104
1105	obj->uevent.events_reported     = 0;
1106	INIT_LIST_HEAD(&obj->uevent.event_list);
1107	INIT_LIST_HEAD(&obj->mcast_list);
1108
1109	qp = pd->device->create_qp(pd, &attr, &udata);
1110	if (IS_ERR(qp)) {
1111		ret = PTR_ERR(qp);
 
 
 
 
 
 
 
1112		goto err_put;
1113	}
1114
1115	qp->device     	  = pd->device;
1116	qp->pd         	  = pd;
1117	qp->send_cq    	  = attr.send_cq;
1118	qp->recv_cq    	  = attr.recv_cq;
1119	qp->srq	       	  = attr.srq;
1120	qp->uobject       = &obj->uevent.uobject;
1121	qp->event_handler = attr.event_handler;
1122	qp->qp_context    = attr.qp_context;
1123	qp->qp_type	  = attr.qp_type;
1124	atomic_inc(&pd->usecnt);
1125	atomic_inc(&attr.send_cq->usecnt);
1126	atomic_inc(&attr.recv_cq->usecnt);
1127	if (attr.srq)
1128		atomic_inc(&attr.srq->usecnt);
1129
1130	obj->uevent.uobject.object = qp;
1131	ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
1132	if (ret)
1133		goto err_destroy;
1134
1135	memset(&resp, 0, sizeof resp);
1136	resp.qpn             = qp->qp_num;
1137	resp.qp_handle       = obj->uevent.uobject.id;
1138	resp.max_recv_sge    = attr.cap.max_recv_sge;
1139	resp.max_send_sge    = attr.cap.max_send_sge;
1140	resp.max_recv_wr     = attr.cap.max_recv_wr;
1141	resp.max_send_wr     = attr.cap.max_send_wr;
1142	resp.max_inline_data = attr.cap.max_inline_data;
1143
1144	if (copy_to_user((void __user *) (unsigned long) cmd.response,
1145			 &resp, sizeof resp)) {
1146		ret = -EFAULT;
1147		goto err_copy;
1148	}
1149
1150	put_pd_read(pd);
1151	put_cq_read(scq);
1152	if (rcq != scq)
1153		put_cq_read(rcq);
1154	if (srq)
1155		put_srq_read(srq);
1156
1157	mutex_lock(&file->mutex);
1158	list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
1159	mutex_unlock(&file->mutex);
 
1160
1161	obj->uevent.uobject.live = 1;
 
 
 
1162
1163	up_write(&obj->uevent.uobject.mutex);
 
 
 
 
 
 
 
 
 
 
 
 
1164
1165	return in_len;
 
 
 
 
 
 
 
 
 
 
1166
1167err_copy:
1168	idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1169
1170err_destroy:
1171	ib_destroy_qp(qp);
1172
1173err_put:
 
 
1174	if (pd)
1175		put_pd_read(pd);
1176	if (scq)
1177		put_cq_read(scq);
 
1178	if (rcq && rcq != scq)
1179		put_cq_read(rcq);
 
1180	if (srq)
1181		put_srq_read(srq);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1182
1183	put_uobj_write(&obj->uevent.uobject);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1184	return ret;
1185}
1186
1187ssize_t ib_uverbs_query_qp(struct ib_uverbs_file *file,
1188			   const char __user *buf, int in_len,
1189			   int out_len)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1190{
1191	struct ib_uverbs_query_qp      cmd;
1192	struct ib_uverbs_query_qp_resp resp;
1193	struct ib_qp                   *qp;
1194	struct ib_qp_attr              *attr;
1195	struct ib_qp_init_attr         *init_attr;
1196	int                            ret;
1197
1198	if (copy_from_user(&cmd, buf, sizeof cmd))
1199		return -EFAULT;
 
1200
1201	attr      = kmalloc(sizeof *attr, GFP_KERNEL);
1202	init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
1203	if (!attr || !init_attr) {
1204		ret = -ENOMEM;
1205		goto out;
1206	}
1207
1208	qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1209	if (!qp) {
1210		ret = -EINVAL;
1211		goto out;
1212	}
1213
1214	ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
1215
1216	put_qp_read(qp);
 
1217
1218	if (ret)
1219		goto out;
1220
1221	memset(&resp, 0, sizeof resp);
1222
1223	resp.qp_state               = attr->qp_state;
1224	resp.cur_qp_state           = attr->cur_qp_state;
1225	resp.path_mtu               = attr->path_mtu;
1226	resp.path_mig_state         = attr->path_mig_state;
1227	resp.qkey                   = attr->qkey;
1228	resp.rq_psn                 = attr->rq_psn;
1229	resp.sq_psn                 = attr->sq_psn;
1230	resp.dest_qp_num            = attr->dest_qp_num;
1231	resp.qp_access_flags        = attr->qp_access_flags;
1232	resp.pkey_index             = attr->pkey_index;
1233	resp.alt_pkey_index         = attr->alt_pkey_index;
1234	resp.sq_draining            = attr->sq_draining;
1235	resp.max_rd_atomic          = attr->max_rd_atomic;
1236	resp.max_dest_rd_atomic     = attr->max_dest_rd_atomic;
1237	resp.min_rnr_timer          = attr->min_rnr_timer;
1238	resp.port_num               = attr->port_num;
1239	resp.timeout                = attr->timeout;
1240	resp.retry_cnt              = attr->retry_cnt;
1241	resp.rnr_retry              = attr->rnr_retry;
1242	resp.alt_port_num           = attr->alt_port_num;
1243	resp.alt_timeout            = attr->alt_timeout;
1244
1245	memcpy(resp.dest.dgid, attr->ah_attr.grh.dgid.raw, 16);
1246	resp.dest.flow_label        = attr->ah_attr.grh.flow_label;
1247	resp.dest.sgid_index        = attr->ah_attr.grh.sgid_index;
1248	resp.dest.hop_limit         = attr->ah_attr.grh.hop_limit;
1249	resp.dest.traffic_class     = attr->ah_attr.grh.traffic_class;
1250	resp.dest.dlid              = attr->ah_attr.dlid;
1251	resp.dest.sl                = attr->ah_attr.sl;
1252	resp.dest.src_path_bits     = attr->ah_attr.src_path_bits;
1253	resp.dest.static_rate       = attr->ah_attr.static_rate;
1254	resp.dest.is_global         = !!(attr->ah_attr.ah_flags & IB_AH_GRH);
1255	resp.dest.port_num          = attr->ah_attr.port_num;
1256
1257	memcpy(resp.alt_dest.dgid, attr->alt_ah_attr.grh.dgid.raw, 16);
1258	resp.alt_dest.flow_label    = attr->alt_ah_attr.grh.flow_label;
1259	resp.alt_dest.sgid_index    = attr->alt_ah_attr.grh.sgid_index;
1260	resp.alt_dest.hop_limit     = attr->alt_ah_attr.grh.hop_limit;
1261	resp.alt_dest.traffic_class = attr->alt_ah_attr.grh.traffic_class;
1262	resp.alt_dest.dlid          = attr->alt_ah_attr.dlid;
1263	resp.alt_dest.sl            = attr->alt_ah_attr.sl;
1264	resp.alt_dest.src_path_bits = attr->alt_ah_attr.src_path_bits;
1265	resp.alt_dest.static_rate   = attr->alt_ah_attr.static_rate;
1266	resp.alt_dest.is_global     = !!(attr->alt_ah_attr.ah_flags & IB_AH_GRH);
1267	resp.alt_dest.port_num      = attr->alt_ah_attr.port_num;
1268
1269	resp.max_send_wr            = init_attr->cap.max_send_wr;
1270	resp.max_recv_wr            = init_attr->cap.max_recv_wr;
1271	resp.max_send_sge           = init_attr->cap.max_send_sge;
1272	resp.max_recv_sge           = init_attr->cap.max_recv_sge;
1273	resp.max_inline_data        = init_attr->cap.max_inline_data;
1274	resp.sq_sig_all             = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
1275
1276	if (copy_to_user((void __user *) (unsigned long) cmd.response,
1277			 &resp, sizeof resp))
1278		ret = -EFAULT;
1279
1280out:
1281	kfree(attr);
1282	kfree(init_attr);
1283
1284	return ret ? ret : in_len;
1285}
1286
1287ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
1288			    const char __user *buf, int in_len,
1289			    int out_len)
1290{
1291	struct ib_uverbs_modify_qp cmd;
1292	struct ib_udata            udata;
1293	struct ib_qp              *qp;
1294	struct ib_qp_attr         *attr;
1295	int                        ret;
 
 
 
 
 
1296
1297	if (copy_from_user(&cmd, buf, sizeof cmd))
1298		return -EFAULT;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1299
1300	INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
1301		   out_len);
 
 
 
 
1302
1303	attr = kmalloc(sizeof *attr, GFP_KERNEL);
1304	if (!attr)
1305		return -ENOMEM;
1306
1307	qp = idr_read_qp(cmd.qp_handle, file->ucontext);
 
1308	if (!qp) {
1309		ret = -EINVAL;
1310		goto out;
1311	}
1312
1313	attr->qp_state 		  = cmd.qp_state;
1314	attr->cur_qp_state 	  = cmd.cur_qp_state;
1315	attr->path_mtu 		  = cmd.path_mtu;
1316	attr->path_mig_state 	  = cmd.path_mig_state;
1317	attr->qkey 		  = cmd.qkey;
1318	attr->rq_psn 		  = cmd.rq_psn;
1319	attr->sq_psn 		  = cmd.sq_psn;
1320	attr->dest_qp_num 	  = cmd.dest_qp_num;
1321	attr->qp_access_flags 	  = cmd.qp_access_flags;
1322	attr->pkey_index 	  = cmd.pkey_index;
1323	attr->alt_pkey_index 	  = cmd.alt_pkey_index;
1324	attr->en_sqd_async_notify = cmd.en_sqd_async_notify;
1325	attr->max_rd_atomic 	  = cmd.max_rd_atomic;
1326	attr->max_dest_rd_atomic  = cmd.max_dest_rd_atomic;
1327	attr->min_rnr_timer 	  = cmd.min_rnr_timer;
1328	attr->port_num 		  = cmd.port_num;
1329	attr->timeout 		  = cmd.timeout;
1330	attr->retry_cnt 	  = cmd.retry_cnt;
1331	attr->rnr_retry 	  = cmd.rnr_retry;
1332	attr->alt_port_num 	  = cmd.alt_port_num;
1333	attr->alt_timeout 	  = cmd.alt_timeout;
1334
1335	memcpy(attr->ah_attr.grh.dgid.raw, cmd.dest.dgid, 16);
1336	attr->ah_attr.grh.flow_label        = cmd.dest.flow_label;
1337	attr->ah_attr.grh.sgid_index        = cmd.dest.sgid_index;
1338	attr->ah_attr.grh.hop_limit         = cmd.dest.hop_limit;
1339	attr->ah_attr.grh.traffic_class     = cmd.dest.traffic_class;
1340	attr->ah_attr.dlid 	    	    = cmd.dest.dlid;
1341	attr->ah_attr.sl   	    	    = cmd.dest.sl;
1342	attr->ah_attr.src_path_bits 	    = cmd.dest.src_path_bits;
1343	attr->ah_attr.static_rate   	    = cmd.dest.static_rate;
1344	attr->ah_attr.ah_flags 	    	    = cmd.dest.is_global ? IB_AH_GRH : 0;
1345	attr->ah_attr.port_num 	    	    = cmd.dest.port_num;
1346
1347	memcpy(attr->alt_ah_attr.grh.dgid.raw, cmd.alt_dest.dgid, 16);
1348	attr->alt_ah_attr.grh.flow_label    = cmd.alt_dest.flow_label;
1349	attr->alt_ah_attr.grh.sgid_index    = cmd.alt_dest.sgid_index;
1350	attr->alt_ah_attr.grh.hop_limit     = cmd.alt_dest.hop_limit;
1351	attr->alt_ah_attr.grh.traffic_class = cmd.alt_dest.traffic_class;
1352	attr->alt_ah_attr.dlid 	    	    = cmd.alt_dest.dlid;
1353	attr->alt_ah_attr.sl   	    	    = cmd.alt_dest.sl;
1354	attr->alt_ah_attr.src_path_bits     = cmd.alt_dest.src_path_bits;
1355	attr->alt_ah_attr.static_rate       = cmd.alt_dest.static_rate;
1356	attr->alt_ah_attr.ah_flags 	    = cmd.alt_dest.is_global ? IB_AH_GRH : 0;
1357	attr->alt_ah_attr.port_num 	    = cmd.alt_dest.port_num;
1358
1359	ret = qp->device->modify_qp(qp, attr, cmd.attr_mask, &udata);
 
 
 
 
1360
1361	put_qp_read(qp);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1362
1363	if (ret)
1364		goto out;
 
 
 
 
 
1365
1366	ret = in_len;
 
 
 
 
 
 
1367
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1368out:
1369	kfree(attr);
1370
1371	return ret;
1372}
1373
1374ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
1375			     const char __user *buf, int in_len,
1376			     int out_len)
1377{
1378	struct ib_uverbs_destroy_qp      cmd;
1379	struct ib_uverbs_destroy_qp_resp resp;
1380	struct ib_uobject		*uobj;
1381	struct ib_qp               	*qp;
1382	struct ib_uqp_object        	*obj;
1383	int                        	 ret = -EINVAL;
1384
1385	if (copy_from_user(&cmd, buf, sizeof cmd))
1386		return -EFAULT;
 
1387
1388	memset(&resp, 0, sizeof resp);
 
1389
1390	uobj = idr_write_uobj(&ib_uverbs_qp_idr, cmd.qp_handle, file->ucontext);
1391	if (!uobj)
1392		return -EINVAL;
1393	qp  = uobj->object;
1394	obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
1395
1396	if (!list_empty(&obj->mcast_list)) {
1397		put_uobj_write(uobj);
1398		return -EBUSY;
1399	}
 
 
 
1400
1401	ret = ib_destroy_qp(qp);
1402	if (!ret)
1403		uobj->live = 0;
1404
1405	put_uobj_write(uobj);
 
 
 
 
 
1406
 
1407	if (ret)
1408		return ret;
1409
1410	idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
 
1411
1412	mutex_lock(&file->mutex);
1413	list_del(&uobj->list);
1414	mutex_unlock(&file->mutex);
 
 
 
 
1415
1416	ib_uverbs_release_uevent(file, &obj->uevent);
 
 
 
 
 
 
1417
 
 
1418	resp.events_reported = obj->uevent.events_reported;
1419
1420	put_uobj(uobj);
1421
1422	if (copy_to_user((void __user *) (unsigned long) cmd.response,
1423			 &resp, sizeof resp))
1424		return -EFAULT;
 
 
 
 
 
1425
1426	return in_len;
 
 
1427}
1428
1429ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
1430			    const char __user *buf, int in_len,
1431			    int out_len)
1432{
1433	struct ib_uverbs_post_send      cmd;
1434	struct ib_uverbs_post_send_resp resp;
1435	struct ib_uverbs_send_wr       *user_wr;
1436	struct ib_send_wr              *wr = NULL, *last, *next, *bad_wr;
 
1437	struct ib_qp                   *qp;
1438	int                             i, sg_ind;
1439	int				is_ud;
1440	ssize_t                         ret = -EINVAL;
 
 
 
 
1441
1442	if (copy_from_user(&cmd, buf, sizeof cmd))
1443		return -EFAULT;
1444
1445	if (in_len < sizeof cmd + cmd.wqe_size * cmd.wr_count +
1446	    cmd.sge_count * sizeof (struct ib_uverbs_sge))
1447		return -EINVAL;
1448
1449	if (cmd.wqe_size < sizeof (struct ib_uverbs_send_wr))
1450		return -EINVAL;
 
 
 
 
1451
1452	user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
1453	if (!user_wr)
1454		return -ENOMEM;
1455
1456	qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1457	if (!qp)
 
1458		goto out;
 
1459
1460	is_ud = qp->qp_type == IB_QPT_UD;
1461	sg_ind = 0;
1462	last = NULL;
1463	for (i = 0; i < cmd.wr_count; ++i) {
1464		if (copy_from_user(user_wr,
1465				   buf + sizeof cmd + i * cmd.wqe_size,
1466				   cmd.wqe_size)) {
1467			ret = -EFAULT;
1468			goto out_put;
1469		}
1470
1471		if (user_wr->num_sge + sg_ind > cmd.sge_count) {
1472			ret = -EINVAL;
1473			goto out_put;
1474		}
1475
1476		next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
1477			       user_wr->num_sge * sizeof (struct ib_sge),
1478			       GFP_KERNEL);
1479		if (!next) {
1480			ret = -ENOMEM;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1481			goto out_put;
1482		}
1483
 
 
 
 
 
 
 
 
1484		if (!last)
1485			wr = next;
1486		else
1487			last->next = next;
1488		last = next;
1489
1490		next->next       = NULL;
1491		next->wr_id      = user_wr->wr_id;
1492		next->num_sge    = user_wr->num_sge;
1493		next->opcode     = user_wr->opcode;
1494		next->send_flags = user_wr->send_flags;
1495
1496		if (is_ud) {
1497			next->wr.ud.ah = idr_read_ah(user_wr->wr.ud.ah,
1498						     file->ucontext);
1499			if (!next->wr.ud.ah) {
1500				ret = -EINVAL;
1501				goto out_put;
1502			}
1503			next->wr.ud.remote_qpn  = user_wr->wr.ud.remote_qpn;
1504			next->wr.ud.remote_qkey = user_wr->wr.ud.remote_qkey;
1505		} else {
1506			switch (next->opcode) {
1507			case IB_WR_RDMA_WRITE_WITH_IMM:
1508				next->ex.imm_data =
1509					(__be32 __force) user_wr->ex.imm_data;
1510			case IB_WR_RDMA_WRITE:
1511			case IB_WR_RDMA_READ:
1512				next->wr.rdma.remote_addr =
1513					user_wr->wr.rdma.remote_addr;
1514				next->wr.rdma.rkey        =
1515					user_wr->wr.rdma.rkey;
1516				break;
1517			case IB_WR_SEND_WITH_IMM:
1518				next->ex.imm_data =
1519					(__be32 __force) user_wr->ex.imm_data;
1520				break;
1521			case IB_WR_SEND_WITH_INV:
1522				next->ex.invalidate_rkey =
1523					user_wr->ex.invalidate_rkey;
1524				break;
1525			case IB_WR_ATOMIC_CMP_AND_SWP:
1526			case IB_WR_ATOMIC_FETCH_AND_ADD:
1527				next->wr.atomic.remote_addr =
1528					user_wr->wr.atomic.remote_addr;
1529				next->wr.atomic.compare_add =
1530					user_wr->wr.atomic.compare_add;
1531				next->wr.atomic.swap = user_wr->wr.atomic.swap;
1532				next->wr.atomic.rkey = user_wr->wr.atomic.rkey;
1533				break;
1534			default:
1535				break;
1536			}
1537		}
1538
1539		if (next->num_sge) {
1540			next->sg_list = (void *) next +
1541				ALIGN(sizeof *next, sizeof (struct ib_sge));
1542			if (copy_from_user(next->sg_list,
1543					   buf + sizeof cmd +
1544					   cmd.wr_count * cmd.wqe_size +
1545					   sg_ind * sizeof (struct ib_sge),
1546					   next->num_sge * sizeof (struct ib_sge))) {
1547				ret = -EFAULT;
1548				goto out_put;
1549			}
1550			sg_ind += next->num_sge;
1551		} else
1552			next->sg_list = NULL;
1553	}
1554
1555	resp.bad_wr = 0;
1556	ret = qp->device->post_send(qp, wr, &bad_wr);
1557	if (ret)
1558		for (next = wr; next; next = next->next) {
1559			++resp.bad_wr;
1560			if (next == bad_wr)
1561				break;
1562		}
1563
1564	if (copy_to_user((void __user *) (unsigned long) cmd.response,
1565			 &resp, sizeof resp))
1566		ret = -EFAULT;
1567
1568out_put:
1569	put_qp_read(qp);
 
1570
1571	while (wr) {
1572		if (is_ud && wr->wr.ud.ah)
1573			put_ah_read(wr->wr.ud.ah);
1574		next = wr->next;
1575		kfree(wr);
1576		wr = next;
1577	}
1578
1579out:
1580	kfree(user_wr);
1581
1582	return ret ? ret : in_len;
1583}
1584
1585static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
1586						    int in_len,
1587						    u32 wr_count,
1588						    u32 sge_count,
1589						    u32 wqe_size)
1590{
1591	struct ib_uverbs_recv_wr *user_wr;
1592	struct ib_recv_wr        *wr = NULL, *last, *next;
1593	int                       sg_ind;
1594	int                       i;
1595	int                       ret;
 
 
1596
1597	if (in_len < wqe_size * wr_count +
1598	    sge_count * sizeof (struct ib_uverbs_sge))
1599		return ERR_PTR(-EINVAL);
1600
1601	if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
1602		return ERR_PTR(-EINVAL);
 
 
 
 
 
 
 
 
1603
1604	user_wr = kmalloc(wqe_size, GFP_KERNEL);
1605	if (!user_wr)
1606		return ERR_PTR(-ENOMEM);
1607
1608	sg_ind = 0;
1609	last = NULL;
1610	for (i = 0; i < wr_count; ++i) {
1611		if (copy_from_user(user_wr, buf + i * wqe_size,
1612				   wqe_size)) {
1613			ret = -EFAULT;
1614			goto err;
1615		}
1616
1617		if (user_wr->num_sge + sg_ind > sge_count) {
1618			ret = -EINVAL;
1619			goto err;
1620		}
1621
1622		next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
1623			       user_wr->num_sge * sizeof (struct ib_sge),
 
 
 
 
 
 
 
1624			       GFP_KERNEL);
1625		if (!next) {
1626			ret = -ENOMEM;
1627			goto err;
1628		}
1629
1630		if (!last)
1631			wr = next;
1632		else
1633			last->next = next;
1634		last = next;
1635
1636		next->next       = NULL;
1637		next->wr_id      = user_wr->wr_id;
1638		next->num_sge    = user_wr->num_sge;
1639
1640		if (next->num_sge) {
1641			next->sg_list = (void *) next +
1642				ALIGN(sizeof *next, sizeof (struct ib_sge));
1643			if (copy_from_user(next->sg_list,
1644					   buf + wr_count * wqe_size +
1645					   sg_ind * sizeof (struct ib_sge),
1646					   next->num_sge * sizeof (struct ib_sge))) {
1647				ret = -EFAULT;
1648				goto err;
1649			}
1650			sg_ind += next->num_sge;
1651		} else
1652			next->sg_list = NULL;
1653	}
1654
1655	kfree(user_wr);
1656	return wr;
1657
1658err:
1659	kfree(user_wr);
1660
1661	while (wr) {
1662		next = wr->next;
1663		kfree(wr);
1664		wr = next;
1665	}
1666
1667	return ERR_PTR(ret);
1668}
1669
1670ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
1671			    const char __user *buf, int in_len,
1672			    int out_len)
1673{
1674	struct ib_uverbs_post_recv      cmd;
1675	struct ib_uverbs_post_recv_resp resp;
1676	struct ib_recv_wr              *wr, *next, *bad_wr;
 
1677	struct ib_qp                   *qp;
1678	ssize_t                         ret = -EINVAL;
 
1679
1680	if (copy_from_user(&cmd, buf, sizeof cmd))
1681		return -EFAULT;
 
1682
1683	wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
1684				       in_len - sizeof cmd, cmd.wr_count,
1685				       cmd.sge_count, cmd.wqe_size);
1686	if (IS_ERR(wr))
1687		return PTR_ERR(wr);
1688
1689	qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1690	if (!qp)
 
1691		goto out;
 
1692
1693	resp.bad_wr = 0;
1694	ret = qp->device->post_recv(qp, wr, &bad_wr);
1695
1696	put_qp_read(qp);
1697
1698	if (ret)
 
 
1699		for (next = wr; next; next = next->next) {
1700			++resp.bad_wr;
1701			if (next == bad_wr)
1702				break;
1703		}
 
1704
1705	if (copy_to_user((void __user *) (unsigned long) cmd.response,
1706			 &resp, sizeof resp))
1707		ret = -EFAULT;
1708
1709out:
1710	while (wr) {
1711		next = wr->next;
1712		kfree(wr);
1713		wr = next;
1714	}
1715
1716	return ret ? ret : in_len;
1717}
1718
1719ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
1720				const char __user *buf, int in_len,
1721				int out_len)
1722{
1723	struct ib_uverbs_post_srq_recv      cmd;
1724	struct ib_uverbs_post_srq_recv_resp resp;
1725	struct ib_recv_wr                  *wr, *next, *bad_wr;
 
1726	struct ib_srq                      *srq;
1727	ssize_t                             ret = -EINVAL;
 
1728
1729	if (copy_from_user(&cmd, buf, sizeof cmd))
1730		return -EFAULT;
 
1731
1732	wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
1733				       in_len - sizeof cmd, cmd.wr_count,
1734				       cmd.sge_count, cmd.wqe_size);
1735	if (IS_ERR(wr))
1736		return PTR_ERR(wr);
1737
1738	srq = idr_read_srq(cmd.srq_handle, file->ucontext);
1739	if (!srq)
 
1740		goto out;
 
1741
1742	resp.bad_wr = 0;
1743	ret = srq->device->post_srq_recv(srq, wr, &bad_wr);
1744
1745	put_srq_read(srq);
 
1746
1747	if (ret)
1748		for (next = wr; next; next = next->next) {
1749			++resp.bad_wr;
1750			if (next == bad_wr)
1751				break;
1752		}
1753
1754	if (copy_to_user((void __user *) (unsigned long) cmd.response,
1755			 &resp, sizeof resp))
1756		ret = -EFAULT;
1757
1758out:
1759	while (wr) {
1760		next = wr->next;
1761		kfree(wr);
1762		wr = next;
1763	}
1764
1765	return ret ? ret : in_len;
1766}
1767
1768ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
1769			    const char __user *buf, int in_len,
1770			    int out_len)
1771{
1772	struct ib_uverbs_create_ah	 cmd;
1773	struct ib_uverbs_create_ah_resp	 resp;
1774	struct ib_uobject		*uobj;
1775	struct ib_pd			*pd;
1776	struct ib_ah			*ah;
1777	struct ib_ah_attr		attr;
1778	int ret;
 
1779
1780	if (out_len < sizeof resp)
1781		return -ENOSPC;
 
1782
1783	if (copy_from_user(&cmd, buf, sizeof cmd))
1784		return -EFAULT;
 
1785
1786	uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
1787	if (!uobj)
1788		return -ENOMEM;
1789
1790	init_uobj(uobj, cmd.user_handle, file->ucontext, &ah_lock_key);
1791	down_write(&uobj->mutex);
1792
1793	pd = idr_read_pd(cmd.pd_handle, file->ucontext);
1794	if (!pd) {
1795		ret = -EINVAL;
1796		goto err;
1797	}
1798
1799	attr.dlid 	       = cmd.attr.dlid;
1800	attr.sl 	       = cmd.attr.sl;
1801	attr.src_path_bits     = cmd.attr.src_path_bits;
1802	attr.static_rate       = cmd.attr.static_rate;
1803	attr.ah_flags          = cmd.attr.is_global ? IB_AH_GRH : 0;
1804	attr.port_num 	       = cmd.attr.port_num;
1805	attr.grh.flow_label    = cmd.attr.grh.flow_label;
1806	attr.grh.sgid_index    = cmd.attr.grh.sgid_index;
1807	attr.grh.hop_limit     = cmd.attr.grh.hop_limit;
1808	attr.grh.traffic_class = cmd.attr.grh.traffic_class;
1809	memcpy(attr.grh.dgid.raw, cmd.attr.grh.dgid, 16);
 
 
 
 
 
 
1810
1811	ah = ib_create_ah(pd, &attr);
1812	if (IS_ERR(ah)) {
1813		ret = PTR_ERR(ah);
1814		goto err_put;
1815	}
1816
1817	ah->uobject  = uobj;
 
1818	uobj->object = ah;
1819
1820	ret = idr_add_uobj(&ib_uverbs_ah_idr, uobj);
1821	if (ret)
1822		goto err_destroy;
1823
1824	resp.ah_handle = uobj->id;
1825
1826	if (copy_to_user((void __user *) (unsigned long) cmd.response,
1827			 &resp, sizeof resp)) {
1828		ret = -EFAULT;
1829		goto err_copy;
1830	}
1831
1832	put_pd_read(pd);
1833
1834	mutex_lock(&file->mutex);
1835	list_add_tail(&uobj->list, &file->ucontext->ah_list);
1836	mutex_unlock(&file->mutex);
1837
1838	uobj->live = 1;
1839
1840	up_write(&uobj->mutex);
1841
1842	return in_len;
1843
1844err_copy:
1845	idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
1846
1847err_destroy:
1848	ib_destroy_ah(ah);
1849
1850err_put:
1851	put_pd_read(pd);
1852
1853err:
1854	put_uobj_write(uobj);
1855	return ret;
1856}
1857
1858ssize_t ib_uverbs_destroy_ah(struct ib_uverbs_file *file,
1859			     const char __user *buf, int in_len, int out_len)
1860{
1861	struct ib_uverbs_destroy_ah cmd;
1862	struct ib_ah		   *ah;
1863	struct ib_uobject	   *uobj;
1864	int			    ret;
1865
1866	if (copy_from_user(&cmd, buf, sizeof cmd))
1867		return -EFAULT;
1868
1869	uobj = idr_write_uobj(&ib_uverbs_ah_idr, cmd.ah_handle, file->ucontext);
1870	if (!uobj)
1871		return -EINVAL;
1872	ah = uobj->object;
1873
1874	ret = ib_destroy_ah(ah);
1875	if (!ret)
1876		uobj->live = 0;
1877
1878	put_uobj_write(uobj);
1879
 
1880	if (ret)
1881		return ret;
1882
1883	idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
1884
1885	mutex_lock(&file->mutex);
1886	list_del(&uobj->list);
1887	mutex_unlock(&file->mutex);
1888
1889	put_uobj(uobj);
1890
1891	return in_len;
1892}
1893
1894ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
1895			       const char __user *buf, int in_len,
1896			       int out_len)
1897{
1898	struct ib_uverbs_attach_mcast cmd;
1899	struct ib_qp                 *qp;
1900	struct ib_uqp_object         *obj;
1901	struct ib_uverbs_mcast_entry *mcast;
1902	int                           ret;
1903
1904	if (copy_from_user(&cmd, buf, sizeof cmd))
1905		return -EFAULT;
 
1906
1907	qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1908	if (!qp)
1909		return -EINVAL;
1910
1911	obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
1912
 
1913	list_for_each_entry(mcast, &obj->mcast_list, list)
1914		if (cmd.mlid == mcast->lid &&
1915		    !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
1916			ret = 0;
1917			goto out_put;
1918		}
1919
1920	mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
1921	if (!mcast) {
1922		ret = -ENOMEM;
1923		goto out_put;
1924	}
1925
1926	mcast->lid = cmd.mlid;
1927	memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
1928
1929	ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
1930	if (!ret)
1931		list_add_tail(&mcast->list, &obj->mcast_list);
1932	else
1933		kfree(mcast);
1934
1935out_put:
1936	put_qp_read(qp);
 
 
1937
1938	return ret ? ret : in_len;
1939}
1940
1941ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
1942			       const char __user *buf, int in_len,
1943			       int out_len)
1944{
1945	struct ib_uverbs_detach_mcast cmd;
1946	struct ib_uqp_object         *obj;
1947	struct ib_qp                 *qp;
1948	struct ib_uverbs_mcast_entry *mcast;
1949	int                           ret = -EINVAL;
 
1950
1951	if (copy_from_user(&cmd, buf, sizeof cmd))
1952		return -EFAULT;
 
1953
1954	qp = idr_read_qp(cmd.qp_handle, file->ucontext);
1955	if (!qp)
1956		return -EINVAL;
1957
1958	ret = ib_detach_mcast(qp, (union ib_gid *) cmd.gid, cmd.mlid);
1959	if (ret)
1960		goto out_put;
1961
1962	obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
1963
1964	list_for_each_entry(mcast, &obj->mcast_list, list)
1965		if (cmd.mlid == mcast->lid &&
1966		    !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
1967			list_del(&mcast->list);
1968			kfree(mcast);
 
1969			break;
1970		}
1971
 
 
 
 
 
 
 
1972out_put:
1973	put_qp_read(qp);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1974
1975	return ret ? ret : in_len;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1976}
 
1977
1978ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
1979			     const char __user *buf, int in_len,
1980			     int out_len)
1981{
1982	struct ib_uverbs_create_srq      cmd;
1983	struct ib_uverbs_create_srq_resp resp;
1984	struct ib_udata                  udata;
1985	struct ib_uevent_object         *obj;
1986	struct ib_pd                    *pd;
1987	struct ib_srq                   *srq;
1988	struct ib_srq_init_attr          attr;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1989	int ret;
1990
1991	if (out_len < sizeof resp)
1992		return -ENOSPC;
 
1993
1994	if (copy_from_user(&cmd, buf, sizeof cmd))
1995		return -EFAULT;
1996
1997	INIT_UDATA(&udata, buf + sizeof cmd,
1998		   (unsigned long) cmd.response + sizeof resp,
1999		   in_len - sizeof cmd, out_len - sizeof resp);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2000
2001	obj = kmalloc(sizeof *obj, GFP_KERNEL);
2002	if (!obj)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2003		return -ENOMEM;
2004
2005	init_uobj(&obj->uobject, cmd.user_handle, file->ucontext, &srq_lock_key);
2006	down_write(&obj->uobject.mutex);
 
 
2007
2008	pd  = idr_read_pd(cmd.pd_handle, file->ucontext);
2009	if (!pd) {
2010		ret = -EINVAL;
2011		goto err;
 
 
 
 
2012	}
2013
2014	attr.event_handler  = ib_uverbs_srq_event_handler;
2015	attr.srq_context    = file;
2016	attr.attr.max_wr    = cmd.max_wr;
2017	attr.attr.max_sge   = cmd.max_sge;
2018	attr.attr.srq_limit = cmd.srq_limit;
 
 
 
2019
2020	obj->events_reported     = 0;
2021	INIT_LIST_HEAD(&obj->event_list);
 
2022
2023	srq = pd->device->create_srq(pd, &attr, &udata);
2024	if (IS_ERR(srq)) {
2025		ret = PTR_ERR(srq);
2026		goto err_put;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2027	}
 
 
 
 
 
2028
2029	srq->device    	   = pd->device;
2030	srq->pd        	   = pd;
2031	srq->uobject       = &obj->uobject;
2032	srq->event_handler = attr.event_handler;
2033	srq->srq_context   = attr.srq_context;
2034	atomic_inc(&pd->usecnt);
2035	atomic_set(&srq->usecnt, 0);
2036
2037	obj->uobject.object = srq;
2038	ret = idr_add_uobj(&ib_uverbs_srq_idr, &obj->uobject);
2039	if (ret)
2040		goto err_destroy;
2041
2042	memset(&resp, 0, sizeof resp);
2043	resp.srq_handle = obj->uobject.id;
2044	resp.max_wr     = attr.attr.max_wr;
2045	resp.max_sge    = attr.attr.max_sge;
2046
2047	if (copy_to_user((void __user *) (unsigned long) cmd.response,
2048			 &resp, sizeof resp)) {
2049		ret = -EFAULT;
2050		goto err_copy;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2051	}
2052
2053	put_pd_read(pd);
 
 
 
2054
2055	mutex_lock(&file->mutex);
2056	list_add_tail(&obj->uobject.list, &file->ucontext->srq_list);
2057	mutex_unlock(&file->mutex);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2058
2059	obj->uobject.live = 1;
 
2060
2061	up_write(&obj->uobject.mutex);
 
 
 
2062
2063	return in_len;
2064
2065err_copy:
2066	idr_remove_uobj(&ib_uverbs_srq_idr, &obj->uobject);
 
 
 
 
 
2067
2068err_destroy:
2069	ib_destroy_srq(srq);
2070
 
 
 
 
2071err_put:
2072	put_pd_read(pd);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2073
2074err:
2075	put_uobj_write(&obj->uobject);
2076	return ret;
2077}
2078
2079ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
2080			     const char __user *buf, int in_len,
2081			     int out_len)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2082{
2083	struct ib_uverbs_modify_srq cmd;
2084	struct ib_udata             udata;
2085	struct ib_srq              *srq;
2086	struct ib_srq_attr          attr;
2087	int                         ret;
2088
2089	if (copy_from_user(&cmd, buf, sizeof cmd))
2090		return -EFAULT;
2091
2092	INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
2093		   out_len);
2094
2095	srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2096	if (!srq)
2097		return -EINVAL;
2098
2099	attr.max_wr    = cmd.max_wr;
2100	attr.srq_limit = cmd.srq_limit;
2101
2102	ret = srq->device->modify_srq(srq, &attr, cmd.attr_mask, &udata);
 
2103
2104	put_srq_read(srq);
 
2105
2106	return ret ? ret : in_len;
2107}
2108
2109ssize_t ib_uverbs_query_srq(struct ib_uverbs_file *file,
2110			    const char __user *buf,
2111			    int in_len, int out_len)
2112{
2113	struct ib_uverbs_query_srq      cmd;
2114	struct ib_uverbs_query_srq_resp resp;
2115	struct ib_srq_attr              attr;
2116	struct ib_srq                   *srq;
2117	int                             ret;
2118
2119	if (out_len < sizeof resp)
2120		return -ENOSPC;
2121
2122	if (copy_from_user(&cmd, buf, sizeof cmd))
2123		return -EFAULT;
2124
2125	srq = idr_read_srq(cmd.srq_handle, file->ucontext);
2126	if (!srq)
2127		return -EINVAL;
2128
2129	ret = ib_query_srq(srq, &attr);
2130
2131	put_srq_read(srq);
 
2132
2133	if (ret)
2134		return ret;
2135
2136	memset(&resp, 0, sizeof resp);
2137
2138	resp.max_wr    = attr.max_wr;
2139	resp.max_sge   = attr.max_sge;
2140	resp.srq_limit = attr.srq_limit;
2141
2142	if (copy_to_user((void __user *) (unsigned long) cmd.response,
2143			 &resp, sizeof resp))
2144		return -EFAULT;
2145
2146	return in_len;
2147}
2148
2149ssize_t ib_uverbs_destroy_srq(struct ib_uverbs_file *file,
2150			      const char __user *buf, int in_len,
2151			      int out_len)
2152{
2153	struct ib_uverbs_destroy_srq      cmd;
2154	struct ib_uverbs_destroy_srq_resp resp;
2155	struct ib_uobject		 *uobj;
2156	struct ib_srq               	 *srq;
2157	struct ib_uevent_object        	 *obj;
2158	int                         	  ret = -EINVAL;
2159
2160	if (copy_from_user(&cmd, buf, sizeof cmd))
2161		return -EFAULT;
 
 
 
 
 
2162
2163	uobj = idr_write_uobj(&ib_uverbs_srq_idr, cmd.srq_handle, file->ucontext);
2164	if (!uobj)
2165		return -EINVAL;
2166	srq = uobj->object;
2167	obj = container_of(uobj, struct ib_uevent_object, uobject);
 
 
2168
2169	ret = ib_destroy_srq(srq);
2170	if (!ret)
2171		uobj->live = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2172
2173	put_uobj_write(uobj);
 
 
 
 
 
 
 
2174
 
2175	if (ret)
2176		return ret;
2177
2178	idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
 
2179
2180	mutex_lock(&file->mutex);
2181	list_del(&uobj->list);
2182	mutex_unlock(&file->mutex);
2183
2184	ib_uverbs_release_uevent(file, obj);
 
 
2185
2186	memset(&resp, 0, sizeof resp);
2187	resp.events_reported = obj->events_reported;
2188
2189	put_uobj(uobj);
 
 
 
2190
2191	if (copy_to_user((void __user *) (unsigned long) cmd.response,
2192			 &resp, sizeof resp))
2193		ret = -EFAULT;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2194
2195	return ret ? ret : in_len;
2196}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
v5.14.15
   1/*
   2 * Copyright (c) 2005 Topspin Communications.  All rights reserved.
   3 * Copyright (c) 2005, 2006, 2007 Cisco Systems.  All rights reserved.
   4 * Copyright (c) 2005 PathScale, Inc.  All rights reserved.
   5 * Copyright (c) 2006 Mellanox Technologies.  All rights reserved.
   6 *
   7 * This software is available to you under a choice of one of two
   8 * licenses.  You may choose to be licensed under the terms of the GNU
   9 * General Public License (GPL) Version 2, available from the file
  10 * COPYING in the main directory of this source tree, or the
  11 * OpenIB.org BSD license below:
  12 *
  13 *     Redistribution and use in source and binary forms, with or
  14 *     without modification, are permitted provided that the following
  15 *     conditions are met:
  16 *
  17 *      - Redistributions of source code must retain the above
  18 *        copyright notice, this list of conditions and the following
  19 *        disclaimer.
  20 *
  21 *      - Redistributions in binary form must reproduce the above
  22 *        copyright notice, this list of conditions and the following
  23 *        disclaimer in the documentation and/or other materials
  24 *        provided with the distribution.
  25 *
  26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33 * SOFTWARE.
  34 */
  35
  36#include <linux/file.h>
  37#include <linux/fs.h>
  38#include <linux/slab.h>
  39#include <linux/sched.h>
  40
  41#include <linux/uaccess.h>
  42
  43#include <rdma/uverbs_types.h>
  44#include <rdma/uverbs_std_types.h>
  45#include "rdma_core.h"
  46
  47#include "uverbs.h"
  48#include "core_priv.h"
 
 
 
 
 
 
 
 
 
 
 
 
  49
  50/*
  51 * Copy a response to userspace. If the provided 'resp' is larger than the
  52 * user buffer it is silently truncated. If the user provided a larger buffer
  53 * then the trailing portion is zero filled.
 
 
 
 
 
 
 
 
 
  54 *
  55 * These semantics are intended to support future extension of the output
  56 * structures.
 
 
 
 
 
 
 
  57 */
  58static int uverbs_response(struct uverbs_attr_bundle *attrs, const void *resp,
  59			   size_t resp_len)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  60{
  61	int ret;
  62
  63	if (uverbs_attr_is_valid(attrs, UVERBS_ATTR_CORE_OUT))
  64		return uverbs_copy_to_struct_or_zero(
  65			attrs, UVERBS_ATTR_CORE_OUT, resp, resp_len);
  66
  67	if (copy_to_user(attrs->ucore.outbuf, resp,
  68			 min(attrs->ucore.outlen, resp_len)))
  69		return -EFAULT;
 
 
 
  70
  71	if (resp_len < attrs->ucore.outlen) {
  72		/*
  73		 * Zero fill any extra memory that user
  74		 * space might have provided.
  75		 */
  76		ret = clear_user(attrs->ucore.outbuf + resp_len,
  77				 attrs->ucore.outlen - resp_len);
  78		if (ret)
  79			return -EFAULT;
  80	}
  81
  82	return 0;
 
 
 
 
  83}
  84
  85/*
  86 * Copy a request from userspace. If the provided 'req' is larger than the
  87 * user buffer then the user buffer is zero extended into the 'req'. If 'req'
  88 * is smaller than the user buffer then the uncopied bytes in the user buffer
  89 * must be zero.
  90 */
  91static int uverbs_request(struct uverbs_attr_bundle *attrs, void *req,
  92			  size_t req_len)
  93{
  94	if (copy_from_user(req, attrs->ucore.inbuf,
  95			   min(attrs->ucore.inlen, req_len)))
  96		return -EFAULT;
  97
  98	if (attrs->ucore.inlen < req_len) {
  99		memset(req + attrs->ucore.inlen, 0,
 100		       req_len - attrs->ucore.inlen);
 101	} else if (attrs->ucore.inlen > req_len) {
 102		if (!ib_is_buffer_cleared(attrs->ucore.inbuf + req_len,
 103					  attrs->ucore.inlen - req_len))
 104			return -EOPNOTSUPP;
 105	}
 106	return 0;
 
 
 107}
 108
 109/*
 110 * Generate the value for the 'response_length' protocol used by write_ex.
 111 * This is the number of bytes the kernel actually wrote. Userspace can use
 112 * this to detect what structure members in the response the kernel
 113 * understood.
 114 */
 115static u32 uverbs_response_length(struct uverbs_attr_bundle *attrs,
 116				  size_t resp_len)
 117{
 118	return min_t(size_t, attrs->ucore.outlen, resp_len);
 119}
 120
 121/*
 122 * The iterator version of the request interface is for handlers that need to
 123 * step over a flex array at the end of a command header.
 124 */
 125struct uverbs_req_iter {
 126	const void __user *cur;
 127	const void __user *end;
 128};
 129
 130static int uverbs_request_start(struct uverbs_attr_bundle *attrs,
 131				struct uverbs_req_iter *iter,
 132				void *req,
 133				size_t req_len)
 134{
 135	if (attrs->ucore.inlen < req_len)
 136		return -ENOSPC;
 137
 138	if (copy_from_user(req, attrs->ucore.inbuf, req_len))
 139		return -EFAULT;
 
 
 
 
 
 
 140
 141	iter->cur = attrs->ucore.inbuf + req_len;
 142	iter->end = attrs->ucore.inbuf + attrs->ucore.inlen;
 143	return 0;
 144}
 145
 146static int uverbs_request_next(struct uverbs_req_iter *iter, void *val,
 147			       size_t len)
 148{
 149	if (iter->cur + len > iter->end)
 150		return -ENOSPC;
 
 
 
 151
 152	if (copy_from_user(val, iter->cur, len))
 153		return -EFAULT;
 
 
 
 154
 155	iter->cur += len;
 156	return 0;
 157}
 158
 159static const void __user *uverbs_request_next_ptr(struct uverbs_req_iter *iter,
 160						  size_t len)
 161{
 162	const void __user *res = iter->cur;
 163
 164	if (iter->cur + len > iter->end)
 165		return (void __force __user *)ERR_PTR(-ENOSPC);
 166	iter->cur += len;
 167	return res;
 168}
 169
 170static int uverbs_request_finish(struct uverbs_req_iter *iter)
 171{
 172	if (!ib_is_buffer_cleared(iter->cur, iter->end - iter->cur))
 173		return -EOPNOTSUPP;
 174	return 0;
 175}
 176
 177/*
 178 * When calling a destroy function during an error unwind we need to pass in
 179 * the udata that is sanitized of all user arguments. Ie from the driver
 180 * perspective it looks like no udata was passed.
 181 */
 182struct ib_udata *uverbs_get_cleared_udata(struct uverbs_attr_bundle *attrs)
 183{
 184	attrs->driver_udata = (struct ib_udata){};
 185	return &attrs->driver_udata;
 186}
 187
 188static struct ib_uverbs_completion_event_file *
 189_ib_uverbs_lookup_comp_file(s32 fd, struct uverbs_attr_bundle *attrs)
 190{
 191	struct ib_uobject *uobj = ufd_get_read(UVERBS_OBJECT_COMP_CHANNEL,
 192					       fd, attrs);
 193
 194	if (IS_ERR(uobj))
 195		return (void *)uobj;
 
 
 196
 197	uverbs_uobject_get(uobj);
 198	uobj_put_read(uobj);
 
 
 199
 200	return container_of(uobj, struct ib_uverbs_completion_event_file,
 201			    uobj);
 
 202}
 203#define ib_uverbs_lookup_comp_file(_fd, _ufile)                                \
 204	_ib_uverbs_lookup_comp_file((_fd)*typecheck(s32, _fd), _ufile)
 205
 206int ib_alloc_ucontext(struct uverbs_attr_bundle *attrs)
 207{
 208	struct ib_uverbs_file *ufile = attrs->ufile;
 209	struct ib_ucontext *ucontext;
 210	struct ib_device *ib_dev;
 211
 212	ib_dev = srcu_dereference(ufile->device->ib_dev,
 213				  &ufile->device->disassociate_srcu);
 214	if (!ib_dev)
 215		return -EIO;
 216
 217	ucontext = rdma_zalloc_drv_obj(ib_dev, ib_ucontext);
 218	if (!ucontext)
 219		return -ENOMEM;
 
 220
 221	ucontext->device = ib_dev;
 222	ucontext->ufile = ufile;
 223	xa_init_flags(&ucontext->mmap_xa, XA_FLAGS_ALLOC);
 224
 225	rdma_restrack_new(&ucontext->res, RDMA_RESTRACK_CTX);
 226	rdma_restrack_set_name(&ucontext->res, NULL);
 227	attrs->context = ucontext;
 228	return 0;
 229}
 230
 231int ib_init_ucontext(struct uverbs_attr_bundle *attrs)
 
 
 232{
 233	struct ib_ucontext *ucontext = attrs->context;
 234	struct ib_uverbs_file *file = attrs->ufile;
 
 
 
 
 235	int ret;
 236
 237	if (!down_read_trylock(&file->hw_destroy_rwsem))
 238		return -EIO;
 239	mutex_lock(&file->ucontext_lock);
 
 
 
 
 
 240	if (file->ucontext) {
 241		ret = -EINVAL;
 242		goto err;
 243	}
 244
 245	ret = ib_rdmacg_try_charge(&ucontext->cg_obj, ucontext->device,
 246				   RDMACG_RESOURCE_HCA_HANDLE);
 247	if (ret)
 
 
 
 
 248		goto err;
 
 249
 250	ret = ucontext->device->ops.alloc_ucontext(ucontext,
 251						   &attrs->driver_udata);
 252	if (ret)
 253		goto err_uncharge;
 
 
 
 
 
 254
 255	rdma_restrack_add(&ucontext->res);
 256
 257	/*
 258	 * Make sure that ib_uverbs_get_ucontext() sees the pointer update
 259	 * only after all writes to setup the ucontext have completed
 260	 */
 261	smp_store_release(&file->ucontext, ucontext);
 262
 263	mutex_unlock(&file->ucontext_lock);
 264	up_read(&file->hw_destroy_rwsem);
 265	return 0;
 
 
 266
 267err_uncharge:
 268	ib_rdmacg_uncharge(&ucontext->cg_obj, ucontext->device,
 269			   RDMACG_RESOURCE_HCA_HANDLE);
 270err:
 271	mutex_unlock(&file->ucontext_lock);
 272	up_read(&file->hw_destroy_rwsem);
 273	return ret;
 274}
 275
 276static int ib_uverbs_get_context(struct uverbs_attr_bundle *attrs)
 277{
 278	struct ib_uverbs_get_context_resp resp;
 279	struct ib_uverbs_get_context cmd;
 280	struct ib_device *ib_dev;
 281	struct ib_uobject *uobj;
 282	int ret;
 283
 284	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
 
 
 285	if (ret)
 286		return ret;
 
 
 
 
 
 
 287
 288	ret = ib_alloc_ucontext(attrs);
 289	if (ret)
 290		return ret;
 291
 292	uobj = uobj_alloc(UVERBS_OBJECT_ASYNC_EVENT, attrs, &ib_dev);
 293	if (IS_ERR(uobj)) {
 294		ret = PTR_ERR(uobj);
 295		goto err_ucontext;
 296	}
 297
 298	resp = (struct ib_uverbs_get_context_resp){
 299		.num_comp_vectors = attrs->ufile->device->num_comp_vectors,
 300		.async_fd = uobj->id,
 301	};
 302	ret = uverbs_response(attrs, &resp, sizeof(resp));
 303	if (ret)
 304		goto err_uobj;
 305
 306	ret = ib_init_ucontext(attrs);
 307	if (ret)
 308		goto err_uobj;
 309
 310	ib_uverbs_init_async_event_file(
 311		container_of(uobj, struct ib_uverbs_async_event_file, uobj));
 312	rdma_alloc_commit_uobject(uobj, attrs);
 313	return 0;
 314
 315err_uobj:
 316	rdma_alloc_abort_uobject(uobj, attrs, false);
 317err_ucontext:
 318	rdma_restrack_put(&attrs->context->res);
 319	kfree(attrs->context);
 320	attrs->context = NULL;
 321	return ret;
 322}
 323
 324static void copy_query_dev_fields(struct ib_ucontext *ucontext,
 325				  struct ib_uverbs_query_device_resp *resp,
 326				  struct ib_device_attr *attr)
 327{
 328	struct ib_device *ib_dev = ucontext->device;
 329
 330	resp->fw_ver		= attr->fw_ver;
 331	resp->node_guid		= ib_dev->node_guid;
 332	resp->sys_image_guid	= attr->sys_image_guid;
 333	resp->max_mr_size	= attr->max_mr_size;
 334	resp->page_size_cap	= attr->page_size_cap;
 335	resp->vendor_id		= attr->vendor_id;
 336	resp->vendor_part_id	= attr->vendor_part_id;
 337	resp->hw_ver		= attr->hw_ver;
 338	resp->max_qp		= attr->max_qp;
 339	resp->max_qp_wr		= attr->max_qp_wr;
 340	resp->device_cap_flags	= lower_32_bits(attr->device_cap_flags);
 341	resp->max_sge		= min(attr->max_send_sge, attr->max_recv_sge);
 342	resp->max_sge_rd	= attr->max_sge_rd;
 343	resp->max_cq		= attr->max_cq;
 344	resp->max_cqe		= attr->max_cqe;
 345	resp->max_mr		= attr->max_mr;
 346	resp->max_pd		= attr->max_pd;
 347	resp->max_qp_rd_atom	= attr->max_qp_rd_atom;
 348	resp->max_ee_rd_atom	= attr->max_ee_rd_atom;
 349	resp->max_res_rd_atom	= attr->max_res_rd_atom;
 350	resp->max_qp_init_rd_atom	= attr->max_qp_init_rd_atom;
 351	resp->max_ee_init_rd_atom	= attr->max_ee_init_rd_atom;
 352	resp->atomic_cap		= attr->atomic_cap;
 353	resp->max_ee			= attr->max_ee;
 354	resp->max_rdd			= attr->max_rdd;
 355	resp->max_mw			= attr->max_mw;
 356	resp->max_raw_ipv6_qp		= attr->max_raw_ipv6_qp;
 357	resp->max_raw_ethy_qp		= attr->max_raw_ethy_qp;
 358	resp->max_mcast_grp		= attr->max_mcast_grp;
 359	resp->max_mcast_qp_attach	= attr->max_mcast_qp_attach;
 360	resp->max_total_mcast_qp_attach	= attr->max_total_mcast_qp_attach;
 361	resp->max_ah			= attr->max_ah;
 362	resp->max_srq			= attr->max_srq;
 363	resp->max_srq_wr		= attr->max_srq_wr;
 364	resp->max_srq_sge		= attr->max_srq_sge;
 365	resp->max_pkeys			= attr->max_pkeys;
 366	resp->local_ca_ack_delay	= attr->local_ca_ack_delay;
 367	resp->phys_port_cnt = min_t(u32, ib_dev->phys_port_cnt, U8_MAX);
 368}
 369
 370static int ib_uverbs_query_device(struct uverbs_attr_bundle *attrs)
 371{
 372	struct ib_uverbs_query_device      cmd;
 373	struct ib_uverbs_query_device_resp resp;
 374	struct ib_ucontext *ucontext;
 375	int ret;
 
 
 
 376
 377	ucontext = ib_uverbs_get_ucontext(attrs);
 378	if (IS_ERR(ucontext))
 379		return PTR_ERR(ucontext);
 380
 381	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
 382	if (ret)
 383		return ret;
 384
 385	memset(&resp, 0, sizeof resp);
 386	copy_query_dev_fields(ucontext, &resp, &ucontext->device->attrs);
 387
 388	return uverbs_response(attrs, &resp, sizeof(resp));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 389}
 390
 391static int ib_uverbs_query_port(struct uverbs_attr_bundle *attrs)
 
 
 392{
 393	struct ib_uverbs_query_port      cmd;
 394	struct ib_uverbs_query_port_resp resp;
 395	struct ib_port_attr              attr;
 396	int                              ret;
 397	struct ib_ucontext *ucontext;
 398	struct ib_device *ib_dev;
 399
 400	ucontext = ib_uverbs_get_ucontext(attrs);
 401	if (IS_ERR(ucontext))
 402		return PTR_ERR(ucontext);
 403	ib_dev = ucontext->device;
 404
 405	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
 406	if (ret)
 407		return ret;
 408
 409	ret = ib_query_port(ib_dev, cmd.port_num, &attr);
 410	if (ret)
 411		return ret;
 412
 413	memset(&resp, 0, sizeof resp);
 414	copy_port_attr_to_resp(&attr, &resp, ib_dev, cmd.port_num);
 415
 416	return uverbs_response(attrs, &resp, sizeof(resp));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 417}
 418
 419static int ib_uverbs_alloc_pd(struct uverbs_attr_bundle *attrs)
 
 
 420{
 421	struct ib_uverbs_alloc_pd_resp resp = {};
 422	struct ib_uverbs_alloc_pd      cmd;
 
 
 423	struct ib_uobject             *uobj;
 424	struct ib_pd                  *pd;
 425	int                            ret;
 426	struct ib_device *ib_dev;
 427
 428	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
 429	if (ret)
 430		return ret;
 
 
 
 
 
 
 
 
 
 
 431
 432	uobj = uobj_alloc(UVERBS_OBJECT_PD, attrs, &ib_dev);
 433	if (IS_ERR(uobj))
 434		return PTR_ERR(uobj);
 435
 436	pd = rdma_zalloc_drv_obj(ib_dev, ib_pd);
 437	if (!pd) {
 438		ret = -ENOMEM;
 
 439		goto err;
 440	}
 441
 442	pd->device  = ib_dev;
 443	pd->uobject = uobj;
 444	atomic_set(&pd->usecnt, 0);
 445
 446	rdma_restrack_new(&pd->res, RDMA_RESTRACK_PD);
 447	rdma_restrack_set_name(&pd->res, NULL);
 448
 449	ret = ib_dev->ops.alloc_pd(pd, &attrs->driver_udata);
 450	if (ret)
 451		goto err_alloc;
 452	rdma_restrack_add(&pd->res);
 453
 454	uobj->object = pd;
 455	uobj_finalize_uobj_create(uobj, attrs);
 456
 
 457	resp.pd_handle = uobj->id;
 458	return uverbs_response(attrs, &resp, sizeof(resp));
 459
 460err_alloc:
 461	rdma_restrack_put(&pd->res);
 462	kfree(pd);
 463err:
 464	uobj_alloc_abort(uobj, attrs);
 465	return ret;
 466}
 467
 468static int ib_uverbs_dealloc_pd(struct uverbs_attr_bundle *attrs)
 469{
 470	struct ib_uverbs_dealloc_pd cmd;
 471	int ret;
 472
 473	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
 474	if (ret)
 475		return ret;
 476
 477	return uobj_perform_destroy(UVERBS_OBJECT_PD, cmd.pd_handle, attrs);
 478}
 479
 480struct xrcd_table_entry {
 481	struct rb_node  node;
 482	struct ib_xrcd *xrcd;
 483	struct inode   *inode;
 484};
 485
 486static int xrcd_table_insert(struct ib_uverbs_device *dev,
 487			    struct inode *inode,
 488			    struct ib_xrcd *xrcd)
 489{
 490	struct xrcd_table_entry *entry, *scan;
 491	struct rb_node **p = &dev->xrcd_tree.rb_node;
 492	struct rb_node *parent = NULL;
 493
 494	entry = kmalloc(sizeof *entry, GFP_KERNEL);
 495	if (!entry)
 496		return -ENOMEM;
 497
 498	entry->xrcd  = xrcd;
 499	entry->inode = inode;
 500
 501	while (*p) {
 502		parent = *p;
 503		scan = rb_entry(parent, struct xrcd_table_entry, node);
 504
 505		if (inode < scan->inode) {
 506			p = &(*p)->rb_left;
 507		} else if (inode > scan->inode) {
 508			p = &(*p)->rb_right;
 509		} else {
 510			kfree(entry);
 511			return -EEXIST;
 512		}
 513	}
 514
 515	rb_link_node(&entry->node, parent, p);
 516	rb_insert_color(&entry->node, &dev->xrcd_tree);
 517	igrab(inode);
 518	return 0;
 519}
 520
 521static struct xrcd_table_entry *xrcd_table_search(struct ib_uverbs_device *dev,
 522						  struct inode *inode)
 523{
 524	struct xrcd_table_entry *entry;
 525	struct rb_node *p = dev->xrcd_tree.rb_node;
 526
 527	while (p) {
 528		entry = rb_entry(p, struct xrcd_table_entry, node);
 529
 530		if (inode < entry->inode)
 531			p = p->rb_left;
 532		else if (inode > entry->inode)
 533			p = p->rb_right;
 534		else
 535			return entry;
 536	}
 537
 538	return NULL;
 539}
 540
 541static struct ib_xrcd *find_xrcd(struct ib_uverbs_device *dev, struct inode *inode)
 542{
 543	struct xrcd_table_entry *entry;
 544
 545	entry = xrcd_table_search(dev, inode);
 546	if (!entry)
 547		return NULL;
 548
 549	return entry->xrcd;
 550}
 551
 552static void xrcd_table_delete(struct ib_uverbs_device *dev,
 553			      struct inode *inode)
 
 554{
 555	struct xrcd_table_entry *entry;
 
 
 556
 557	entry = xrcd_table_search(dev, inode);
 558	if (entry) {
 559		iput(inode);
 560		rb_erase(&entry->node, &dev->xrcd_tree);
 561		kfree(entry);
 562	}
 563}
 564
 565static int ib_uverbs_open_xrcd(struct uverbs_attr_bundle *attrs)
 566{
 567	struct ib_uverbs_device *ibudev = attrs->ufile->device;
 568	struct ib_uverbs_open_xrcd_resp	resp = {};
 569	struct ib_uverbs_open_xrcd	cmd;
 570	struct ib_uxrcd_object         *obj;
 571	struct ib_xrcd                 *xrcd = NULL;
 572	struct inode                   *inode = NULL;
 573	int				new_xrcd = 0;
 574	struct ib_device *ib_dev;
 575	struct fd f = {};
 576	int ret;
 577
 578	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
 579	if (ret)
 580		return ret;
 581
 582	mutex_lock(&ibudev->xrcd_tree_mutex);
 583
 584	if (cmd.fd != -1) {
 585		/* search for file descriptor */
 586		f = fdget(cmd.fd);
 587		if (!f.file) {
 588			ret = -EBADF;
 589			goto err_tree_mutex_unlock;
 590		}
 591
 592		inode = file_inode(f.file);
 593		xrcd = find_xrcd(ibudev, inode);
 594		if (!xrcd && !(cmd.oflags & O_CREAT)) {
 595			/* no file descriptor. Need CREATE flag */
 596			ret = -EAGAIN;
 597			goto err_tree_mutex_unlock;
 598		}
 599
 600		if (xrcd && cmd.oflags & O_EXCL) {
 601			ret = -EINVAL;
 602			goto err_tree_mutex_unlock;
 603		}
 604	}
 605
 606	obj = (struct ib_uxrcd_object *)uobj_alloc(UVERBS_OBJECT_XRCD, attrs,
 607						   &ib_dev);
 608	if (IS_ERR(obj)) {
 609		ret = PTR_ERR(obj);
 610		goto err_tree_mutex_unlock;
 611	}
 612
 613	if (!xrcd) {
 614		xrcd = ib_alloc_xrcd_user(ib_dev, inode, &attrs->driver_udata);
 615		if (IS_ERR(xrcd)) {
 616			ret = PTR_ERR(xrcd);
 617			goto err;
 618		}
 619		new_xrcd = 1;
 620	}
 621
 622	atomic_set(&obj->refcnt, 0);
 623	obj->uobject.object = xrcd;
 624
 625	if (inode) {
 626		if (new_xrcd) {
 627			/* create new inode/xrcd table entry */
 628			ret = xrcd_table_insert(ibudev, inode, xrcd);
 629			if (ret)
 630				goto err_dealloc_xrcd;
 631		}
 632		atomic_inc(&xrcd->usecnt);
 633	}
 634
 635	if (f.file)
 636		fdput(f);
 637
 638	mutex_unlock(&ibudev->xrcd_tree_mutex);
 639	uobj_finalize_uobj_create(&obj->uobject, attrs);
 640
 641	resp.xrcd_handle = obj->uobject.id;
 642	return uverbs_response(attrs, &resp, sizeof(resp));
 643
 644err_dealloc_xrcd:
 645	ib_dealloc_xrcd_user(xrcd, uverbs_get_cleared_udata(attrs));
 646
 647err:
 648	uobj_alloc_abort(&obj->uobject, attrs);
 649
 650err_tree_mutex_unlock:
 651	if (f.file)
 652		fdput(f);
 653
 654	mutex_unlock(&ibudev->xrcd_tree_mutex);
 655
 656	return ret;
 657}
 658
 659static int ib_uverbs_close_xrcd(struct uverbs_attr_bundle *attrs)
 660{
 661	struct ib_uverbs_close_xrcd cmd;
 662	int ret;
 663
 664	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
 665	if (ret)
 666		return ret;
 667
 668	return uobj_perform_destroy(UVERBS_OBJECT_XRCD, cmd.xrcd_handle, attrs);
 669}
 670
 671int ib_uverbs_dealloc_xrcd(struct ib_uobject *uobject, struct ib_xrcd *xrcd,
 672			   enum rdma_remove_reason why,
 673			   struct uverbs_attr_bundle *attrs)
 674{
 675	struct inode *inode;
 676	int ret;
 677	struct ib_uverbs_device *dev = attrs->ufile->device;
 678
 679	inode = xrcd->inode;
 680	if (inode && !atomic_dec_and_test(&xrcd->usecnt))
 681		return 0;
 682
 683	ret = ib_dealloc_xrcd_user(xrcd, &attrs->driver_udata);
 684	if (ret) {
 685		atomic_inc(&xrcd->usecnt);
 686		return ret;
 687	}
 688
 689	if (inode)
 690		xrcd_table_delete(dev, inode);
 691
 692	return 0;
 693}
 694
 695static int ib_uverbs_reg_mr(struct uverbs_attr_bundle *attrs)
 
 
 696{
 697	struct ib_uverbs_reg_mr_resp resp = {};
 698	struct ib_uverbs_reg_mr      cmd;
 
 
 699	struct ib_uobject           *uobj;
 700	struct ib_pd                *pd;
 701	struct ib_mr                *mr;
 702	int                          ret;
 703	struct ib_device *ib_dev;
 704
 705	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
 706	if (ret)
 707		return ret;
 
 
 
 
 
 
 708
 709	if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
 710		return -EINVAL;
 711
 712	uobj = uobj_alloc(UVERBS_OBJECT_MR, attrs, &ib_dev);
 713	if (IS_ERR(uobj))
 714		return PTR_ERR(uobj);
 
 
 
 
 
 
 
 
 715
 716	ret = ib_check_mr_access(ib_dev, cmd.access_flags);
 717	if (ret)
 718		goto err_free;
 719
 720	pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, attrs);
 721	if (!pd) {
 722		ret = -EINVAL;
 723		goto err_free;
 724	}
 725
 726	mr = pd->device->ops.reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,
 727					 cmd.access_flags,
 728					 &attrs->driver_udata);
 729	if (IS_ERR(mr)) {
 730		ret = PTR_ERR(mr);
 731		goto err_put;
 732	}
 733
 734	mr->device  = pd->device;
 735	mr->pd      = pd;
 736	mr->type    = IB_MR_TYPE_USER;
 737	mr->dm	    = NULL;
 738	mr->sig_attrs = NULL;
 739	mr->uobject = uobj;
 740	atomic_inc(&pd->usecnt);
 741	mr->iova = cmd.hca_va;
 742
 743	rdma_restrack_new(&mr->res, RDMA_RESTRACK_MR);
 744	rdma_restrack_set_name(&mr->res, NULL);
 745	rdma_restrack_add(&mr->res);
 746
 747	uobj->object = mr;
 748	uobj_put_obj_read(pd);
 749	uobj_finalize_uobj_create(uobj, attrs);
 
 750
 751	resp.lkey = mr->lkey;
 752	resp.rkey = mr->rkey;
 
 753	resp.mr_handle = uobj->id;
 754	return uverbs_response(attrs, &resp, sizeof(resp));
 755
 756err_put:
 757	uobj_put_obj_read(pd);
 758err_free:
 759	uobj_alloc_abort(uobj, attrs);
 760	return ret;
 761}
 762
 763static int ib_uverbs_rereg_mr(struct uverbs_attr_bundle *attrs)
 764{
 765	struct ib_uverbs_rereg_mr      cmd;
 766	struct ib_uverbs_rereg_mr_resp resp;
 767	struct ib_mr                *mr;
 768	int                          ret;
 769	struct ib_uobject	    *uobj;
 770	struct ib_uobject *new_uobj;
 771	struct ib_device *ib_dev;
 772	struct ib_pd *orig_pd;
 773	struct ib_pd *new_pd;
 774	struct ib_mr *new_mr;
 775
 776	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
 777	if (ret)
 778		return ret;
 779
 780	if (!cmd.flags)
 781		return -EINVAL;
 782
 783	if (cmd.flags & ~IB_MR_REREG_SUPPORTED)
 784		return -EOPNOTSUPP;
 785
 786	if ((cmd.flags & IB_MR_REREG_TRANS) &&
 787	    (cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
 788		return -EINVAL;
 789
 790	uobj = uobj_get_write(UVERBS_OBJECT_MR, cmd.mr_handle, attrs);
 791	if (IS_ERR(uobj))
 792		return PTR_ERR(uobj);
 793
 794	mr = uobj->object;
 
 795
 796	if (mr->dm) {
 797		ret = -EINVAL;
 798		goto put_uobjs;
 799	}
 800
 801	if (cmd.flags & IB_MR_REREG_ACCESS) {
 802		ret = ib_check_mr_access(mr->device, cmd.access_flags);
 803		if (ret)
 804			goto put_uobjs;
 805	}
 806
 807	orig_pd = mr->pd;
 808	if (cmd.flags & IB_MR_REREG_PD) {
 809		new_pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle,
 810					   attrs);
 811		if (!new_pd) {
 812			ret = -EINVAL;
 813			goto put_uobjs;
 814		}
 815	} else {
 816		new_pd = mr->pd;
 817	}
 818
 819	/*
 820	 * The driver might create a new HW object as part of the rereg, we need
 821	 * to have a uobject ready to hold it.
 822	 */
 823	new_uobj = uobj_alloc(UVERBS_OBJECT_MR, attrs, &ib_dev);
 824	if (IS_ERR(new_uobj)) {
 825		ret = PTR_ERR(new_uobj);
 826		goto put_uobj_pd;
 827	}
 828
 829	new_mr = ib_dev->ops.rereg_user_mr(mr, cmd.flags, cmd.start, cmd.length,
 830					   cmd.hca_va, cmd.access_flags, new_pd,
 831					   &attrs->driver_udata);
 832	if (IS_ERR(new_mr)) {
 833		ret = PTR_ERR(new_mr);
 834		goto put_new_uobj;
 835	}
 836	if (new_mr) {
 837		new_mr->device = new_pd->device;
 838		new_mr->pd = new_pd;
 839		new_mr->type = IB_MR_TYPE_USER;
 840		new_mr->dm = NULL;
 841		new_mr->sig_attrs = NULL;
 842		new_mr->uobject = uobj;
 843		atomic_inc(&new_pd->usecnt);
 844		new_mr->iova = cmd.hca_va;
 845		new_uobj->object = new_mr;
 846
 847		rdma_restrack_new(&new_mr->res, RDMA_RESTRACK_MR);
 848		rdma_restrack_set_name(&new_mr->res, NULL);
 849		rdma_restrack_add(&new_mr->res);
 850
 851		/*
 852		 * The new uobj for the new HW object is put into the same spot
 853		 * in the IDR and the old uobj & HW object is deleted.
 854		 */
 855		rdma_assign_uobject(uobj, new_uobj, attrs);
 856		rdma_alloc_commit_uobject(new_uobj, attrs);
 857		uobj_put_destroy(uobj);
 858		new_uobj = NULL;
 859		uobj = NULL;
 860		mr = new_mr;
 861	} else {
 862		if (cmd.flags & IB_MR_REREG_PD) {
 863			atomic_dec(&orig_pd->usecnt);
 864			mr->pd = new_pd;
 865			atomic_inc(&new_pd->usecnt);
 866		}
 867		if (cmd.flags & IB_MR_REREG_TRANS)
 868			mr->iova = cmd.hca_va;
 869	}
 870
 871	memset(&resp, 0, sizeof(resp));
 872	resp.lkey      = mr->lkey;
 873	resp.rkey      = mr->rkey;
 874
 875	ret = uverbs_response(attrs, &resp, sizeof(resp));
 876
 877put_new_uobj:
 878	if (new_uobj)
 879		uobj_alloc_abort(new_uobj, attrs);
 880put_uobj_pd:
 881	if (cmd.flags & IB_MR_REREG_PD)
 882		uobj_put_obj_read(new_pd);
 883
 884put_uobjs:
 885	if (uobj)
 886		uobj_put_write(uobj);
 887
 
 
 888	return ret;
 889}
 890
 891static int ib_uverbs_dereg_mr(struct uverbs_attr_bundle *attrs)
 
 
 892{
 893	struct ib_uverbs_dereg_mr cmd;
 894	int ret;
 
 
 895
 896	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
 897	if (ret)
 898		return ret;
 899
 900	return uobj_perform_destroy(UVERBS_OBJECT_MR, cmd.mr_handle, attrs);
 901}
 
 902
 903static int ib_uverbs_alloc_mw(struct uverbs_attr_bundle *attrs)
 904{
 905	struct ib_uverbs_alloc_mw      cmd;
 906	struct ib_uverbs_alloc_mw_resp resp = {};
 907	struct ib_uobject             *uobj;
 908	struct ib_pd                  *pd;
 909	struct ib_mw                  *mw;
 910	int                            ret;
 911	struct ib_device *ib_dev;
 912
 913	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
 914	if (ret)
 915		return ret;
 916
 917	uobj = uobj_alloc(UVERBS_OBJECT_MW, attrs, &ib_dev);
 918	if (IS_ERR(uobj))
 919		return PTR_ERR(uobj);
 920
 921	pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, attrs);
 922	if (!pd) {
 923		ret = -EINVAL;
 924		goto err_free;
 925	}
 926
 927	if (cmd.mw_type != IB_MW_TYPE_1 && cmd.mw_type != IB_MW_TYPE_2) {
 928		ret = -EINVAL;
 929		goto err_put;
 930	}
 931
 932	mw = rdma_zalloc_drv_obj(ib_dev, ib_mw);
 933	if (!mw) {
 934		ret = -ENOMEM;
 935		goto err_put;
 936	}
 937
 938	mw->device = ib_dev;
 939	mw->pd = pd;
 940	mw->uobject = uobj;
 941	mw->type = cmd.mw_type;
 942
 943	ret = pd->device->ops.alloc_mw(mw, &attrs->driver_udata);
 944	if (ret)
 945		goto err_alloc;
 946
 947	atomic_inc(&pd->usecnt);
 948
 949	uobj->object = mw;
 950	uobj_put_obj_read(pd);
 951	uobj_finalize_uobj_create(uobj, attrs);
 952
 953	resp.rkey = mw->rkey;
 954	resp.mw_handle = uobj->id;
 955	return uverbs_response(attrs, &resp, sizeof(resp));
 956
 957err_alloc:
 958	kfree(mw);
 959err_put:
 960	uobj_put_obj_read(pd);
 961err_free:
 962	uobj_alloc_abort(uobj, attrs);
 963	return ret;
 964}
 965
 966static int ib_uverbs_dealloc_mw(struct uverbs_attr_bundle *attrs)
 967{
 968	struct ib_uverbs_dealloc_mw cmd;
 969	int ret;
 970
 971	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
 972	if (ret)
 973		return ret;
 974
 975	return uobj_perform_destroy(UVERBS_OBJECT_MW, cmd.mw_handle, attrs);
 976}
 977
 978static int ib_uverbs_create_comp_channel(struct uverbs_attr_bundle *attrs)
 
 
 979{
 980	struct ib_uverbs_create_comp_channel	   cmd;
 981	struct ib_uverbs_create_comp_channel_resp  resp;
 982	struct ib_uobject			  *uobj;
 983	struct ib_uverbs_completion_event_file	  *ev_file;
 984	struct ib_device *ib_dev;
 985	int ret;
 986
 987	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
 988	if (ret)
 
 
 
 
 
 
 989		return ret;
 
 990
 991	uobj = uobj_alloc(UVERBS_OBJECT_COMP_CHANNEL, attrs, &ib_dev);
 992	if (IS_ERR(uobj))
 993		return PTR_ERR(uobj);
 
 
 994
 995	ev_file = container_of(uobj, struct ib_uverbs_completion_event_file,
 996			       uobj);
 997	ib_uverbs_init_event_queue(&ev_file->ev_queue);
 998	uobj_finalize_uobj_create(uobj, attrs);
 
 
 999
1000	resp.fd = uobj->id;
1001	return uverbs_response(attrs, &resp, sizeof(resp));
1002}
1003
1004static int create_cq(struct uverbs_attr_bundle *attrs,
1005		     struct ib_uverbs_ex_create_cq *cmd)
 
1006{
 
 
 
1007	struct ib_ucq_object           *obj;
1008	struct ib_uverbs_completion_event_file    *ev_file = NULL;
1009	struct ib_cq                   *cq;
1010	int                             ret;
1011	struct ib_uverbs_ex_create_cq_resp resp = {};
1012	struct ib_cq_init_attr attr = {};
1013	struct ib_device *ib_dev;
1014
1015	if (cmd->comp_vector >= attrs->ufile->device->num_comp_vectors)
 
 
 
 
 
 
 
 
 
 
1016		return -EINVAL;
1017
1018	obj = (struct ib_ucq_object *)uobj_alloc(UVERBS_OBJECT_CQ, attrs,
1019						 &ib_dev);
1020	if (IS_ERR(obj))
1021		return PTR_ERR(obj);
1022
1023	if (cmd->comp_channel >= 0) {
1024		ev_file = ib_uverbs_lookup_comp_file(cmd->comp_channel, attrs);
1025		if (IS_ERR(ev_file)) {
1026			ret = PTR_ERR(ev_file);
 
 
1027			goto err;
1028		}
1029	}
1030
1031	obj->uevent.uobject.user_handle = cmd->user_handle;
 
 
1032	INIT_LIST_HEAD(&obj->comp_list);
1033	INIT_LIST_HEAD(&obj->uevent.event_list);
1034
1035	attr.cqe = cmd->cqe;
1036	attr.comp_vector = cmd->comp_vector;
1037	attr.flags = cmd->flags;
1038
1039	cq = rdma_zalloc_drv_obj(ib_dev, ib_cq);
1040	if (!cq) {
1041		ret = -ENOMEM;
 
 
1042		goto err_file;
1043	}
1044	cq->device        = ib_dev;
1045	cq->uobject       = obj;
 
1046	cq->comp_handler  = ib_uverbs_comp_handler;
1047	cq->event_handler = ib_uverbs_cq_event_handler;
1048	cq->cq_context    = ev_file ? &ev_file->ev_queue : NULL;
1049	atomic_set(&cq->usecnt, 0);
1050
1051	rdma_restrack_new(&cq->res, RDMA_RESTRACK_CQ);
1052	rdma_restrack_set_name(&cq->res, NULL);
1053
1054	ret = ib_dev->ops.create_cq(cq, &attr, &attrs->driver_udata);
1055	if (ret)
1056		goto err_free;
1057	rdma_restrack_add(&cq->res);
1058
1059	obj->uevent.uobject.object = cq;
1060	obj->uevent.event_file = READ_ONCE(attrs->ufile->default_async_file);
1061	if (obj->uevent.event_file)
1062		uverbs_uobject_get(&obj->uevent.event_file->uobj);
1063	uobj_finalize_uobj_create(&obj->uevent.uobject, attrs);
1064
1065	resp.base.cq_handle = obj->uevent.uobject.id;
1066	resp.base.cqe = cq->cqe;
1067	resp.response_length = uverbs_response_length(attrs, sizeof(resp));
1068	return uverbs_response(attrs, &resp, sizeof(resp));
1069
1070err_free:
1071	rdma_restrack_put(&cq->res);
1072	kfree(cq);
1073err_file:
1074	if (ev_file)
1075		ib_uverbs_release_ucq(ev_file, obj);
1076err:
1077	uobj_alloc_abort(&obj->uevent.uobject, attrs);
1078	return ret;
1079}
1080
1081static int ib_uverbs_create_cq(struct uverbs_attr_bundle *attrs)
1082{
1083	struct ib_uverbs_create_cq      cmd;
1084	struct ib_uverbs_ex_create_cq	cmd_ex;
1085	int ret;
1086
1087	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1088	if (ret)
1089		return ret;
1090
1091	memset(&cmd_ex, 0, sizeof(cmd_ex));
1092	cmd_ex.user_handle = cmd.user_handle;
1093	cmd_ex.cqe = cmd.cqe;
1094	cmd_ex.comp_vector = cmd.comp_vector;
1095	cmd_ex.comp_channel = cmd.comp_channel;
1096
1097	return create_cq(attrs, &cmd_ex);
1098}
1099
1100static int ib_uverbs_ex_create_cq(struct uverbs_attr_bundle *attrs)
1101{
1102	struct ib_uverbs_ex_create_cq  cmd;
1103	int ret;
1104
1105	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1106	if (ret)
1107		return ret;
1108
1109	if (cmd.comp_mask)
1110		return -EINVAL;
 
1111
1112	if (cmd.reserved)
1113		return -EINVAL;
1114
1115	return create_cq(attrs, &cmd);
1116}
1117
1118static int ib_uverbs_resize_cq(struct uverbs_attr_bundle *attrs)
 
 
1119{
1120	struct ib_uverbs_resize_cq	cmd;
1121	struct ib_uverbs_resize_cq_resp	resp = {};
 
1122	struct ib_cq			*cq;
1123	int ret;
 
 
 
1124
1125	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1126	if (ret)
1127		return ret;
1128
1129	cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
1130	if (!cq)
1131		return -EINVAL;
1132
1133	ret = cq->device->ops.resize_cq(cq, cmd.cqe, &attrs->driver_udata);
1134	if (ret)
1135		goto out;
1136
1137	resp.cqe = cq->cqe;
1138
1139	ret = uverbs_response(attrs, &resp, sizeof(resp));
 
 
 
1140out:
1141	rdma_lookup_put_uobject(&cq->uobject->uevent.uobject,
1142				UVERBS_LOOKUP_READ);
1143
1144	return ret;
1145}
1146
1147static int copy_wc_to_user(struct ib_device *ib_dev, void __user *dest,
1148			   struct ib_wc *wc)
1149{
1150	struct ib_uverbs_wc tmp;
1151
1152	tmp.wr_id		= wc->wr_id;
1153	tmp.status		= wc->status;
1154	tmp.opcode		= wc->opcode;
1155	tmp.vendor_err		= wc->vendor_err;
1156	tmp.byte_len		= wc->byte_len;
1157	tmp.ex.imm_data		= wc->ex.imm_data;
1158	tmp.qp_num		= wc->qp->qp_num;
1159	tmp.src_qp		= wc->src_qp;
1160	tmp.wc_flags		= wc->wc_flags;
1161	tmp.pkey_index		= wc->pkey_index;
1162	if (rdma_cap_opa_ah(ib_dev, wc->port_num))
1163		tmp.slid	= OPA_TO_IB_UCAST_LID(wc->slid);
1164	else
1165		tmp.slid	= ib_lid_cpu16(wc->slid);
1166	tmp.sl			= wc->sl;
1167	tmp.dlid_path_bits	= wc->dlid_path_bits;
1168	tmp.port_num		= wc->port_num;
1169	tmp.reserved		= 0;
1170
1171	if (copy_to_user(dest, &tmp, sizeof tmp))
1172		return -EFAULT;
1173
1174	return 0;
1175}
1176
1177static int ib_uverbs_poll_cq(struct uverbs_attr_bundle *attrs)
 
 
1178{
1179	struct ib_uverbs_poll_cq       cmd;
1180	struct ib_uverbs_poll_cq_resp  resp;
1181	u8 __user                     *header_ptr;
1182	u8 __user                     *data_ptr;
1183	struct ib_cq                  *cq;
1184	struct ib_wc                   wc;
1185	int                            ret;
1186
1187	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1188	if (ret)
1189		return ret;
1190
1191	cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
1192	if (!cq)
1193		return -EINVAL;
1194
1195	/* we copy a struct ib_uverbs_poll_cq_resp to user space */
1196	header_ptr = attrs->ucore.outbuf;
1197	data_ptr = header_ptr + sizeof resp;
1198
1199	memset(&resp, 0, sizeof resp);
1200	while (resp.count < cmd.ne) {
1201		ret = ib_poll_cq(cq, 1, &wc);
1202		if (ret < 0)
1203			goto out_put;
1204		if (!ret)
1205			break;
1206
1207		ret = copy_wc_to_user(cq->device, data_ptr, &wc);
1208		if (ret)
1209			goto out_put;
1210
1211		data_ptr += sizeof(struct ib_uverbs_wc);
1212		++resp.count;
1213	}
1214
1215	if (copy_to_user(header_ptr, &resp, sizeof resp)) {
1216		ret = -EFAULT;
1217		goto out_put;
1218	}
1219	ret = 0;
1220
1221	if (uverbs_attr_is_valid(attrs, UVERBS_ATTR_CORE_OUT))
1222		ret = uverbs_output_written(attrs, UVERBS_ATTR_CORE_OUT);
1223
1224out_put:
1225	rdma_lookup_put_uobject(&cq->uobject->uevent.uobject,
1226				UVERBS_LOOKUP_READ);
1227	return ret;
1228}
1229
1230static int ib_uverbs_req_notify_cq(struct uverbs_attr_bundle *attrs)
 
 
1231{
1232	struct ib_uverbs_req_notify_cq cmd;
1233	struct ib_cq                  *cq;
1234	int ret;
1235
1236	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1237	if (ret)
1238		return ret;
1239
1240	cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
1241	if (!cq)
1242		return -EINVAL;
1243
1244	ib_req_notify_cq(cq, cmd.solicited_only ?
1245			 IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
1246
1247	rdma_lookup_put_uobject(&cq->uobject->uevent.uobject,
1248				UVERBS_LOOKUP_READ);
1249	return 0;
1250}
1251
1252static int ib_uverbs_destroy_cq(struct uverbs_attr_bundle *attrs)
 
 
1253{
1254	struct ib_uverbs_destroy_cq      cmd;
1255	struct ib_uverbs_destroy_cq_resp resp;
1256	struct ib_uobject		*uobj;
 
1257	struct ib_ucq_object        	*obj;
1258	int ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1259
1260	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1261	if (ret)
1262		return ret;
1263
1264	uobj = uobj_get_destroy(UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
1265	if (IS_ERR(uobj))
1266		return PTR_ERR(uobj);
1267
1268	obj = container_of(uobj, struct ib_ucq_object, uevent.uobject);
1269	memset(&resp, 0, sizeof(resp));
1270	resp.comp_events_reported  = obj->comp_events_reported;
1271	resp.async_events_reported = obj->uevent.events_reported;
1272
1273	uobj_put_destroy(uobj);
1274
1275	return uverbs_response(attrs, &resp, sizeof(resp));
1276}
 
1277
1278static int create_qp(struct uverbs_attr_bundle *attrs,
1279		     struct ib_uverbs_ex_create_qp *cmd)
1280{
1281	struct ib_uqp_object		*obj;
1282	struct ib_device		*device;
1283	struct ib_pd			*pd = NULL;
1284	struct ib_xrcd			*xrcd = NULL;
1285	struct ib_uobject		*xrcd_uobj = ERR_PTR(-ENOENT);
1286	struct ib_cq			*scq = NULL, *rcq = NULL;
1287	struct ib_srq			*srq = NULL;
1288	struct ib_qp			*qp;
1289	struct ib_qp_init_attr		attr = {};
1290	struct ib_uverbs_ex_create_qp_resp resp = {};
1291	int				ret;
1292	struct ib_rwq_ind_table *ind_tbl = NULL;
1293	bool has_sq = true;
1294	struct ib_device *ib_dev;
1295
1296	switch (cmd->qp_type) {
1297	case IB_QPT_RAW_PACKET:
1298		if (!capable(CAP_NET_RAW))
1299			return -EPERM;
1300		break;
1301	case IB_QPT_RC:
1302	case IB_QPT_UC:
1303	case IB_QPT_UD:
1304	case IB_QPT_XRC_INI:
1305	case IB_QPT_XRC_TGT:
1306	case IB_QPT_DRIVER:
1307		break;
1308	default:
1309		return -EINVAL;
1310	}
1311
1312	obj = (struct ib_uqp_object *)uobj_alloc(UVERBS_OBJECT_QP, attrs,
1313						 &ib_dev);
1314	if (IS_ERR(obj))
1315		return PTR_ERR(obj);
1316	obj->uxrcd = NULL;
1317	obj->uevent.uobject.user_handle = cmd->user_handle;
1318	mutex_init(&obj->mcast_lock);
1319
1320	if (cmd->comp_mask & IB_UVERBS_CREATE_QP_MASK_IND_TABLE) {
1321		ind_tbl = uobj_get_obj_read(rwq_ind_table,
1322					    UVERBS_OBJECT_RWQ_IND_TBL,
1323					    cmd->rwq_ind_tbl_handle, attrs);
1324		if (!ind_tbl) {
1325			ret = -EINVAL;
1326			goto err_put;
1327		}
1328
1329		attr.rwq_ind_tbl = ind_tbl;
1330	}
1331
1332	if (ind_tbl && (cmd->max_recv_wr || cmd->max_recv_sge || cmd->is_srq)) {
1333		ret = -EINVAL;
1334		goto err_put;
1335	}
 
 
 
 
 
 
 
 
 
 
1336
1337	if (ind_tbl && !cmd->max_send_wr)
1338		has_sq = false;
1339
1340	if (cmd->qp_type == IB_QPT_XRC_TGT) {
1341		xrcd_uobj = uobj_get_read(UVERBS_OBJECT_XRCD, cmd->pd_handle,
1342					  attrs);
1343
1344		if (IS_ERR(xrcd_uobj)) {
1345			ret = -EINVAL;
1346			goto err_put;
1347		}
1348
1349		xrcd = (struct ib_xrcd *)xrcd_uobj->object;
1350		if (!xrcd) {
1351			ret = -EINVAL;
1352			goto err_put;
1353		}
1354		device = xrcd->device;
1355	} else {
1356		if (cmd->qp_type == IB_QPT_XRC_INI) {
1357			cmd->max_recv_wr = 0;
1358			cmd->max_recv_sge = 0;
1359		} else {
1360			if (cmd->is_srq) {
1361				srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ,
1362							cmd->srq_handle, attrs);
1363				if (!srq || srq->srq_type == IB_SRQT_XRC) {
1364					ret = -EINVAL;
1365					goto err_put;
1366				}
1367			}
1368
1369			if (!ind_tbl) {
1370				if (cmd->recv_cq_handle != cmd->send_cq_handle) {
1371					rcq = uobj_get_obj_read(
1372						cq, UVERBS_OBJECT_CQ,
1373						cmd->recv_cq_handle, attrs);
1374					if (!rcq) {
1375						ret = -EINVAL;
1376						goto err_put;
1377					}
1378				}
1379			}
1380		}
1381
1382		if (has_sq)
1383			scq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ,
1384						cmd->send_cq_handle, attrs);
1385		if (!ind_tbl && cmd->qp_type != IB_QPT_XRC_INI)
1386			rcq = rcq ?: scq;
1387		pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd->pd_handle,
1388				       attrs);
1389		if (!pd || (!scq && has_sq)) {
1390			ret = -EINVAL;
1391			goto err_put;
1392		}
1393
1394		device = pd->device;
 
 
1395	}
1396
1397	attr.event_handler = ib_uverbs_qp_event_handler;
 
1398	attr.send_cq       = scq;
1399	attr.recv_cq       = rcq;
1400	attr.srq           = srq;
1401	attr.xrcd	   = xrcd;
1402	attr.sq_sig_type   = cmd->sq_sig_all ? IB_SIGNAL_ALL_WR :
1403					      IB_SIGNAL_REQ_WR;
1404	attr.qp_type       = cmd->qp_type;
1405	attr.create_flags  = 0;
1406
1407	attr.cap.max_send_wr     = cmd->max_send_wr;
1408	attr.cap.max_recv_wr     = cmd->max_recv_wr;
1409	attr.cap.max_send_sge    = cmd->max_send_sge;
1410	attr.cap.max_recv_sge    = cmd->max_recv_sge;
1411	attr.cap.max_inline_data = cmd->max_inline_data;
1412
 
1413	INIT_LIST_HEAD(&obj->uevent.event_list);
1414	INIT_LIST_HEAD(&obj->mcast_list);
1415
1416	attr.create_flags = cmd->create_flags;
1417	if (attr.create_flags & ~(IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
1418				IB_QP_CREATE_CROSS_CHANNEL |
1419				IB_QP_CREATE_MANAGED_SEND |
1420				IB_QP_CREATE_MANAGED_RECV |
1421				IB_QP_CREATE_SCATTER_FCS |
1422				IB_QP_CREATE_CVLAN_STRIPPING |
1423				IB_QP_CREATE_SOURCE_QPN |
1424				IB_QP_CREATE_PCI_WRITE_END_PADDING)) {
1425		ret = -EINVAL;
1426		goto err_put;
1427	}
1428
1429	if (attr.create_flags & IB_QP_CREATE_SOURCE_QPN) {
1430		if (!capable(CAP_NET_RAW)) {
1431			ret = -EPERM;
1432			goto err_put;
1433		}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1434
1435		attr.source_qpn = cmd->source_qpn;
 
 
 
1436	}
1437
1438	if (cmd->qp_type == IB_QPT_XRC_TGT)
1439		qp = ib_create_qp(pd, &attr);
1440	else
1441		qp = _ib_create_qp(device, pd, &attr, &attrs->driver_udata, obj,
1442				   NULL);
 
1443
1444	if (IS_ERR(qp)) {
1445		ret = PTR_ERR(qp);
1446		goto err_put;
1447	}
1448
1449	if (cmd->qp_type != IB_QPT_XRC_TGT) {
1450		ret = ib_create_qp_security(qp, device);
1451		if (ret)
1452			goto err_cb;
1453
1454		atomic_inc(&pd->usecnt);
1455		if (attr.send_cq)
1456			atomic_inc(&attr.send_cq->usecnt);
1457		if (attr.recv_cq)
1458			atomic_inc(&attr.recv_cq->usecnt);
1459		if (attr.srq)
1460			atomic_inc(&attr.srq->usecnt);
1461		if (ind_tbl)
1462			atomic_inc(&ind_tbl->usecnt);
1463	} else {
1464		/* It is done in _ib_create_qp for other QP types */
1465		qp->uobject = obj;
1466	}
1467
1468	obj->uevent.uobject.object = qp;
1469	obj->uevent.event_file = READ_ONCE(attrs->ufile->default_async_file);
1470	if (obj->uevent.event_file)
1471		uverbs_uobject_get(&obj->uevent.event_file->uobj);
1472
1473	if (xrcd) {
1474		obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object,
1475					  uobject);
1476		atomic_inc(&obj->uxrcd->refcnt);
1477		uobj_put_read(xrcd_uobj);
1478	}
1479
1480	if (pd)
1481		uobj_put_obj_read(pd);
1482	if (scq)
1483		rdma_lookup_put_uobject(&scq->uobject->uevent.uobject,
1484					UVERBS_LOOKUP_READ);
1485	if (rcq && rcq != scq)
1486		rdma_lookup_put_uobject(&rcq->uobject->uevent.uobject,
1487					UVERBS_LOOKUP_READ);
1488	if (srq)
1489		rdma_lookup_put_uobject(&srq->uobject->uevent.uobject,
1490					UVERBS_LOOKUP_READ);
1491	if (ind_tbl)
1492		uobj_put_obj_read(ind_tbl);
1493	uobj_finalize_uobj_create(&obj->uevent.uobject, attrs);
1494
1495	resp.base.qpn             = qp->qp_num;
1496	resp.base.qp_handle       = obj->uevent.uobject.id;
1497	resp.base.max_recv_sge    = attr.cap.max_recv_sge;
1498	resp.base.max_send_sge    = attr.cap.max_send_sge;
1499	resp.base.max_recv_wr     = attr.cap.max_recv_wr;
1500	resp.base.max_send_wr     = attr.cap.max_send_wr;
1501	resp.base.max_inline_data = attr.cap.max_inline_data;
1502	resp.response_length = uverbs_response_length(attrs, sizeof(resp));
1503	return uverbs_response(attrs, &resp, sizeof(resp));
1504
1505err_cb:
1506	ib_destroy_qp_user(qp, uverbs_get_cleared_udata(attrs));
1507
1508err_put:
1509	if (!IS_ERR(xrcd_uobj))
1510		uobj_put_read(xrcd_uobj);
1511	if (pd)
1512		uobj_put_obj_read(pd);
1513	if (scq)
1514		rdma_lookup_put_uobject(&scq->uobject->uevent.uobject,
1515					UVERBS_LOOKUP_READ);
1516	if (rcq && rcq != scq)
1517		rdma_lookup_put_uobject(&rcq->uobject->uevent.uobject,
1518					UVERBS_LOOKUP_READ);
1519	if (srq)
1520		rdma_lookup_put_uobject(&srq->uobject->uevent.uobject,
1521					UVERBS_LOOKUP_READ);
1522	if (ind_tbl)
1523		uobj_put_obj_read(ind_tbl);
1524
1525	uobj_alloc_abort(&obj->uevent.uobject, attrs);
1526	return ret;
1527}
1528
1529static int ib_uverbs_create_qp(struct uverbs_attr_bundle *attrs)
1530{
1531	struct ib_uverbs_create_qp      cmd;
1532	struct ib_uverbs_ex_create_qp	cmd_ex;
1533	int ret;
1534
1535	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1536	if (ret)
1537		return ret;
1538
1539	memset(&cmd_ex, 0, sizeof(cmd_ex));
1540	cmd_ex.user_handle = cmd.user_handle;
1541	cmd_ex.pd_handle = cmd.pd_handle;
1542	cmd_ex.send_cq_handle = cmd.send_cq_handle;
1543	cmd_ex.recv_cq_handle = cmd.recv_cq_handle;
1544	cmd_ex.srq_handle = cmd.srq_handle;
1545	cmd_ex.max_send_wr = cmd.max_send_wr;
1546	cmd_ex.max_recv_wr = cmd.max_recv_wr;
1547	cmd_ex.max_send_sge = cmd.max_send_sge;
1548	cmd_ex.max_recv_sge = cmd.max_recv_sge;
1549	cmd_ex.max_inline_data = cmd.max_inline_data;
1550	cmd_ex.sq_sig_all = cmd.sq_sig_all;
1551	cmd_ex.qp_type = cmd.qp_type;
1552	cmd_ex.is_srq = cmd.is_srq;
1553
1554	return create_qp(attrs, &cmd_ex);
1555}
1556
1557static int ib_uverbs_ex_create_qp(struct uverbs_attr_bundle *attrs)
1558{
1559	struct ib_uverbs_ex_create_qp cmd;
1560	int ret;
1561
1562	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1563	if (ret)
1564		return ret;
1565
1566	if (cmd.comp_mask & ~IB_UVERBS_CREATE_QP_SUP_COMP_MASK)
1567		return -EINVAL;
1568
1569	if (cmd.reserved)
1570		return -EINVAL;
1571
1572	return create_qp(attrs, &cmd);
1573}
1574
1575static int ib_uverbs_open_qp(struct uverbs_attr_bundle *attrs)
1576{
1577	struct ib_uverbs_create_qp_resp resp = {};
1578	struct ib_uverbs_open_qp        cmd;
1579	struct ib_uqp_object           *obj;
1580	struct ib_xrcd		       *xrcd;
1581	struct ib_qp                   *qp;
1582	struct ib_qp_open_attr          attr = {};
1583	int ret;
1584	struct ib_uobject *xrcd_uobj;
1585	struct ib_device *ib_dev;
1586
1587	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1588	if (ret)
1589		return ret;
1590
1591	obj = (struct ib_uqp_object *)uobj_alloc(UVERBS_OBJECT_QP, attrs,
1592						 &ib_dev);
1593	if (IS_ERR(obj))
1594		return PTR_ERR(obj);
1595
1596	xrcd_uobj = uobj_get_read(UVERBS_OBJECT_XRCD, cmd.pd_handle, attrs);
1597	if (IS_ERR(xrcd_uobj)) {
1598		ret = -EINVAL;
1599		goto err_put;
1600	}
1601
1602	xrcd = (struct ib_xrcd *)xrcd_uobj->object;
1603	if (!xrcd) {
1604		ret = -EINVAL;
1605		goto err_xrcd;
1606	}
1607
1608	attr.event_handler = ib_uverbs_qp_event_handler;
1609	attr.qp_num        = cmd.qpn;
1610	attr.qp_type       = cmd.qp_type;
1611
1612	INIT_LIST_HEAD(&obj->uevent.event_list);
1613	INIT_LIST_HEAD(&obj->mcast_list);
1614
1615	qp = ib_open_qp(xrcd, &attr);
1616	if (IS_ERR(qp)) {
1617		ret = PTR_ERR(qp);
1618		goto err_xrcd;
1619	}
1620
1621	obj->uevent.uobject.object = qp;
1622	obj->uevent.uobject.user_handle = cmd.user_handle;
1623
1624	obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
1625	atomic_inc(&obj->uxrcd->refcnt);
1626	qp->uobject = obj;
1627	uobj_put_read(xrcd_uobj);
1628	uobj_finalize_uobj_create(&obj->uevent.uobject, attrs);
1629
1630	resp.qpn = qp->qp_num;
1631	resp.qp_handle = obj->uevent.uobject.id;
1632	return uverbs_response(attrs, &resp, sizeof(resp));
1633
1634err_xrcd:
1635	uobj_put_read(xrcd_uobj);
1636err_put:
1637	uobj_alloc_abort(&obj->uevent.uobject, attrs);
1638	return ret;
1639}
1640
1641static void copy_ah_attr_to_uverbs(struct ib_uverbs_qp_dest *uverb_attr,
1642				   struct rdma_ah_attr *rdma_attr)
1643{
1644	const struct ib_global_route   *grh;
1645
1646	uverb_attr->dlid              = rdma_ah_get_dlid(rdma_attr);
1647	uverb_attr->sl                = rdma_ah_get_sl(rdma_attr);
1648	uverb_attr->src_path_bits     = rdma_ah_get_path_bits(rdma_attr);
1649	uverb_attr->static_rate       = rdma_ah_get_static_rate(rdma_attr);
1650	uverb_attr->is_global         = !!(rdma_ah_get_ah_flags(rdma_attr) &
1651					 IB_AH_GRH);
1652	if (uverb_attr->is_global) {
1653		grh = rdma_ah_read_grh(rdma_attr);
1654		memcpy(uverb_attr->dgid, grh->dgid.raw, 16);
1655		uverb_attr->flow_label        = grh->flow_label;
1656		uverb_attr->sgid_index        = grh->sgid_index;
1657		uverb_attr->hop_limit         = grh->hop_limit;
1658		uverb_attr->traffic_class     = grh->traffic_class;
1659	}
1660	uverb_attr->port_num          = rdma_ah_get_port_num(rdma_attr);
1661}
1662
1663static int ib_uverbs_query_qp(struct uverbs_attr_bundle *attrs)
1664{
1665	struct ib_uverbs_query_qp      cmd;
1666	struct ib_uverbs_query_qp_resp resp;
1667	struct ib_qp                   *qp;
1668	struct ib_qp_attr              *attr;
1669	struct ib_qp_init_attr         *init_attr;
1670	int                            ret;
1671
1672	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1673	if (ret)
1674		return ret;
1675
1676	attr      = kmalloc(sizeof *attr, GFP_KERNEL);
1677	init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
1678	if (!attr || !init_attr) {
1679		ret = -ENOMEM;
1680		goto out;
1681	}
1682
1683	qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
1684	if (!qp) {
1685		ret = -EINVAL;
1686		goto out;
1687	}
1688
1689	ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
1690
1691	rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
1692				UVERBS_LOOKUP_READ);
1693
1694	if (ret)
1695		goto out;
1696
1697	memset(&resp, 0, sizeof resp);
1698
1699	resp.qp_state               = attr->qp_state;
1700	resp.cur_qp_state           = attr->cur_qp_state;
1701	resp.path_mtu               = attr->path_mtu;
1702	resp.path_mig_state         = attr->path_mig_state;
1703	resp.qkey                   = attr->qkey;
1704	resp.rq_psn                 = attr->rq_psn;
1705	resp.sq_psn                 = attr->sq_psn;
1706	resp.dest_qp_num            = attr->dest_qp_num;
1707	resp.qp_access_flags        = attr->qp_access_flags;
1708	resp.pkey_index             = attr->pkey_index;
1709	resp.alt_pkey_index         = attr->alt_pkey_index;
1710	resp.sq_draining            = attr->sq_draining;
1711	resp.max_rd_atomic          = attr->max_rd_atomic;
1712	resp.max_dest_rd_atomic     = attr->max_dest_rd_atomic;
1713	resp.min_rnr_timer          = attr->min_rnr_timer;
1714	resp.port_num               = attr->port_num;
1715	resp.timeout                = attr->timeout;
1716	resp.retry_cnt              = attr->retry_cnt;
1717	resp.rnr_retry              = attr->rnr_retry;
1718	resp.alt_port_num           = attr->alt_port_num;
1719	resp.alt_timeout            = attr->alt_timeout;
1720
1721	copy_ah_attr_to_uverbs(&resp.dest, &attr->ah_attr);
1722	copy_ah_attr_to_uverbs(&resp.alt_dest, &attr->alt_ah_attr);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1723
1724	resp.max_send_wr            = init_attr->cap.max_send_wr;
1725	resp.max_recv_wr            = init_attr->cap.max_recv_wr;
1726	resp.max_send_sge           = init_attr->cap.max_send_sge;
1727	resp.max_recv_sge           = init_attr->cap.max_recv_sge;
1728	resp.max_inline_data        = init_attr->cap.max_inline_data;
1729	resp.sq_sig_all             = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
1730
1731	ret = uverbs_response(attrs, &resp, sizeof(resp));
 
 
1732
1733out:
1734	kfree(attr);
1735	kfree(init_attr);
1736
1737	return ret;
1738}
1739
1740/* Remove ignored fields set in the attribute mask */
1741static int modify_qp_mask(enum ib_qp_type qp_type, int mask)
 
1742{
1743	switch (qp_type) {
1744	case IB_QPT_XRC_INI:
1745		return mask & ~(IB_QP_MAX_DEST_RD_ATOMIC | IB_QP_MIN_RNR_TIMER);
1746	case IB_QPT_XRC_TGT:
1747		return mask & ~(IB_QP_MAX_QP_RD_ATOMIC | IB_QP_RETRY_CNT |
1748				IB_QP_RNR_RETRY);
1749	default:
1750		return mask;
1751	}
1752}
1753
1754static void copy_ah_attr_from_uverbs(struct ib_device *dev,
1755				     struct rdma_ah_attr *rdma_attr,
1756				     struct ib_uverbs_qp_dest *uverb_attr)
1757{
1758	rdma_attr->type = rdma_ah_find_type(dev, uverb_attr->port_num);
1759	if (uverb_attr->is_global) {
1760		rdma_ah_set_grh(rdma_attr, NULL,
1761				uverb_attr->flow_label,
1762				uverb_attr->sgid_index,
1763				uverb_attr->hop_limit,
1764				uverb_attr->traffic_class);
1765		rdma_ah_set_dgid_raw(rdma_attr, uverb_attr->dgid);
1766	} else {
1767		rdma_ah_set_ah_flags(rdma_attr, 0);
1768	}
1769	rdma_ah_set_dlid(rdma_attr, uverb_attr->dlid);
1770	rdma_ah_set_sl(rdma_attr, uverb_attr->sl);
1771	rdma_ah_set_path_bits(rdma_attr, uverb_attr->src_path_bits);
1772	rdma_ah_set_static_rate(rdma_attr, uverb_attr->static_rate);
1773	rdma_ah_set_port_num(rdma_attr, uverb_attr->port_num);
1774	rdma_ah_set_make_grd(rdma_attr, false);
1775}
1776
1777static int modify_qp(struct uverbs_attr_bundle *attrs,
1778		     struct ib_uverbs_ex_modify_qp *cmd)
1779{
1780	struct ib_qp_attr *attr;
1781	struct ib_qp *qp;
1782	int ret;
1783
1784	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1785	if (!attr)
1786		return -ENOMEM;
1787
1788	qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd->base.qp_handle,
1789			       attrs);
1790	if (!qp) {
1791		ret = -EINVAL;
1792		goto out;
1793	}
1794
1795	if ((cmd->base.attr_mask & IB_QP_PORT) &&
1796	    !rdma_is_port_valid(qp->device, cmd->base.port_num)) {
1797		ret = -EINVAL;
1798		goto release_qp;
1799	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1800
1801	if ((cmd->base.attr_mask & IB_QP_AV)) {
1802		if (!rdma_is_port_valid(qp->device, cmd->base.dest.port_num)) {
1803			ret = -EINVAL;
1804			goto release_qp;
1805		}
1806
1807		if (cmd->base.attr_mask & IB_QP_STATE &&
1808		    cmd->base.qp_state == IB_QPS_RTR) {
1809		/* We are in INIT->RTR TRANSITION (if we are not,
1810		 * this transition will be rejected in subsequent checks).
1811		 * In the INIT->RTR transition, we cannot have IB_QP_PORT set,
1812		 * but the IB_QP_STATE flag is required.
1813		 *
1814		 * Since kernel 3.14 (commit dbf727de7440), the uverbs driver,
1815		 * when IB_QP_AV is set, has required inclusion of a valid
1816		 * port number in the primary AV. (AVs are created and handled
1817		 * differently for infiniband and ethernet (RoCE) ports).
1818		 *
1819		 * Check the port number included in the primary AV against
1820		 * the port number in the qp struct, which was set (and saved)
1821		 * in the RST->INIT transition.
1822		 */
1823			if (cmd->base.dest.port_num != qp->real_qp->port) {
1824				ret = -EINVAL;
1825				goto release_qp;
1826			}
1827		} else {
1828		/* We are in SQD->SQD. (If we are not, this transition will
1829		 * be rejected later in the verbs layer checks).
1830		 * Check for both IB_QP_PORT and IB_QP_AV, these can be set
1831		 * together in the SQD->SQD transition.
1832		 *
1833		 * If only IP_QP_AV was set, add in IB_QP_PORT as well (the
1834		 * verbs layer driver does not track primary port changes
1835		 * resulting from path migration. Thus, in SQD, if the primary
1836		 * AV is modified, the primary port should also be modified).
1837		 *
1838		 * Note that in this transition, the IB_QP_STATE flag
1839		 * is not allowed.
1840		 */
1841			if (((cmd->base.attr_mask & (IB_QP_AV | IB_QP_PORT))
1842			     == (IB_QP_AV | IB_QP_PORT)) &&
1843			    cmd->base.port_num != cmd->base.dest.port_num) {
1844				ret = -EINVAL;
1845				goto release_qp;
1846			}
1847			if ((cmd->base.attr_mask & (IB_QP_AV | IB_QP_PORT))
1848			    == IB_QP_AV) {
1849				cmd->base.attr_mask |= IB_QP_PORT;
1850				cmd->base.port_num = cmd->base.dest.port_num;
1851			}
1852		}
1853	}
1854
1855	if ((cmd->base.attr_mask & IB_QP_ALT_PATH) &&
1856	    (!rdma_is_port_valid(qp->device, cmd->base.alt_port_num) ||
1857	    !rdma_is_port_valid(qp->device, cmd->base.alt_dest.port_num) ||
1858	    cmd->base.alt_port_num != cmd->base.alt_dest.port_num)) {
1859		ret = -EINVAL;
1860		goto release_qp;
1861	}
1862
1863	if ((cmd->base.attr_mask & IB_QP_CUR_STATE &&
1864	    cmd->base.cur_qp_state > IB_QPS_ERR) ||
1865	    (cmd->base.attr_mask & IB_QP_STATE &&
1866	    cmd->base.qp_state > IB_QPS_ERR)) {
1867		ret = -EINVAL;
1868		goto release_qp;
1869	}
1870
1871	if (cmd->base.attr_mask & IB_QP_STATE)
1872		attr->qp_state = cmd->base.qp_state;
1873	if (cmd->base.attr_mask & IB_QP_CUR_STATE)
1874		attr->cur_qp_state = cmd->base.cur_qp_state;
1875	if (cmd->base.attr_mask & IB_QP_PATH_MTU)
1876		attr->path_mtu = cmd->base.path_mtu;
1877	if (cmd->base.attr_mask & IB_QP_PATH_MIG_STATE)
1878		attr->path_mig_state = cmd->base.path_mig_state;
1879	if (cmd->base.attr_mask & IB_QP_QKEY)
1880		attr->qkey = cmd->base.qkey;
1881	if (cmd->base.attr_mask & IB_QP_RQ_PSN)
1882		attr->rq_psn = cmd->base.rq_psn;
1883	if (cmd->base.attr_mask & IB_QP_SQ_PSN)
1884		attr->sq_psn = cmd->base.sq_psn;
1885	if (cmd->base.attr_mask & IB_QP_DEST_QPN)
1886		attr->dest_qp_num = cmd->base.dest_qp_num;
1887	if (cmd->base.attr_mask & IB_QP_ACCESS_FLAGS)
1888		attr->qp_access_flags = cmd->base.qp_access_flags;
1889	if (cmd->base.attr_mask & IB_QP_PKEY_INDEX)
1890		attr->pkey_index = cmd->base.pkey_index;
1891	if (cmd->base.attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY)
1892		attr->en_sqd_async_notify = cmd->base.en_sqd_async_notify;
1893	if (cmd->base.attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
1894		attr->max_rd_atomic = cmd->base.max_rd_atomic;
1895	if (cmd->base.attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1896		attr->max_dest_rd_atomic = cmd->base.max_dest_rd_atomic;
1897	if (cmd->base.attr_mask & IB_QP_MIN_RNR_TIMER)
1898		attr->min_rnr_timer = cmd->base.min_rnr_timer;
1899	if (cmd->base.attr_mask & IB_QP_PORT)
1900		attr->port_num = cmd->base.port_num;
1901	if (cmd->base.attr_mask & IB_QP_TIMEOUT)
1902		attr->timeout = cmd->base.timeout;
1903	if (cmd->base.attr_mask & IB_QP_RETRY_CNT)
1904		attr->retry_cnt = cmd->base.retry_cnt;
1905	if (cmd->base.attr_mask & IB_QP_RNR_RETRY)
1906		attr->rnr_retry = cmd->base.rnr_retry;
1907	if (cmd->base.attr_mask & IB_QP_ALT_PATH) {
1908		attr->alt_port_num = cmd->base.alt_port_num;
1909		attr->alt_timeout = cmd->base.alt_timeout;
1910		attr->alt_pkey_index = cmd->base.alt_pkey_index;
1911	}
1912	if (cmd->base.attr_mask & IB_QP_RATE_LIMIT)
1913		attr->rate_limit = cmd->rate_limit;
1914
1915	if (cmd->base.attr_mask & IB_QP_AV)
1916		copy_ah_attr_from_uverbs(qp->device, &attr->ah_attr,
1917					 &cmd->base.dest);
1918
1919	if (cmd->base.attr_mask & IB_QP_ALT_PATH)
1920		copy_ah_attr_from_uverbs(qp->device, &attr->alt_ah_attr,
1921					 &cmd->base.alt_dest);
1922
1923	ret = ib_modify_qp_with_udata(qp, attr,
1924				      modify_qp_mask(qp->qp_type,
1925						     cmd->base.attr_mask),
1926				      &attrs->driver_udata);
1927
1928release_qp:
1929	rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
1930				UVERBS_LOOKUP_READ);
1931out:
1932	kfree(attr);
1933
1934	return ret;
1935}
1936
1937static int ib_uverbs_modify_qp(struct uverbs_attr_bundle *attrs)
 
 
1938{
1939	struct ib_uverbs_ex_modify_qp cmd;
1940	int ret;
 
 
 
 
1941
1942	ret = uverbs_request(attrs, &cmd.base, sizeof(cmd.base));
1943	if (ret)
1944		return ret;
1945
1946	if (cmd.base.attr_mask & ~IB_QP_ATTR_STANDARD_BITS)
1947		return -EOPNOTSUPP;
1948
1949	return modify_qp(attrs, &cmd);
1950}
 
 
 
1951
1952static int ib_uverbs_ex_modify_qp(struct uverbs_attr_bundle *attrs)
1953{
1954	struct ib_uverbs_ex_modify_qp cmd;
1955	struct ib_uverbs_ex_modify_qp_resp resp = {
1956		.response_length = uverbs_response_length(attrs, sizeof(resp))
1957	};
1958	int ret;
1959
1960	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1961	if (ret)
1962		return ret;
1963
1964	/*
1965	 * Last bit is reserved for extending the attr_mask by
1966	 * using another field.
1967	 */
1968	if (cmd.base.attr_mask & ~(IB_QP_ATTR_STANDARD_BITS | IB_QP_RATE_LIMIT))
1969		return -EOPNOTSUPP;
1970
1971	ret = modify_qp(attrs, &cmd);
1972	if (ret)
1973		return ret;
1974
1975	return uverbs_response(attrs, &resp, sizeof(resp));
1976}
1977
1978static int ib_uverbs_destroy_qp(struct uverbs_attr_bundle *attrs)
1979{
1980	struct ib_uverbs_destroy_qp      cmd;
1981	struct ib_uverbs_destroy_qp_resp resp;
1982	struct ib_uobject		*uobj;
1983	struct ib_uqp_object        	*obj;
1984	int ret;
1985
1986	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1987	if (ret)
1988		return ret;
1989
1990	uobj = uobj_get_destroy(UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
1991	if (IS_ERR(uobj))
1992		return PTR_ERR(uobj);
1993
1994	obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
1995	memset(&resp, 0, sizeof(resp));
1996	resp.events_reported = obj->uevent.events_reported;
1997
1998	uobj_put_destroy(uobj);
1999
2000	return uverbs_response(attrs, &resp, sizeof(resp));
2001}
2002
2003static void *alloc_wr(size_t wr_size, __u32 num_sge)
2004{
2005	if (num_sge >= (U32_MAX - ALIGN(wr_size, sizeof(struct ib_sge))) /
2006			       sizeof(struct ib_sge))
2007		return NULL;
2008
2009	return kmalloc(ALIGN(wr_size, sizeof(struct ib_sge)) +
2010			       num_sge * sizeof(struct ib_sge),
2011		       GFP_KERNEL);
2012}
2013
2014static int ib_uverbs_post_send(struct uverbs_attr_bundle *attrs)
 
 
2015{
2016	struct ib_uverbs_post_send      cmd;
2017	struct ib_uverbs_post_send_resp resp;
2018	struct ib_uverbs_send_wr       *user_wr;
2019	struct ib_send_wr              *wr = NULL, *last, *next;
2020	const struct ib_send_wr	       *bad_wr;
2021	struct ib_qp                   *qp;
2022	int                             i, sg_ind;
2023	int				is_ud;
2024	int ret, ret2;
2025	size_t                          next_size;
2026	const struct ib_sge __user *sgls;
2027	const void __user *wqes;
2028	struct uverbs_req_iter iter;
2029
2030	ret = uverbs_request_start(attrs, &iter, &cmd, sizeof(cmd));
2031	if (ret)
2032		return ret;
2033	wqes = uverbs_request_next_ptr(&iter, cmd.wqe_size * cmd.wr_count);
2034	if (IS_ERR(wqes))
2035		return PTR_ERR(wqes);
2036	sgls = uverbs_request_next_ptr(
2037		&iter, cmd.sge_count * sizeof(struct ib_uverbs_sge));
2038	if (IS_ERR(sgls))
2039		return PTR_ERR(sgls);
2040	ret = uverbs_request_finish(&iter);
2041	if (ret)
2042		return ret;
2043
2044	user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
2045	if (!user_wr)
2046		return -ENOMEM;
2047
2048	qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
2049	if (!qp) {
2050		ret = -EINVAL;
2051		goto out;
2052	}
2053
2054	is_ud = qp->qp_type == IB_QPT_UD;
2055	sg_ind = 0;
2056	last = NULL;
2057	for (i = 0; i < cmd.wr_count; ++i) {
2058		if (copy_from_user(user_wr, wqes + i * cmd.wqe_size,
 
2059				   cmd.wqe_size)) {
2060			ret = -EFAULT;
2061			goto out_put;
2062		}
2063
2064		if (user_wr->num_sge + sg_ind > cmd.sge_count) {
2065			ret = -EINVAL;
2066			goto out_put;
2067		}
2068
2069		if (is_ud) {
2070			struct ib_ud_wr *ud;
2071
2072			if (user_wr->opcode != IB_WR_SEND &&
2073			    user_wr->opcode != IB_WR_SEND_WITH_IMM) {
2074				ret = -EINVAL;
2075				goto out_put;
2076			}
2077
2078			next_size = sizeof(*ud);
2079			ud = alloc_wr(next_size, user_wr->num_sge);
2080			if (!ud) {
2081				ret = -ENOMEM;
2082				goto out_put;
2083			}
2084
2085			ud->ah = uobj_get_obj_read(ah, UVERBS_OBJECT_AH,
2086						   user_wr->wr.ud.ah, attrs);
2087			if (!ud->ah) {
2088				kfree(ud);
2089				ret = -EINVAL;
2090				goto out_put;
2091			}
2092			ud->remote_qpn = user_wr->wr.ud.remote_qpn;
2093			ud->remote_qkey = user_wr->wr.ud.remote_qkey;
2094
2095			next = &ud->wr;
2096		} else if (user_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
2097			   user_wr->opcode == IB_WR_RDMA_WRITE ||
2098			   user_wr->opcode == IB_WR_RDMA_READ) {
2099			struct ib_rdma_wr *rdma;
2100
2101			next_size = sizeof(*rdma);
2102			rdma = alloc_wr(next_size, user_wr->num_sge);
2103			if (!rdma) {
2104				ret = -ENOMEM;
2105				goto out_put;
2106			}
2107
2108			rdma->remote_addr = user_wr->wr.rdma.remote_addr;
2109			rdma->rkey = user_wr->wr.rdma.rkey;
2110
2111			next = &rdma->wr;
2112		} else if (user_wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
2113			   user_wr->opcode == IB_WR_ATOMIC_FETCH_AND_ADD) {
2114			struct ib_atomic_wr *atomic;
2115
2116			next_size = sizeof(*atomic);
2117			atomic = alloc_wr(next_size, user_wr->num_sge);
2118			if (!atomic) {
2119				ret = -ENOMEM;
2120				goto out_put;
2121			}
2122
2123			atomic->remote_addr = user_wr->wr.atomic.remote_addr;
2124			atomic->compare_add = user_wr->wr.atomic.compare_add;
2125			atomic->swap = user_wr->wr.atomic.swap;
2126			atomic->rkey = user_wr->wr.atomic.rkey;
2127
2128			next = &atomic->wr;
2129		} else if (user_wr->opcode == IB_WR_SEND ||
2130			   user_wr->opcode == IB_WR_SEND_WITH_IMM ||
2131			   user_wr->opcode == IB_WR_SEND_WITH_INV) {
2132			next_size = sizeof(*next);
2133			next = alloc_wr(next_size, user_wr->num_sge);
2134			if (!next) {
2135				ret = -ENOMEM;
2136				goto out_put;
2137			}
2138		} else {
2139			ret = -EINVAL;
2140			goto out_put;
2141		}
2142
2143		if (user_wr->opcode == IB_WR_SEND_WITH_IMM ||
2144		    user_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM) {
2145			next->ex.imm_data =
2146					(__be32 __force) user_wr->ex.imm_data;
2147		} else if (user_wr->opcode == IB_WR_SEND_WITH_INV) {
2148			next->ex.invalidate_rkey = user_wr->ex.invalidate_rkey;
2149		}
2150
2151		if (!last)
2152			wr = next;
2153		else
2154			last->next = next;
2155		last = next;
2156
2157		next->next       = NULL;
2158		next->wr_id      = user_wr->wr_id;
2159		next->num_sge    = user_wr->num_sge;
2160		next->opcode     = user_wr->opcode;
2161		next->send_flags = user_wr->send_flags;
2162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2163		if (next->num_sge) {
2164			next->sg_list = (void *) next +
2165				ALIGN(next_size, sizeof(struct ib_sge));
2166			if (copy_from_user(next->sg_list, sgls + sg_ind,
2167					   next->num_sge *
2168						   sizeof(struct ib_sge))) {
 
 
2169				ret = -EFAULT;
2170				goto out_put;
2171			}
2172			sg_ind += next->num_sge;
2173		} else
2174			next->sg_list = NULL;
2175	}
2176
2177	resp.bad_wr = 0;
2178	ret = qp->device->ops.post_send(qp->real_qp, wr, &bad_wr);
2179	if (ret)
2180		for (next = wr; next; next = next->next) {
2181			++resp.bad_wr;
2182			if (next == bad_wr)
2183				break;
2184		}
2185
2186	ret2 = uverbs_response(attrs, &resp, sizeof(resp));
2187	if (ret2)
2188		ret = ret2;
2189
2190out_put:
2191	rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
2192				UVERBS_LOOKUP_READ);
2193
2194	while (wr) {
2195		if (is_ud && ud_wr(wr)->ah)
2196			uobj_put_obj_read(ud_wr(wr)->ah);
2197		next = wr->next;
2198		kfree(wr);
2199		wr = next;
2200	}
2201
2202out:
2203	kfree(user_wr);
2204
2205	return ret;
2206}
2207
2208static struct ib_recv_wr *
2209ib_uverbs_unmarshall_recv(struct uverbs_req_iter *iter, u32 wr_count,
2210			  u32 wqe_size, u32 sge_count)
 
 
2211{
2212	struct ib_uverbs_recv_wr *user_wr;
2213	struct ib_recv_wr        *wr = NULL, *last, *next;
2214	int                       sg_ind;
2215	int                       i;
2216	int                       ret;
2217	const struct ib_sge __user *sgls;
2218	const void __user *wqes;
2219
2220	if (wqe_size < sizeof(struct ib_uverbs_recv_wr))
 
2221		return ERR_PTR(-EINVAL);
2222
2223	wqes = uverbs_request_next_ptr(iter, wqe_size * wr_count);
2224	if (IS_ERR(wqes))
2225		return ERR_CAST(wqes);
2226	sgls = uverbs_request_next_ptr(
2227		iter, sge_count * sizeof(struct ib_uverbs_sge));
2228	if (IS_ERR(sgls))
2229		return ERR_CAST(sgls);
2230	ret = uverbs_request_finish(iter);
2231	if (ret)
2232		return ERR_PTR(ret);
2233
2234	user_wr = kmalloc(wqe_size, GFP_KERNEL);
2235	if (!user_wr)
2236		return ERR_PTR(-ENOMEM);
2237
2238	sg_ind = 0;
2239	last = NULL;
2240	for (i = 0; i < wr_count; ++i) {
2241		if (copy_from_user(user_wr, wqes + i * wqe_size,
2242				   wqe_size)) {
2243			ret = -EFAULT;
2244			goto err;
2245		}
2246
2247		if (user_wr->num_sge + sg_ind > sge_count) {
2248			ret = -EINVAL;
2249			goto err;
2250		}
2251
2252		if (user_wr->num_sge >=
2253		    (U32_MAX - ALIGN(sizeof(*next), sizeof(struct ib_sge))) /
2254			    sizeof(struct ib_sge)) {
2255			ret = -EINVAL;
2256			goto err;
2257		}
2258
2259		next = kmalloc(ALIGN(sizeof(*next), sizeof(struct ib_sge)) +
2260				       user_wr->num_sge * sizeof(struct ib_sge),
2261			       GFP_KERNEL);
2262		if (!next) {
2263			ret = -ENOMEM;
2264			goto err;
2265		}
2266
2267		if (!last)
2268			wr = next;
2269		else
2270			last->next = next;
2271		last = next;
2272
2273		next->next       = NULL;
2274		next->wr_id      = user_wr->wr_id;
2275		next->num_sge    = user_wr->num_sge;
2276
2277		if (next->num_sge) {
2278			next->sg_list = (void *)next +
2279				ALIGN(sizeof(*next), sizeof(struct ib_sge));
2280			if (copy_from_user(next->sg_list, sgls + sg_ind,
2281					   next->num_sge *
2282						   sizeof(struct ib_sge))) {
 
2283				ret = -EFAULT;
2284				goto err;
2285			}
2286			sg_ind += next->num_sge;
2287		} else
2288			next->sg_list = NULL;
2289	}
2290
2291	kfree(user_wr);
2292	return wr;
2293
2294err:
2295	kfree(user_wr);
2296
2297	while (wr) {
2298		next = wr->next;
2299		kfree(wr);
2300		wr = next;
2301	}
2302
2303	return ERR_PTR(ret);
2304}
2305
2306static int ib_uverbs_post_recv(struct uverbs_attr_bundle *attrs)
 
 
2307{
2308	struct ib_uverbs_post_recv      cmd;
2309	struct ib_uverbs_post_recv_resp resp;
2310	struct ib_recv_wr              *wr, *next;
2311	const struct ib_recv_wr	       *bad_wr;
2312	struct ib_qp                   *qp;
2313	int ret, ret2;
2314	struct uverbs_req_iter iter;
2315
2316	ret = uverbs_request_start(attrs, &iter, &cmd, sizeof(cmd));
2317	if (ret)
2318		return ret;
2319
2320	wr = ib_uverbs_unmarshall_recv(&iter, cmd.wr_count, cmd.wqe_size,
2321				       cmd.sge_count);
 
2322	if (IS_ERR(wr))
2323		return PTR_ERR(wr);
2324
2325	qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
2326	if (!qp) {
2327		ret = -EINVAL;
2328		goto out;
2329	}
2330
2331	resp.bad_wr = 0;
2332	ret = qp->device->ops.post_recv(qp->real_qp, wr, &bad_wr);
 
 
2333
2334	rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
2335				UVERBS_LOOKUP_READ);
2336	if (ret) {
2337		for (next = wr; next; next = next->next) {
2338			++resp.bad_wr;
2339			if (next == bad_wr)
2340				break;
2341		}
2342	}
2343
2344	ret2 = uverbs_response(attrs, &resp, sizeof(resp));
2345	if (ret2)
2346		ret = ret2;
 
2347out:
2348	while (wr) {
2349		next = wr->next;
2350		kfree(wr);
2351		wr = next;
2352	}
2353
2354	return ret;
2355}
2356
2357static int ib_uverbs_post_srq_recv(struct uverbs_attr_bundle *attrs)
 
 
2358{
2359	struct ib_uverbs_post_srq_recv      cmd;
2360	struct ib_uverbs_post_srq_recv_resp resp;
2361	struct ib_recv_wr                  *wr, *next;
2362	const struct ib_recv_wr		   *bad_wr;
2363	struct ib_srq                      *srq;
2364	int ret, ret2;
2365	struct uverbs_req_iter iter;
2366
2367	ret = uverbs_request_start(attrs, &iter, &cmd, sizeof(cmd));
2368	if (ret)
2369		return ret;
2370
2371	wr = ib_uverbs_unmarshall_recv(&iter, cmd.wr_count, cmd.wqe_size,
2372				       cmd.sge_count);
 
2373	if (IS_ERR(wr))
2374		return PTR_ERR(wr);
2375
2376	srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, attrs);
2377	if (!srq) {
2378		ret = -EINVAL;
2379		goto out;
2380	}
2381
2382	resp.bad_wr = 0;
2383	ret = srq->device->ops.post_srq_recv(srq, wr, &bad_wr);
2384
2385	rdma_lookup_put_uobject(&srq->uobject->uevent.uobject,
2386				UVERBS_LOOKUP_READ);
2387
2388	if (ret)
2389		for (next = wr; next; next = next->next) {
2390			++resp.bad_wr;
2391			if (next == bad_wr)
2392				break;
2393		}
2394
2395	ret2 = uverbs_response(attrs, &resp, sizeof(resp));
2396	if (ret2)
2397		ret = ret2;
2398
2399out:
2400	while (wr) {
2401		next = wr->next;
2402		kfree(wr);
2403		wr = next;
2404	}
2405
2406	return ret;
2407}
2408
2409static int ib_uverbs_create_ah(struct uverbs_attr_bundle *attrs)
 
 
2410{
2411	struct ib_uverbs_create_ah	 cmd;
2412	struct ib_uverbs_create_ah_resp	 resp;
2413	struct ib_uobject		*uobj;
2414	struct ib_pd			*pd;
2415	struct ib_ah			*ah;
2416	struct rdma_ah_attr		attr = {};
2417	int ret;
2418	struct ib_device *ib_dev;
2419
2420	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
2421	if (ret)
2422		return ret;
2423
2424	uobj = uobj_alloc(UVERBS_OBJECT_AH, attrs, &ib_dev);
2425	if (IS_ERR(uobj))
2426		return PTR_ERR(uobj);
2427
2428	if (!rdma_is_port_valid(ib_dev, cmd.attr.port_num)) {
2429		ret = -EINVAL;
2430		goto err;
2431	}
 
 
2432
2433	pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, attrs);
2434	if (!pd) {
2435		ret = -EINVAL;
2436		goto err;
2437	}
2438
2439	attr.type = rdma_ah_find_type(ib_dev, cmd.attr.port_num);
2440	rdma_ah_set_make_grd(&attr, false);
2441	rdma_ah_set_dlid(&attr, cmd.attr.dlid);
2442	rdma_ah_set_sl(&attr, cmd.attr.sl);
2443	rdma_ah_set_path_bits(&attr, cmd.attr.src_path_bits);
2444	rdma_ah_set_static_rate(&attr, cmd.attr.static_rate);
2445	rdma_ah_set_port_num(&attr, cmd.attr.port_num);
2446
2447	if (cmd.attr.is_global) {
2448		rdma_ah_set_grh(&attr, NULL, cmd.attr.grh.flow_label,
2449				cmd.attr.grh.sgid_index,
2450				cmd.attr.grh.hop_limit,
2451				cmd.attr.grh.traffic_class);
2452		rdma_ah_set_dgid_raw(&attr, cmd.attr.grh.dgid);
2453	} else {
2454		rdma_ah_set_ah_flags(&attr, 0);
2455	}
2456
2457	ah = rdma_create_user_ah(pd, &attr, &attrs->driver_udata);
2458	if (IS_ERR(ah)) {
2459		ret = PTR_ERR(ah);
2460		goto err_put;
2461	}
2462
2463	ah->uobject  = uobj;
2464	uobj->user_handle = cmd.user_handle;
2465	uobj->object = ah;
2466	uobj_put_obj_read(pd);
2467	uobj_finalize_uobj_create(uobj, attrs);
 
 
2468
2469	resp.ah_handle = uobj->id;
2470	return uverbs_response(attrs, &resp, sizeof(resp));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2471
2472err_put:
2473	uobj_put_obj_read(pd);
 
2474err:
2475	uobj_alloc_abort(uobj, attrs);
2476	return ret;
2477}
2478
2479static int ib_uverbs_destroy_ah(struct uverbs_attr_bundle *attrs)
 
2480{
2481	struct ib_uverbs_destroy_ah cmd;
2482	int ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2483
2484	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
2485	if (ret)
2486		return ret;
2487
2488	return uobj_perform_destroy(UVERBS_OBJECT_AH, cmd.ah_handle, attrs);
 
 
 
 
 
 
 
 
2489}
2490
2491static int ib_uverbs_attach_mcast(struct uverbs_attr_bundle *attrs)
 
 
2492{
2493	struct ib_uverbs_attach_mcast cmd;
2494	struct ib_qp                 *qp;
2495	struct ib_uqp_object         *obj;
2496	struct ib_uverbs_mcast_entry *mcast;
2497	int                           ret;
2498
2499	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
2500	if (ret)
2501		return ret;
2502
2503	qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
2504	if (!qp)
2505		return -EINVAL;
2506
2507	obj = qp->uobject;
2508
2509	mutex_lock(&obj->mcast_lock);
2510	list_for_each_entry(mcast, &obj->mcast_list, list)
2511		if (cmd.mlid == mcast->lid &&
2512		    !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2513			ret = 0;
2514			goto out_put;
2515		}
2516
2517	mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
2518	if (!mcast) {
2519		ret = -ENOMEM;
2520		goto out_put;
2521	}
2522
2523	mcast->lid = cmd.mlid;
2524	memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
2525
2526	ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
2527	if (!ret)
2528		list_add_tail(&mcast->list, &obj->mcast_list);
2529	else
2530		kfree(mcast);
2531
2532out_put:
2533	mutex_unlock(&obj->mcast_lock);
2534	rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
2535				UVERBS_LOOKUP_READ);
2536
2537	return ret;
2538}
2539
2540static int ib_uverbs_detach_mcast(struct uverbs_attr_bundle *attrs)
 
 
2541{
2542	struct ib_uverbs_detach_mcast cmd;
2543	struct ib_uqp_object         *obj;
2544	struct ib_qp                 *qp;
2545	struct ib_uverbs_mcast_entry *mcast;
2546	int                           ret;
2547	bool                          found = false;
2548
2549	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
2550	if (ret)
2551		return ret;
2552
2553	qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
2554	if (!qp)
2555		return -EINVAL;
2556
2557	obj = qp->uobject;
2558	mutex_lock(&obj->mcast_lock);
 
 
 
2559
2560	list_for_each_entry(mcast, &obj->mcast_list, list)
2561		if (cmd.mlid == mcast->lid &&
2562		    !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2563			list_del(&mcast->list);
2564			kfree(mcast);
2565			found = true;
2566			break;
2567		}
2568
2569	if (!found) {
2570		ret = -EINVAL;
2571		goto out_put;
2572	}
2573
2574	ret = ib_detach_mcast(qp, (union ib_gid *)cmd.gid, cmd.mlid);
2575
2576out_put:
2577	mutex_unlock(&obj->mcast_lock);
2578	rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
2579				UVERBS_LOOKUP_READ);
2580	return ret;
2581}
2582
2583struct ib_uflow_resources *flow_resources_alloc(size_t num_specs)
2584{
2585	struct ib_uflow_resources *resources;
2586
2587	resources = kzalloc(sizeof(*resources), GFP_KERNEL);
2588
2589	if (!resources)
2590		return NULL;
2591
2592	if (!num_specs)
2593		goto out;
2594
2595	resources->counters =
2596		kcalloc(num_specs, sizeof(*resources->counters), GFP_KERNEL);
2597	resources->collection =
2598		kcalloc(num_specs, sizeof(*resources->collection), GFP_KERNEL);
2599
2600	if (!resources->counters || !resources->collection)
2601		goto err;
2602
2603out:
2604	resources->max = num_specs;
2605	return resources;
2606
2607err:
2608	kfree(resources->counters);
2609	kfree(resources);
2610
2611	return NULL;
2612}
2613EXPORT_SYMBOL(flow_resources_alloc);
2614
2615void ib_uverbs_flow_resources_free(struct ib_uflow_resources *uflow_res)
 
 
2616{
2617	unsigned int i;
2618
2619	if (!uflow_res)
2620		return;
2621
2622	for (i = 0; i < uflow_res->collection_num; i++)
2623		atomic_dec(&uflow_res->collection[i]->usecnt);
2624
2625	for (i = 0; i < uflow_res->counters_num; i++)
2626		atomic_dec(&uflow_res->counters[i]->usecnt);
2627
2628	kfree(uflow_res->collection);
2629	kfree(uflow_res->counters);
2630	kfree(uflow_res);
2631}
2632EXPORT_SYMBOL(ib_uverbs_flow_resources_free);
2633
2634void flow_resources_add(struct ib_uflow_resources *uflow_res,
2635			enum ib_flow_spec_type type,
2636			void *ibobj)
2637{
2638	WARN_ON(uflow_res->num >= uflow_res->max);
2639
2640	switch (type) {
2641	case IB_FLOW_SPEC_ACTION_HANDLE:
2642		atomic_inc(&((struct ib_flow_action *)ibobj)->usecnt);
2643		uflow_res->collection[uflow_res->collection_num++] =
2644			(struct ib_flow_action *)ibobj;
2645		break;
2646	case IB_FLOW_SPEC_ACTION_COUNT:
2647		atomic_inc(&((struct ib_counters *)ibobj)->usecnt);
2648		uflow_res->counters[uflow_res->counters_num++] =
2649			(struct ib_counters *)ibobj;
2650		break;
2651	default:
2652		WARN_ON(1);
2653	}
2654
2655	uflow_res->num++;
2656}
2657EXPORT_SYMBOL(flow_resources_add);
2658
2659static int kern_spec_to_ib_spec_action(struct uverbs_attr_bundle *attrs,
2660				       struct ib_uverbs_flow_spec *kern_spec,
2661				       union ib_flow_spec *ib_spec,
2662				       struct ib_uflow_resources *uflow_res)
2663{
2664	ib_spec->type = kern_spec->type;
2665	switch (ib_spec->type) {
2666	case IB_FLOW_SPEC_ACTION_TAG:
2667		if (kern_spec->flow_tag.size !=
2668		    sizeof(struct ib_uverbs_flow_spec_action_tag))
2669			return -EINVAL;
2670
2671		ib_spec->flow_tag.size = sizeof(struct ib_flow_spec_action_tag);
2672		ib_spec->flow_tag.tag_id = kern_spec->flow_tag.tag_id;
2673		break;
2674	case IB_FLOW_SPEC_ACTION_DROP:
2675		if (kern_spec->drop.size !=
2676		    sizeof(struct ib_uverbs_flow_spec_action_drop))
2677			return -EINVAL;
2678
2679		ib_spec->drop.size = sizeof(struct ib_flow_spec_action_drop);
2680		break;
2681	case IB_FLOW_SPEC_ACTION_HANDLE:
2682		if (kern_spec->action.size !=
2683		    sizeof(struct ib_uverbs_flow_spec_action_handle))
2684			return -EOPNOTSUPP;
2685		ib_spec->action.act = uobj_get_obj_read(flow_action,
2686							UVERBS_OBJECT_FLOW_ACTION,
2687							kern_spec->action.handle,
2688							attrs);
2689		if (!ib_spec->action.act)
2690			return -EINVAL;
2691		ib_spec->action.size =
2692			sizeof(struct ib_flow_spec_action_handle);
2693		flow_resources_add(uflow_res,
2694				   IB_FLOW_SPEC_ACTION_HANDLE,
2695				   ib_spec->action.act);
2696		uobj_put_obj_read(ib_spec->action.act);
2697		break;
2698	case IB_FLOW_SPEC_ACTION_COUNT:
2699		if (kern_spec->flow_count.size !=
2700			sizeof(struct ib_uverbs_flow_spec_action_count))
2701			return -EINVAL;
2702		ib_spec->flow_count.counters =
2703			uobj_get_obj_read(counters,
2704					  UVERBS_OBJECT_COUNTERS,
2705					  kern_spec->flow_count.handle,
2706					  attrs);
2707		if (!ib_spec->flow_count.counters)
2708			return -EINVAL;
2709		ib_spec->flow_count.size =
2710				sizeof(struct ib_flow_spec_action_count);
2711		flow_resources_add(uflow_res,
2712				   IB_FLOW_SPEC_ACTION_COUNT,
2713				   ib_spec->flow_count.counters);
2714		uobj_put_obj_read(ib_spec->flow_count.counters);
2715		break;
2716	default:
2717		return -EINVAL;
2718	}
2719	return 0;
2720}
2721
2722static ssize_t spec_filter_size(const void *kern_spec_filter, u16 kern_filter_size,
2723				u16 ib_real_filter_sz)
2724{
2725	/*
2726	 * User space filter structures must be 64 bit aligned, otherwise this
2727	 * may pass, but we won't handle additional new attributes.
2728	 */
2729
2730	if (kern_filter_size > ib_real_filter_sz) {
2731		if (memchr_inv(kern_spec_filter +
2732			       ib_real_filter_sz, 0,
2733			       kern_filter_size - ib_real_filter_sz))
2734			return -EINVAL;
2735		return ib_real_filter_sz;
2736	}
2737	return kern_filter_size;
2738}
2739
2740int ib_uverbs_kern_spec_to_ib_spec_filter(enum ib_flow_spec_type type,
2741					  const void *kern_spec_mask,
2742					  const void *kern_spec_val,
2743					  size_t kern_filter_sz,
2744					  union ib_flow_spec *ib_spec)
2745{
2746	ssize_t actual_filter_sz;
2747	ssize_t ib_filter_sz;
2748
2749	/* User flow spec size must be aligned to 4 bytes */
2750	if (kern_filter_sz != ALIGN(kern_filter_sz, 4))
2751		return -EINVAL;
2752
2753	ib_spec->type = type;
2754
2755	if (ib_spec->type == (IB_FLOW_SPEC_INNER | IB_FLOW_SPEC_VXLAN_TUNNEL))
2756		return -EINVAL;
2757
2758	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
2759	case IB_FLOW_SPEC_ETH:
2760		ib_filter_sz = offsetof(struct ib_flow_eth_filter, real_sz);
2761		actual_filter_sz = spec_filter_size(kern_spec_mask,
2762						    kern_filter_sz,
2763						    ib_filter_sz);
2764		if (actual_filter_sz <= 0)
2765			return -EINVAL;
2766		ib_spec->size = sizeof(struct ib_flow_spec_eth);
2767		memcpy(&ib_spec->eth.val, kern_spec_val, actual_filter_sz);
2768		memcpy(&ib_spec->eth.mask, kern_spec_mask, actual_filter_sz);
2769		break;
2770	case IB_FLOW_SPEC_IPV4:
2771		ib_filter_sz = offsetof(struct ib_flow_ipv4_filter, real_sz);
2772		actual_filter_sz = spec_filter_size(kern_spec_mask,
2773						    kern_filter_sz,
2774						    ib_filter_sz);
2775		if (actual_filter_sz <= 0)
2776			return -EINVAL;
2777		ib_spec->size = sizeof(struct ib_flow_spec_ipv4);
2778		memcpy(&ib_spec->ipv4.val, kern_spec_val, actual_filter_sz);
2779		memcpy(&ib_spec->ipv4.mask, kern_spec_mask, actual_filter_sz);
2780		break;
2781	case IB_FLOW_SPEC_IPV6:
2782		ib_filter_sz = offsetof(struct ib_flow_ipv6_filter, real_sz);
2783		actual_filter_sz = spec_filter_size(kern_spec_mask,
2784						    kern_filter_sz,
2785						    ib_filter_sz);
2786		if (actual_filter_sz <= 0)
2787			return -EINVAL;
2788		ib_spec->size = sizeof(struct ib_flow_spec_ipv6);
2789		memcpy(&ib_spec->ipv6.val, kern_spec_val, actual_filter_sz);
2790		memcpy(&ib_spec->ipv6.mask, kern_spec_mask, actual_filter_sz);
2791
2792		if ((ntohl(ib_spec->ipv6.mask.flow_label)) >= BIT(20) ||
2793		    (ntohl(ib_spec->ipv6.val.flow_label)) >= BIT(20))
2794			return -EINVAL;
2795		break;
2796	case IB_FLOW_SPEC_TCP:
2797	case IB_FLOW_SPEC_UDP:
2798		ib_filter_sz = offsetof(struct ib_flow_tcp_udp_filter, real_sz);
2799		actual_filter_sz = spec_filter_size(kern_spec_mask,
2800						    kern_filter_sz,
2801						    ib_filter_sz);
2802		if (actual_filter_sz <= 0)
2803			return -EINVAL;
2804		ib_spec->size = sizeof(struct ib_flow_spec_tcp_udp);
2805		memcpy(&ib_spec->tcp_udp.val, kern_spec_val, actual_filter_sz);
2806		memcpy(&ib_spec->tcp_udp.mask, kern_spec_mask, actual_filter_sz);
2807		break;
2808	case IB_FLOW_SPEC_VXLAN_TUNNEL:
2809		ib_filter_sz = offsetof(struct ib_flow_tunnel_filter, real_sz);
2810		actual_filter_sz = spec_filter_size(kern_spec_mask,
2811						    kern_filter_sz,
2812						    ib_filter_sz);
2813		if (actual_filter_sz <= 0)
2814			return -EINVAL;
2815		ib_spec->tunnel.size = sizeof(struct ib_flow_spec_tunnel);
2816		memcpy(&ib_spec->tunnel.val, kern_spec_val, actual_filter_sz);
2817		memcpy(&ib_spec->tunnel.mask, kern_spec_mask, actual_filter_sz);
2818
2819		if ((ntohl(ib_spec->tunnel.mask.tunnel_id)) >= BIT(24) ||
2820		    (ntohl(ib_spec->tunnel.val.tunnel_id)) >= BIT(24))
2821			return -EINVAL;
2822		break;
2823	case IB_FLOW_SPEC_ESP:
2824		ib_filter_sz = offsetof(struct ib_flow_esp_filter, real_sz);
2825		actual_filter_sz = spec_filter_size(kern_spec_mask,
2826						    kern_filter_sz,
2827						    ib_filter_sz);
2828		if (actual_filter_sz <= 0)
2829			return -EINVAL;
2830		ib_spec->esp.size = sizeof(struct ib_flow_spec_esp);
2831		memcpy(&ib_spec->esp.val, kern_spec_val, actual_filter_sz);
2832		memcpy(&ib_spec->esp.mask, kern_spec_mask, actual_filter_sz);
2833		break;
2834	case IB_FLOW_SPEC_GRE:
2835		ib_filter_sz = offsetof(struct ib_flow_gre_filter, real_sz);
2836		actual_filter_sz = spec_filter_size(kern_spec_mask,
2837						    kern_filter_sz,
2838						    ib_filter_sz);
2839		if (actual_filter_sz <= 0)
2840			return -EINVAL;
2841		ib_spec->gre.size = sizeof(struct ib_flow_spec_gre);
2842		memcpy(&ib_spec->gre.val, kern_spec_val, actual_filter_sz);
2843		memcpy(&ib_spec->gre.mask, kern_spec_mask, actual_filter_sz);
2844		break;
2845	case IB_FLOW_SPEC_MPLS:
2846		ib_filter_sz = offsetof(struct ib_flow_mpls_filter, real_sz);
2847		actual_filter_sz = spec_filter_size(kern_spec_mask,
2848						    kern_filter_sz,
2849						    ib_filter_sz);
2850		if (actual_filter_sz <= 0)
2851			return -EINVAL;
2852		ib_spec->mpls.size = sizeof(struct ib_flow_spec_mpls);
2853		memcpy(&ib_spec->mpls.val, kern_spec_val, actual_filter_sz);
2854		memcpy(&ib_spec->mpls.mask, kern_spec_mask, actual_filter_sz);
2855		break;
2856	default:
2857		return -EINVAL;
2858	}
2859	return 0;
2860}
2861
2862static int kern_spec_to_ib_spec_filter(struct ib_uverbs_flow_spec *kern_spec,
2863				       union ib_flow_spec *ib_spec)
2864{
2865	size_t kern_filter_sz;
2866	void *kern_spec_mask;
2867	void *kern_spec_val;
2868
2869	if (check_sub_overflow((size_t)kern_spec->hdr.size,
2870			       sizeof(struct ib_uverbs_flow_spec_hdr),
2871			       &kern_filter_sz))
2872		return -EINVAL;
2873
2874	kern_filter_sz /= 2;
2875
2876	kern_spec_val = (void *)kern_spec +
2877		sizeof(struct ib_uverbs_flow_spec_hdr);
2878	kern_spec_mask = kern_spec_val + kern_filter_sz;
2879
2880	return ib_uverbs_kern_spec_to_ib_spec_filter(kern_spec->type,
2881						     kern_spec_mask,
2882						     kern_spec_val,
2883						     kern_filter_sz, ib_spec);
2884}
2885
2886static int kern_spec_to_ib_spec(struct uverbs_attr_bundle *attrs,
2887				struct ib_uverbs_flow_spec *kern_spec,
2888				union ib_flow_spec *ib_spec,
2889				struct ib_uflow_resources *uflow_res)
2890{
2891	if (kern_spec->reserved)
2892		return -EINVAL;
2893
2894	if (kern_spec->type >= IB_FLOW_SPEC_ACTION_TAG)
2895		return kern_spec_to_ib_spec_action(attrs, kern_spec, ib_spec,
2896						   uflow_res);
2897	else
2898		return kern_spec_to_ib_spec_filter(kern_spec, ib_spec);
2899}
2900
2901static int ib_uverbs_ex_create_wq(struct uverbs_attr_bundle *attrs)
2902{
2903	struct ib_uverbs_ex_create_wq cmd;
2904	struct ib_uverbs_ex_create_wq_resp resp = {};
2905	struct ib_uwq_object           *obj;
2906	int err = 0;
2907	struct ib_cq *cq;
2908	struct ib_pd *pd;
2909	struct ib_wq *wq;
2910	struct ib_wq_init_attr wq_init_attr = {};
2911	struct ib_device *ib_dev;
2912
2913	err = uverbs_request(attrs, &cmd, sizeof(cmd));
2914	if (err)
2915		return err;
2916
2917	if (cmd.comp_mask)
2918		return -EOPNOTSUPP;
2919
2920	obj = (struct ib_uwq_object *)uobj_alloc(UVERBS_OBJECT_WQ, attrs,
2921						 &ib_dev);
2922	if (IS_ERR(obj))
2923		return PTR_ERR(obj);
2924
2925	pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, attrs);
2926	if (!pd) {
2927		err = -EINVAL;
2928		goto err_uobj;
2929	}
2930
2931	cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
2932	if (!cq) {
2933		err = -EINVAL;
2934		goto err_put_pd;
2935	}
2936
2937	wq_init_attr.cq = cq;
2938	wq_init_attr.max_sge = cmd.max_sge;
2939	wq_init_attr.max_wr = cmd.max_wr;
2940	wq_init_attr.wq_type = cmd.wq_type;
2941	wq_init_attr.event_handler = ib_uverbs_wq_event_handler;
2942	wq_init_attr.create_flags = cmd.create_flags;
2943	INIT_LIST_HEAD(&obj->uevent.event_list);
2944	obj->uevent.uobject.user_handle = cmd.user_handle;
2945
2946	wq = pd->device->ops.create_wq(pd, &wq_init_attr, &attrs->driver_udata);
2947	if (IS_ERR(wq)) {
2948		err = PTR_ERR(wq);
2949		goto err_put_cq;
2950	}
2951
2952	wq->uobject = obj;
2953	obj->uevent.uobject.object = wq;
2954	wq->wq_type = wq_init_attr.wq_type;
2955	wq->cq = cq;
2956	wq->pd = pd;
2957	wq->device = pd->device;
2958	atomic_set(&wq->usecnt, 0);
2959	atomic_inc(&pd->usecnt);
2960	atomic_inc(&cq->usecnt);
2961	obj->uevent.event_file = READ_ONCE(attrs->ufile->default_async_file);
2962	if (obj->uevent.event_file)
2963		uverbs_uobject_get(&obj->uevent.event_file->uobj);
2964
2965	uobj_put_obj_read(pd);
2966	rdma_lookup_put_uobject(&cq->uobject->uevent.uobject,
2967				UVERBS_LOOKUP_READ);
2968	uobj_finalize_uobj_create(&obj->uevent.uobject, attrs);
2969
2970	resp.wq_handle = obj->uevent.uobject.id;
2971	resp.max_sge = wq_init_attr.max_sge;
2972	resp.max_wr = wq_init_attr.max_wr;
2973	resp.wqn = wq->wq_num;
2974	resp.response_length = uverbs_response_length(attrs, sizeof(resp));
2975	return uverbs_response(attrs, &resp, sizeof(resp));
2976
2977err_put_cq:
2978	rdma_lookup_put_uobject(&cq->uobject->uevent.uobject,
2979				UVERBS_LOOKUP_READ);
2980err_put_pd:
2981	uobj_put_obj_read(pd);
2982err_uobj:
2983	uobj_alloc_abort(&obj->uevent.uobject, attrs);
2984
2985	return err;
2986}
2987
2988static int ib_uverbs_ex_destroy_wq(struct uverbs_attr_bundle *attrs)
2989{
2990	struct ib_uverbs_ex_destroy_wq	cmd;
2991	struct ib_uverbs_ex_destroy_wq_resp	resp = {};
2992	struct ib_uobject		*uobj;
2993	struct ib_uwq_object		*obj;
2994	int				ret;
2995
2996	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
2997	if (ret)
2998		return ret;
2999
3000	if (cmd.comp_mask)
3001		return -EOPNOTSUPP;
3002
3003	resp.response_length = uverbs_response_length(attrs, sizeof(resp));
3004	uobj = uobj_get_destroy(UVERBS_OBJECT_WQ, cmd.wq_handle, attrs);
3005	if (IS_ERR(uobj))
3006		return PTR_ERR(uobj);
3007
3008	obj = container_of(uobj, struct ib_uwq_object, uevent.uobject);
3009	resp.events_reported = obj->uevent.events_reported;
3010
3011	uobj_put_destroy(uobj);
3012
3013	return uverbs_response(attrs, &resp, sizeof(resp));
3014}
3015
3016static int ib_uverbs_ex_modify_wq(struct uverbs_attr_bundle *attrs)
3017{
3018	struct ib_uverbs_ex_modify_wq cmd;
3019	struct ib_wq *wq;
3020	struct ib_wq_attr wq_attr = {};
3021	int ret;
3022
3023	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3024	if (ret)
3025		return ret;
3026
3027	if (!cmd.attr_mask)
3028		return -EINVAL;
3029
3030	if (cmd.attr_mask > (IB_WQ_STATE | IB_WQ_CUR_STATE | IB_WQ_FLAGS))
3031		return -EINVAL;
3032
3033	wq = uobj_get_obj_read(wq, UVERBS_OBJECT_WQ, cmd.wq_handle, attrs);
3034	if (!wq)
3035		return -EINVAL;
3036
3037	if (cmd.attr_mask & IB_WQ_FLAGS) {
3038		wq_attr.flags = cmd.flags;
3039		wq_attr.flags_mask = cmd.flags_mask;
3040	}
3041
3042	if (cmd.attr_mask & IB_WQ_CUR_STATE) {
3043		if (cmd.curr_wq_state > IB_WQS_ERR)
3044			return -EINVAL;
3045
3046		wq_attr.curr_wq_state = cmd.curr_wq_state;
3047	} else {
3048		wq_attr.curr_wq_state = wq->state;
3049	}
3050
3051	if (cmd.attr_mask & IB_WQ_STATE) {
3052		if (cmd.wq_state > IB_WQS_ERR)
3053			return -EINVAL;
3054
3055		wq_attr.wq_state = cmd.wq_state;
3056	} else {
3057		wq_attr.wq_state = wq_attr.curr_wq_state;
3058	}
3059
3060	ret = wq->device->ops.modify_wq(wq, &wq_attr, cmd.attr_mask,
3061					&attrs->driver_udata);
3062	rdma_lookup_put_uobject(&wq->uobject->uevent.uobject,
3063				UVERBS_LOOKUP_READ);
3064	return ret;
3065}
3066
3067static int ib_uverbs_ex_create_rwq_ind_table(struct uverbs_attr_bundle *attrs)
3068{
3069	struct ib_uverbs_ex_create_rwq_ind_table cmd;
3070	struct ib_uverbs_ex_create_rwq_ind_table_resp  resp = {};
3071	struct ib_uobject *uobj;
3072	int err;
3073	struct ib_rwq_ind_table_init_attr init_attr = {};
3074	struct ib_rwq_ind_table *rwq_ind_tbl;
3075	struct ib_wq **wqs = NULL;
3076	u32 *wqs_handles = NULL;
3077	struct ib_wq	*wq = NULL;
3078	int i, num_read_wqs;
3079	u32 num_wq_handles;
3080	struct uverbs_req_iter iter;
3081	struct ib_device *ib_dev;
3082
3083	err = uverbs_request_start(attrs, &iter, &cmd, sizeof(cmd));
3084	if (err)
3085		return err;
3086
3087	if (cmd.comp_mask)
3088		return -EOPNOTSUPP;
3089
3090	if (cmd.log_ind_tbl_size > IB_USER_VERBS_MAX_LOG_IND_TBL_SIZE)
3091		return -EINVAL;
3092
3093	num_wq_handles = 1 << cmd.log_ind_tbl_size;
3094	wqs_handles = kcalloc(num_wq_handles, sizeof(*wqs_handles),
3095			      GFP_KERNEL);
3096	if (!wqs_handles)
3097		return -ENOMEM;
3098
3099	err = uverbs_request_next(&iter, wqs_handles,
3100				  num_wq_handles * sizeof(__u32));
3101	if (err)
3102		goto err_free;
3103
3104	err = uverbs_request_finish(&iter);
3105	if (err)
3106		goto err_free;
3107
3108	wqs = kcalloc(num_wq_handles, sizeof(*wqs), GFP_KERNEL);
3109	if (!wqs) {
3110		err = -ENOMEM;
3111		goto  err_free;
3112	}
3113
3114	for (num_read_wqs = 0; num_read_wqs < num_wq_handles;
3115			num_read_wqs++) {
3116		wq = uobj_get_obj_read(wq, UVERBS_OBJECT_WQ,
3117				       wqs_handles[num_read_wqs], attrs);
3118		if (!wq) {
3119			err = -EINVAL;
3120			goto put_wqs;
3121		}
3122
3123		wqs[num_read_wqs] = wq;
3124		atomic_inc(&wqs[num_read_wqs]->usecnt);
3125	}
3126
3127	uobj = uobj_alloc(UVERBS_OBJECT_RWQ_IND_TBL, attrs, &ib_dev);
3128	if (IS_ERR(uobj)) {
3129		err = PTR_ERR(uobj);
3130		goto put_wqs;
3131	}
3132
3133	rwq_ind_tbl = rdma_zalloc_drv_obj(ib_dev, ib_rwq_ind_table);
3134	if (!rwq_ind_tbl) {
3135		err = -ENOMEM;
3136		goto err_uobj;
3137	}
3138
3139	init_attr.log_ind_tbl_size = cmd.log_ind_tbl_size;
3140	init_attr.ind_tbl = wqs;
3141
3142	rwq_ind_tbl->ind_tbl = wqs;
3143	rwq_ind_tbl->log_ind_tbl_size = init_attr.log_ind_tbl_size;
3144	rwq_ind_tbl->uobject = uobj;
3145	uobj->object = rwq_ind_tbl;
3146	rwq_ind_tbl->device = ib_dev;
3147	atomic_set(&rwq_ind_tbl->usecnt, 0);
3148
3149	err = ib_dev->ops.create_rwq_ind_table(rwq_ind_tbl, &init_attr,
3150					       &attrs->driver_udata);
3151	if (err)
3152		goto err_create;
3153
3154	for (i = 0; i < num_wq_handles; i++)
3155		rdma_lookup_put_uobject(&wqs[i]->uobject->uevent.uobject,
3156					UVERBS_LOOKUP_READ);
3157	kfree(wqs_handles);
3158	uobj_finalize_uobj_create(uobj, attrs);
3159
3160	resp.ind_tbl_handle = uobj->id;
3161	resp.ind_tbl_num = rwq_ind_tbl->ind_tbl_num;
3162	resp.response_length = uverbs_response_length(attrs, sizeof(resp));
3163	return uverbs_response(attrs, &resp, sizeof(resp));
3164
3165err_create:
3166	kfree(rwq_ind_tbl);
3167err_uobj:
3168	uobj_alloc_abort(uobj, attrs);
3169put_wqs:
3170	for (i = 0; i < num_read_wqs; i++) {
3171		rdma_lookup_put_uobject(&wqs[i]->uobject->uevent.uobject,
3172					UVERBS_LOOKUP_READ);
3173		atomic_dec(&wqs[i]->usecnt);
3174	}
3175err_free:
3176	kfree(wqs_handles);
3177	kfree(wqs);
3178	return err;
3179}
3180
3181static int ib_uverbs_ex_destroy_rwq_ind_table(struct uverbs_attr_bundle *attrs)
3182{
3183	struct ib_uverbs_ex_destroy_rwq_ind_table cmd;
3184	int ret;
 
 
 
3185
3186	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
 
3187	if (ret)
3188		return ret;
3189
3190	if (cmd.comp_mask)
3191		return -EOPNOTSUPP;
 
 
3192
3193	return uobj_perform_destroy(UVERBS_OBJECT_RWQ_IND_TBL,
3194				    cmd.ind_tbl_handle, attrs);
3195}
3196
3197static int ib_uverbs_ex_create_flow(struct uverbs_attr_bundle *attrs)
3198{
3199	struct ib_uverbs_create_flow	  cmd;
3200	struct ib_uverbs_create_flow_resp resp = {};
3201	struct ib_uobject		  *uobj;
3202	struct ib_flow			  *flow_id;
3203	struct ib_uverbs_flow_attr	  *kern_flow_attr;
3204	struct ib_flow_attr		  *flow_attr;
3205	struct ib_qp			  *qp;
3206	struct ib_uflow_resources	  *uflow_res;
3207	struct ib_uverbs_flow_spec_hdr	  *kern_spec;
3208	struct uverbs_req_iter iter;
3209	int err;
3210	void *ib_spec;
3211	int i;
3212	struct ib_device *ib_dev;
3213
3214	err = uverbs_request_start(attrs, &iter, &cmd, sizeof(cmd));
3215	if (err)
3216		return err;
3217
3218	if (cmd.comp_mask)
3219		return -EINVAL;
3220
3221	if (!capable(CAP_NET_RAW))
3222		return -EPERM;
3223
3224	if (cmd.flow_attr.flags >= IB_FLOW_ATTR_FLAGS_RESERVED)
3225		return -EINVAL;
3226
3227	if ((cmd.flow_attr.flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) &&
3228	    ((cmd.flow_attr.type == IB_FLOW_ATTR_ALL_DEFAULT) ||
3229	     (cmd.flow_attr.type == IB_FLOW_ATTR_MC_DEFAULT)))
3230		return -EINVAL;
3231
3232	if (cmd.flow_attr.num_of_specs > IB_FLOW_SPEC_SUPPORT_LAYERS)
3233		return -EINVAL;
3234
3235	if (cmd.flow_attr.size >
3236	    (cmd.flow_attr.num_of_specs * sizeof(struct ib_uverbs_flow_spec)))
3237		return -EINVAL;
3238
3239	if (cmd.flow_attr.reserved[0] ||
3240	    cmd.flow_attr.reserved[1])
3241		return -EINVAL;
3242
3243	if (cmd.flow_attr.num_of_specs) {
3244		kern_flow_attr = kmalloc(sizeof(*kern_flow_attr) + cmd.flow_attr.size,
3245					 GFP_KERNEL);
3246		if (!kern_flow_attr)
3247			return -ENOMEM;
3248
3249		*kern_flow_attr = cmd.flow_attr;
3250		err = uverbs_request_next(&iter, &kern_flow_attr->flow_specs,
3251					  cmd.flow_attr.size);
3252		if (err)
3253			goto err_free_attr;
3254	} else {
3255		kern_flow_attr = &cmd.flow_attr;
3256	}
3257
3258	err = uverbs_request_finish(&iter);
3259	if (err)
3260		goto err_free_attr;
3261
3262	uobj = uobj_alloc(UVERBS_OBJECT_FLOW, attrs, &ib_dev);
3263	if (IS_ERR(uobj)) {
3264		err = PTR_ERR(uobj);
3265		goto err_free_attr;
3266	}
3267
3268	if (!rdma_is_port_valid(uobj->context->device, cmd.flow_attr.port)) {
3269		err = -EINVAL;
3270		goto err_uobj;
3271	}
3272
3273	qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
3274	if (!qp) {
3275		err = -EINVAL;
3276		goto err_uobj;
3277	}
3278
3279	if (qp->qp_type != IB_QPT_UD && qp->qp_type != IB_QPT_RAW_PACKET) {
3280		err = -EINVAL;
3281		goto err_put;
3282	}
3283
3284	flow_attr = kzalloc(struct_size(flow_attr, flows,
3285				cmd.flow_attr.num_of_specs), GFP_KERNEL);
3286	if (!flow_attr) {
3287		err = -ENOMEM;
3288		goto err_put;
3289	}
3290	uflow_res = flow_resources_alloc(cmd.flow_attr.num_of_specs);
3291	if (!uflow_res) {
3292		err = -ENOMEM;
3293		goto err_free_flow_attr;
3294	}
3295
3296	flow_attr->type = kern_flow_attr->type;
3297	flow_attr->priority = kern_flow_attr->priority;
3298	flow_attr->num_of_specs = kern_flow_attr->num_of_specs;
3299	flow_attr->port = kern_flow_attr->port;
3300	flow_attr->flags = kern_flow_attr->flags;
3301	flow_attr->size = sizeof(*flow_attr);
3302
3303	kern_spec = kern_flow_attr->flow_specs;
3304	ib_spec = flow_attr + 1;
3305	for (i = 0; i < flow_attr->num_of_specs &&
3306			cmd.flow_attr.size >= sizeof(*kern_spec) &&
3307			cmd.flow_attr.size >= kern_spec->size;
3308	     i++) {
3309		err = kern_spec_to_ib_spec(
3310				attrs, (struct ib_uverbs_flow_spec *)kern_spec,
3311				ib_spec, uflow_res);
3312		if (err)
3313			goto err_free;
3314
3315		flow_attr->size +=
3316			((union ib_flow_spec *) ib_spec)->size;
3317		cmd.flow_attr.size -= kern_spec->size;
3318		kern_spec = ((void *)kern_spec) + kern_spec->size;
3319		ib_spec += ((union ib_flow_spec *) ib_spec)->size;
3320	}
3321	if (cmd.flow_attr.size || (i != flow_attr->num_of_specs)) {
3322		pr_warn("create flow failed, flow %d: %u bytes left from uverb cmd\n",
3323			i, cmd.flow_attr.size);
3324		err = -EINVAL;
3325		goto err_free;
3326	}
3327
3328	flow_id = qp->device->ops.create_flow(qp, flow_attr,
3329					      &attrs->driver_udata);
3330
3331	if (IS_ERR(flow_id)) {
3332		err = PTR_ERR(flow_id);
3333		goto err_free;
3334	}
3335
3336	ib_set_flow(uobj, flow_id, qp, qp->device, uflow_res);
3337
3338	rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
3339				UVERBS_LOOKUP_READ);
3340	kfree(flow_attr);
3341
3342	if (cmd.flow_attr.num_of_specs)
3343		kfree(kern_flow_attr);
3344	uobj_finalize_uobj_create(uobj, attrs);
3345
3346	resp.flow_handle = uobj->id;
3347	return uverbs_response(attrs, &resp, sizeof(resp));
3348
3349err_free:
3350	ib_uverbs_flow_resources_free(uflow_res);
3351err_free_flow_attr:
3352	kfree(flow_attr);
3353err_put:
3354	rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
3355				UVERBS_LOOKUP_READ);
3356err_uobj:
3357	uobj_alloc_abort(uobj, attrs);
3358err_free_attr:
3359	if (cmd.flow_attr.num_of_specs)
3360		kfree(kern_flow_attr);
3361	return err;
3362}
3363
3364static int ib_uverbs_ex_destroy_flow(struct uverbs_attr_bundle *attrs)
3365{
3366	struct ib_uverbs_destroy_flow	cmd;
3367	int				ret;
3368
3369	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3370	if (ret)
3371		return ret;
3372
3373	if (cmd.comp_mask)
3374		return -EINVAL;
3375
3376	return uobj_perform_destroy(UVERBS_OBJECT_FLOW, cmd.flow_handle, attrs);
3377}
3378
3379static int __uverbs_create_xsrq(struct uverbs_attr_bundle *attrs,
3380				struct ib_uverbs_create_xsrq *cmd,
3381				struct ib_udata *udata)
3382{
3383	struct ib_uverbs_create_srq_resp resp = {};
3384	struct ib_usrq_object           *obj;
3385	struct ib_pd                    *pd;
3386	struct ib_srq                   *srq;
3387	struct ib_srq_init_attr          attr;
3388	int ret;
3389	struct ib_uobject *xrcd_uobj;
3390	struct ib_device *ib_dev;
3391
3392	obj = (struct ib_usrq_object *)uobj_alloc(UVERBS_OBJECT_SRQ, attrs,
3393						  &ib_dev);
3394	if (IS_ERR(obj))
3395		return PTR_ERR(obj);
3396
3397	if (cmd->srq_type == IB_SRQT_TM)
3398		attr.ext.tag_matching.max_num_tags = cmd->max_num_tags;
3399
3400	if (cmd->srq_type == IB_SRQT_XRC) {
3401		xrcd_uobj = uobj_get_read(UVERBS_OBJECT_XRCD, cmd->xrcd_handle,
3402					  attrs);
3403		if (IS_ERR(xrcd_uobj)) {
3404			ret = -EINVAL;
3405			goto err;
3406		}
3407
3408		attr.ext.xrc.xrcd = (struct ib_xrcd *)xrcd_uobj->object;
3409		if (!attr.ext.xrc.xrcd) {
3410			ret = -EINVAL;
3411			goto err_put_xrcd;
3412		}
3413
3414		obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
3415		atomic_inc(&obj->uxrcd->refcnt);
3416	}
3417
3418	if (ib_srq_has_cq(cmd->srq_type)) {
3419		attr.ext.cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ,
3420						cmd->cq_handle, attrs);
3421		if (!attr.ext.cq) {
3422			ret = -EINVAL;
3423			goto err_put_xrcd;
3424		}
3425	}
3426
3427	pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd->pd_handle, attrs);
3428	if (!pd) {
3429		ret = -EINVAL;
3430		goto err_put_cq;
3431	}
3432
3433	attr.event_handler  = ib_uverbs_srq_event_handler;
3434	attr.srq_type       = cmd->srq_type;
3435	attr.attr.max_wr    = cmd->max_wr;
3436	attr.attr.max_sge   = cmd->max_sge;
3437	attr.attr.srq_limit = cmd->srq_limit;
3438
3439	INIT_LIST_HEAD(&obj->uevent.event_list);
3440	obj->uevent.uobject.user_handle = cmd->user_handle;
3441
3442	srq = ib_create_srq_user(pd, &attr, obj, udata);
3443	if (IS_ERR(srq)) {
3444		ret = PTR_ERR(srq);
3445		goto err_put_pd;
3446	}
3447
3448	obj->uevent.uobject.object = srq;
3449	obj->uevent.uobject.user_handle = cmd->user_handle;
3450	obj->uevent.event_file = READ_ONCE(attrs->ufile->default_async_file);
3451	if (obj->uevent.event_file)
3452		uverbs_uobject_get(&obj->uevent.event_file->uobj);
3453
3454	if (cmd->srq_type == IB_SRQT_XRC)
3455		resp.srqn = srq->ext.xrc.srq_num;
3456
3457	if (cmd->srq_type == IB_SRQT_XRC)
3458		uobj_put_read(xrcd_uobj);
3459
3460	if (ib_srq_has_cq(cmd->srq_type))
3461		rdma_lookup_put_uobject(&attr.ext.cq->uobject->uevent.uobject,
3462					UVERBS_LOOKUP_READ);
3463
3464	uobj_put_obj_read(pd);
3465	uobj_finalize_uobj_create(&obj->uevent.uobject, attrs);
3466
3467	resp.srq_handle = obj->uevent.uobject.id;
3468	resp.max_wr = attr.attr.max_wr;
3469	resp.max_sge = attr.attr.max_sge;
3470	return uverbs_response(attrs, &resp, sizeof(resp));
3471
3472err_put_pd:
3473	uobj_put_obj_read(pd);
3474err_put_cq:
3475	if (ib_srq_has_cq(cmd->srq_type))
3476		rdma_lookup_put_uobject(&attr.ext.cq->uobject->uevent.uobject,
3477					UVERBS_LOOKUP_READ);
3478
3479err_put_xrcd:
3480	if (cmd->srq_type == IB_SRQT_XRC) {
3481		atomic_dec(&obj->uxrcd->refcnt);
3482		uobj_put_read(xrcd_uobj);
3483	}
3484
3485err:
3486	uobj_alloc_abort(&obj->uevent.uobject, attrs);
3487	return ret;
3488}
3489
3490static int ib_uverbs_create_srq(struct uverbs_attr_bundle *attrs)
3491{
3492	struct ib_uverbs_create_srq      cmd;
3493	struct ib_uverbs_create_xsrq     xcmd;
3494	int ret;
3495
3496	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3497	if (ret)
3498		return ret;
3499
3500	memset(&xcmd, 0, sizeof(xcmd));
3501	xcmd.response	 = cmd.response;
3502	xcmd.user_handle = cmd.user_handle;
3503	xcmd.srq_type	 = IB_SRQT_BASIC;
3504	xcmd.pd_handle	 = cmd.pd_handle;
3505	xcmd.max_wr	 = cmd.max_wr;
3506	xcmd.max_sge	 = cmd.max_sge;
3507	xcmd.srq_limit	 = cmd.srq_limit;
3508
3509	return __uverbs_create_xsrq(attrs, &xcmd, &attrs->driver_udata);
3510}
3511
3512static int ib_uverbs_create_xsrq(struct uverbs_attr_bundle *attrs)
3513{
3514	struct ib_uverbs_create_xsrq     cmd;
3515	int ret;
3516
3517	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3518	if (ret)
3519		return ret;
3520
3521	return __uverbs_create_xsrq(attrs, &cmd, &attrs->driver_udata);
3522}
3523
3524static int ib_uverbs_modify_srq(struct uverbs_attr_bundle *attrs)
3525{
3526	struct ib_uverbs_modify_srq cmd;
 
3527	struct ib_srq              *srq;
3528	struct ib_srq_attr          attr;
3529	int                         ret;
3530
3531	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3532	if (ret)
3533		return ret;
 
 
3534
3535	srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, attrs);
3536	if (!srq)
3537		return -EINVAL;
3538
3539	attr.max_wr    = cmd.max_wr;
3540	attr.srq_limit = cmd.srq_limit;
3541
3542	ret = srq->device->ops.modify_srq(srq, &attr, cmd.attr_mask,
3543					  &attrs->driver_udata);
3544
3545	rdma_lookup_put_uobject(&srq->uobject->uevent.uobject,
3546				UVERBS_LOOKUP_READ);
3547
3548	return ret;
3549}
3550
3551static int ib_uverbs_query_srq(struct uverbs_attr_bundle *attrs)
 
 
3552{
3553	struct ib_uverbs_query_srq      cmd;
3554	struct ib_uverbs_query_srq_resp resp;
3555	struct ib_srq_attr              attr;
3556	struct ib_srq                   *srq;
3557	int                             ret;
3558
3559	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3560	if (ret)
3561		return ret;
 
 
3562
3563	srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, attrs);
3564	if (!srq)
3565		return -EINVAL;
3566
3567	ret = ib_query_srq(srq, &attr);
3568
3569	rdma_lookup_put_uobject(&srq->uobject->uevent.uobject,
3570				UVERBS_LOOKUP_READ);
3571
3572	if (ret)
3573		return ret;
3574
3575	memset(&resp, 0, sizeof resp);
3576
3577	resp.max_wr    = attr.max_wr;
3578	resp.max_sge   = attr.max_sge;
3579	resp.srq_limit = attr.srq_limit;
3580
3581	return uverbs_response(attrs, &resp, sizeof(resp));
 
 
 
 
3582}
3583
3584static int ib_uverbs_destroy_srq(struct uverbs_attr_bundle *attrs)
 
 
3585{
3586	struct ib_uverbs_destroy_srq      cmd;
3587	struct ib_uverbs_destroy_srq_resp resp;
3588	struct ib_uobject		 *uobj;
 
3589	struct ib_uevent_object        	 *obj;
3590	int ret;
3591
3592	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3593	if (ret)
3594		return ret;
3595
3596	uobj = uobj_get_destroy(UVERBS_OBJECT_SRQ, cmd.srq_handle, attrs);
3597	if (IS_ERR(uobj))
3598		return PTR_ERR(uobj);
3599
 
 
 
 
3600	obj = container_of(uobj, struct ib_uevent_object, uobject);
3601	memset(&resp, 0, sizeof(resp));
3602	resp.events_reported = obj->events_reported;
3603
3604	uobj_put_destroy(uobj);
3605
3606	return uverbs_response(attrs, &resp, sizeof(resp));
3607}
3608
3609static int ib_uverbs_ex_query_device(struct uverbs_attr_bundle *attrs)
3610{
3611	struct ib_uverbs_ex_query_device_resp resp = {};
3612	struct ib_uverbs_ex_query_device  cmd;
3613	struct ib_device_attr attr = {0};
3614	struct ib_ucontext *ucontext;
3615	struct ib_device *ib_dev;
3616	int err;
3617
3618	ucontext = ib_uverbs_get_ucontext(attrs);
3619	if (IS_ERR(ucontext))
3620		return PTR_ERR(ucontext);
3621	ib_dev = ucontext->device;
3622
3623	err = uverbs_request(attrs, &cmd, sizeof(cmd));
3624	if (err)
3625		return err;
3626
3627	if (cmd.comp_mask)
3628		return -EINVAL;
3629
3630	if (cmd.reserved)
3631		return -EINVAL;
3632
3633	err = ib_dev->ops.query_device(ib_dev, &attr, &attrs->driver_udata);
3634	if (err)
3635		return err;
3636
3637	copy_query_dev_fields(ucontext, &resp.base, &attr);
3638
3639	resp.odp_caps.general_caps = attr.odp_caps.general_caps;
3640	resp.odp_caps.per_transport_caps.rc_odp_caps =
3641		attr.odp_caps.per_transport_caps.rc_odp_caps;
3642	resp.odp_caps.per_transport_caps.uc_odp_caps =
3643		attr.odp_caps.per_transport_caps.uc_odp_caps;
3644	resp.odp_caps.per_transport_caps.ud_odp_caps =
3645		attr.odp_caps.per_transport_caps.ud_odp_caps;
3646	resp.xrc_odp_caps = attr.odp_caps.per_transport_caps.xrc_odp_caps;
3647
3648	resp.timestamp_mask = attr.timestamp_mask;
3649	resp.hca_core_clock = attr.hca_core_clock;
3650	resp.device_cap_flags_ex = attr.device_cap_flags;
3651	resp.rss_caps.supported_qpts = attr.rss_caps.supported_qpts;
3652	resp.rss_caps.max_rwq_indirection_tables =
3653		attr.rss_caps.max_rwq_indirection_tables;
3654	resp.rss_caps.max_rwq_indirection_table_size =
3655		attr.rss_caps.max_rwq_indirection_table_size;
3656	resp.max_wq_type_rq = attr.max_wq_type_rq;
3657	resp.raw_packet_caps = attr.raw_packet_caps;
3658	resp.tm_caps.max_rndv_hdr_size	= attr.tm_caps.max_rndv_hdr_size;
3659	resp.tm_caps.max_num_tags	= attr.tm_caps.max_num_tags;
3660	resp.tm_caps.max_ops		= attr.tm_caps.max_ops;
3661	resp.tm_caps.max_sge		= attr.tm_caps.max_sge;
3662	resp.tm_caps.flags		= attr.tm_caps.flags;
3663	resp.cq_moderation_caps.max_cq_moderation_count  =
3664		attr.cq_caps.max_cq_moderation_count;
3665	resp.cq_moderation_caps.max_cq_moderation_period =
3666		attr.cq_caps.max_cq_moderation_period;
3667	resp.max_dm_size = attr.max_dm_size;
3668	resp.response_length = uverbs_response_length(attrs, sizeof(resp));
3669
3670	return uverbs_response(attrs, &resp, sizeof(resp));
3671}
3672
3673static int ib_uverbs_ex_modify_cq(struct uverbs_attr_bundle *attrs)
3674{
3675	struct ib_uverbs_ex_modify_cq cmd;
3676	struct ib_cq *cq;
3677	int ret;
3678
3679	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3680	if (ret)
3681		return ret;
3682
3683	if (!cmd.attr_mask || cmd.reserved)
3684		return -EINVAL;
3685
3686	if (cmd.attr_mask > IB_CQ_MODERATE)
3687		return -EOPNOTSUPP;
 
3688
3689	cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
3690	if (!cq)
3691		return -EINVAL;
3692
3693	ret = rdma_set_cq_moderation(cq, cmd.attr.cq_count, cmd.attr.cq_period);
 
3694
3695	rdma_lookup_put_uobject(&cq->uobject->uevent.uobject,
3696				UVERBS_LOOKUP_READ);
3697	return ret;
3698}
3699
3700/*
3701 * Describe the input structs for write(). Some write methods have an input
3702 * only struct, most have an input and output. If the struct has an output then
3703 * the 'response' u64 must be the first field in the request structure.
3704 *
3705 * If udata is present then both the request and response structs have a
3706 * trailing driver_data flex array. In this case the size of the base struct
3707 * cannot be changed.
3708 */
3709#define UAPI_DEF_WRITE_IO(req, resp)                                           \
3710	.write.has_resp = 1 +                                                  \
3711			  BUILD_BUG_ON_ZERO(offsetof(req, response) != 0) +    \
3712			  BUILD_BUG_ON_ZERO(sizeof_field(req, response) !=    \
3713					    sizeof(u64)),                      \
3714	.write.req_size = sizeof(req), .write.resp_size = sizeof(resp)
3715
3716#define UAPI_DEF_WRITE_I(req) .write.req_size = sizeof(req)
3717
3718#define UAPI_DEF_WRITE_UDATA_IO(req, resp)                                     \
3719	UAPI_DEF_WRITE_IO(req, resp),                                          \
3720		.write.has_udata =                                             \
3721			1 +                                                    \
3722			BUILD_BUG_ON_ZERO(offsetof(req, driver_data) !=        \
3723					  sizeof(req)) +                       \
3724			BUILD_BUG_ON_ZERO(offsetof(resp, driver_data) !=       \
3725					  sizeof(resp))
3726
3727#define UAPI_DEF_WRITE_UDATA_I(req)                                            \
3728	UAPI_DEF_WRITE_I(req),                                                 \
3729		.write.has_udata =                                             \
3730			1 + BUILD_BUG_ON_ZERO(offsetof(req, driver_data) !=    \
3731					      sizeof(req))
3732
3733/*
3734 * The _EX versions are for use with WRITE_EX and allow the last struct member
3735 * to be specified. Buffers that do not include that member will be rejected.
3736 */
3737#define UAPI_DEF_WRITE_IO_EX(req, req_last_member, resp, resp_last_member)     \
3738	.write.has_resp = 1,                                                   \
3739	.write.req_size = offsetofend(req, req_last_member),                   \
3740	.write.resp_size = offsetofend(resp, resp_last_member)
3741
3742#define UAPI_DEF_WRITE_I_EX(req, req_last_member)                              \
3743	.write.req_size = offsetofend(req, req_last_member)
3744
3745const struct uapi_definition uverbs_def_write_intf[] = {
3746	DECLARE_UVERBS_OBJECT(
3747		UVERBS_OBJECT_AH,
3748		DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_AH,
3749				     ib_uverbs_create_ah,
3750				     UAPI_DEF_WRITE_UDATA_IO(
3751					     struct ib_uverbs_create_ah,
3752					     struct ib_uverbs_create_ah_resp)),
3753		DECLARE_UVERBS_WRITE(
3754			IB_USER_VERBS_CMD_DESTROY_AH,
3755			ib_uverbs_destroy_ah,
3756			UAPI_DEF_WRITE_I(struct ib_uverbs_destroy_ah)),
3757		UAPI_DEF_OBJ_NEEDS_FN(create_user_ah),
3758		UAPI_DEF_OBJ_NEEDS_FN(destroy_ah)),
3759
3760	DECLARE_UVERBS_OBJECT(
3761		UVERBS_OBJECT_COMP_CHANNEL,
3762		DECLARE_UVERBS_WRITE(
3763			IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL,
3764			ib_uverbs_create_comp_channel,
3765			UAPI_DEF_WRITE_IO(
3766				struct ib_uverbs_create_comp_channel,
3767				struct ib_uverbs_create_comp_channel_resp))),
3768
3769	DECLARE_UVERBS_OBJECT(
3770		UVERBS_OBJECT_CQ,
3771		DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_CQ,
3772				     ib_uverbs_create_cq,
3773				     UAPI_DEF_WRITE_UDATA_IO(
3774					     struct ib_uverbs_create_cq,
3775					     struct ib_uverbs_create_cq_resp),
3776				     UAPI_DEF_METHOD_NEEDS_FN(create_cq)),
3777		DECLARE_UVERBS_WRITE(
3778			IB_USER_VERBS_CMD_DESTROY_CQ,
3779			ib_uverbs_destroy_cq,
3780			UAPI_DEF_WRITE_IO(struct ib_uverbs_destroy_cq,
3781					  struct ib_uverbs_destroy_cq_resp),
3782			UAPI_DEF_METHOD_NEEDS_FN(destroy_cq)),
3783		DECLARE_UVERBS_WRITE(
3784			IB_USER_VERBS_CMD_POLL_CQ,
3785			ib_uverbs_poll_cq,
3786			UAPI_DEF_WRITE_IO(struct ib_uverbs_poll_cq,
3787					  struct ib_uverbs_poll_cq_resp),
3788			UAPI_DEF_METHOD_NEEDS_FN(poll_cq)),
3789		DECLARE_UVERBS_WRITE(
3790			IB_USER_VERBS_CMD_REQ_NOTIFY_CQ,
3791			ib_uverbs_req_notify_cq,
3792			UAPI_DEF_WRITE_I(struct ib_uverbs_req_notify_cq),
3793			UAPI_DEF_METHOD_NEEDS_FN(req_notify_cq)),
3794		DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_RESIZE_CQ,
3795				     ib_uverbs_resize_cq,
3796				     UAPI_DEF_WRITE_UDATA_IO(
3797					     struct ib_uverbs_resize_cq,
3798					     struct ib_uverbs_resize_cq_resp),
3799				     UAPI_DEF_METHOD_NEEDS_FN(resize_cq)),
3800		DECLARE_UVERBS_WRITE_EX(
3801			IB_USER_VERBS_EX_CMD_CREATE_CQ,
3802			ib_uverbs_ex_create_cq,
3803			UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_create_cq,
3804					     reserved,
3805					     struct ib_uverbs_ex_create_cq_resp,
3806					     response_length),
3807			UAPI_DEF_METHOD_NEEDS_FN(create_cq)),
3808		DECLARE_UVERBS_WRITE_EX(
3809			IB_USER_VERBS_EX_CMD_MODIFY_CQ,
3810			ib_uverbs_ex_modify_cq,
3811			UAPI_DEF_WRITE_I(struct ib_uverbs_ex_modify_cq),
3812			UAPI_DEF_METHOD_NEEDS_FN(modify_cq))),
3813
3814	DECLARE_UVERBS_OBJECT(
3815		UVERBS_OBJECT_DEVICE,
3816		DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_GET_CONTEXT,
3817				     ib_uverbs_get_context,
3818				     UAPI_DEF_WRITE_UDATA_IO(
3819					     struct ib_uverbs_get_context,
3820					     struct ib_uverbs_get_context_resp)),
3821		DECLARE_UVERBS_WRITE(
3822			IB_USER_VERBS_CMD_QUERY_DEVICE,
3823			ib_uverbs_query_device,
3824			UAPI_DEF_WRITE_IO(struct ib_uverbs_query_device,
3825					  struct ib_uverbs_query_device_resp)),
3826		DECLARE_UVERBS_WRITE(
3827			IB_USER_VERBS_CMD_QUERY_PORT,
3828			ib_uverbs_query_port,
3829			UAPI_DEF_WRITE_IO(struct ib_uverbs_query_port,
3830					  struct ib_uverbs_query_port_resp),
3831			UAPI_DEF_METHOD_NEEDS_FN(query_port)),
3832		DECLARE_UVERBS_WRITE_EX(
3833			IB_USER_VERBS_EX_CMD_QUERY_DEVICE,
3834			ib_uverbs_ex_query_device,
3835			UAPI_DEF_WRITE_IO_EX(
3836				struct ib_uverbs_ex_query_device,
3837				reserved,
3838				struct ib_uverbs_ex_query_device_resp,
3839				response_length),
3840			UAPI_DEF_METHOD_NEEDS_FN(query_device)),
3841		UAPI_DEF_OBJ_NEEDS_FN(alloc_ucontext),
3842		UAPI_DEF_OBJ_NEEDS_FN(dealloc_ucontext)),
3843
3844	DECLARE_UVERBS_OBJECT(
3845		UVERBS_OBJECT_FLOW,
3846		DECLARE_UVERBS_WRITE_EX(
3847			IB_USER_VERBS_EX_CMD_CREATE_FLOW,
3848			ib_uverbs_ex_create_flow,
3849			UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_create_flow,
3850					     flow_attr,
3851					     struct ib_uverbs_create_flow_resp,
3852					     flow_handle),
3853			UAPI_DEF_METHOD_NEEDS_FN(create_flow)),
3854		DECLARE_UVERBS_WRITE_EX(
3855			IB_USER_VERBS_EX_CMD_DESTROY_FLOW,
3856			ib_uverbs_ex_destroy_flow,
3857			UAPI_DEF_WRITE_I(struct ib_uverbs_destroy_flow),
3858			UAPI_DEF_METHOD_NEEDS_FN(destroy_flow))),
3859
3860	DECLARE_UVERBS_OBJECT(
3861		UVERBS_OBJECT_MR,
3862		DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_DEREG_MR,
3863				     ib_uverbs_dereg_mr,
3864				     UAPI_DEF_WRITE_I(struct ib_uverbs_dereg_mr),
3865				     UAPI_DEF_METHOD_NEEDS_FN(dereg_mr)),
3866		DECLARE_UVERBS_WRITE(
3867			IB_USER_VERBS_CMD_REG_MR,
3868			ib_uverbs_reg_mr,
3869			UAPI_DEF_WRITE_UDATA_IO(struct ib_uverbs_reg_mr,
3870						struct ib_uverbs_reg_mr_resp),
3871			UAPI_DEF_METHOD_NEEDS_FN(reg_user_mr)),
3872		DECLARE_UVERBS_WRITE(
3873			IB_USER_VERBS_CMD_REREG_MR,
3874			ib_uverbs_rereg_mr,
3875			UAPI_DEF_WRITE_UDATA_IO(struct ib_uverbs_rereg_mr,
3876						struct ib_uverbs_rereg_mr_resp),
3877			UAPI_DEF_METHOD_NEEDS_FN(rereg_user_mr))),
3878
3879	DECLARE_UVERBS_OBJECT(
3880		UVERBS_OBJECT_MW,
3881		DECLARE_UVERBS_WRITE(
3882			IB_USER_VERBS_CMD_ALLOC_MW,
3883			ib_uverbs_alloc_mw,
3884			UAPI_DEF_WRITE_UDATA_IO(struct ib_uverbs_alloc_mw,
3885						struct ib_uverbs_alloc_mw_resp),
3886			UAPI_DEF_METHOD_NEEDS_FN(alloc_mw)),
3887		DECLARE_UVERBS_WRITE(
3888			IB_USER_VERBS_CMD_DEALLOC_MW,
3889			ib_uverbs_dealloc_mw,
3890			UAPI_DEF_WRITE_I(struct ib_uverbs_dealloc_mw),
3891			UAPI_DEF_METHOD_NEEDS_FN(dealloc_mw))),
3892
3893	DECLARE_UVERBS_OBJECT(
3894		UVERBS_OBJECT_PD,
3895		DECLARE_UVERBS_WRITE(
3896			IB_USER_VERBS_CMD_ALLOC_PD,
3897			ib_uverbs_alloc_pd,
3898			UAPI_DEF_WRITE_UDATA_IO(struct ib_uverbs_alloc_pd,
3899						struct ib_uverbs_alloc_pd_resp),
3900			UAPI_DEF_METHOD_NEEDS_FN(alloc_pd)),
3901		DECLARE_UVERBS_WRITE(
3902			IB_USER_VERBS_CMD_DEALLOC_PD,
3903			ib_uverbs_dealloc_pd,
3904			UAPI_DEF_WRITE_I(struct ib_uverbs_dealloc_pd),
3905			UAPI_DEF_METHOD_NEEDS_FN(dealloc_pd))),
3906
3907	DECLARE_UVERBS_OBJECT(
3908		UVERBS_OBJECT_QP,
3909		DECLARE_UVERBS_WRITE(
3910			IB_USER_VERBS_CMD_ATTACH_MCAST,
3911			ib_uverbs_attach_mcast,
3912			UAPI_DEF_WRITE_I(struct ib_uverbs_attach_mcast),
3913			UAPI_DEF_METHOD_NEEDS_FN(attach_mcast),
3914			UAPI_DEF_METHOD_NEEDS_FN(detach_mcast)),
3915		DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_QP,
3916				     ib_uverbs_create_qp,
3917				     UAPI_DEF_WRITE_UDATA_IO(
3918					     struct ib_uverbs_create_qp,
3919					     struct ib_uverbs_create_qp_resp),
3920				     UAPI_DEF_METHOD_NEEDS_FN(create_qp)),
3921		DECLARE_UVERBS_WRITE(
3922			IB_USER_VERBS_CMD_DESTROY_QP,
3923			ib_uverbs_destroy_qp,
3924			UAPI_DEF_WRITE_IO(struct ib_uverbs_destroy_qp,
3925					  struct ib_uverbs_destroy_qp_resp),
3926			UAPI_DEF_METHOD_NEEDS_FN(destroy_qp)),
3927		DECLARE_UVERBS_WRITE(
3928			IB_USER_VERBS_CMD_DETACH_MCAST,
3929			ib_uverbs_detach_mcast,
3930			UAPI_DEF_WRITE_I(struct ib_uverbs_detach_mcast),
3931			UAPI_DEF_METHOD_NEEDS_FN(detach_mcast)),
3932		DECLARE_UVERBS_WRITE(
3933			IB_USER_VERBS_CMD_MODIFY_QP,
3934			ib_uverbs_modify_qp,
3935			UAPI_DEF_WRITE_I(struct ib_uverbs_modify_qp),
3936			UAPI_DEF_METHOD_NEEDS_FN(modify_qp)),
3937		DECLARE_UVERBS_WRITE(
3938			IB_USER_VERBS_CMD_POST_RECV,
3939			ib_uverbs_post_recv,
3940			UAPI_DEF_WRITE_IO(struct ib_uverbs_post_recv,
3941					  struct ib_uverbs_post_recv_resp),
3942			UAPI_DEF_METHOD_NEEDS_FN(post_recv)),
3943		DECLARE_UVERBS_WRITE(
3944			IB_USER_VERBS_CMD_POST_SEND,
3945			ib_uverbs_post_send,
3946			UAPI_DEF_WRITE_IO(struct ib_uverbs_post_send,
3947					  struct ib_uverbs_post_send_resp),
3948			UAPI_DEF_METHOD_NEEDS_FN(post_send)),
3949		DECLARE_UVERBS_WRITE(
3950			IB_USER_VERBS_CMD_QUERY_QP,
3951			ib_uverbs_query_qp,
3952			UAPI_DEF_WRITE_IO(struct ib_uverbs_query_qp,
3953					  struct ib_uverbs_query_qp_resp),
3954			UAPI_DEF_METHOD_NEEDS_FN(query_qp)),
3955		DECLARE_UVERBS_WRITE_EX(
3956			IB_USER_VERBS_EX_CMD_CREATE_QP,
3957			ib_uverbs_ex_create_qp,
3958			UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_create_qp,
3959					     comp_mask,
3960					     struct ib_uverbs_ex_create_qp_resp,
3961					     response_length),
3962			UAPI_DEF_METHOD_NEEDS_FN(create_qp)),
3963		DECLARE_UVERBS_WRITE_EX(
3964			IB_USER_VERBS_EX_CMD_MODIFY_QP,
3965			ib_uverbs_ex_modify_qp,
3966			UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_modify_qp,
3967					     base,
3968					     struct ib_uverbs_ex_modify_qp_resp,
3969					     response_length),
3970			UAPI_DEF_METHOD_NEEDS_FN(modify_qp))),
3971
3972	DECLARE_UVERBS_OBJECT(
3973		UVERBS_OBJECT_RWQ_IND_TBL,
3974		DECLARE_UVERBS_WRITE_EX(
3975			IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL,
3976			ib_uverbs_ex_create_rwq_ind_table,
3977			UAPI_DEF_WRITE_IO_EX(
3978				struct ib_uverbs_ex_create_rwq_ind_table,
3979				log_ind_tbl_size,
3980				struct ib_uverbs_ex_create_rwq_ind_table_resp,
3981				ind_tbl_num),
3982			UAPI_DEF_METHOD_NEEDS_FN(create_rwq_ind_table)),
3983		DECLARE_UVERBS_WRITE_EX(
3984			IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL,
3985			ib_uverbs_ex_destroy_rwq_ind_table,
3986			UAPI_DEF_WRITE_I(
3987				struct ib_uverbs_ex_destroy_rwq_ind_table),
3988			UAPI_DEF_METHOD_NEEDS_FN(destroy_rwq_ind_table))),
3989
3990	DECLARE_UVERBS_OBJECT(
3991		UVERBS_OBJECT_WQ,
3992		DECLARE_UVERBS_WRITE_EX(
3993			IB_USER_VERBS_EX_CMD_CREATE_WQ,
3994			ib_uverbs_ex_create_wq,
3995			UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_create_wq,
3996					     max_sge,
3997					     struct ib_uverbs_ex_create_wq_resp,
3998					     wqn),
3999			UAPI_DEF_METHOD_NEEDS_FN(create_wq)),
4000		DECLARE_UVERBS_WRITE_EX(
4001			IB_USER_VERBS_EX_CMD_DESTROY_WQ,
4002			ib_uverbs_ex_destroy_wq,
4003			UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_destroy_wq,
4004					     wq_handle,
4005					     struct ib_uverbs_ex_destroy_wq_resp,
4006					     reserved),
4007			UAPI_DEF_METHOD_NEEDS_FN(destroy_wq)),
4008		DECLARE_UVERBS_WRITE_EX(
4009			IB_USER_VERBS_EX_CMD_MODIFY_WQ,
4010			ib_uverbs_ex_modify_wq,
4011			UAPI_DEF_WRITE_I_EX(struct ib_uverbs_ex_modify_wq,
4012					    curr_wq_state),
4013			UAPI_DEF_METHOD_NEEDS_FN(modify_wq))),
4014
4015	DECLARE_UVERBS_OBJECT(
4016		UVERBS_OBJECT_SRQ,
4017		DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_SRQ,
4018				     ib_uverbs_create_srq,
4019				     UAPI_DEF_WRITE_UDATA_IO(
4020					     struct ib_uverbs_create_srq,
4021					     struct ib_uverbs_create_srq_resp),
4022				     UAPI_DEF_METHOD_NEEDS_FN(create_srq)),
4023		DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_XSRQ,
4024				     ib_uverbs_create_xsrq,
4025				     UAPI_DEF_WRITE_UDATA_IO(
4026					     struct ib_uverbs_create_xsrq,
4027					     struct ib_uverbs_create_srq_resp),
4028				     UAPI_DEF_METHOD_NEEDS_FN(create_srq)),
4029		DECLARE_UVERBS_WRITE(
4030			IB_USER_VERBS_CMD_DESTROY_SRQ,
4031			ib_uverbs_destroy_srq,
4032			UAPI_DEF_WRITE_IO(struct ib_uverbs_destroy_srq,
4033					  struct ib_uverbs_destroy_srq_resp),
4034			UAPI_DEF_METHOD_NEEDS_FN(destroy_srq)),
4035		DECLARE_UVERBS_WRITE(
4036			IB_USER_VERBS_CMD_MODIFY_SRQ,
4037			ib_uverbs_modify_srq,
4038			UAPI_DEF_WRITE_UDATA_I(struct ib_uverbs_modify_srq),
4039			UAPI_DEF_METHOD_NEEDS_FN(modify_srq)),
4040		DECLARE_UVERBS_WRITE(
4041			IB_USER_VERBS_CMD_POST_SRQ_RECV,
4042			ib_uverbs_post_srq_recv,
4043			UAPI_DEF_WRITE_IO(struct ib_uverbs_post_srq_recv,
4044					  struct ib_uverbs_post_srq_recv_resp),
4045			UAPI_DEF_METHOD_NEEDS_FN(post_srq_recv)),
4046		DECLARE_UVERBS_WRITE(
4047			IB_USER_VERBS_CMD_QUERY_SRQ,
4048			ib_uverbs_query_srq,
4049			UAPI_DEF_WRITE_IO(struct ib_uverbs_query_srq,
4050					  struct ib_uverbs_query_srq_resp),
4051			UAPI_DEF_METHOD_NEEDS_FN(query_srq))),
4052
4053	DECLARE_UVERBS_OBJECT(
4054		UVERBS_OBJECT_XRCD,
4055		DECLARE_UVERBS_WRITE(
4056			IB_USER_VERBS_CMD_CLOSE_XRCD,
4057			ib_uverbs_close_xrcd,
4058			UAPI_DEF_WRITE_I(struct ib_uverbs_close_xrcd)),
4059		DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_OPEN_QP,
4060				     ib_uverbs_open_qp,
4061				     UAPI_DEF_WRITE_UDATA_IO(
4062					     struct ib_uverbs_open_qp,
4063					     struct ib_uverbs_create_qp_resp)),
4064		DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_OPEN_XRCD,
4065				     ib_uverbs_open_xrcd,
4066				     UAPI_DEF_WRITE_UDATA_IO(
4067					     struct ib_uverbs_open_xrcd,
4068					     struct ib_uverbs_open_xrcd_resp)),
4069		UAPI_DEF_OBJ_NEEDS_FN(alloc_xrcd),
4070		UAPI_DEF_OBJ_NEEDS_FN(dealloc_xrcd)),
4071
4072	{},
4073};