WIP FPC-III support
[linux/fpc-iii.git] / drivers / infiniband / core / cma_configfs.c
blob7f70e5a7de10543318f1afe420193e8f42084c59
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 <rdma/rdma_cm.h>
38 #include "core_priv.h"
39 #include "cma_priv.h"
41 struct cma_device;
43 struct cma_dev_group;
45 struct cma_dev_port_group {
46 unsigned int port_num;
47 struct cma_dev_group *cma_dev_group;
48 struct config_group group;
51 struct cma_dev_group {
52 char name[IB_DEVICE_NAME_MAX];
53 struct config_group device_group;
54 struct config_group ports_group;
55 struct cma_dev_port_group *ports;
58 static struct cma_dev_port_group *to_dev_port_group(struct config_item *item)
60 struct config_group *group;
62 if (!item)
63 return NULL;
65 group = container_of(item, struct config_group, cg_item);
66 return container_of(group, struct cma_dev_port_group, group);
69 static bool filter_by_name(struct ib_device *ib_dev, void *cookie)
71 return !strcmp(dev_name(&ib_dev->dev), cookie);
74 static int cma_configfs_params_get(struct config_item *item,
75 struct cma_device **pcma_dev,
76 struct cma_dev_port_group **pgroup)
78 struct cma_dev_port_group *group = to_dev_port_group(item);
79 struct cma_device *cma_dev;
81 if (!group)
82 return -ENODEV;
84 cma_dev = cma_enum_devices_by_ibdev(filter_by_name,
85 group->cma_dev_group->name);
86 if (!cma_dev)
87 return -ENODEV;
89 *pcma_dev = cma_dev;
90 *pgroup = group;
92 return 0;
95 static void cma_configfs_params_put(struct cma_device *cma_dev)
97 cma_dev_put(cma_dev);
100 static ssize_t default_roce_mode_show(struct config_item *item,
101 char *buf)
103 struct cma_device *cma_dev;
104 struct cma_dev_port_group *group;
105 int gid_type;
106 ssize_t ret;
108 ret = cma_configfs_params_get(item, &cma_dev, &group);
109 if (ret)
110 return ret;
112 gid_type = cma_get_default_gid_type(cma_dev, group->port_num);
113 cma_configfs_params_put(cma_dev);
115 if (gid_type < 0)
116 return gid_type;
118 return sysfs_emit(buf, "%s\n", ib_cache_gid_type_str(gid_type));
121 static ssize_t default_roce_mode_store(struct config_item *item,
122 const char *buf, size_t count)
124 struct cma_device *cma_dev;
125 struct cma_dev_port_group *group;
126 int gid_type;
127 ssize_t ret;
129 ret = cma_configfs_params_get(item, &cma_dev, &group);
130 if (ret)
131 return ret;
133 gid_type = ib_cache_gid_parse_type_str(buf);
134 if (gid_type < 0)
135 return -EINVAL;
137 ret = cma_set_default_gid_type(cma_dev, group->port_num, gid_type);
139 cma_configfs_params_put(cma_dev);
141 return !ret ? strnlen(buf, count) : ret;
144 CONFIGFS_ATTR(, default_roce_mode);
146 static ssize_t default_roce_tos_show(struct config_item *item, char *buf)
148 struct cma_device *cma_dev;
149 struct cma_dev_port_group *group;
150 ssize_t ret;
151 u8 tos;
153 ret = cma_configfs_params_get(item, &cma_dev, &group);
154 if (ret)
155 return ret;
157 tos = cma_get_default_roce_tos(cma_dev, group->port_num);
158 cma_configfs_params_put(cma_dev);
160 return sysfs_emit(buf, "%u\n", tos);
163 static ssize_t default_roce_tos_store(struct config_item *item,
164 const char *buf, size_t count)
166 struct cma_device *cma_dev;
167 struct cma_dev_port_group *group;
168 ssize_t ret;
169 u8 tos;
171 ret = kstrtou8(buf, 0, &tos);
172 if (ret)
173 return ret;
175 ret = cma_configfs_params_get(item, &cma_dev, &group);
176 if (ret)
177 return ret;
179 ret = cma_set_default_roce_tos(cma_dev, group->port_num, tos);
180 cma_configfs_params_put(cma_dev);
182 return ret ? ret : strnlen(buf, count);
185 CONFIGFS_ATTR(, default_roce_tos);
187 static struct configfs_attribute *cma_configfs_attributes[] = {
188 &attr_default_roce_mode,
189 &attr_default_roce_tos,
190 NULL,
193 static const struct config_item_type cma_port_group_type = {
194 .ct_attrs = cma_configfs_attributes,
195 .ct_owner = THIS_MODULE
198 static int make_cma_ports(struct cma_dev_group *cma_dev_group,
199 struct cma_device *cma_dev)
201 struct ib_device *ibdev;
202 unsigned int i;
203 unsigned int ports_num;
204 struct cma_dev_port_group *ports;
205 int err;
207 ibdev = cma_get_ib_dev(cma_dev);
209 if (!ibdev)
210 return -ENODEV;
212 ports_num = ibdev->phys_port_cnt;
213 ports = kcalloc(ports_num, sizeof(*cma_dev_group->ports),
214 GFP_KERNEL);
216 if (!ports) {
217 err = -ENOMEM;
218 goto free;
221 for (i = 0; i < ports_num; i++) {
222 char port_str[10];
224 ports[i].port_num = i + 1;
225 snprintf(port_str, sizeof(port_str), "%u", i + 1);
226 ports[i].cma_dev_group = cma_dev_group;
227 config_group_init_type_name(&ports[i].group,
228 port_str,
229 &cma_port_group_type);
230 configfs_add_default_group(&ports[i].group,
231 &cma_dev_group->ports_group);
234 cma_dev_group->ports = ports;
236 return 0;
237 free:
238 kfree(ports);
239 cma_dev_group->ports = NULL;
240 return err;
243 static void release_cma_dev(struct config_item *item)
245 struct config_group *group = container_of(item, struct config_group,
246 cg_item);
247 struct cma_dev_group *cma_dev_group = container_of(group,
248 struct cma_dev_group,
249 device_group);
251 kfree(cma_dev_group);
254 static void release_cma_ports_group(struct config_item *item)
256 struct config_group *group = container_of(item, struct config_group,
257 cg_item);
258 struct cma_dev_group *cma_dev_group = container_of(group,
259 struct cma_dev_group,
260 ports_group);
262 kfree(cma_dev_group->ports);
263 cma_dev_group->ports = NULL;
266 static struct configfs_item_operations cma_ports_item_ops = {
267 .release = release_cma_ports_group
270 static const struct config_item_type cma_ports_group_type = {
271 .ct_item_ops = &cma_ports_item_ops,
272 .ct_owner = THIS_MODULE
275 static struct configfs_item_operations cma_device_item_ops = {
276 .release = release_cma_dev
279 static const struct config_item_type cma_device_group_type = {
280 .ct_item_ops = &cma_device_item_ops,
281 .ct_owner = THIS_MODULE
284 static struct config_group *make_cma_dev(struct config_group *group,
285 const char *name)
287 int err = -ENODEV;
288 struct cma_device *cma_dev = cma_enum_devices_by_ibdev(filter_by_name,
289 (void *)name);
290 struct cma_dev_group *cma_dev_group = NULL;
292 if (!cma_dev)
293 goto fail;
295 cma_dev_group = kzalloc(sizeof(*cma_dev_group), GFP_KERNEL);
297 if (!cma_dev_group) {
298 err = -ENOMEM;
299 goto fail;
302 strlcpy(cma_dev_group->name, name, sizeof(cma_dev_group->name));
304 config_group_init_type_name(&cma_dev_group->ports_group, "ports",
305 &cma_ports_group_type);
307 err = make_cma_ports(cma_dev_group, cma_dev);
308 if (err)
309 goto fail;
311 config_group_init_type_name(&cma_dev_group->device_group, name,
312 &cma_device_group_type);
313 configfs_add_default_group(&cma_dev_group->ports_group,
314 &cma_dev_group->device_group);
316 cma_dev_put(cma_dev);
317 return &cma_dev_group->device_group;
319 fail:
320 if (cma_dev)
321 cma_dev_put(cma_dev);
322 kfree(cma_dev_group);
323 return ERR_PTR(err);
326 static void drop_cma_dev(struct config_group *cgroup, struct config_item *item)
328 struct config_group *group =
329 container_of(item, struct config_group, cg_item);
330 struct cma_dev_group *cma_dev_group =
331 container_of(group, struct cma_dev_group, device_group);
333 configfs_remove_default_groups(&cma_dev_group->ports_group);
334 configfs_remove_default_groups(&cma_dev_group->device_group);
335 config_item_put(item);
338 static struct configfs_group_operations cma_subsys_group_ops = {
339 .make_group = make_cma_dev,
340 .drop_item = drop_cma_dev,
343 static const struct config_item_type cma_subsys_type = {
344 .ct_group_ops = &cma_subsys_group_ops,
345 .ct_owner = THIS_MODULE,
348 static struct configfs_subsystem cma_subsys = {
349 .su_group = {
350 .cg_item = {
351 .ci_namebuf = "rdma_cm",
352 .ci_type = &cma_subsys_type,
357 int __init cma_configfs_init(void)
359 int ret;
361 config_group_init(&cma_subsys.su_group);
362 mutex_init(&cma_subsys.su_mutex);
363 ret = configfs_register_subsystem(&cma_subsys);
364 if (ret)
365 mutex_destroy(&cma_subsys.su_mutex);
366 return ret;
369 void __exit cma_configfs_exit(void)
371 configfs_unregister_subsystem(&cma_subsys);
372 mutex_destroy(&cma_subsys.su_mutex);