uverbs_ioctl.h 6.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*
 * Copyright (c) 2017, Mellanox Technologies inc.  All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef _UVERBS_IOCTL_
#define _UVERBS_IOCTL_

#include <rdma/uverbs_types.h>

/*
 * =======================================
 *	Verbs action specifications
 * =======================================
 */

44 45
enum uverbs_attr_type {
	UVERBS_ATTR_TYPE_NA,
M
Matan Barak 已提交
46 47
	UVERBS_ATTR_TYPE_PTR_IN,
	UVERBS_ATTR_TYPE_PTR_OUT,
48 49 50 51
	UVERBS_ATTR_TYPE_IDR,
	UVERBS_ATTR_TYPE_FD,
};

52 53 54 55 56 57 58
enum uverbs_obj_access {
	UVERBS_ACCESS_READ,
	UVERBS_ACCESS_WRITE,
	UVERBS_ACCESS_NEW,
	UVERBS_ACCESS_DESTROY
};

M
Matan Barak 已提交
59 60 61 62 63 64
enum {
	UVERBS_ATTR_SPEC_F_MANDATORY	= 1U << 0,
	/* Support extending attributes by length */
	UVERBS_ATTR_SPEC_F_MIN_SZ	= 1U << 1,
};

65 66
struct uverbs_attr_spec {
	enum uverbs_attr_type		type;
M
Matan Barak 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79
	union {
		u16				len;
		struct {
			/*
			 * higher bits mean the namespace and lower bits mean
			 * the type id within the namespace.
			 */
			u16			obj_type;
			u8			access;
		} obj;
	};
	/* Combination of bits from enum UVERBS_ATTR_SPEC_F_XXXX */
	u8				flags;
80 81 82 83
};

struct uverbs_attr_spec_hash {
	size_t				num_attrs;
M
Matan Barak 已提交
84
	unsigned long			*mandatory_attrs_bitmask;
85 86 87
	struct uverbs_attr_spec		attrs[0];
};

M
Matan Barak 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
struct uverbs_attr_bundle;
struct ib_uverbs_file;

enum {
	/*
	 * Action marked with this flag creates a context (or root for all
	 * objects).
	 */
	UVERBS_ACTION_FLAG_CREATE_ROOT = 1U << 0,
};

struct uverbs_method_spec {
	/* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
	u32						flags;
	size_t						num_buckets;
	size_t						num_child_attrs;
	int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile,
		       struct uverbs_attr_bundle *ctx);
	struct uverbs_attr_spec_hash		*attr_buckets[0];
};

struct uverbs_method_spec_hash {
	size_t					num_methods;
	struct uverbs_method_spec		*methods[0];
};

struct uverbs_object_spec {
	const struct uverbs_obj_type		*type_attrs;
	size_t					num_buckets;
	struct uverbs_method_spec_hash		*method_buckets[0];
};

struct uverbs_object_spec_hash {
	size_t					num_objects;
	struct uverbs_object_spec		*objects[0];
};

struct uverbs_root_spec {
	size_t					num_buckets;
	struct uverbs_object_spec_hash		*object_buckets[0];
};

130 131 132 133 134 135
/*
 * =======================================
 *	Verbs definitions
 * =======================================
 */

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
struct uverbs_attr_def {
	u16                           id;
	struct uverbs_attr_spec       attr;
};

struct uverbs_method_def {
	u16                                  id;
	/* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
	u32				     flags;
	size_t				     num_attrs;
	const struct uverbs_attr_def * const (*attrs)[];
	int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile,
		       struct uverbs_attr_bundle *ctx);
};

151
struct uverbs_object_def {
152
	u16					 id;
153
	const struct uverbs_obj_type	        *type_attrs;
154 155 156 157 158 159 160
	size_t				         num_methods;
	const struct uverbs_method_def * const (*methods)[];
};

struct uverbs_object_tree_def {
	size_t					 num_objects;
	const struct uverbs_object_def * const (*objects)[];
161 162 163 164
};

#define _UVERBS_OBJECT(_id, _type_attrs, ...)				\
	((const struct uverbs_object_def) {				\
165
	 .id = _id,							\
166 167 168 169
	 .type_attrs = _type_attrs})
#define DECLARE_UVERBS_OBJECT(_name, _id, _type_attrs, ...)		\
	const struct uverbs_object_def _name =				\
		_UVERBS_OBJECT(_id, _type_attrs, ##__VA_ARGS__)
170 171 172 173 174 175 176 177 178 179 180
#define _UVERBS_TREE_OBJECTS_SZ(...)					\
	(sizeof((const struct uverbs_object_def * const []){__VA_ARGS__}) / \
	 sizeof(const struct uverbs_object_def *))
#define _UVERBS_OBJECT_TREE(...)					\
	((const struct uverbs_object_tree_def) {			\
	 .num_objects = _UVERBS_TREE_OBJECTS_SZ(__VA_ARGS__),		\
	 .objects = &(const struct uverbs_object_def * const []){__VA_ARGS__} })
#define DECLARE_UVERBS_OBJECT_TREE(_name, ...)				\
	const struct uverbs_object_tree_def _name =			\
		_UVERBS_OBJECT_TREE(__VA_ARGS__)

M
Matan Barak 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
/* =================================================
 *              Parsing infrastructure
 * =================================================
 */

struct uverbs_ptr_attr {
	union {
		u64		data;
		void	__user *ptr;
	};
	u16		len;
	/* Combination of bits from enum UVERBS_ATTR_F_XXXX */
	u16		flags;
};

196
struct uverbs_obj_attr {
M
Matan Barak 已提交
197 198
	/* pointer to the kernel descriptor -> type, access, etc */
	const struct uverbs_obj_type	*type;
199
	struct ib_uobject		*uobject;
M
Matan Barak 已提交
200 201
	/* fd or id in idr of this object */
	int				id;
202 203 204
};

struct uverbs_attr {
M
Matan Barak 已提交
205 206 207 208 209 210 211 212 213
	/*
	 * pointer to the user-space given attribute, in order to write the
	 * new uobject's id or update flags.
	 */
	struct ib_uverbs_attr __user	*uattr;
	union {
		struct uverbs_ptr_attr	ptr_attr;
		struct uverbs_obj_attr	obj_attr;
	};
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
};

struct uverbs_attr_bundle_hash {
	/* if bit i is set, it means attrs[i] contains valid information */
	unsigned long *valid_bitmap;
	size_t num_attrs;
	/*
	 * arrays of attributes, each element corresponds to the specification
	 * of the attribute in the same index.
	 */
	struct uverbs_attr *attrs;
};

struct uverbs_attr_bundle {
	size_t				num_buckets;
	struct uverbs_attr_bundle_hash  hash[];
};

static inline bool uverbs_attr_is_valid_in_hash(const struct uverbs_attr_bundle_hash *attrs_hash,
						unsigned int idx)
{
	return test_bit(idx, attrs_hash->valid_bitmap);
}

238 239
#endif