2 * Copyright (c) 2017, Mellanox Technologies inc. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #ifndef _UVERBS_TYPES_
34 #define _UVERBS_TYPES_
36 #include <linux/kernel.h>
37 #include <rdma/ib_verbs.h>
39 struct uverbs_obj_type
;
40 struct uverbs_api_object
;
42 enum rdma_lookup_mode
{
46 * Destroy is like LOOKUP_WRITE, except that the uobject is not
47 * locked. uobj_destroy is used to convert a LOOKUP_DESTROY lock into
48 * a LOOKUP_WRITE lock.
50 UVERBS_LOOKUP_DESTROY
,
54 * The following sequences are valid:
60 * lookup_get(exclusive=false) & uverbs_try_lock_object
61 * lookup_put(exclusive=false) via rdma_lookup_put_uobject
63 * lookup_get(exclusive=true) & uverbs_try_lock_object
65 * remove_handle (optional)
66 * lookup_put(exclusive=true) via rdma_lookup_put_uobject
68 * Allocate Error flow #1
71 * Allocate Error flow #2
75 * Allocate Error flow #3
77 * alloc_commit (fails)
81 * In all cases the caller must hold the ufile kref until alloc_commit or
82 * alloc_abort returns.
84 struct uverbs_obj_type_class
{
85 struct ib_uobject
*(*alloc_begin
)(const struct uverbs_api_object
*obj
,
86 struct uverbs_attr_bundle
*attrs
);
87 /* This consumes the kref on uobj */
88 void (*alloc_commit
)(struct ib_uobject
*uobj
);
89 /* This does not consume the kref on uobj */
90 void (*alloc_abort
)(struct ib_uobject
*uobj
);
92 struct ib_uobject
*(*lookup_get
)(const struct uverbs_api_object
*obj
,
93 struct ib_uverbs_file
*ufile
, s64 id
,
94 enum rdma_lookup_mode mode
);
95 void (*lookup_put
)(struct ib_uobject
*uobj
, enum rdma_lookup_mode mode
);
96 /* This does not consume the kref on uobj */
97 int __must_check (*destroy_hw
)(struct ib_uobject
*uobj
,
98 enum rdma_remove_reason why
,
99 struct uverbs_attr_bundle
*attrs
);
100 void (*remove_handle
)(struct ib_uobject
*uobj
);
103 struct uverbs_obj_type
{
104 const struct uverbs_obj_type_class
* const type_class
;
109 * Objects type classes which support a detach state (object is still alive but
110 * it's not attached to any context need to make sure:
111 * (a) no call through to a driver after a detach is called
112 * (b) detach isn't called concurrently with context_cleanup
115 struct uverbs_obj_idr_type
{
117 * In idr based objects, uverbs_obj_type_class points to a generic
118 * idr operations. In order to specialize the underlying types (e.g. CQ,
119 * QPs, etc.), we add destroy_object specific callbacks.
121 struct uverbs_obj_type type
;
123 /* Free driver resources from the uobject, make the driver uncallable,
124 * and move the uobject to the detached state. If the object was
125 * destroyed by the user's request, a failure should leave the uobject
126 * completely unchanged.
128 int __must_check (*destroy_object
)(struct ib_uobject
*uobj
,
129 enum rdma_remove_reason why
,
130 struct uverbs_attr_bundle
*attrs
);
133 struct ib_uobject
*rdma_lookup_get_uobject(const struct uverbs_api_object
*obj
,
134 struct ib_uverbs_file
*ufile
, s64 id
,
135 enum rdma_lookup_mode mode
,
136 struct uverbs_attr_bundle
*attrs
);
137 void rdma_lookup_put_uobject(struct ib_uobject
*uobj
,
138 enum rdma_lookup_mode mode
);
139 struct ib_uobject
*rdma_alloc_begin_uobject(const struct uverbs_api_object
*obj
,
140 struct uverbs_attr_bundle
*attrs
);
141 void rdma_alloc_abort_uobject(struct ib_uobject
*uobj
,
142 struct uverbs_attr_bundle
*attrs
);
143 void rdma_alloc_commit_uobject(struct ib_uobject
*uobj
,
144 struct uverbs_attr_bundle
*attrs
);
147 * uverbs_uobject_get is called in order to increase the reference count on
148 * an uobject. This is useful when a handler wants to keep the uobject's memory
149 * alive, regardless if this uobject is still alive in the context's objects
150 * repository. Objects are put via uverbs_uobject_put.
152 static inline void uverbs_uobject_get(struct ib_uobject
*uobject
)
154 kref_get(&uobject
->ref
);
156 void uverbs_uobject_put(struct ib_uobject
*uobject
);
158 struct uverbs_obj_fd_type
{
160 * In fd based objects, uverbs_obj_type_ops points to generic
161 * fd operations. In order to specialize the underlying types (e.g.
162 * completion_channel), we use fops, name and flags for fd creation.
163 * destroy_object is called when the uobject is to be destroyed,
164 * because the driver is removed or the FD is closed.
166 struct uverbs_obj_type type
;
167 int (*destroy_object
)(struct ib_uobject
*uobj
,
168 enum rdma_remove_reason why
);
169 const struct file_operations
*fops
;
174 extern const struct uverbs_obj_type_class uverbs_idr_class
;
175 extern const struct uverbs_obj_type_class uverbs_fd_class
;
176 int uverbs_uobject_fd_release(struct inode
*inode
, struct file
*filp
);
178 #define UVERBS_BUILD_BUG_ON(cond) (sizeof(char[1 - 2 * !!(cond)]) - \
180 #define UVERBS_TYPE_ALLOC_FD(_obj_size, _destroy_object, _fops, _name, _flags) \
181 ((&((const struct uverbs_obj_fd_type) \
183 .type_class = &uverbs_fd_class, \
184 .obj_size = (_obj_size) + \
185 UVERBS_BUILD_BUG_ON((_obj_size) < \
186 sizeof(struct ib_uobject)), \
188 .destroy_object = _destroy_object, \
191 .flags = _flags}))->type)
192 #define UVERBS_TYPE_ALLOC_IDR_SZ(_size, _destroy_object) \
193 ((&((const struct uverbs_obj_idr_type) \
195 .type_class = &uverbs_idr_class, \
196 .obj_size = (_size) + \
197 UVERBS_BUILD_BUG_ON((_size) < \
198 sizeof(struct ib_uobject)) \
200 .destroy_object = _destroy_object,}))->type)
201 #define UVERBS_TYPE_ALLOC_IDR(_destroy_object) \
202 UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_uobject), \