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