root/drivers/infiniband/core/uverbs_cmd.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. uverbs_response
  2. uverbs_request
  3. uverbs_response_length
  4. uverbs_request_start
  5. uverbs_request_next
  6. uverbs_request_next_ptr
  7. uverbs_request_finish
  8. uverbs_get_cleared_udata
  9. _ib_uverbs_lookup_comp_file
  10. ib_uverbs_get_context
  11. copy_query_dev_fields
  12. ib_uverbs_query_device
  13. ib_uverbs_query_port
  14. ib_uverbs_alloc_pd
  15. ib_uverbs_dealloc_pd
  16. xrcd_table_insert
  17. xrcd_table_search
  18. find_xrcd
  19. xrcd_table_delete
  20. ib_uverbs_open_xrcd
  21. ib_uverbs_close_xrcd
  22. ib_uverbs_dealloc_xrcd
  23. ib_uverbs_reg_mr
  24. ib_uverbs_rereg_mr
  25. ib_uverbs_dereg_mr
  26. ib_uverbs_alloc_mw
  27. ib_uverbs_dealloc_mw
  28. ib_uverbs_create_comp_channel
  29. create_cq
  30. ib_uverbs_create_cq
  31. ib_uverbs_ex_create_cq
  32. ib_uverbs_resize_cq
  33. copy_wc_to_user
  34. ib_uverbs_poll_cq
  35. ib_uverbs_req_notify_cq
  36. ib_uverbs_destroy_cq
  37. create_qp
  38. ib_uverbs_create_qp
  39. ib_uverbs_ex_create_qp
  40. ib_uverbs_open_qp
  41. copy_ah_attr_to_uverbs
  42. ib_uverbs_query_qp
  43. modify_qp_mask
  44. copy_ah_attr_from_uverbs
  45. modify_qp
  46. ib_uverbs_modify_qp
  47. ib_uverbs_ex_modify_qp
  48. ib_uverbs_destroy_qp
  49. alloc_wr
  50. ib_uverbs_post_send
  51. ib_uverbs_unmarshall_recv
  52. ib_uverbs_post_recv
  53. ib_uverbs_post_srq_recv
  54. ib_uverbs_create_ah
  55. ib_uverbs_destroy_ah
  56. ib_uverbs_attach_mcast
  57. ib_uverbs_detach_mcast
  58. flow_resources_alloc
  59. ib_uverbs_flow_resources_free
  60. flow_resources_add
  61. kern_spec_to_ib_spec_action
  62. spec_filter_size
  63. ib_uverbs_kern_spec_to_ib_spec_filter
  64. kern_spec_to_ib_spec_filter
  65. kern_spec_to_ib_spec
  66. ib_uverbs_ex_create_wq
  67. ib_uverbs_ex_destroy_wq
  68. ib_uverbs_ex_modify_wq
  69. ib_uverbs_ex_create_rwq_ind_table
  70. ib_uverbs_ex_destroy_rwq_ind_table
  71. ib_uverbs_ex_create_flow
  72. ib_uverbs_ex_destroy_flow
  73. __uverbs_create_xsrq
  74. ib_uverbs_create_srq
  75. ib_uverbs_create_xsrq
  76. ib_uverbs_modify_srq
  77. ib_uverbs_query_srq
  78. ib_uverbs_destroy_srq
  79. ib_uverbs_ex_query_device
  80. ib_uverbs_ex_modify_cq

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

/* [<][>][^][v][top][bottom][index][help] */