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