16be60aedSMatan Barak /*
26be60aedSMatan Barak  * Copyright (c) 2017, Mellanox Technologies inc.  All rights reserved.
36be60aedSMatan Barak  *
46be60aedSMatan Barak  * This software is available to you under a choice of one of two
56be60aedSMatan Barak  * licenses.  You may choose to be licensed under the terms of the GNU
66be60aedSMatan Barak  * General Public License (GPL) Version 2, available from the file
76be60aedSMatan Barak  * COPYING in the main directory of this source tree, or the
86be60aedSMatan Barak  * OpenIB.org BSD license below:
96be60aedSMatan Barak  *
106be60aedSMatan Barak  *     Redistribution and use in source and binary forms, with or
116be60aedSMatan Barak  *     without modification, are permitted provided that the following
126be60aedSMatan Barak  *     conditions are met:
136be60aedSMatan Barak  *
146be60aedSMatan Barak  *      - Redistributions of source code must retain the above
156be60aedSMatan Barak  *        copyright notice, this list of conditions and the following
166be60aedSMatan Barak  *        disclaimer.
176be60aedSMatan Barak  *
186be60aedSMatan Barak  *      - Redistributions in binary form must reproduce the above
196be60aedSMatan Barak  *        copyright notice, this list of conditions and the following
206be60aedSMatan Barak  *        disclaimer in the documentation and/or other materials
216be60aedSMatan Barak  *        provided with the distribution.
226be60aedSMatan Barak  *
236be60aedSMatan Barak  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
246be60aedSMatan Barak  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
256be60aedSMatan Barak  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
266be60aedSMatan Barak  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
276be60aedSMatan Barak  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
286be60aedSMatan Barak  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
296be60aedSMatan Barak  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
306be60aedSMatan Barak  * SOFTWARE.
316be60aedSMatan Barak  */
326be60aedSMatan Barak 
336be60aedSMatan Barak #ifndef _UVERBS_STD_TYPES__
346be60aedSMatan Barak #define _UVERBS_STD_TYPES__
356be60aedSMatan Barak 
366be60aedSMatan Barak #include <rdma/uverbs_types.h>
3752427112SMatan Barak #include <rdma/uverbs_ioctl.h>
3864b19e13SMatan Barak #include <rdma/ib_user_ioctl_verbs.h>
3909e3ebf8SMatan Barak 
40dfb13955SMatan Barak #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
41dfb13955SMatan Barak const struct uverbs_object_tree_def *uverbs_default_get_objects(void);
4252427112SMatan Barak #else
4352427112SMatan Barak static inline const struct uverbs_object_tree_def *uverbs_default_get_objects(void)
4452427112SMatan Barak {
4552427112SMatan Barak 	return NULL;
4652427112SMatan Barak }
4752427112SMatan Barak #endif
4852427112SMatan Barak 
491250c304SJason Gunthorpe /* Returns _id, or causes a compile error if _id is not a u32.
501250c304SJason Gunthorpe  *
511250c304SJason Gunthorpe  * The uobj APIs should only be used with the write based uAPI to access
521250c304SJason Gunthorpe  * object IDs. The write API must use a u32 for the object handle, which is
531250c304SJason Gunthorpe  * checked by this macro.
541250c304SJason Gunthorpe  */
551250c304SJason Gunthorpe #define _uobj_check_id(_id) ((_id) * typecheck(u32, _id))
56fd3c7904SMatan Barak 
576b0d08f4SJason Gunthorpe #define uobj_get_type(_ufile, _object)                                         \
586b0d08f4SJason Gunthorpe 	uapi_get_object((_ufile)->device->uapi, _object)
59fd3c7904SMatan Barak 
602cc1e3b8SJason Gunthorpe #define uobj_get_read(_type, _id, _ufile)                                      \
616b0d08f4SJason Gunthorpe 	rdma_lookup_get_uobject(uobj_get_type(_ufile, _type), _ufile,          \
629867f5c6SJason Gunthorpe 				_uobj_check_id(_id), UVERBS_LOOKUP_READ)
63fd3c7904SMatan Barak 
641250c304SJason Gunthorpe #define ufd_get_read(_type, _fdnum, _ufile)                                    \
656b0d08f4SJason Gunthorpe 	rdma_lookup_get_uobject(uobj_get_type(_ufile, _type), _ufile,          \
669867f5c6SJason Gunthorpe 				(_fdnum)*typecheck(s32, _fdnum),               \
679867f5c6SJason Gunthorpe 				UVERBS_LOOKUP_READ)
681250c304SJason Gunthorpe 
691250c304SJason Gunthorpe static inline void *_uobj_get_obj_read(struct ib_uobject *uobj)
702cc1e3b8SJason Gunthorpe {
712cc1e3b8SJason Gunthorpe 	if (IS_ERR(uobj))
722cc1e3b8SJason Gunthorpe 		return NULL;
732cc1e3b8SJason Gunthorpe 	return uobj->object;
742cc1e3b8SJason Gunthorpe }
752cc1e3b8SJason Gunthorpe #define uobj_get_obj_read(_object, _type, _id, _ufile)                         \
761250c304SJason Gunthorpe 	((struct ib_##_object *)_uobj_get_obj_read(                            \
771250c304SJason Gunthorpe 		uobj_get_read(_type, _id, _ufile)))
782cc1e3b8SJason Gunthorpe 
792cc1e3b8SJason Gunthorpe #define uobj_get_write(_type, _id, _ufile)                                     \
806b0d08f4SJason Gunthorpe 	rdma_lookup_get_uobject(uobj_get_type(_ufile, _type), _ufile,          \
819867f5c6SJason Gunthorpe 				_uobj_check_id(_id), UVERBS_LOOKUP_WRITE)
82fd3c7904SMatan Barak 
836b0d08f4SJason Gunthorpe int __uobj_perform_destroy(const struct uverbs_api_object *obj, u32 id,
84c33e73afSJason Gunthorpe 			   struct ib_uverbs_file *ufile, int success_res);
85c33e73afSJason Gunthorpe #define uobj_perform_destroy(_type, _id, _ufile, _success_res)                 \
866b0d08f4SJason Gunthorpe 	__uobj_perform_destroy(uobj_get_type(_ufile, _type),                   \
876b0d08f4SJason Gunthorpe 			       _uobj_check_id(_id), _ufile, _success_res)
88c33e73afSJason Gunthorpe 
896b0d08f4SJason Gunthorpe struct ib_uobject *__uobj_get_destroy(const struct uverbs_api_object *obj,
9032ed5c00SJason Gunthorpe 				      u32 id, struct ib_uverbs_file *ufile);
9132ed5c00SJason Gunthorpe 
9232ed5c00SJason Gunthorpe #define uobj_get_destroy(_type, _id, _ufile)                                   \
936b0d08f4SJason Gunthorpe 	__uobj_get_destroy(uobj_get_type(_ufile, _type), _uobj_check_id(_id),  \
946b0d08f4SJason Gunthorpe 			   _ufile)
9532ed5c00SJason Gunthorpe 
9632ed5c00SJason Gunthorpe static inline void uobj_put_destroy(struct ib_uobject *uobj)
9732ed5c00SJason Gunthorpe {
989867f5c6SJason Gunthorpe 	rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_WRITE);
9932ed5c00SJason Gunthorpe }
10032ed5c00SJason Gunthorpe 
101fd3c7904SMatan Barak static inline void uobj_put_read(struct ib_uobject *uobj)
102fd3c7904SMatan Barak {
1039867f5c6SJason Gunthorpe 	rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_READ);
104fd3c7904SMatan Barak }
105fd3c7904SMatan Barak 
106fd3c7904SMatan Barak #define uobj_put_obj_read(_obj)					\
107fd3c7904SMatan Barak 	uobj_put_read((_obj)->uobject)
108fd3c7904SMatan Barak 
109fd3c7904SMatan Barak static inline void uobj_put_write(struct ib_uobject *uobj)
110fd3c7904SMatan Barak {
1119867f5c6SJason Gunthorpe 	rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_WRITE);
112fd3c7904SMatan Barak }
113fd3c7904SMatan Barak 
1142c96eb7dSJason Gunthorpe static inline int __must_check uobj_alloc_commit(struct ib_uobject *uobj,
1152c96eb7dSJason Gunthorpe 						 int success_res)
116fd3c7904SMatan Barak {
1172c96eb7dSJason Gunthorpe 	int ret = rdma_alloc_commit_uobject(uobj);
1182c96eb7dSJason Gunthorpe 
1192c96eb7dSJason Gunthorpe 	if (ret)
1202c96eb7dSJason Gunthorpe 		return ret;
1212c96eb7dSJason Gunthorpe 	return success_res;
122fd3c7904SMatan Barak }
123fd3c7904SMatan Barak 
124fd3c7904SMatan Barak static inline void uobj_alloc_abort(struct ib_uobject *uobj)
125fd3c7904SMatan Barak {
126fd3c7904SMatan Barak 	rdma_alloc_abort_uobject(uobj);
127fd3c7904SMatan Barak }
128fd3c7904SMatan Barak 
1296b0d08f4SJason Gunthorpe static inline struct ib_uobject *
1306b0d08f4SJason Gunthorpe __uobj_alloc(const struct uverbs_api_object *obj, struct ib_uverbs_file *ufile,
131bbd51e88SJason Gunthorpe 	     struct ib_device **ib_dev)
132fd3c7904SMatan Barak {
1336b0d08f4SJason Gunthorpe 	struct ib_uobject *uobj = rdma_alloc_begin_uobject(obj, ufile);
134bbd51e88SJason Gunthorpe 
135bbd51e88SJason Gunthorpe 	if (!IS_ERR(uobj))
136bbd51e88SJason Gunthorpe 		*ib_dev = uobj->context->device;
137bbd51e88SJason Gunthorpe 	return uobj;
138fd3c7904SMatan Barak }
139fd3c7904SMatan Barak 
140bbd51e88SJason Gunthorpe #define uobj_alloc(_type, _ufile, _ib_dev)                                     \
1416b0d08f4SJason Gunthorpe 	__uobj_alloc(uobj_get_type(_ufile, _type), _ufile, _ib_dev)
142fd3c7904SMatan Barak 
1436be60aedSMatan Barak #endif
1446be60aedSMatan Barak 
145