1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
3 * Copyright (c) 2017, Mellanox Technologies inc. All rights reserved.
6 #ifndef _UVERBS_STD_TYPES__
7 #define _UVERBS_STD_TYPES__
9 #include <rdma/uverbs_types.h>
10 #include <rdma/uverbs_ioctl.h>
11 #include <rdma/ib_user_ioctl_verbs.h>
13 /* Returns _id, or causes a compile error if _id is not a u32.
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.
19 #define _uobj_check_id(_id) ((_id) * typecheck(u32, _id))
21 #define uobj_get_type(_attrs, _object) \
22 uapi_get_object((_attrs)->ufile->device->uapi, _object)
24 #define uobj_get_read(_type, _id, _attrs) \
25 rdma_lookup_get_uobject(uobj_get_type(_attrs, _type), (_attrs)->ufile, \
26 _uobj_check_id(_id), UVERBS_LOOKUP_READ, \
29 #define ufd_get_read(_type, _fdnum, _attrs) \
30 rdma_lookup_get_uobject(uobj_get_type(_attrs, _type), (_attrs)->ufile, \
31 (_fdnum)*typecheck(s32, _fdnum), \
32 UVERBS_LOOKUP_READ, _attrs)
34 static inline void *_uobj_get_obj_read(struct ib_uobject
*uobj
)
40 #define uobj_get_obj_read(_object, _type, _id, _attrs) \
41 ((struct ib_##_object *)_uobj_get_obj_read( \
42 uobj_get_read(_type, _id, _attrs)))
44 #define uobj_get_write(_type, _id, _attrs) \
45 rdma_lookup_get_uobject(uobj_get_type(_attrs, _type), (_attrs)->ufile, \
46 _uobj_check_id(_id), UVERBS_LOOKUP_WRITE, \
49 int __uobj_perform_destroy(const struct uverbs_api_object
*obj
, u32 id
,
50 struct uverbs_attr_bundle
*attrs
);
51 #define uobj_perform_destroy(_type, _id, _attrs) \
52 __uobj_perform_destroy(uobj_get_type(_attrs, _type), \
53 _uobj_check_id(_id), _attrs)
55 struct ib_uobject
*__uobj_get_destroy(const struct uverbs_api_object
*obj
,
56 u32 id
, struct uverbs_attr_bundle
*attrs
);
58 #define uobj_get_destroy(_type, _id, _attrs) \
59 __uobj_get_destroy(uobj_get_type(_attrs, _type), _uobj_check_id(_id), \
62 static inline void uobj_put_destroy(struct ib_uobject
*uobj
)
64 rdma_lookup_put_uobject(uobj
, UVERBS_LOOKUP_DESTROY
);
67 static inline void uobj_put_read(struct ib_uobject
*uobj
)
69 rdma_lookup_put_uobject(uobj
, UVERBS_LOOKUP_READ
);
72 #define uobj_put_obj_read(_obj) \
73 uobj_put_read((_obj)->uobject)
75 static inline void uobj_put_write(struct ib_uobject
*uobj
)
77 rdma_lookup_put_uobject(uobj
, UVERBS_LOOKUP_WRITE
);
80 static inline void uobj_alloc_abort(struct ib_uobject
*uobj
,
81 struct uverbs_attr_bundle
*attrs
)
83 rdma_alloc_abort_uobject(uobj
, attrs
, false);
86 static inline void uobj_finalize_uobj_create(struct ib_uobject
*uobj
,
87 struct uverbs_attr_bundle
*attrs
)
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
96 WARN_ON(attrs
->uobject
);
97 attrs
->uobject
= uobj
;
100 static inline struct ib_uobject
*
101 __uobj_alloc(const struct uverbs_api_object
*obj
,
102 struct uverbs_attr_bundle
*attrs
, struct ib_device
**ib_dev
)
104 struct ib_uobject
*uobj
= rdma_alloc_begin_uobject(obj
, attrs
);
107 *ib_dev
= attrs
->context
->device
;
111 #define uobj_alloc(_type, _attrs, _ib_dev) \
112 __uobj_alloc(uobj_get_type(_attrs, _type), _attrs, _ib_dev)
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
)
119 atomic_set(&action
->usecnt
, 0);
120 action
->device
= ib_dev
;
122 action
->uobject
= uobj
;
123 uobj
->object
= action
;
126 struct ib_uflow_resources
{
129 size_t collection_num
;
131 struct ib_counters
**counters
;
132 struct ib_flow_action
**collection
;
135 struct ib_uflow_object
{
136 struct ib_uobject uobject
;
137 struct ib_uflow_resources
*resources
;
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
,
144 void ib_uverbs_flow_resources_free(struct ib_uflow_resources
*uflow_res
);
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
)
150 struct ib_uflow_object
*uflow
;
152 uobj
->object
= ibflow
;
153 ibflow
->uobject
= uobj
;
156 atomic_inc(&qp
->usecnt
);
160 ibflow
->device
= device
;
161 uflow
= container_of(uobj
, typeof(*uflow
), uobject
);
162 uflow
->resources
= uflow_res
;
165 struct uverbs_api_object
{
166 const struct uverbs_obj_type
*type_attrs
;
167 const struct uverbs_obj_type_class
*type_class
;
172 static inline u32
uobj_get_object_id(struct ib_uobject
*uobj
)
174 return uobj
->uapi_object
->id
;