1/*
2 * Copyright (c) 2004 Topspin Communications.  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 <sys/cdefs.h>
34__FBSDID("$FreeBSD$");
35
36#include <linux/err.h>
37#include <linux/seq_file.h>
38
39struct file_operations;
40
41#include <linux/debugfs.h>
42
43#include "ipoib.h"
44
45static struct dentry *ipoib_root;
46
47static void format_gid(union ib_gid *gid, char *buf)
48{
49	int i, n;
50
51	for (n = 0, i = 0; i < 8; ++i) {
52		n += sprintf(buf + n, "%x",
53			     be16_to_cpu(((__be16 *) gid->raw)[i]));
54		if (i < 7)
55			buf[n++] = ':';
56	}
57}
58
59static void *ipoib_mcg_seq_start(struct seq_file *file, loff_t *pos)
60{
61	struct ipoib_mcast_iter *iter;
62	loff_t n = *pos;
63
64	iter = ipoib_mcast_iter_init(file->private);
65	if (!iter)
66		return NULL;
67
68	while (n--) {
69		if (ipoib_mcast_iter_next(iter)) {
70			kfree(iter);
71			return NULL;
72		}
73	}
74
75	return iter;
76}
77
78static void *ipoib_mcg_seq_next(struct seq_file *file, void *iter_ptr,
79				   loff_t *pos)
80{
81	struct ipoib_mcast_iter *iter = iter_ptr;
82
83	(*pos)++;
84
85	if (ipoib_mcast_iter_next(iter)) {
86		kfree(iter);
87		return NULL;
88	}
89
90	return iter;
91}
92
93static void ipoib_mcg_seq_stop(struct seq_file *file, void *iter_ptr)
94{
95	/* nothing for now */
96}
97
98static int ipoib_mcg_seq_show(struct seq_file *file, void *iter_ptr)
99{
100	struct ipoib_mcast_iter *iter = iter_ptr;
101	char gid_buf[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"];
102	union ib_gid mgid;
103	unsigned long created;
104	unsigned int queuelen, complete, send_only;
105
106	if (!iter)
107		return 0;
108
109	ipoib_mcast_iter_read(iter, &mgid, &created, &queuelen,
110			      &complete, &send_only);
111
112	format_gid(&mgid, gid_buf);
113
114	seq_printf(file,
115		   "GID: %s\n"
116		   "  created: %10ld\n"
117		   "  queuelen: %9d\n"
118		   "  complete: %9s\n"
119		   "  send_only: %8s\n"
120		   "\n",
121		   gid_buf, created, queuelen,
122		   complete ? "yes" : "no",
123		   send_only ? "yes" : "no");
124
125	return 0;
126}
127
128static const struct seq_operations ipoib_mcg_seq_ops = {
129	.start = ipoib_mcg_seq_start,
130	.next  = ipoib_mcg_seq_next,
131	.stop  = ipoib_mcg_seq_stop,
132	.show  = ipoib_mcg_seq_show,
133};
134
135static int ipoib_mcg_open(struct inode *inode, struct file *file)
136{
137	struct seq_file *seq;
138	int ret;
139
140	ret = seq_open(file, &ipoib_mcg_seq_ops);
141	if (ret)
142		return ret;
143
144	seq = file->private_data;
145	seq->private = inode->i_private;
146
147	return 0;
148}
149
150static const struct file_operations ipoib_mcg_fops = {
151	.owner   = THIS_MODULE,
152	.open    = ipoib_mcg_open,
153	.read    = seq_read,
154	.llseek  = seq_lseek,
155	.release = seq_release
156};
157
158static void *ipoib_path_seq_start(struct seq_file *file, loff_t *pos)
159{
160	struct ipoib_path_iter *iter;
161	loff_t n = *pos;
162
163	iter = ipoib_path_iter_init(file->private);
164	if (!iter)
165		return NULL;
166
167	while (n--) {
168		if (ipoib_path_iter_next(iter)) {
169			kfree(iter);
170			return NULL;
171		}
172	}
173
174	return iter;
175}
176
177static void *ipoib_path_seq_next(struct seq_file *file, void *iter_ptr,
178				   loff_t *pos)
179{
180	struct ipoib_path_iter *iter = iter_ptr;
181
182	(*pos)++;
183
184	if (ipoib_path_iter_next(iter)) {
185		kfree(iter);
186		return NULL;
187	}
188
189	return iter;
190}
191
192static void ipoib_path_seq_stop(struct seq_file *file, void *iter_ptr)
193{
194	/* nothing for now */
195}
196
197static int ipoib_path_seq_show(struct seq_file *file, void *iter_ptr)
198{
199	struct ipoib_path_iter *iter = iter_ptr;
200	char gid_buf[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"];
201	struct ipoib_path path;
202	int rate;
203
204	if (!iter)
205		return 0;
206
207	ipoib_path_iter_read(iter, &path);
208
209	format_gid(&path.pathrec.dgid, gid_buf);
210
211	seq_printf(file,
212		   "GID: %s\n"
213		   "  complete: %6s\n",
214		   gid_buf, path.pathrec.dlid ? "yes" : "no");
215
216	if (path.pathrec.dlid) {
217		rate = ib_rate_to_mult(path.pathrec.rate) * 25;
218
219		seq_printf(file,
220			   "  DLID:     0x%04x\n"
221			   "  SL: %12d\n"
222			   "  rate: %*d%s Gb/sec\n",
223			   be16_to_cpu(path.pathrec.dlid),
224			   path.pathrec.sl,
225			   10 - ((rate % 10) ? 2 : 0),
226			   rate / 10, rate % 10 ? ".5" : "");
227	}
228
229	seq_putc(file, '\n');
230
231	return 0;
232}
233
234static const struct seq_operations ipoib_path_seq_ops = {
235	.start = ipoib_path_seq_start,
236	.next  = ipoib_path_seq_next,
237	.stop  = ipoib_path_seq_stop,
238	.show  = ipoib_path_seq_show,
239};
240
241static int ipoib_path_open(struct inode *inode, struct file *file)
242{
243	struct seq_file *seq;
244	int ret;
245
246	ret = seq_open(file, &ipoib_path_seq_ops);
247	if (ret)
248		return ret;
249
250	seq = file->private_data;
251	seq->private = inode->i_private;
252
253	return 0;
254}
255
256static const struct file_operations ipoib_path_fops = {
257	.owner   = THIS_MODULE,
258	.open    = ipoib_path_open,
259	.read    = seq_read,
260	.llseek  = seq_lseek,
261	.release = seq_release
262};
263
264void ipoib_create_debug_files(struct ifnet *dev)
265{
266	struct ipoib_dev_priv *priv = dev->if_softc;
267	char name[IFNAMSIZ + sizeof "_path"];
268
269	snprintf(name, sizeof name, "%s_mcg", if_name(dev));
270	priv->mcg_dentry = debugfs_create_file(name, S_IFREG | S_IRUGO,
271					       ipoib_root, dev, &ipoib_mcg_fops);
272	if (!priv->mcg_dentry)
273		ipoib_warn(priv, "failed to create mcg debug file\n");
274
275	snprintf(name, sizeof name, "%s_path", if_name(dev));
276	priv->path_dentry = debugfs_create_file(name, S_IFREG | S_IRUGO,
277						ipoib_root, dev, &ipoib_path_fops);
278	if (!priv->path_dentry)
279		ipoib_warn(priv, "failed to create path debug file\n");
280}
281
282void ipoib_delete_debug_files(struct ifnet *dev)
283{
284	struct ipoib_dev_priv *priv = dev->if_softc;
285
286	if (priv->mcg_dentry)
287		debugfs_remove(priv->mcg_dentry);
288	if (priv->path_dentry)
289		debugfs_remove(priv->path_dentry);
290}
291
292int ipoib_register_debugfs(void)
293{
294	ipoib_root = debugfs_create_dir("ipoib", NULL);
295	return ipoib_root ? 0 : -ENOMEM;
296}
297
298void ipoib_unregister_debugfs(void)
299{
300	debugfs_remove(ipoib_root);
301}
302