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 "core_priv.h"
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;
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;
81 cma_dev = cma_enum_devices_by_ibdev(filter_by_name,
82 group->cma_dev_group->name);
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,
100 struct cma_device *cma_dev;
101 struct cma_dev_port_group *group;
105 ret = cma_configfs_params_get(item, &cma_dev, &group);
109 gid_type = cma_get_default_gid_type(cma_dev, group->port_num);
110 cma_configfs_params_put(cma_dev);
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);
129 ret = cma_configfs_params_get(item, &cma_dev, &group);
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 struct configfs_attribute *cma_configfs_attributes[] = {
143 &attr_default_roce_mode,
147 static struct config_item_type cma_port_group_type = {
148 .ct_attrs = cma_configfs_attributes,
149 .ct_owner = THIS_MODULE
152 static int make_cma_ports(struct cma_dev_group *cma_dev_group,
153 struct cma_device *cma_dev)
155 struct ib_device *ibdev;
157 unsigned int ports_num;
158 struct cma_dev_port_group *ports;
161 ibdev = cma_get_ib_dev(cma_dev);
166 ports_num = ibdev->phys_port_cnt;
167 ports = kcalloc(ports_num, sizeof(*cma_dev_group->ports),
175 for (i = 0; i < ports_num; i++) {
178 ports[i].port_num = i + 1;
179 snprintf(port_str, sizeof(port_str), "%u", i + 1);
180 ports[i].cma_dev_group = cma_dev_group;
181 config_group_init_type_name(&ports[i].group,
183 &cma_port_group_type);
184 configfs_add_default_group(&ports[i].group,
185 &cma_dev_group->ports_group);
188 cma_dev_group->ports = ports;
193 cma_dev_group->ports = NULL;
197 static void release_cma_dev(struct config_item *item)
199 struct config_group *group = container_of(item, struct config_group,
201 struct cma_dev_group *cma_dev_group = container_of(group,
202 struct cma_dev_group,
205 kfree(cma_dev_group);
208 static void release_cma_ports_group(struct config_item *item)
210 struct config_group *group = container_of(item, struct config_group,
212 struct cma_dev_group *cma_dev_group = container_of(group,
213 struct cma_dev_group,
216 kfree(cma_dev_group->ports);
217 cma_dev_group->ports = NULL;
220 static struct configfs_item_operations cma_ports_item_ops = {
221 .release = release_cma_ports_group
224 static struct config_item_type cma_ports_group_type = {
225 .ct_item_ops = &cma_ports_item_ops,
226 .ct_owner = THIS_MODULE
229 static struct configfs_item_operations cma_device_item_ops = {
230 .release = release_cma_dev
233 static struct config_item_type cma_device_group_type = {
234 .ct_item_ops = &cma_device_item_ops,
235 .ct_owner = THIS_MODULE
238 static struct config_group *make_cma_dev(struct config_group *group,
242 struct cma_device *cma_dev = cma_enum_devices_by_ibdev(filter_by_name,
244 struct cma_dev_group *cma_dev_group = NULL;
249 cma_dev_group = kzalloc(sizeof(*cma_dev_group), GFP_KERNEL);
251 if (!cma_dev_group) {
256 strncpy(cma_dev_group->name, name, sizeof(cma_dev_group->name));
258 config_group_init_type_name(&cma_dev_group->ports_group, "ports",
259 &cma_ports_group_type);
261 err = make_cma_ports(cma_dev_group, cma_dev);
265 config_group_init_type_name(&cma_dev_group->device_group, name,
266 &cma_device_group_type);
267 configfs_add_default_group(&cma_dev_group->ports_group,
268 &cma_dev_group->device_group);
270 cma_deref_dev(cma_dev);
271 return &cma_dev_group->device_group;
275 cma_deref_dev(cma_dev);
276 kfree(cma_dev_group);
280 static void drop_cma_dev(struct config_group *cgroup, struct config_item *item)
282 struct config_group *group =
283 container_of(item, struct config_group, cg_item);
284 struct cma_dev_group *cma_dev_group =
285 container_of(group, struct cma_dev_group, device_group);
287 configfs_remove_default_groups(&cma_dev_group->ports_group);
288 configfs_remove_default_groups(&cma_dev_group->device_group);
289 config_item_put(item);
292 static struct configfs_group_operations cma_subsys_group_ops = {
293 .make_group = make_cma_dev,
294 .drop_item = drop_cma_dev,
297 static struct config_item_type cma_subsys_type = {
298 .ct_group_ops = &cma_subsys_group_ops,
299 .ct_owner = THIS_MODULE,
302 static struct configfs_subsystem cma_subsys = {
305 .ci_namebuf = "rdma_cm",
306 .ci_type = &cma_subsys_type,
311 int __init cma_configfs_init(void)
313 config_group_init(&cma_subsys.su_group);
314 mutex_init(&cma_subsys.su_mutex);
315 return configfs_register_subsystem(&cma_subsys);
318 void __exit cma_configfs_exit(void)
320 configfs_unregister_subsystem(&cma_subsys);