Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / infiniband / core / cma_configfs.c
blobeee38b40be99041e69b28cd77c6322848e5663b2
1 /*
2 * Copyright (c) 2015, Mellanox Technologies inc. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
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.
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.
33 #include <linux/module.h>
34 #include <linux/configfs.h>
35 #include <rdma/ib_verbs.h>
36 #include "core_priv.h"
38 struct cma_device;
40 struct cma_dev_group;
42 struct cma_dev_port_group {
43 unsigned int port_num;
44 struct cma_dev_group *cma_dev_group;
45 struct config_group group;
48 struct cma_dev_group {
49 char name[IB_DEVICE_NAME_MAX];
50 struct config_group device_group;
51 struct config_group ports_group;
52 struct cma_dev_port_group *ports;
55 static struct cma_dev_port_group *to_dev_port_group(struct config_item *item)
57 struct config_group *group;
59 if (!item)
60 return NULL;
62 group = container_of(item, struct config_group, cg_item);
63 return container_of(group, struct cma_dev_port_group, group);
66 static bool filter_by_name(struct ib_device *ib_dev, void *cookie)
68 return !strcmp(ib_dev->name, cookie);
71 static int cma_configfs_params_get(struct config_item *item,
72 struct cma_device **pcma_dev,
73 struct cma_dev_port_group **pgroup)
75 struct cma_dev_port_group *group = to_dev_port_group(item);
76 struct cma_device *cma_dev;
78 if (!group)
79 return -ENODEV;
81 cma_dev = cma_enum_devices_by_ibdev(filter_by_name,
82 group->cma_dev_group->name);
83 if (!cma_dev)
84 return -ENODEV;
86 *pcma_dev = cma_dev;
87 *pgroup = group;
89 return 0;
92 static void cma_configfs_params_put(struct cma_device *cma_dev)
94 cma_deref_dev(cma_dev);
97 static ssize_t default_roce_mode_show(struct config_item *item,
98 char *buf)
100 struct cma_device *cma_dev;
101 struct cma_dev_port_group *group;
102 int gid_type;
103 ssize_t ret;
105 ret = cma_configfs_params_get(item, &cma_dev, &group);
106 if (ret)
107 return ret;
109 gid_type = cma_get_default_gid_type(cma_dev, group->port_num);
110 cma_configfs_params_put(cma_dev);
112 if (gid_type < 0)
113 return gid_type;
115 return sprintf(buf, "%s\n", ib_cache_gid_type_str(gid_type));
118 static ssize_t default_roce_mode_store(struct config_item *item,
119 const char *buf, size_t count)
121 struct cma_device *cma_dev;
122 struct cma_dev_port_group *group;
123 int gid_type = ib_cache_gid_parse_type_str(buf);
124 ssize_t ret;
126 if (gid_type < 0)
127 return -EINVAL;
129 ret = cma_configfs_params_get(item, &cma_dev, &group);
130 if (ret)
131 return ret;
133 ret = cma_set_default_gid_type(cma_dev, group->port_num, gid_type);
135 cma_configfs_params_put(cma_dev);
137 return !ret ? strnlen(buf, count) : ret;
140 CONFIGFS_ATTR(, default_roce_mode);
142 static ssize_t default_roce_tos_show(struct config_item *item, char *buf)
144 struct cma_device *cma_dev;
145 struct cma_dev_port_group *group;
146 ssize_t ret;
147 u8 tos;
149 ret = cma_configfs_params_get(item, &cma_dev, &group);
150 if (ret)
151 return ret;
153 tos = cma_get_default_roce_tos(cma_dev, group->port_num);
154 cma_configfs_params_put(cma_dev);
156 return sprintf(buf, "%u\n", tos);
159 static ssize_t default_roce_tos_store(struct config_item *item,
160 const char *buf, size_t count)
162 struct cma_device *cma_dev;
163 struct cma_dev_port_group *group;
164 ssize_t ret;
165 u8 tos;
167 ret = kstrtou8(buf, 0, &tos);
168 if (ret)
169 return ret;
171 ret = cma_configfs_params_get(item, &cma_dev, &group);
172 if (ret)
173 return ret;
175 ret = cma_set_default_roce_tos(cma_dev, group->port_num, tos);
176 cma_configfs_params_put(cma_dev);
178 return ret ? ret : strnlen(buf, count);
181 CONFIGFS_ATTR(, default_roce_tos);
183 static struct configfs_attribute *cma_configfs_attributes[] = {
184 &attr_default_roce_mode,
185 &attr_default_roce_tos,
186 NULL,
189 static const struct config_item_type cma_port_group_type = {
190 .ct_attrs = cma_configfs_attributes,
191 .ct_owner = THIS_MODULE
194 static int make_cma_ports(struct cma_dev_group *cma_dev_group,
195 struct cma_device *cma_dev)
197 struct ib_device *ibdev;
198 unsigned int i;
199 unsigned int ports_num;
200 struct cma_dev_port_group *ports;
201 int err;
203 ibdev = cma_get_ib_dev(cma_dev);
205 if (!ibdev)
206 return -ENODEV;
208 ports_num = ibdev->phys_port_cnt;
209 ports = kcalloc(ports_num, sizeof(*cma_dev_group->ports),
210 GFP_KERNEL);
212 if (!ports) {
213 err = -ENOMEM;
214 goto free;
217 for (i = 0; i < ports_num; i++) {
218 char port_str[10];
220 ports[i].port_num = i + 1;
221 snprintf(port_str, sizeof(port_str), "%u", i + 1);
222 ports[i].cma_dev_group = cma_dev_group;
223 config_group_init_type_name(&ports[i].group,
224 port_str,
225 &cma_port_group_type);
226 configfs_add_default_group(&ports[i].group,
227 &cma_dev_group->ports_group);
230 cma_dev_group->ports = ports;
232 return 0;
233 free:
234 kfree(ports);
235 cma_dev_group->ports = NULL;
236 return err;
239 static void release_cma_dev(struct config_item *item)
241 struct config_group *group = container_of(item, struct config_group,
242 cg_item);
243 struct cma_dev_group *cma_dev_group = container_of(group,
244 struct cma_dev_group,
245 device_group);
247 kfree(cma_dev_group);
250 static void release_cma_ports_group(struct config_item *item)
252 struct config_group *group = container_of(item, struct config_group,
253 cg_item);
254 struct cma_dev_group *cma_dev_group = container_of(group,
255 struct cma_dev_group,
256 ports_group);
258 kfree(cma_dev_group->ports);
259 cma_dev_group->ports = NULL;
262 static struct configfs_item_operations cma_ports_item_ops = {
263 .release = release_cma_ports_group
266 static const struct config_item_type cma_ports_group_type = {
267 .ct_item_ops = &cma_ports_item_ops,
268 .ct_owner = THIS_MODULE
271 static struct configfs_item_operations cma_device_item_ops = {
272 .release = release_cma_dev
275 static const struct config_item_type cma_device_group_type = {
276 .ct_item_ops = &cma_device_item_ops,
277 .ct_owner = THIS_MODULE
280 static struct config_group *make_cma_dev(struct config_group *group,
281 const char *name)
283 int err = -ENODEV;
284 struct cma_device *cma_dev = cma_enum_devices_by_ibdev(filter_by_name,
285 (void *)name);
286 struct cma_dev_group *cma_dev_group = NULL;
288 if (!cma_dev)
289 goto fail;
291 cma_dev_group = kzalloc(sizeof(*cma_dev_group), GFP_KERNEL);
293 if (!cma_dev_group) {
294 err = -ENOMEM;
295 goto fail;
298 strlcpy(cma_dev_group->name, name, sizeof(cma_dev_group->name));
300 config_group_init_type_name(&cma_dev_group->ports_group, "ports",
301 &cma_ports_group_type);
303 err = make_cma_ports(cma_dev_group, cma_dev);
304 if (err)
305 goto fail;
307 config_group_init_type_name(&cma_dev_group->device_group, name,
308 &cma_device_group_type);
309 configfs_add_default_group(&cma_dev_group->ports_group,
310 &cma_dev_group->device_group);
312 cma_deref_dev(cma_dev);
313 return &cma_dev_group->device_group;
315 fail:
316 if (cma_dev)
317 cma_deref_dev(cma_dev);
318 kfree(cma_dev_group);
319 return ERR_PTR(err);
322 static struct configfs_group_operations cma_subsys_group_ops = {
323 .make_group = make_cma_dev,
326 static const struct config_item_type cma_subsys_type = {
327 .ct_group_ops = &cma_subsys_group_ops,
328 .ct_owner = THIS_MODULE,
331 static struct configfs_subsystem cma_subsys = {
332 .su_group = {
333 .cg_item = {
334 .ci_namebuf = "rdma_cm",
335 .ci_type = &cma_subsys_type,
340 int __init cma_configfs_init(void)
342 config_group_init(&cma_subsys.su_group);
343 mutex_init(&cma_subsys.su_mutex);
344 return configfs_register_subsystem(&cma_subsys);
347 void __exit cma_configfs_exit(void)
349 configfs_unregister_subsystem(&cma_subsys);