aboutsummaryrefslogtreecommitdiff
path: root/drivers/infiniband/ulp/ipoib/ipoib_fs.c
blob: 044f2c78ef15d13595d444693a11eaaeb1f0357c (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
/*
 * Copyright (c) 2004 Topspin Communications.  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.
 *
 * $Id: ipoib_fs.c 1389 2004-12-27 22:56:47Z roland $
 */

#include <linux/pagemap.h>
#include <linux/seq_file.h>

#include "ipoib.h"

enum {
	IPOIB_MAGIC = 0x49504942 /* "IPIB" */
};

static DECLARE_MUTEX(ipoib_fs_mutex);
static struct dentry *ipoib_root;
static struct super_block *ipoib_sb;
static LIST_HEAD(ipoib_device_list);

static void *ipoib_mcg_seq_start(struct seq_file *file, loff_t *pos)
{
	struct ipoib_mcast_iter *iter;
	loff_t n = *pos;

	iter = ipoib_mcast_iter_init(file->private);
	if (!iter)
		return NULL;

	while (n--) {
		if (ipoib_mcast_iter_next(iter)) {
			ipoib_mcast_iter_free(iter);
			return NULL;
		}
	}

	return iter;
}

static void *ipoib_mcg_seq_next(struct seq_file *file, void *iter_ptr,
				   loff_t *pos)
{
	struct ipoib_mcast_iter *iter = iter_ptr;

	(*pos)++;

	if (ipoib_mcast_iter_next(iter)) {
		ipoib_mcast_iter_free(iter);
		return NULL;
	}

	return iter;
}

static void ipoib_mcg_seq_stop(struct seq_file *file, void *iter_ptr)
{
	/* nothing for now */
}

static int ipoib_mcg_seq_show(struct seq_file *file, void *iter_ptr)
{
	struct ipoib_mcast_iter *iter = iter_ptr;
	char gid_buf[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"];
	union ib_gid mgid;
	int i, n;
	unsigned long created;
	unsigned int queuelen, complete, send_only;

	if (iter) {
		ipoib_mcast_iter_read(iter, &mgid, &created, &queuelen,
				      &complete, &send_only);

		for (n = 0, i = 0; i < sizeof mgid / 2; ++i) {
			n += sprintf(gid_buf + n, "%x",
				     be16_to_cpu(((u16 *)mgid.raw)[i]));
			if (i < sizeof mgid / 2 - 1)
				gid_buf[n++] = ':';
		}
	}

	seq_printf(file, "GID: %*s", -(1 + (int) sizeof gid_buf), gid_buf);

	seq_printf(file,
		   " created: %10ld queuelen: %4d complete: %d send_only: %d\n",
		   created, queuelen, complete, send_only);

	return 0;
}

static struct seq_operations ipoib_seq_ops = {
	.start = ipoib_mcg_seq_start,
	.next  = ipoib_mcg_seq_next,
	.stop  = ipoib_mcg_seq_stop,
	.show  = ipoib_mcg_seq_show,
};

static int ipoib_mcg_open(struct inode *inode, struct file *file)
{
	struct seq_file *seq;
	int ret;

	ret = seq_open(file, &ipoib_seq_ops);
	if (ret)
		return ret;

	seq = file->private_data;
	seq->private = inode->u.generic_ip;

	return 0;
}

static struct file_operations ipoib_fops = {
	.owner   = THIS_MODULE,
	.open    = ipoib_mcg_open,
	.read    = seq_read,
	.llseek  = seq_lseek,
	.release = seq_release
};

static struct inode *ipoib_get_inode(void)
{
	struct inode *inode = new_inode(ipoib_sb);

	if (inode) {
		inode->i_mode 	 = S_IFREG | S_IRUGO;
		inode->i_uid 	 = 0;
		inode->i_gid 	 = 0;
		inode->i_blksize = PAGE_CACHE_SIZE;
		inode->i_blocks  = 0;
		inode->i_atime 	 = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
		inode->i_fop     = &ipoib_fops;
	}

	return inode;
}

static int __ipoib_create_debug_file(struct net_device *dev)
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);
	struct dentry *dentry;
	struct inode *inode;
	char name[IFNAMSIZ + sizeof "_mcg"];

	snprintf(name, sizeof name, "%s_mcg", dev->name);

	dentry = d_alloc_name(ipoib_root, name);
	if (!dentry)
		return -ENOMEM;

	inode = ipoib_get_inode();
	if (!inode) {
		dput(dentry);
		return -ENOMEM;
	}

	inode->u.generic_ip = dev;
	priv->mcg_dentry = dentry;

	d_add(dentry, inode);

	return 0;
}

int ipoib_create_debug_file(struct net_device *dev)
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);

	down(&ipoib_fs_mutex);

	list_add_tail(&priv->fs_list, &ipoib_device_list);

	if (!ipoib_sb) {
		up(&ipoib_fs_mutex);
		return 0;
	}

	up(&ipoib_fs_mutex);

	return __ipoib_create_debug_file(dev);
}

void ipoib_delete_debug_file(struct net_device *dev)
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);

	down(&ipoib_fs_mutex);
	list_del(&priv->fs_list);
	if (!ipoib_sb) {
		up(&ipoib_fs_mutex);
		return;
	}
	up(&ipoib_fs_mutex);

	if (priv->mcg_dentry) {
		d_drop(priv->mcg_dentry);
		simple_unlink(ipoib_root->d_inode, priv->mcg_dentry);
	}
}

static int ipoib_fill_super(struct super_block *sb, void *data, int silent)
{
	static struct tree_descr ipoib_files[] = {
		{ "" }
	};
	struct ipoib_dev_priv *priv;
	int ret;

	ret = simple_fill_super(sb, IPOIB_MAGIC, ipoib_files);
	if (ret)
		return ret;

	ipoib_root = sb->s_root;

	down(&ipoib_fs_mutex);

	ipoib_sb = sb;

	list_for_each_entry(priv, &ipoib_device_list, fs_list) {
		ret = __ipoib_create_debug_file(priv->dev);
		if (ret)
			break;
	}

	up(&ipoib_fs_mutex);

	return ret;
}

static struct super_block *ipoib_get_sb(struct file_system_type *fs_type,
	int flags, const char *dev_name, void *data)
{
	return get_sb_single(fs_type, flags, data, ipoib_fill_super);
}

static void ipoib_kill_sb(struct super_block *sb)
{
	down(&ipoib_fs_mutex);
	ipoib_sb = NULL;
	up(&ipoib_fs_mutex);

	kill_litter_super(sb);
}

static struct file_system_type ipoib_fs_type = {
	.owner		= THIS_MODULE,
	.name		= "ipoib_debugfs",
	.get_sb		= ipoib_get_sb,
	.kill_sb	= ipoib_kill_sb,
};

int ipoib_register_debugfs(void)
{
	return register_filesystem(&ipoib_fs_type);
}

void ipoib_unregister_debugfs(void)
{
	unregister_filesystem(&ipoib_fs_type);
}