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
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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
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"
45 struct cma_dev_port_group {
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;
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;
84 cma_dev = cma_enum_devices_by_ibdev(filter_by_name,
85 group->cma_dev_group->name);
95 static void cma_configfs_params_put(struct cma_device *cma_dev)
100 static ssize_t default_roce_mode_show(struct config_item *item,
103 struct cma_device *cma_dev;
104 struct cma_dev_port_group *group;
108 ret = cma_configfs_params_get(item, &cma_dev, &group);
112 gid_type = cma_get_default_gid_type(cma_dev, group->port_num);
113 cma_configfs_params_put(cma_dev);
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;
129 ret = cma_configfs_params_get(item, &cma_dev, &group);
133 gid_type = ib_cache_gid_parse_type_str(buf);
135 cma_configfs_params_put(cma_dev);
139 ret = cma_set_default_gid_type(cma_dev, group->port_num, gid_type);
141 cma_configfs_params_put(cma_dev);
143 return !ret ? strnlen(buf, count) : ret;
146 CONFIGFS_ATTR(, default_roce_mode);
148 static ssize_t default_roce_tos_show(struct config_item *item, char *buf)
150 struct cma_device *cma_dev;
151 struct cma_dev_port_group *group;
155 ret = cma_configfs_params_get(item, &cma_dev, &group);
159 tos = cma_get_default_roce_tos(cma_dev, group->port_num);
160 cma_configfs_params_put(cma_dev);
162 return sysfs_emit(buf, "%u\n", tos);
165 static ssize_t default_roce_tos_store(struct config_item *item,
166 const char *buf, size_t count)
168 struct cma_device *cma_dev;
169 struct cma_dev_port_group *group;
173 ret = kstrtou8(buf, 0, &tos);
177 ret = cma_configfs_params_get(item, &cma_dev, &group);
181 ret = cma_set_default_roce_tos(cma_dev, group->port_num, tos);
182 cma_configfs_params_put(cma_dev);
184 return ret ? ret : strnlen(buf, count);
187 CONFIGFS_ATTR(, default_roce_tos);
189 static struct configfs_attribute *cma_configfs_attributes[] = {
190 &attr_default_roce_mode,
191 &attr_default_roce_tos,
195 static const struct config_item_type cma_port_group_type = {
196 .ct_attrs = cma_configfs_attributes,
197 .ct_owner = THIS_MODULE
200 static int make_cma_ports(struct cma_dev_group *cma_dev_group,
201 struct cma_device *cma_dev)
203 struct cma_dev_port_group *ports;
204 struct ib_device *ibdev;
208 ibdev = cma_get_ib_dev(cma_dev);
213 ports_num = ibdev->phys_port_cnt;
214 ports = kcalloc(ports_num, sizeof(*cma_dev_group->ports),
220 for (i = 0; i < ports_num; i++) {
223 ports[i].port_num = i + 1;
224 snprintf(port_str, sizeof(port_str), "%u", i + 1);
225 ports[i].cma_dev_group = cma_dev_group;
226 config_group_init_type_name(&ports[i].group,
228 &cma_port_group_type);
229 configfs_add_default_group(&ports[i].group,
230 &cma_dev_group->ports_group);
233 cma_dev_group->ports = ports;
237 static void release_cma_dev(struct config_item *item)
239 struct config_group *group = container_of(item, struct config_group,
241 struct cma_dev_group *cma_dev_group = container_of(group,
242 struct cma_dev_group,
245 kfree(cma_dev_group);
248 static void release_cma_ports_group(struct config_item *item)
250 struct config_group *group = container_of(item, struct config_group,
252 struct cma_dev_group *cma_dev_group = container_of(group,
253 struct cma_dev_group,
256 kfree(cma_dev_group->ports);
257 cma_dev_group->ports = NULL;
260 static struct configfs_item_operations cma_ports_item_ops = {
261 .release = release_cma_ports_group
264 static const struct config_item_type cma_ports_group_type = {
265 .ct_item_ops = &cma_ports_item_ops,
266 .ct_owner = THIS_MODULE
269 static struct configfs_item_operations cma_device_item_ops = {
270 .release = release_cma_dev
273 static const struct config_item_type cma_device_group_type = {
274 .ct_item_ops = &cma_device_item_ops,
275 .ct_owner = THIS_MODULE
278 static struct config_group *make_cma_dev(struct config_group *group,
282 struct cma_device *cma_dev = cma_enum_devices_by_ibdev(filter_by_name,
284 struct cma_dev_group *cma_dev_group = NULL;
289 cma_dev_group = kzalloc(sizeof(*cma_dev_group), GFP_KERNEL);
291 if (!cma_dev_group) {
296 strlcpy(cma_dev_group->name, name, sizeof(cma_dev_group->name));
298 config_group_init_type_name(&cma_dev_group->ports_group, "ports",
299 &cma_ports_group_type);
301 err = make_cma_ports(cma_dev_group, cma_dev);
305 config_group_init_type_name(&cma_dev_group->device_group, name,
306 &cma_device_group_type);
307 configfs_add_default_group(&cma_dev_group->ports_group,
308 &cma_dev_group->device_group);
310 cma_dev_put(cma_dev);
311 return &cma_dev_group->device_group;
315 cma_dev_put(cma_dev);
316 kfree(cma_dev_group);
320 static void drop_cma_dev(struct config_group *cgroup, struct config_item *item)
322 struct config_group *group =
323 container_of(item, struct config_group, cg_item);
324 struct cma_dev_group *cma_dev_group =
325 container_of(group, struct cma_dev_group, device_group);
327 configfs_remove_default_groups(&cma_dev_group->ports_group);
328 configfs_remove_default_groups(&cma_dev_group->device_group);
329 config_item_put(item);
332 static struct configfs_group_operations cma_subsys_group_ops = {
333 .make_group = make_cma_dev,
334 .drop_item = drop_cma_dev,
337 static const struct config_item_type cma_subsys_type = {
338 .ct_group_ops = &cma_subsys_group_ops,
339 .ct_owner = THIS_MODULE,
342 static struct configfs_subsystem cma_subsys = {
345 .ci_namebuf = "rdma_cm",
346 .ci_type = &cma_subsys_type,
351 int __init cma_configfs_init(void)
355 config_group_init(&cma_subsys.su_group);
356 mutex_init(&cma_subsys.su_mutex);
357 ret = configfs_register_subsystem(&cma_subsys);
359 mutex_destroy(&cma_subsys.su_mutex);
363 void __exit cma_configfs_exit(void)
365 configfs_unregister_subsystem(&cma_subsys);
366 mutex_destroy(&cma_subsys.su_mutex);