blob: 2e8925434d742c654006e637a82ff02c61405e80 [file] [log] [blame]
Matan Baraka0aa3092017-08-03 16:06:55 +03001/*
2 * Copyright (c) 2017, Mellanox Technologies inc. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33#ifndef _UVERBS_IOCTL_
34#define _UVERBS_IOCTL_
35
36#include <rdma/uverbs_types.h>
37
38/*
39 * =======================================
40 * Verbs action specifications
41 * =======================================
42 */
43
Matan Barakf43dbeb2017-08-03 16:06:56 +030044enum uverbs_attr_type {
45 UVERBS_ATTR_TYPE_NA,
Matan Barakfac96582017-08-03 16:06:57 +030046 UVERBS_ATTR_TYPE_PTR_IN,
47 UVERBS_ATTR_TYPE_PTR_OUT,
Matan Barakf43dbeb2017-08-03 16:06:56 +030048 UVERBS_ATTR_TYPE_IDR,
49 UVERBS_ATTR_TYPE_FD,
50};
51
Matan Baraka0aa3092017-08-03 16:06:55 +030052enum uverbs_obj_access {
53 UVERBS_ACCESS_READ,
54 UVERBS_ACCESS_WRITE,
55 UVERBS_ACCESS_NEW,
56 UVERBS_ACCESS_DESTROY
57};
58
Matan Barakfac96582017-08-03 16:06:57 +030059enum {
60 UVERBS_ATTR_SPEC_F_MANDATORY = 1U << 0,
61 /* Support extending attributes by length */
62 UVERBS_ATTR_SPEC_F_MIN_SZ = 1U << 1,
63};
64
Matan Barakf43dbeb2017-08-03 16:06:56 +030065struct uverbs_attr_spec {
66 enum uverbs_attr_type type;
Matan Barakfac96582017-08-03 16:06:57 +030067 union {
68 u16 len;
69 struct {
70 /*
71 * higher bits mean the namespace and lower bits mean
72 * the type id within the namespace.
73 */
74 u16 obj_type;
75 u8 access;
76 } obj;
77 };
78 /* Combination of bits from enum UVERBS_ATTR_SPEC_F_XXXX */
79 u8 flags;
Matan Barakf43dbeb2017-08-03 16:06:56 +030080};
81
82struct uverbs_attr_spec_hash {
83 size_t num_attrs;
Matan Barakfac96582017-08-03 16:06:57 +030084 unsigned long *mandatory_attrs_bitmask;
Matan Barakf43dbeb2017-08-03 16:06:56 +030085 struct uverbs_attr_spec attrs[0];
86};
87
Matan Barakfac96582017-08-03 16:06:57 +030088struct uverbs_attr_bundle;
89struct ib_uverbs_file;
90
91enum {
92 /*
93 * Action marked with this flag creates a context (or root for all
94 * objects).
95 */
96 UVERBS_ACTION_FLAG_CREATE_ROOT = 1U << 0,
97};
98
99struct uverbs_method_spec {
100 /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
101 u32 flags;
102 size_t num_buckets;
103 size_t num_child_attrs;
104 int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile,
105 struct uverbs_attr_bundle *ctx);
106 struct uverbs_attr_spec_hash *attr_buckets[0];
107};
108
109struct uverbs_method_spec_hash {
110 size_t num_methods;
111 struct uverbs_method_spec *methods[0];
112};
113
114struct uverbs_object_spec {
115 const struct uverbs_obj_type *type_attrs;
116 size_t num_buckets;
117 struct uverbs_method_spec_hash *method_buckets[0];
118};
119
120struct uverbs_object_spec_hash {
121 size_t num_objects;
122 struct uverbs_object_spec *objects[0];
123};
124
125struct uverbs_root_spec {
126 size_t num_buckets;
127 struct uverbs_object_spec_hash *object_buckets[0];
128};
129
Matan Barak50090102017-08-03 16:06:58 +0300130/*
131 * =======================================
132 * Verbs definitions
133 * =======================================
134 */
135
Matan Barak09e3ebf2017-08-03 16:06:59 +0300136struct uverbs_attr_def {
137 u16 id;
138 struct uverbs_attr_spec attr;
139};
140
141struct uverbs_method_def {
142 u16 id;
143 /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
144 u32 flags;
145 size_t num_attrs;
146 const struct uverbs_attr_def * const (*attrs)[];
147 int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile,
148 struct uverbs_attr_bundle *ctx);
149};
150
Matan Barak50090102017-08-03 16:06:58 +0300151struct uverbs_object_def {
Matan Barak09e3ebf2017-08-03 16:06:59 +0300152 u16 id;
Matan Barak50090102017-08-03 16:06:58 +0300153 const struct uverbs_obj_type *type_attrs;
Matan Barak09e3ebf2017-08-03 16:06:59 +0300154 size_t num_methods;
155 const struct uverbs_method_def * const (*methods)[];
156};
157
158struct uverbs_object_tree_def {
159 size_t num_objects;
160 const struct uverbs_object_def * const (*objects)[];
Matan Barak50090102017-08-03 16:06:58 +0300161};
162
163#define _UVERBS_OBJECT(_id, _type_attrs, ...) \
164 ((const struct uverbs_object_def) { \
Matan Barak09e3ebf2017-08-03 16:06:59 +0300165 .id = _id, \
Matan Barak50090102017-08-03 16:06:58 +0300166 .type_attrs = _type_attrs})
167#define DECLARE_UVERBS_OBJECT(_name, _id, _type_attrs, ...) \
168 const struct uverbs_object_def _name = \
169 _UVERBS_OBJECT(_id, _type_attrs, ##__VA_ARGS__)
Matan Barak09e3ebf2017-08-03 16:06:59 +0300170#define _UVERBS_TREE_OBJECTS_SZ(...) \
171 (sizeof((const struct uverbs_object_def * const []){__VA_ARGS__}) / \
172 sizeof(const struct uverbs_object_def *))
173#define _UVERBS_OBJECT_TREE(...) \
174 ((const struct uverbs_object_tree_def) { \
175 .num_objects = _UVERBS_TREE_OBJECTS_SZ(__VA_ARGS__), \
176 .objects = &(const struct uverbs_object_def * const []){__VA_ARGS__} })
177#define DECLARE_UVERBS_OBJECT_TREE(_name, ...) \
178 const struct uverbs_object_tree_def _name = \
179 _UVERBS_OBJECT_TREE(__VA_ARGS__)
180
Matan Barakfac96582017-08-03 16:06:57 +0300181/* =================================================
182 * Parsing infrastructure
183 * =================================================
184 */
185
186struct uverbs_ptr_attr {
187 union {
188 u64 data;
189 void __user *ptr;
190 };
191 u16 len;
192 /* Combination of bits from enum UVERBS_ATTR_F_XXXX */
193 u16 flags;
194};
195
Matan Barakf43dbeb2017-08-03 16:06:56 +0300196struct uverbs_obj_attr {
Matan Barakfac96582017-08-03 16:06:57 +0300197 /* pointer to the kernel descriptor -> type, access, etc */
198 const struct uverbs_obj_type *type;
Matan Barakf43dbeb2017-08-03 16:06:56 +0300199 struct ib_uobject *uobject;
Matan Barakfac96582017-08-03 16:06:57 +0300200 /* fd or id in idr of this object */
201 int id;
Matan Barakf43dbeb2017-08-03 16:06:56 +0300202};
203
204struct uverbs_attr {
Matan Barakfac96582017-08-03 16:06:57 +0300205 /*
206 * pointer to the user-space given attribute, in order to write the
207 * new uobject's id or update flags.
208 */
209 struct ib_uverbs_attr __user *uattr;
210 union {
211 struct uverbs_ptr_attr ptr_attr;
212 struct uverbs_obj_attr obj_attr;
213 };
Matan Barakf43dbeb2017-08-03 16:06:56 +0300214};
215
216struct uverbs_attr_bundle_hash {
217 /* if bit i is set, it means attrs[i] contains valid information */
218 unsigned long *valid_bitmap;
219 size_t num_attrs;
220 /*
221 * arrays of attributes, each element corresponds to the specification
222 * of the attribute in the same index.
223 */
224 struct uverbs_attr *attrs;
225};
226
227struct uverbs_attr_bundle {
228 size_t num_buckets;
229 struct uverbs_attr_bundle_hash hash[];
230};
231
232static inline bool uverbs_attr_is_valid_in_hash(const struct uverbs_attr_bundle_hash *attrs_hash,
233 unsigned int idx)
234{
235 return test_bit(idx, attrs_hash->valid_bitmap);
236}
237
Matan Baraka0aa3092017-08-03 16:06:55 +0300238#endif
239