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