RDMA/core: Change rdma_get_gid_attr returned error code
[linux-block.git] / drivers / infiniband / core / cma_configfs.c
CommitLineData
045959db
MB
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>
eeb8df87
PP
36#include <rdma/rdma_cm.h>
37
045959db 38#include "core_priv.h"
eeb8df87 39#include "cma_priv.h"
045959db
MB
40
41struct cma_device;
42
43struct cma_dev_group;
44
45struct cma_dev_port_group {
46 unsigned int port_num;
47 struct cma_dev_group *cma_dev_group;
48 struct config_group group;
49};
50
51struct cma_dev_group {
52 char name[IB_DEVICE_NAME_MAX];
53 struct config_group device_group;
54 struct config_group ports_group;
045959db
MB
55 struct cma_dev_port_group *ports;
56};
57
58static 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
69static bool filter_by_name(struct ib_device *ib_dev, void *cookie)
70{
896de009 71 return !strcmp(dev_name(&ib_dev->dev), cookie);
045959db
MB
72}
73
74static 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
95static void cma_configfs_params_put(struct cma_device *cma_dev)
96{
5ff8c8fa 97 cma_dev_put(cma_dev);
045959db
MB
98}
99
100static 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 sprintf(buf, "%s\n", ib_cache_gid_type_str(gid_type));
119}
120
121static 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 = ib_cache_gid_parse_type_str(buf);
127 ssize_t ret;
128
129 if (gid_type < 0)
130 return -EINVAL;
131
132 ret = cma_configfs_params_get(item, &cma_dev, &group);
133 if (ret)
134 return ret;
135
136 ret = cma_set_default_gid_type(cma_dev, group->port_num, gid_type);
137
138 cma_configfs_params_put(cma_dev);
139
140 return !ret ? strnlen(buf, count) : ret;
141}
142
143CONFIGFS_ATTR(, default_roce_mode);
144
89052d78
MD
145static ssize_t default_roce_tos_show(struct config_item *item, char *buf)
146{
147 struct cma_device *cma_dev;
148 struct cma_dev_port_group *group;
149 ssize_t ret;
150 u8 tos;
151
152 ret = cma_configfs_params_get(item, &cma_dev, &group);
153 if (ret)
154 return ret;
155
156 tos = cma_get_default_roce_tos(cma_dev, group->port_num);
157 cma_configfs_params_put(cma_dev);
158
159 return sprintf(buf, "%u\n", tos);
160}
161
162static ssize_t default_roce_tos_store(struct config_item *item,
163 const char *buf, size_t count)
164{
165 struct cma_device *cma_dev;
166 struct cma_dev_port_group *group;
167 ssize_t ret;
168 u8 tos;
169
170 ret = kstrtou8(buf, 0, &tos);
171 if (ret)
172 return ret;
173
174 ret = cma_configfs_params_get(item, &cma_dev, &group);
175 if (ret)
176 return ret;
177
178 ret = cma_set_default_roce_tos(cma_dev, group->port_num, tos);
179 cma_configfs_params_put(cma_dev);
180
181 return ret ? ret : strnlen(buf, count);
182}
183
184CONFIGFS_ATTR(, default_roce_tos);
185
045959db
MB
186static struct configfs_attribute *cma_configfs_attributes[] = {
187 &attr_default_roce_mode,
89052d78 188 &attr_default_roce_tos,
045959db
MB
189 NULL,
190};
191
6ace4f6b 192static const struct config_item_type cma_port_group_type = {
045959db
MB
193 .ct_attrs = cma_configfs_attributes,
194 .ct_owner = THIS_MODULE
195};
196
197static int make_cma_ports(struct cma_dev_group *cma_dev_group,
198 struct cma_device *cma_dev)
199{
200 struct ib_device *ibdev;
201 unsigned int i;
202 unsigned int ports_num;
203 struct cma_dev_port_group *ports;
204 int err;
205
206 ibdev = cma_get_ib_dev(cma_dev);
207
208 if (!ibdev)
209 return -ENODEV;
210
211 ports_num = ibdev->phys_port_cnt;
212 ports = kcalloc(ports_num, sizeof(*cma_dev_group->ports),
213 GFP_KERNEL);
214
1ae1602d 215 if (!ports) {
045959db
MB
216 err = -ENOMEM;
217 goto free;
218 }
219
220 for (i = 0; i < ports_num; i++) {
221 char port_str[10];
222
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,
227 port_str,
228 &cma_port_group_type);
1ae1602d
CH
229 configfs_add_default_group(&ports[i].group,
230 &cma_dev_group->ports_group);
231
045959db 232 }
045959db
MB
233 cma_dev_group->ports = ports;
234
235 return 0;
236free:
237 kfree(ports);
045959db 238 cma_dev_group->ports = NULL;
045959db
MB
239 return err;
240}
241
242static void release_cma_dev(struct config_item *item)
243{
244 struct config_group *group = container_of(item, struct config_group,
245 cg_item);
246 struct cma_dev_group *cma_dev_group = container_of(group,
247 struct cma_dev_group,
248 device_group);
249
250 kfree(cma_dev_group);
251};
252
253static void release_cma_ports_group(struct config_item *item)
254{
255 struct config_group *group = container_of(item, struct config_group,
256 cg_item);
257 struct cma_dev_group *cma_dev_group = container_of(group,
258 struct cma_dev_group,
259 ports_group);
260
261 kfree(cma_dev_group->ports);
045959db 262 cma_dev_group->ports = NULL;
045959db
MB
263};
264
265static struct configfs_item_operations cma_ports_item_ops = {
266 .release = release_cma_ports_group
267};
268
6ace4f6b 269static const struct config_item_type cma_ports_group_type = {
045959db
MB
270 .ct_item_ops = &cma_ports_item_ops,
271 .ct_owner = THIS_MODULE
272};
273
274static struct configfs_item_operations cma_device_item_ops = {
275 .release = release_cma_dev
276};
277
6ace4f6b 278static const struct config_item_type cma_device_group_type = {
045959db
MB
279 .ct_item_ops = &cma_device_item_ops,
280 .ct_owner = THIS_MODULE
281};
282
283static struct config_group *make_cma_dev(struct config_group *group,
284 const char *name)
285{
286 int err = -ENODEV;
287 struct cma_device *cma_dev = cma_enum_devices_by_ibdev(filter_by_name,
288 (void *)name);
289 struct cma_dev_group *cma_dev_group = NULL;
290
291 if (!cma_dev)
292 goto fail;
293
294 cma_dev_group = kzalloc(sizeof(*cma_dev_group), GFP_KERNEL);
295
296 if (!cma_dev_group) {
297 err = -ENOMEM;
298 goto fail;
299 }
300
979a459c 301 strlcpy(cma_dev_group->name, name, sizeof(cma_dev_group->name));
045959db 302
045959db
MB
303 config_group_init_type_name(&cma_dev_group->ports_group, "ports",
304 &cma_ports_group_type);
305
1ae1602d
CH
306 err = make_cma_ports(cma_dev_group, cma_dev);
307 if (err)
308 goto fail;
045959db
MB
309
310 config_group_init_type_name(&cma_dev_group->device_group, name,
311 &cma_device_group_type);
1ae1602d
CH
312 configfs_add_default_group(&cma_dev_group->ports_group,
313 &cma_dev_group->device_group);
045959db 314
5ff8c8fa 315 cma_dev_put(cma_dev);
045959db
MB
316 return &cma_dev_group->device_group;
317
318fail:
319 if (cma_dev)
5ff8c8fa 320 cma_dev_put(cma_dev);
045959db
MB
321 kfree(cma_dev_group);
322 return ERR_PTR(err);
323}
324
63a3345c
MG
325static void drop_cma_dev(struct config_group *cgroup, struct config_item *item)
326{
327 struct config_group *group =
328 container_of(item, struct config_group, cg_item);
329 struct cma_dev_group *cma_dev_group =
330 container_of(group, struct cma_dev_group, device_group);
331
332 configfs_remove_default_groups(&cma_dev_group->ports_group);
333 configfs_remove_default_groups(&cma_dev_group->device_group);
334 config_item_put(item);
335}
336
045959db
MB
337static struct configfs_group_operations cma_subsys_group_ops = {
338 .make_group = make_cma_dev,
63a3345c 339 .drop_item = drop_cma_dev,
045959db
MB
340};
341
6ace4f6b 342static const struct config_item_type cma_subsys_type = {
045959db
MB
343 .ct_group_ops = &cma_subsys_group_ops,
344 .ct_owner = THIS_MODULE,
345};
346
347static struct configfs_subsystem cma_subsys = {
348 .su_group = {
349 .cg_item = {
350 .ci_namebuf = "rdma_cm",
351 .ci_type = &cma_subsys_type,
352 },
353 },
354};
355
356int __init cma_configfs_init(void)
357{
56594ae1
PP
358 int ret;
359
045959db
MB
360 config_group_init(&cma_subsys.su_group);
361 mutex_init(&cma_subsys.su_mutex);
56594ae1
PP
362 ret = configfs_register_subsystem(&cma_subsys);
363 if (ret)
364 mutex_destroy(&cma_subsys.su_mutex);
365 return ret;
045959db
MB
366}
367
368void __exit cma_configfs_exit(void)
369{
370 configfs_unregister_subsystem(&cma_subsys);
56594ae1 371 mutex_destroy(&cma_subsys.su_mutex);
045959db 372}