1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
3 * Copyright (c) 2017, Mellanox Technologies inc. All rights reserved.
9 #include <linux/kernel.h>
10 #include <rdma/ib_verbs.h>
12 struct uverbs_obj_type
;
13 struct uverbs_api_object
;
15 enum rdma_lookup_mode
{
19 * Destroy is like LOOKUP_WRITE, except that the uobject is not
20 * locked. uobj_destroy is used to convert a LOOKUP_DESTROY lock into
21 * a LOOKUP_WRITE lock.
23 UVERBS_LOOKUP_DESTROY
,
27 * The following sequences are valid:
33 * lookup_get(exclusive=false) & uverbs_try_lock_object
34 * lookup_put(exclusive=false) via rdma_lookup_put_uobject
36 * lookup_get(exclusive=true) & uverbs_try_lock_object
38 * remove_handle (optional)
39 * lookup_put(exclusive=true) via rdma_lookup_put_uobject
41 * Allocate Error flow #1
44 * Allocate Error flow #2
48 * Allocate Error flow #3
50 * alloc_commit (fails)
54 * In all cases the caller must hold the ufile kref until alloc_commit or
55 * alloc_abort returns.
57 struct uverbs_obj_type_class
{
58 struct ib_uobject
*(*alloc_begin
)(const struct uverbs_api_object
*obj
,
59 struct uverbs_attr_bundle
*attrs
);
60 /* This consumes the kref on uobj */
61 void (*alloc_commit
)(struct ib_uobject
*uobj
);
62 /* This does not consume the kref on uobj */
63 void (*alloc_abort
)(struct ib_uobject
*uobj
);
65 struct ib_uobject
*(*lookup_get
)(const struct uverbs_api_object
*obj
,
66 struct ib_uverbs_file
*ufile
, s64 id
,
67 enum rdma_lookup_mode mode
);
68 void (*lookup_put
)(struct ib_uobject
*uobj
, enum rdma_lookup_mode mode
);
69 /* This does not consume the kref on uobj */
70 int __must_check (*destroy_hw
)(struct ib_uobject
*uobj
,
71 enum rdma_remove_reason why
,
72 struct uverbs_attr_bundle
*attrs
);
73 void (*remove_handle
)(struct ib_uobject
*uobj
);
76 struct uverbs_obj_type
{
77 const struct uverbs_obj_type_class
* const type_class
;
82 * Objects type classes which support a detach state (object is still alive but
83 * it's not attached to any context need to make sure:
84 * (a) no call through to a driver after a detach is called
85 * (b) detach isn't called concurrently with context_cleanup
88 struct uverbs_obj_idr_type
{
90 * In idr based objects, uverbs_obj_type_class points to a generic
91 * idr operations. In order to specialize the underlying types (e.g. CQ,
92 * QPs, etc.), we add destroy_object specific callbacks.
94 struct uverbs_obj_type type
;
96 /* Free driver resources from the uobject, make the driver uncallable,
97 * and move the uobject to the detached state. If the object was
98 * destroyed by the user's request, a failure should leave the uobject
99 * completely unchanged.
101 int __must_check (*destroy_object
)(struct ib_uobject
*uobj
,
102 enum rdma_remove_reason why
,
103 struct uverbs_attr_bundle
*attrs
);
106 struct ib_uobject
*rdma_lookup_get_uobject(const struct uverbs_api_object
*obj
,
107 struct ib_uverbs_file
*ufile
, s64 id
,
108 enum rdma_lookup_mode mode
,
109 struct uverbs_attr_bundle
*attrs
);
110 void rdma_lookup_put_uobject(struct ib_uobject
*uobj
,
111 enum rdma_lookup_mode mode
);
112 struct ib_uobject
*rdma_alloc_begin_uobject(const struct uverbs_api_object
*obj
,
113 struct uverbs_attr_bundle
*attrs
);
114 void rdma_alloc_abort_uobject(struct ib_uobject
*uobj
,
115 struct uverbs_attr_bundle
*attrs
,
117 void rdma_alloc_commit_uobject(struct ib_uobject
*uobj
,
118 struct uverbs_attr_bundle
*attrs
);
121 * uverbs_uobject_get is called in order to increase the reference count on
122 * an uobject. This is useful when a handler wants to keep the uobject's memory
123 * alive, regardless if this uobject is still alive in the context's objects
124 * repository. Objects are put via uverbs_uobject_put.
126 static inline void uverbs_uobject_get(struct ib_uobject
*uobject
)
128 kref_get(&uobject
->ref
);
130 void uverbs_uobject_put(struct ib_uobject
*uobject
);
132 struct uverbs_obj_fd_type
{
134 * In fd based objects, uverbs_obj_type_ops points to generic
135 * fd operations. In order to specialize the underlying types (e.g.
136 * completion_channel), we use fops, name and flags for fd creation.
137 * destroy_object is called when the uobject is to be destroyed,
138 * because the driver is removed or the FD is closed.
140 struct uverbs_obj_type type
;
141 int (*destroy_object
)(struct ib_uobject
*uobj
,
142 enum rdma_remove_reason why
);
143 const struct file_operations
*fops
;
148 extern const struct uverbs_obj_type_class uverbs_idr_class
;
149 extern const struct uverbs_obj_type_class uverbs_fd_class
;
150 int uverbs_uobject_fd_release(struct inode
*inode
, struct file
*filp
);
152 #define UVERBS_BUILD_BUG_ON(cond) (sizeof(char[1 - 2 * !!(cond)]) - \
154 #define UVERBS_TYPE_ALLOC_FD(_obj_size, _destroy_object, _fops, _name, _flags) \
155 ((&((const struct uverbs_obj_fd_type) \
157 .type_class = &uverbs_fd_class, \
158 .obj_size = (_obj_size) + \
159 UVERBS_BUILD_BUG_ON((_obj_size) < \
160 sizeof(struct ib_uobject)), \
162 .destroy_object = _destroy_object, \
165 .flags = _flags}))->type)
166 #define UVERBS_TYPE_ALLOC_IDR_SZ(_size, _destroy_object) \
167 ((&((const struct uverbs_obj_idr_type) \
169 .type_class = &uverbs_idr_class, \
170 .obj_size = (_size) + \
171 UVERBS_BUILD_BUG_ON((_size) < \
172 sizeof(struct ib_uobject)) \
174 .destroy_object = _destroy_object,}))->type)
175 #define UVERBS_TYPE_ALLOC_IDR(_destroy_object) \
176 UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_uobject), \