1 /*
2  * Copyright (c) 2015, 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 #include <linux/module.h>
34 #include <linux/configfs.h>
35 #include <rdma/ib_verbs.h>
36 #include <rdma/rdma_cm.h>
37 
38 #include "core_priv.h"
39 #include "cma_priv.h"
40 
41 struct cma_device;
42 
43 struct cma_dev_group;
44 
45 struct cma_dev_port_group {
46 	unsigned int		port_num;
47 	struct cma_dev_group	*cma_dev_group;
48 	struct config_group	group;
49 };
50 
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;
56 };
57 
58 static struct cma_dev_port_group *to_dev_port_group(struct config_item *item)
59 {
60 	struct config_group *group;
61 
62 	if (!item)
63 		return NULL;
64 
65 	group = container_of(item, struct config_group, cg_item);
66 	return container_of(group, struct cma_dev_port_group, group);
67 }
68 
69 static bool filter_by_name(struct ib_device *ib_dev, void *cookie)
70 {
71 	return !strcmp(dev_name(&ib_dev->dev), cookie);
72 }
73 
74 static int cma_configfs_params_get(struct config_item *item,
75 				   struct cma_device **pcma_dev,
76 				   struct cma_dev_port_group **pgroup)
77 {
78 	struct cma_dev_port_group *group = to_dev_port_group(item);
79 	struct cma_device *cma_dev;
80 
81 	if (!group)
82 		return -ENODEV;
83 
84 	cma_dev = cma_enum_devices_by_ibdev(filter_by_name,
85 					    group->cma_dev_group->name);
86 	if (!cma_dev)
87 		return -ENODEV;
88 
89 	*pcma_dev = cma_dev;
90 	*pgroup = group;
91 
92 	return 0;
93 }
94 
95 static void cma_configfs_params_put(struct cma_device *cma_dev)
96 {
97 	cma_dev_put(cma_dev);
98 }
99 
100 static ssize_t default_roce_mode_show(struct config_item *item,
101 				      char *buf)
102 {
103 	struct cma_device *cma_dev;
104 	struct cma_dev_port_group *group;
105 	int gid_type;
106 	ssize_t ret;
107 
108 	ret = cma_configfs_params_get(item, &cma_dev, &group);
109 	if (ret)
110 		return ret;
111 
112 	gid_type = cma_get_default_gid_type(cma_dev, group->port_num);
113 	cma_configfs_params_put(cma_dev);
114 
115 	if (gid_type < 0)
116 		return gid_type;
117 
118 	return sysfs_emit(buf, "%s\n", ib_cache_gid_type_str(gid_type));
119 }
120 
121 static ssize_t default_roce_mode_store(struct config_item *item,
122 				       const char *buf, size_t count)
123 {
124 	struct cma_device *cma_dev;
125 	struct cma_dev_port_group *group;
126 	int gid_type;
127 	ssize_t ret;
128 
129 	ret = cma_configfs_params_get(item, &cma_dev, &group);
130 	if (ret)
131 		return ret;
132 
133 	gid_type = ib_cache_gid_parse_type_str(buf);
134 	if (gid_type < 0) {
135 		cma_configfs_params_put(cma_dev);
136 		return -EINVAL;
137 	}
138 
139 	ret = cma_set_default_gid_type(cma_dev, group->port_num, gid_type);
140 
141 	cma_configfs_params_put(cma_dev);
142 
143 	return !ret ? strnlen(buf, count) : ret;
144 }
145 
146 CONFIGFS_ATTR(, default_roce_mode);
147 
148 static ssize_t default_roce_tos_show(struct config_item *item, char *buf)
149 {
150 	struct cma_device *cma_dev;
151 	struct cma_dev_port_group *group;
152 	ssize_t ret;
153 	u8 tos;
154 
155 	ret = cma_configfs_params_get(item, &cma_dev, &group);
156 	if (ret)
157 		return ret;
158 
159 	tos = cma_get_default_roce_tos(cma_dev, group->port_num);
160 	cma_configfs_params_put(cma_dev);
161 
162 	return sysfs_emit(buf, "%u\n", tos);
163 }
164 
165 static ssize_t default_roce_tos_store(struct config_item *item,
166 				      const char *buf, size_t count)
167 {
168 	struct cma_device *cma_dev;
169 	struct cma_dev_port_group *group;
170 	ssize_t ret;
171 	u8 tos;
172 
173 	ret = kstrtou8(buf, 0, &tos);
174 	if (ret)
175 		return ret;
176 
177 	ret = cma_configfs_params_get(item, &cma_dev, &group);
178 	if (ret)
179 		return ret;
180 
181 	ret = cma_set_default_roce_tos(cma_dev, group->port_num, tos);
182 	cma_configfs_params_put(cma_dev);
183 
184 	return ret ? ret : strnlen(buf, count);
185 }
186 
187 CONFIGFS_ATTR(, default_roce_tos);
188 
189 static struct configfs_attribute *cma_configfs_attributes[] = {
190 	&attr_default_roce_mode,
191 	&attr_default_roce_tos,
192 	NULL,
193 };
194 
195 static const struct config_item_type cma_port_group_type = {
196 	.ct_attrs	= cma_configfs_attributes,
197 	.ct_owner	= THIS_MODULE
198 };
199 
200 static int make_cma_ports(struct cma_dev_group *cma_dev_group,
201 			  struct cma_device *cma_dev)
202 {
203 	struct ib_device *ibdev;
204 	unsigned int i;
205 	unsigned int ports_num;
206 	struct cma_dev_port_group *ports;
207 	int err;
208 
209 	ibdev = cma_get_ib_dev(cma_dev);
210 
211 	if (!ibdev)
212 		return -ENODEV;
213 
214 	ports_num = ibdev->phys_port_cnt;
215 	ports = kcalloc(ports_num, sizeof(*cma_dev_group->ports),
216 			GFP_KERNEL);
217 
218 	if (!ports) {
219 		err = -ENOMEM;
220 		goto free;
221 	}
222 
223 	for (i = 0; i < ports_num; i++) {
224 		char port_str[10];
225 
226 		ports[i].port_num = i + 1;
227 		snprintf(port_str, sizeof(port_str), "%u", i + 1);
228 		ports[i].cma_dev_group = cma_dev_group;
229 		config_group_init_type_name(&ports[i].group,
230 					    port_str,
231 					    &cma_port_group_type);
232 		configfs_add_default_group(&ports[i].group,
233 				&cma_dev_group->ports_group);
234 
235 	}
236 	cma_dev_group->ports = ports;
237 
238 	return 0;
239 free:
240 	kfree(ports);
241 	cma_dev_group->ports = NULL;
242 	return err;
243 }
244 
245 static void release_cma_dev(struct config_item  *item)
246 {
247 	struct config_group *group = container_of(item, struct config_group,
248 						  cg_item);
249 	struct cma_dev_group *cma_dev_group = container_of(group,
250 							   struct cma_dev_group,
251 							   device_group);
252 
253 	kfree(cma_dev_group);
254 };
255 
256 static void release_cma_ports_group(struct config_item  *item)
257 {
258 	struct config_group *group = container_of(item, struct config_group,
259 						  cg_item);
260 	struct cma_dev_group *cma_dev_group = container_of(group,
261 							   struct cma_dev_group,
262 							   ports_group);
263 
264 	kfree(cma_dev_group->ports);
265 	cma_dev_group->ports = NULL;
266 };
267 
268 static struct configfs_item_operations cma_ports_item_ops = {
269 	.release = release_cma_ports_group
270 };
271 
272 static const struct config_item_type cma_ports_group_type = {
273 	.ct_item_ops	= &cma_ports_item_ops,
274 	.ct_owner	= THIS_MODULE
275 };
276 
277 static struct configfs_item_operations cma_device_item_ops = {
278 	.release = release_cma_dev
279 };
280 
281 static const struct config_item_type cma_device_group_type = {
282 	.ct_item_ops	= &cma_device_item_ops,
283 	.ct_owner	= THIS_MODULE
284 };
285 
286 static struct config_group *make_cma_dev(struct config_group *group,
287 					 const char *name)
288 {
289 	int err = -ENODEV;
290 	struct cma_device *cma_dev = cma_enum_devices_by_ibdev(filter_by_name,
291 							       (void *)name);
292 	struct cma_dev_group *cma_dev_group = NULL;
293 
294 	if (!cma_dev)
295 		goto fail;
296 
297 	cma_dev_group = kzalloc(sizeof(*cma_dev_group), GFP_KERNEL);
298 
299 	if (!cma_dev_group) {
300 		err = -ENOMEM;
301 		goto fail;
302 	}
303 
304 	strlcpy(cma_dev_group->name, name, sizeof(cma_dev_group->name));
305 
306 	config_group_init_type_name(&cma_dev_group->ports_group, "ports",
307 				    &cma_ports_group_type);
308 
309 	err = make_cma_ports(cma_dev_group, cma_dev);
310 	if (err)
311 		goto fail;
312 
313 	config_group_init_type_name(&cma_dev_group->device_group, name,
314 				    &cma_device_group_type);
315 	configfs_add_default_group(&cma_dev_group->ports_group,
316 			&cma_dev_group->device_group);
317 
318 	cma_dev_put(cma_dev);
319 	return &cma_dev_group->device_group;
320 
321 fail:
322 	if (cma_dev)
323 		cma_dev_put(cma_dev);
324 	kfree(cma_dev_group);
325 	return ERR_PTR(err);
326 }
327 
328 static void drop_cma_dev(struct config_group *cgroup, struct config_item *item)
329 {
330 	struct config_group *group =
331 		container_of(item, struct config_group, cg_item);
332 	struct cma_dev_group *cma_dev_group =
333 		container_of(group, struct cma_dev_group, device_group);
334 
335 	configfs_remove_default_groups(&cma_dev_group->ports_group);
336 	configfs_remove_default_groups(&cma_dev_group->device_group);
337 	config_item_put(item);
338 }
339 
340 static struct configfs_group_operations cma_subsys_group_ops = {
341 	.make_group	= make_cma_dev,
342 	.drop_item	= drop_cma_dev,
343 };
344 
345 static const struct config_item_type cma_subsys_type = {
346 	.ct_group_ops	= &cma_subsys_group_ops,
347 	.ct_owner	= THIS_MODULE,
348 };
349 
350 static struct configfs_subsystem cma_subsys = {
351 	.su_group	= {
352 		.cg_item	= {
353 			.ci_namebuf	= "rdma_cm",
354 			.ci_type	= &cma_subsys_type,
355 		},
356 	},
357 };
358 
359 int __init cma_configfs_init(void)
360 {
361 	int ret;
362 
363 	config_group_init(&cma_subsys.su_group);
364 	mutex_init(&cma_subsys.su_mutex);
365 	ret = configfs_register_subsystem(&cma_subsys);
366 	if (ret)
367 		mutex_destroy(&cma_subsys.su_mutex);
368 	return ret;
369 }
370 
371 void __exit cma_configfs_exit(void)
372 {
373 	configfs_unregister_subsystem(&cma_subsys);
374 	mutex_destroy(&cma_subsys.su_mutex);
375 }
376