summaryrefslogtreecommitdiff
path: root/drivers/infiniband/core/cma_configfs.c
blob: 18b112aa577e5a260b3081bfa944b6b222074fa3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
 * Copyright (c) 2015, Mellanox Technologies inc.  All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <linux/module.h>
#include <linux/configfs.h>
#include <rdma/ib_verbs.h>
#include "core_priv.h"

struct cma_device;

struct cma_dev_group;

struct cma_dev_port_group {
	unsigned int		port_num;
	struct cma_dev_group	*cma_dev_group;
	struct config_group	group;
};

struct cma_dev_group {
	char				name[IB_DEVICE_NAME_MAX];
	struct config_group		device_group;
	struct config_group		ports_group;
	struct config_group		*default_dev_group[2];
	struct config_group		**default_ports_group;
	struct cma_dev_port_group	*ports;
};

static struct cma_dev_port_group *to_dev_port_group(struct config_item *item)
{
	struct config_group *group;

	if (!item)
		return NULL;

	group = container_of(item, struct config_group, cg_item);
	return container_of(group, struct cma_dev_port_group, group);
}

static bool filter_by_name(struct ib_device *ib_dev, void *cookie)
{
	return !strcmp(ib_dev->name, cookie);
}

static int cma_configfs_params_get(struct config_item *item,
				   struct cma_device **pcma_dev,
				   struct cma_dev_port_group **pgroup)
{
	struct cma_dev_port_group *group = to_dev_port_group(item);
	struct cma_device *cma_dev;

	if (!group)
		return -ENODEV;

	cma_dev = cma_enum_devices_by_ibdev(filter_by_name,
					    group->cma_dev_group->name);
	if (!cma_dev)
		return -ENODEV;

	*pcma_dev = cma_dev;
	*pgroup = group;

	return 0;
}

static void cma_configfs_params_put(struct cma_device *cma_dev)
{
	cma_deref_dev(cma_dev);
}

static ssize_t default_roce_mode_show(struct config_item *item,
				      char *buf)
{
	struct cma_device *cma_dev;
	struct cma_dev_port_group *group;
	int gid_type;
	ssize_t ret;

	ret = cma_configfs_params_get(item, &cma_dev, &group);
	if (ret)
		return ret;

	gid_type = cma_get_default_gid_type(cma_dev, group->port_num);
	cma_configfs_params_put(cma_dev);

	if (gid_type < 0)
		return gid_type;

	return sprintf(buf, "%s\n", ib_cache_gid_type_str(gid_type));
}

static ssize_t default_roce_mode_store(struct config_item *item,
				       const char *buf, size_t count)
{
	struct cma_device *cma_dev;
	struct cma_dev_port_group *group;
	int gid_type = ib_cache_gid_parse_type_str(buf);
	ssize_t ret;

	if (gid_type < 0)
		return -EINVAL;

	ret = cma_configfs_params_get(item, &cma_dev, &group);
	if (ret)
		return ret;

	ret = cma_set_default_gid_type(cma_dev, group->port_num, gid_type);

	cma_configfs_params_put(cma_dev);

	return !ret ? strnlen(buf, count) : ret;
}

CONFIGFS_ATTR(, default_roce_mode);

static struct configfs_attribute *cma_configfs_attributes[] = {
	&attr_default_roce_mode,
	NULL,
};

static struct config_item_type cma_port_group_type = {
	.ct_attrs	= cma_configfs_attributes,
	.ct_owner	= THIS_MODULE
};

static int make_cma_ports(struct cma_dev_group *cma_dev_group,
			  struct cma_device *cma_dev)
{
	struct ib_device *ibdev;
	unsigned int i;
	unsigned int ports_num;
	struct cma_dev_port_group *ports;
	struct config_group **ports_group;
	int err;

	ibdev = cma_get_ib_dev(cma_dev);

	if (!ibdev)
		return -ENODEV;

	ports_num = ibdev->phys_port_cnt;
	ports = kcalloc(ports_num, sizeof(*cma_dev_group->ports),
			GFP_KERNEL);
	ports_group = kcalloc(ports_num + 1, sizeof(*ports_group), GFP_KERNEL);

	if (!ports || !ports_group) {
		err = -ENOMEM;
		goto free;
	}

	for (i = 0; i < ports_num; i++) {
		char port_str[10];

		ports[i].port_num = i + 1;
		snprintf(port_str, sizeof(port_str), "%u", i + 1);
		ports[i].cma_dev_group = cma_dev_group;
		config_group_init_type_name(&ports[i].group,
					    port_str,
					    &cma_port_group_type);
		ports_group[i] = &ports[i].group;
	}
	ports_group[i] = NULL;
	cma_dev_group->default_ports_group = ports_group;
	cma_dev_group->ports = ports;

	return 0;
free:
	kfree(ports);
	kfree(ports_group);
	cma_dev_group->ports = NULL;
	cma_dev_group->default_ports_group = NULL;
	return err;
}

static void release_cma_dev(struct config_item  *item)
{
	struct config_group *group = container_of(item, struct config_group,
						  cg_item);
	struct cma_dev_group *cma_dev_group = container_of(group,
							   struct cma_dev_group,
							   device_group);

	kfree(cma_dev_group);
};

static void release_cma_ports_group(struct config_item  *item)
{
	struct config_group *group = container_of(item, struct config_group,
						  cg_item);
	struct cma_dev_group *cma_dev_group = container_of(group,
							   struct cma_dev_group,
							   ports_group);

	kfree(cma_dev_group->ports);
	kfree(cma_dev_group->default_ports_group);
	cma_dev_group->ports = NULL;
	cma_dev_group->default_ports_group = NULL;
};

static struct configfs_item_operations cma_ports_item_ops = {
	.release = release_cma_ports_group
};

static struct config_item_type cma_ports_group_type = {
	.ct_item_ops	= &cma_ports_item_ops,
	.ct_owner	= THIS_MODULE
};

static struct configfs_item_operations cma_device_item_ops = {
	.release = release_cma_dev
};

static struct config_item_type cma_device_group_type = {
	.ct_item_ops	= &cma_device_item_ops,
	.ct_owner	= THIS_MODULE
};

static struct config_group *make_cma_dev(struct config_group *group,
					 const char *name)
{
	int err = -ENODEV;
	struct cma_device *cma_dev = cma_enum_devices_by_ibdev(filter_by_name,
							       (void *)name);
	struct cma_dev_group *cma_dev_group = NULL;

	if (!cma_dev)
		goto fail;

	cma_dev_group = kzalloc(sizeof(*cma_dev_group), GFP_KERNEL);

	if (!cma_dev_group) {
		err = -ENOMEM;
		goto fail;
	}

	strncpy(cma_dev_group->name, name, sizeof(cma_dev_group->name));

	err = make_cma_ports(cma_dev_group, cma_dev);
	if (err)
		goto fail;

	cma_dev_group->ports_group.default_groups =
		cma_dev_group->default_ports_group;
	config_group_init_type_name(&cma_dev_group->ports_group, "ports",
				    &cma_ports_group_type);

	cma_dev_group->device_group.default_groups
		= cma_dev_group->default_dev_group;
	cma_dev_group->default_dev_group[0] = &cma_dev_group->ports_group;
	cma_dev_group->default_dev_group[1] = NULL;

	config_group_init_type_name(&cma_dev_group->device_group, name,
				    &cma_device_group_type);

	cma_deref_dev(cma_dev);
	return &cma_dev_group->device_group;

fail:
	if (cma_dev)
		cma_deref_dev(cma_dev);
	kfree(cma_dev_group);
	return ERR_PTR(err);
}

static struct configfs_group_operations cma_subsys_group_ops = {
	.make_group	= make_cma_dev,
};

static struct config_item_type cma_subsys_type = {
	.ct_group_ops	= &cma_subsys_group_ops,
	.ct_owner	= THIS_MODULE,
};

static struct configfs_subsystem cma_subsys = {
	.su_group	= {
		.cg_item	= {
			.ci_namebuf	= "rdma_cm",
			.ci_type	= &cma_subsys_type,
		},
	},
};

int __init cma_configfs_init(void)
{
	config_group_init(&cma_subsys.su_group);
	mutex_init(&cma_subsys.su_mutex);
	return configfs_register_subsystem(&cma_subsys);
}

void __exit cma_configfs_exit(void)
{
	configfs_unregister_subsystem(&cma_subsys);
}