Commit | Line | Data |
---|---|---|
6bf9d8f6 | 1 | /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */ |
6be60aed MB |
2 | /* |
3 | * Copyright (c) 2017, Mellanox Technologies inc. All rights reserved. | |
6be60aed MB |
4 | */ |
5 | ||
6 | #ifndef _UVERBS_STD_TYPES__ | |
7 | #define _UVERBS_STD_TYPES__ | |
8 | ||
9 | #include <rdma/uverbs_types.h> | |
52427112 | 10 | #include <rdma/uverbs_ioctl.h> |
64b19e13 | 11 | #include <rdma/ib_user_ioctl_verbs.h> |
09e3ebf8 | 12 | |
1250c304 JG |
13 | /* Returns _id, or causes a compile error if _id is not a u32. |
14 | * | |
15 | * The uobj APIs should only be used with the write based uAPI to access | |
16 | * object IDs. The write API must use a u32 for the object handle, which is | |
17 | * checked by this macro. | |
18 | */ | |
19 | #define _uobj_check_id(_id) ((_id) * typecheck(u32, _id)) | |
fd3c7904 | 20 | |
8313c10f JG |
21 | #define uobj_get_type(_attrs, _object) \ |
22 | uapi_get_object((_attrs)->ufile->device->uapi, _object) | |
fd3c7904 | 23 | |
8313c10f | 24 | #define uobj_get_read(_type, _id, _attrs) \ |
70f06b26 SR |
25 | rdma_lookup_get_uobject(uobj_get_type(_attrs, _type), (_attrs)->ufile, \ |
26 | _uobj_check_id(_id), UVERBS_LOOKUP_READ, \ | |
27 | _attrs) | |
fd3c7904 | 28 | |
8313c10f JG |
29 | #define ufd_get_read(_type, _fdnum, _attrs) \ |
30 | rdma_lookup_get_uobject(uobj_get_type(_attrs, _type), (_attrs)->ufile, \ | |
9867f5c6 | 31 | (_fdnum)*typecheck(s32, _fdnum), \ |
70f06b26 | 32 | UVERBS_LOOKUP_READ, _attrs) |
2cc1e3b8 | 33 | |
1250c304 JG |
34 | static inline void *_uobj_get_obj_read(struct ib_uobject *uobj) |
35 | { | |
2cc1e3b8 JG |
36 | if (IS_ERR(uobj)) |
37 | return NULL; | |
38 | return uobj->object; | |
39 | } | |
8313c10f | 40 | #define uobj_get_obj_read(_object, _type, _id, _attrs) \ |
1250c304 | 41 | ((struct ib_##_object *)_uobj_get_obj_read( \ |
8313c10f | 42 | uobj_get_read(_type, _id, _attrs))) |
fd3c7904 | 43 | |
8313c10f | 44 | #define uobj_get_write(_type, _id, _attrs) \ |
70f06b26 SR |
45 | rdma_lookup_get_uobject(uobj_get_type(_attrs, _type), (_attrs)->ufile, \ |
46 | _uobj_check_id(_id), UVERBS_LOOKUP_WRITE, \ | |
47 | _attrs) | |
fd3c7904 | 48 | |
6b0d08f4 | 49 | int __uobj_perform_destroy(const struct uverbs_api_object *obj, u32 id, |
70f06b26 | 50 | struct uverbs_attr_bundle *attrs); |
7106a976 | 51 | #define uobj_perform_destroy(_type, _id, _attrs) \ |
8313c10f | 52 | __uobj_perform_destroy(uobj_get_type(_attrs, _type), \ |
7106a976 | 53 | _uobj_check_id(_id), _attrs) |
c33e73af | 54 | |
6b0d08f4 | 55 | struct ib_uobject *__uobj_get_destroy(const struct uverbs_api_object *obj, |
70f06b26 | 56 | u32 id, struct uverbs_attr_bundle *attrs); |
32ed5c00 | 57 | |
8313c10f JG |
58 | #define uobj_get_destroy(_type, _id, _attrs) \ |
59 | __uobj_get_destroy(uobj_get_type(_attrs, _type), _uobj_check_id(_id), \ | |
60 | _attrs) | |
32ed5c00 JG |
61 | |
62 | static inline void uobj_put_destroy(struct ib_uobject *uobj) | |
63 | { | |
c85f4abe | 64 | rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_DESTROY); |
32ed5c00 JG |
65 | } |
66 | ||
fd3c7904 MB |
67 | static inline void uobj_put_read(struct ib_uobject *uobj) |
68 | { | |
9867f5c6 | 69 | rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_READ); |
fd3c7904 MB |
70 | } |
71 | ||
72 | #define uobj_put_obj_read(_obj) \ | |
73 | uobj_put_read((_obj)->uobject) | |
74 | ||
75 | static inline void uobj_put_write(struct ib_uobject *uobj) | |
76 | { | |
9867f5c6 | 77 | rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_WRITE); |
fd3c7904 MB |
78 | } |
79 | ||
a6a3797d SR |
80 | static inline void uobj_alloc_abort(struct ib_uobject *uobj, |
81 | struct uverbs_attr_bundle *attrs) | |
fd3c7904 | 82 | { |
0ac8903c | 83 | rdma_alloc_abort_uobject(uobj, attrs, false); |
fd3c7904 MB |
84 | } |
85 | ||
0f63ef1d LR |
86 | static inline void uobj_finalize_uobj_create(struct ib_uobject *uobj, |
87 | struct uverbs_attr_bundle *attrs) | |
88 | { | |
89 | /* | |
90 | * Tell the core code that the write() handler has completed | |
91 | * initializing the object and that the core should commit or | |
92 | * abort this object based upon the return code from the write() | |
93 | * method. Similar to what uverbs_finalize_uobj_create() does for | |
94 | * ioctl() | |
95 | */ | |
96 | WARN_ON(attrs->uobject); | |
97 | attrs->uobject = uobj; | |
98 | } | |
99 | ||
6b0d08f4 | 100 | static inline struct ib_uobject * |
8313c10f JG |
101 | __uobj_alloc(const struct uverbs_api_object *obj, |
102 | struct uverbs_attr_bundle *attrs, struct ib_device **ib_dev) | |
fd3c7904 | 103 | { |
39e83af8 | 104 | struct ib_uobject *uobj = rdma_alloc_begin_uobject(obj, attrs); |
bbd51e88 | 105 | |
70f06b26 | 106 | if (!IS_ERR(uobj)) |
feec576a | 107 | *ib_dev = attrs->context->device; |
bbd51e88 | 108 | return uobj; |
fd3c7904 MB |
109 | } |
110 | ||
8313c10f JG |
111 | #define uobj_alloc(_type, _attrs, _ib_dev) \ |
112 | __uobj_alloc(uobj_get_type(_attrs, _type), _attrs, _ib_dev) | |
fd3c7904 | 113 | |
841eefc5 MB |
114 | static inline void uverbs_flow_action_fill_action(struct ib_flow_action *action, |
115 | struct ib_uobject *uobj, | |
116 | struct ib_device *ib_dev, | |
117 | enum ib_flow_action_type type) | |
118 | { | |
119 | atomic_set(&action->usecnt, 0); | |
120 | action->device = ib_dev; | |
121 | action->type = type; | |
122 | action->uobject = uobj; | |
123 | uobj->object = action; | |
124 | } | |
125 | ||
86e1d464 MB |
126 | struct ib_uflow_resources { |
127 | size_t max; | |
128 | size_t num; | |
129 | size_t collection_num; | |
130 | size_t counters_num; | |
131 | struct ib_counters **counters; | |
132 | struct ib_flow_action **collection; | |
133 | }; | |
134 | ||
135 | struct ib_uflow_object { | |
136 | struct ib_uobject uobject; | |
137 | struct ib_uflow_resources *resources; | |
138 | }; | |
139 | ||
fa76d24e MB |
140 | struct ib_uflow_resources *flow_resources_alloc(size_t num_specs); |
141 | void flow_resources_add(struct ib_uflow_resources *uflow_res, | |
142 | enum ib_flow_spec_type type, | |
143 | void *ibobj); | |
144 | void ib_uverbs_flow_resources_free(struct ib_uflow_resources *uflow_res); | |
145 | ||
86e1d464 MB |
146 | static inline void ib_set_flow(struct ib_uobject *uobj, struct ib_flow *ibflow, |
147 | struct ib_qp *qp, struct ib_device *device, | |
148 | struct ib_uflow_resources *uflow_res) | |
149 | { | |
150 | struct ib_uflow_object *uflow; | |
151 | ||
152 | uobj->object = ibflow; | |
153 | ibflow->uobject = uobj; | |
154 | ||
155 | if (qp) { | |
156 | atomic_inc(&qp->usecnt); | |
157 | ibflow->qp = qp; | |
158 | } | |
159 | ||
160 | ibflow->device = device; | |
161 | uflow = container_of(uobj, typeof(*uflow), uobject); | |
162 | uflow->resources = uflow_res; | |
163 | } | |
164 | ||
04ca16cc YH |
165 | struct uverbs_api_object { |
166 | const struct uverbs_obj_type *type_attrs; | |
167 | const struct uverbs_obj_type_class *type_class; | |
168 | u8 disabled:1; | |
169 | u32 id; | |
170 | }; | |
171 | ||
172 | static inline u32 uobj_get_object_id(struct ib_uobject *uobj) | |
173 | { | |
174 | return uobj->uapi_object->id; | |
175 | } | |
176 | ||
6be60aed MB |
177 | #endif |
178 |