162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (c) 2018, Mellanox Technologies inc.  All rights reserved.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#ifndef _UVERBS_NAMED_IOCTL_
762306a36Sopenharmony_ci#define _UVERBS_NAMED_IOCTL_
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <rdma/uverbs_ioctl.h>
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#ifndef UVERBS_MODULE_NAME
1262306a36Sopenharmony_ci#error "Please #define UVERBS_MODULE_NAME before including rdma/uverbs_named_ioctl.h"
1362306a36Sopenharmony_ci#endif
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#define _UVERBS_PASTE(x, y)	x ## y
1662306a36Sopenharmony_ci#define _UVERBS_NAME(x, y)	_UVERBS_PASTE(x, y)
1762306a36Sopenharmony_ci#define UVERBS_METHOD(id)	_UVERBS_NAME(UVERBS_MODULE_NAME, _method_##id)
1862306a36Sopenharmony_ci#define UVERBS_HANDLER(id)	_UVERBS_NAME(UVERBS_MODULE_NAME, _handler_##id)
1962306a36Sopenharmony_ci#define UVERBS_OBJECT(id)	_UVERBS_NAME(UVERBS_MODULE_NAME, _object_##id)
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci/* These are static so they do not need to be qualified */
2262306a36Sopenharmony_ci#define UVERBS_METHOD_ATTRS(method_id) _method_attrs_##method_id
2362306a36Sopenharmony_ci#define UVERBS_OBJECT_METHODS(object_id) _UVERBS_NAME(_object_methods_##object_id, __LINE__)
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci#define DECLARE_UVERBS_NAMED_METHOD(_method_id, ...)                           \
2662306a36Sopenharmony_ci	static const struct uverbs_attr_def *const UVERBS_METHOD_ATTRS(        \
2762306a36Sopenharmony_ci		_method_id)[] = { __VA_ARGS__ };                               \
2862306a36Sopenharmony_ci	static const struct uverbs_method_def UVERBS_METHOD(_method_id) = {    \
2962306a36Sopenharmony_ci		.id = _method_id,                                              \
3062306a36Sopenharmony_ci		.handler = UVERBS_HANDLER(_method_id),                         \
3162306a36Sopenharmony_ci		.num_attrs = ARRAY_SIZE(UVERBS_METHOD_ATTRS(_method_id)),      \
3262306a36Sopenharmony_ci		.attrs = &UVERBS_METHOD_ATTRS(_method_id),                     \
3362306a36Sopenharmony_ci	}
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci/* Create a standard destroy method using the default handler. The handle_attr
3662306a36Sopenharmony_ci * argument must be the attribute specifying the handle to destroy, the
3762306a36Sopenharmony_ci * default handler does not support any other attributes.
3862306a36Sopenharmony_ci */
3962306a36Sopenharmony_ci#define DECLARE_UVERBS_NAMED_METHOD_DESTROY(_method_id, _handle_attr)          \
4062306a36Sopenharmony_ci	static const struct uverbs_attr_def *const UVERBS_METHOD_ATTRS(        \
4162306a36Sopenharmony_ci		_method_id)[] = { _handle_attr };                              \
4262306a36Sopenharmony_ci	static const struct uverbs_method_def UVERBS_METHOD(_method_id) = {    \
4362306a36Sopenharmony_ci		.id = _method_id,                                              \
4462306a36Sopenharmony_ci		.handler = uverbs_destroy_def_handler,                         \
4562306a36Sopenharmony_ci		.num_attrs = ARRAY_SIZE(UVERBS_METHOD_ATTRS(_method_id)),      \
4662306a36Sopenharmony_ci		.attrs = &UVERBS_METHOD_ATTRS(_method_id),                     \
4762306a36Sopenharmony_ci	}
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci#define DECLARE_UVERBS_NAMED_OBJECT(_object_id, _type_attrs, ...)              \
5062306a36Sopenharmony_ci	static const struct uverbs_method_def *const UVERBS_OBJECT_METHODS(    \
5162306a36Sopenharmony_ci		_object_id)[] = { __VA_ARGS__ };                               \
5262306a36Sopenharmony_ci	static const struct uverbs_object_def UVERBS_OBJECT(_object_id) = {    \
5362306a36Sopenharmony_ci		.id = _object_id,                                              \
5462306a36Sopenharmony_ci		.type_attrs = &_type_attrs,                                    \
5562306a36Sopenharmony_ci		.num_methods = ARRAY_SIZE(UVERBS_OBJECT_METHODS(_object_id)),  \
5662306a36Sopenharmony_ci		.methods = &UVERBS_OBJECT_METHODS(_object_id)                  \
5762306a36Sopenharmony_ci	}
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci/*
6062306a36Sopenharmony_ci * Declare global methods. These still have a unique object_id because we
6162306a36Sopenharmony_ci * identify all uapi methods with a (object,method) tuple. However, they have
6262306a36Sopenharmony_ci * no type pointer.
6362306a36Sopenharmony_ci */
6462306a36Sopenharmony_ci#define DECLARE_UVERBS_GLOBAL_METHODS(_object_id, ...)                         \
6562306a36Sopenharmony_ci	static const struct uverbs_method_def *const UVERBS_OBJECT_METHODS(    \
6662306a36Sopenharmony_ci		_object_id)[] = { __VA_ARGS__ };                               \
6762306a36Sopenharmony_ci	static const struct uverbs_object_def UVERBS_OBJECT(_object_id) = {    \
6862306a36Sopenharmony_ci		.id = _object_id,                                              \
6962306a36Sopenharmony_ci		.num_methods = ARRAY_SIZE(UVERBS_OBJECT_METHODS(_object_id)),  \
7062306a36Sopenharmony_ci		.methods = &UVERBS_OBJECT_METHODS(_object_id)                  \
7162306a36Sopenharmony_ci	}
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci/* Used by drivers to declare a complete parsing tree for new methods
7462306a36Sopenharmony_ci */
7562306a36Sopenharmony_ci#define ADD_UVERBS_METHODS(_name, _object_id, ...)                             \
7662306a36Sopenharmony_ci	static const struct uverbs_method_def *const UVERBS_OBJECT_METHODS(    \
7762306a36Sopenharmony_ci		_object_id)[] = { __VA_ARGS__ };                               \
7862306a36Sopenharmony_ci	static const struct uverbs_object_def _name = {                        \
7962306a36Sopenharmony_ci		.id = _object_id,                                              \
8062306a36Sopenharmony_ci		.num_methods = ARRAY_SIZE(UVERBS_OBJECT_METHODS(_object_id)),  \
8162306a36Sopenharmony_ci		.methods = &UVERBS_OBJECT_METHODS(_object_id)                  \
8262306a36Sopenharmony_ci	};
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci/* Used by drivers to declare a complete parsing tree for a single method that
8562306a36Sopenharmony_ci * differs only in having additional driver specific attributes.
8662306a36Sopenharmony_ci */
8762306a36Sopenharmony_ci#define ADD_UVERBS_ATTRIBUTES_SIMPLE(_name, _object_id, _method_id, ...)       \
8862306a36Sopenharmony_ci	static const struct uverbs_attr_def *const UVERBS_METHOD_ATTRS(        \
8962306a36Sopenharmony_ci		_method_id)[] = { __VA_ARGS__ };                               \
9062306a36Sopenharmony_ci	static const struct uverbs_method_def UVERBS_METHOD(_method_id) = {    \
9162306a36Sopenharmony_ci		.id = _method_id,                                              \
9262306a36Sopenharmony_ci		.num_attrs = ARRAY_SIZE(UVERBS_METHOD_ATTRS(_method_id)),      \
9362306a36Sopenharmony_ci		.attrs = &UVERBS_METHOD_ATTRS(_method_id),                     \
9462306a36Sopenharmony_ci	};                                                                     \
9562306a36Sopenharmony_ci	ADD_UVERBS_METHODS(_name, _object_id, &UVERBS_METHOD(_method_id))
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci#endif
98