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