Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
0002 /*
0003  * Copyright (c) 2017, Mellanox Technologies inc.  All rights reserved.
0004  */
0005 
0006 #ifndef _UVERBS_TYPES_
0007 #define _UVERBS_TYPES_
0008 
0009 #include <linux/kernel.h>
0010 #include <rdma/ib_verbs.h>
0011 
0012 struct uverbs_obj_type;
0013 struct uverbs_api_object;
0014 
0015 enum rdma_lookup_mode {
0016     UVERBS_LOOKUP_READ,
0017     UVERBS_LOOKUP_WRITE,
0018     /*
0019      * Destroy is like LOOKUP_WRITE, except that the uobject is not
0020      * locked.  uobj_destroy is used to convert a LOOKUP_DESTROY lock into
0021      * a LOOKUP_WRITE lock.
0022      */
0023     UVERBS_LOOKUP_DESTROY,
0024 };
0025 
0026 /*
0027  * The following sequences are valid:
0028  * Success flow:
0029  *   alloc_begin
0030  *   alloc_commit
0031  *    [..]
0032  * Access flow:
0033  *   lookup_get(exclusive=false) & uverbs_try_lock_object
0034  *   lookup_put(exclusive=false) via rdma_lookup_put_uobject
0035  * Destruction flow:
0036  *   lookup_get(exclusive=true) & uverbs_try_lock_object
0037  *   remove_commit
0038  *   remove_handle (optional)
0039  *   lookup_put(exclusive=true) via rdma_lookup_put_uobject
0040  *
0041  * Allocate Error flow #1
0042  *   alloc_begin
0043  *   alloc_abort
0044  * Allocate Error flow #2
0045  *   alloc_begin
0046  *   remove_commit
0047  *   alloc_abort
0048  * Allocate Error flow #3
0049  *   alloc_begin
0050  *   alloc_commit (fails)
0051  *   remove_commit
0052  *   alloc_abort
0053  *
0054  * In all cases the caller must hold the ufile kref until alloc_commit or
0055  * alloc_abort returns.
0056  */
0057 struct uverbs_obj_type_class {
0058     struct ib_uobject *(*alloc_begin)(const struct uverbs_api_object *obj,
0059                       struct uverbs_attr_bundle *attrs);
0060     /* This consumes the kref on uobj */
0061     void (*alloc_commit)(struct ib_uobject *uobj);
0062     /* This does not consume the kref on uobj */
0063     void (*alloc_abort)(struct ib_uobject *uobj);
0064 
0065     struct ib_uobject *(*lookup_get)(const struct uverbs_api_object *obj,
0066                      struct ib_uverbs_file *ufile, s64 id,
0067                      enum rdma_lookup_mode mode);
0068     void (*lookup_put)(struct ib_uobject *uobj, enum rdma_lookup_mode mode);
0069     /* This does not consume the kref on uobj */
0070     int __must_check (*destroy_hw)(struct ib_uobject *uobj,
0071                        enum rdma_remove_reason why,
0072                        struct uverbs_attr_bundle *attrs);
0073     void (*remove_handle)(struct ib_uobject *uobj);
0074     void (*swap_uobjects)(struct ib_uobject *obj_old,
0075                   struct ib_uobject *obj_new);
0076 };
0077 
0078 struct uverbs_obj_type {
0079     const struct uverbs_obj_type_class * const type_class;
0080     size_t       obj_size;
0081 };
0082 
0083 /*
0084  * Objects type classes which support a detach state (object is still alive but
0085  * it's not attached to any context need to make sure:
0086  * (a) no call through to a driver after a detach is called
0087  * (b) detach isn't called concurrently with context_cleanup
0088  */
0089 
0090 struct uverbs_obj_idr_type {
0091     /*
0092      * In idr based objects, uverbs_obj_type_class points to a generic
0093      * idr operations. In order to specialize the underlying types (e.g. CQ,
0094      * QPs, etc.), we add destroy_object specific callbacks.
0095      */
0096     struct uverbs_obj_type  type;
0097 
0098     /* Free driver resources from the uobject, make the driver uncallable,
0099      * and move the uobject to the detached state. If the object was
0100      * destroyed by the user's request, a failure should leave the uobject
0101      * completely unchanged.
0102      */
0103     int __must_check (*destroy_object)(struct ib_uobject *uobj,
0104                        enum rdma_remove_reason why,
0105                        struct uverbs_attr_bundle *attrs);
0106 };
0107 
0108 struct ib_uobject *rdma_lookup_get_uobject(const struct uverbs_api_object *obj,
0109                        struct ib_uverbs_file *ufile, s64 id,
0110                        enum rdma_lookup_mode mode,
0111                        struct uverbs_attr_bundle *attrs);
0112 void rdma_lookup_put_uobject(struct ib_uobject *uobj,
0113                  enum rdma_lookup_mode mode);
0114 struct ib_uobject *rdma_alloc_begin_uobject(const struct uverbs_api_object *obj,
0115                         struct uverbs_attr_bundle *attrs);
0116 void rdma_alloc_abort_uobject(struct ib_uobject *uobj,
0117                   struct uverbs_attr_bundle *attrs,
0118                   bool hw_obj_valid);
0119 void rdma_alloc_commit_uobject(struct ib_uobject *uobj,
0120                    struct uverbs_attr_bundle *attrs);
0121 void rdma_assign_uobject(struct ib_uobject *to_uobj,
0122              struct ib_uobject *new_uobj,
0123              struct uverbs_attr_bundle *attrs);
0124 
0125 /*
0126  * uverbs_uobject_get is called in order to increase the reference count on
0127  * an uobject. This is useful when a handler wants to keep the uobject's memory
0128  * alive, regardless if this uobject is still alive in the context's objects
0129  * repository. Objects are put via uverbs_uobject_put.
0130  */
0131 static inline void uverbs_uobject_get(struct ib_uobject *uobject)
0132 {
0133     kref_get(&uobject->ref);
0134 }
0135 void uverbs_uobject_put(struct ib_uobject *uobject);
0136 
0137 struct uverbs_obj_fd_type {
0138     /*
0139      * In fd based objects, uverbs_obj_type_ops points to generic
0140      * fd operations. In order to specialize the underlying types (e.g.
0141      * completion_channel), we use fops, name and flags for fd creation.
0142      * destroy_object is called when the uobject is to be destroyed,
0143      * because the driver is removed or the FD is closed.
0144      */
0145     struct uverbs_obj_type  type;
0146     void (*destroy_object)(struct ib_uobject *uobj,
0147                    enum rdma_remove_reason why);
0148     const struct file_operations    *fops;
0149     const char          *name;
0150     int             flags;
0151 };
0152 
0153 extern const struct uverbs_obj_type_class uverbs_idr_class;
0154 extern const struct uverbs_obj_type_class uverbs_fd_class;
0155 int uverbs_uobject_fd_release(struct inode *inode, struct file *filp);
0156 
0157 #define UVERBS_BUILD_BUG_ON(cond) (sizeof(char[1 - 2 * !!(cond)]) - \
0158                    sizeof(char))
0159 #define UVERBS_TYPE_ALLOC_FD(_obj_size, _destroy_object, _fops, _name, _flags) \
0160     ((&((const struct uverbs_obj_fd_type)               \
0161      {.type = {                         \
0162         .type_class = &uverbs_fd_class,             \
0163         .obj_size = (_obj_size) +               \
0164             UVERBS_BUILD_BUG_ON((_obj_size) <               \
0165                         sizeof(struct ib_uobject)), \
0166      },                             \
0167      .destroy_object = _destroy_object,             \
0168      .fops = _fops,                         \
0169      .name = _name,                         \
0170      .flags = _flags}))->type)
0171 #define UVERBS_TYPE_ALLOC_IDR_SZ(_size, _destroy_object)    \
0172     ((&((const struct uverbs_obj_idr_type)              \
0173      {.type = {                         \
0174         .type_class = &uverbs_idr_class,            \
0175         .obj_size = (_size) +                   \
0176             UVERBS_BUILD_BUG_ON((_size) <           \
0177                         sizeof(struct ib_uobject))  \
0178      },                             \
0179      .destroy_object = _destroy_object,}))->type)
0180 #define UVERBS_TYPE_ALLOC_IDR(_destroy_object)          \
0181      UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_uobject),    \
0182                   _destroy_object)
0183 
0184 #endif