1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3  *
4  * Copyright (c) 2004 Topspin Communications.  All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <linux/err.h>
39 #include <linux/seq_file.h>
40 
41 struct file_operations;
42 
43 #include <linux/debugfs.h>
44 
45 #include "ipoib.h"
46 
47 static struct dentry *ipoib_root;
48 
49 static void format_gid(union ib_gid *gid, char *buf)
50 {
51 	int i, n;
52 
53 	for (n = 0, i = 0; i < 8; ++i) {
54 		n += sprintf(buf + n, "%x",
55 			     be16_to_cpu(((__be16 *) gid->raw)[i]));
56 		if (i < 7)
57 			buf[n++] = ':';
58 	}
59 }
60 
61 static void *ipoib_mcg_seq_start(struct seq_file *file, loff_t *pos)
62 {
63 	struct ipoib_mcast_iter *iter;
64 	loff_t n = *pos;
65 
66 	iter = ipoib_mcast_iter_init(file->private);
67 	if (!iter)
68 		return NULL;
69 
70 	while (n--) {
71 		if (ipoib_mcast_iter_next(iter)) {
72 			kfree(iter);
73 			return NULL;
74 		}
75 	}
76 
77 	return iter;
78 }
79 
80 static void *ipoib_mcg_seq_next(struct seq_file *file, void *iter_ptr,
81 				   loff_t *pos)
82 {
83 	struct ipoib_mcast_iter *iter = iter_ptr;
84 
85 	(*pos)++;
86 
87 	if (ipoib_mcast_iter_next(iter)) {
88 		kfree(iter);
89 		return NULL;
90 	}
91 
92 	return iter;
93 }
94 
95 static void ipoib_mcg_seq_stop(struct seq_file *file, void *iter_ptr)
96 {
97 	/* nothing for now */
98 }
99 
100 static int ipoib_mcg_seq_show(struct seq_file *file, void *iter_ptr)
101 {
102 	struct ipoib_mcast_iter *iter = iter_ptr;
103 	char gid_buf[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"];
104 	union ib_gid mgid;
105 	unsigned long created;
106 	unsigned int queuelen, complete, send_only;
107 
108 	if (!iter)
109 		return 0;
110 
111 	ipoib_mcast_iter_read(iter, &mgid, &created, &queuelen,
112 			      &complete, &send_only);
113 
114 	format_gid(&mgid, gid_buf);
115 
116 	seq_printf(file,
117 		   "GID: %s\n"
118 		   "  created: %10ld\n"
119 		   "  queuelen: %9d\n"
120 		   "  complete: %9s\n"
121 		   "  send_only: %8s\n"
122 		   "\n",
123 		   gid_buf, created, queuelen,
124 		   complete ? "yes" : "no",
125 		   send_only ? "yes" : "no");
126 
127 	return 0;
128 }
129 
130 static const struct seq_operations ipoib_mcg_seq_ops = {
131 	.start = ipoib_mcg_seq_start,
132 	.next  = ipoib_mcg_seq_next,
133 	.stop  = ipoib_mcg_seq_stop,
134 	.show  = ipoib_mcg_seq_show,
135 };
136 
137 static int ipoib_mcg_open(struct inode *inode, struct file *file)
138 {
139 	struct seq_file *seq;
140 	int ret;
141 
142 	ret = seq_open(file, &ipoib_mcg_seq_ops);
143 	if (ret)
144 		return ret;
145 
146 	seq = file->private_data;
147 	seq->private = inode->i_private;
148 
149 	return 0;
150 }
151 
152 static const struct file_operations ipoib_mcg_fops = {
153 	.owner   = THIS_MODULE,
154 	.open    = ipoib_mcg_open,
155 	.read    = seq_read,
156 	.llseek  = seq_lseek,
157 	.release = seq_release
158 };
159 
160 static void *ipoib_path_seq_start(struct seq_file *file, loff_t *pos)
161 {
162 	struct ipoib_path_iter *iter;
163 	loff_t n = *pos;
164 
165 	iter = ipoib_path_iter_init(file->private);
166 	if (!iter)
167 		return NULL;
168 
169 	while (n--) {
170 		if (ipoib_path_iter_next(iter)) {
171 			kfree(iter);
172 			return NULL;
173 		}
174 	}
175 
176 	return iter;
177 }
178 
179 static void *ipoib_path_seq_next(struct seq_file *file, void *iter_ptr,
180 				   loff_t *pos)
181 {
182 	struct ipoib_path_iter *iter = iter_ptr;
183 
184 	(*pos)++;
185 
186 	if (ipoib_path_iter_next(iter)) {
187 		kfree(iter);
188 		return NULL;
189 	}
190 
191 	return iter;
192 }
193 
194 static void ipoib_path_seq_stop(struct seq_file *file, void *iter_ptr)
195 {
196 	/* nothing for now */
197 }
198 
199 static int ipoib_path_seq_show(struct seq_file *file, void *iter_ptr)
200 {
201 	struct ipoib_path_iter *iter = iter_ptr;
202 	char gid_buf[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"];
203 	struct ipoib_path path;
204 	int rate;
205 
206 	if (!iter)
207 		return 0;
208 
209 	ipoib_path_iter_read(iter, &path);
210 
211 	format_gid(&path.pathrec.dgid, gid_buf);
212 
213 	seq_printf(file,
214 		   "GID: %s\n"
215 		   "  complete: %6s\n",
216 		   gid_buf, path.pathrec.dlid ? "yes" : "no");
217 
218 	if (path.pathrec.dlid) {
219 		rate = ib_rate_to_mult(path.pathrec.rate) * 25;
220 
221 		seq_printf(file,
222 			   "  DLID:     0x%04x\n"
223 			   "  SL: %12d\n"
224 			   "  rate: %*d%s Gb/sec\n",
225 			   be16_to_cpu(path.pathrec.dlid),
226 			   path.pathrec.sl,
227 			   10 - ((rate % 10) ? 2 : 0),
228 			   rate / 10, rate % 10 ? ".5" : "");
229 	}
230 
231 	seq_putc(file, '\n');
232 
233 	return 0;
234 }
235 
236 static const struct seq_operations ipoib_path_seq_ops = {
237 	.start = ipoib_path_seq_start,
238 	.next  = ipoib_path_seq_next,
239 	.stop  = ipoib_path_seq_stop,
240 	.show  = ipoib_path_seq_show,
241 };
242 
243 static int ipoib_path_open(struct inode *inode, struct file *file)
244 {
245 	struct seq_file *seq;
246 	int ret;
247 
248 	ret = seq_open(file, &ipoib_path_seq_ops);
249 	if (ret)
250 		return ret;
251 
252 	seq = file->private_data;
253 	seq->private = inode->i_private;
254 
255 	return 0;
256 }
257 
258 static const struct file_operations ipoib_path_fops = {
259 	.owner   = THIS_MODULE,
260 	.open    = ipoib_path_open,
261 	.read    = seq_read,
262 	.llseek  = seq_lseek,
263 	.release = seq_release
264 };
265 
266 void ipoib_create_debug_files(struct ifnet *dev)
267 {
268 	struct ipoib_dev_priv *priv = dev->if_softc;
269 	char name[IFNAMSIZ + sizeof "_path"];
270 
271 	snprintf(name, sizeof name, "%s_mcg", if_name(dev));
272 	priv->mcg_dentry = debugfs_create_file(name, S_IFREG | S_IRUGO,
273 					       ipoib_root, dev, &ipoib_mcg_fops);
274 	if (!priv->mcg_dentry)
275 		ipoib_warn(priv, "failed to create mcg debug file\n");
276 
277 	snprintf(name, sizeof name, "%s_path", if_name(dev));
278 	priv->path_dentry = debugfs_create_file(name, S_IFREG | S_IRUGO,
279 						ipoib_root, dev, &ipoib_path_fops);
280 	if (!priv->path_dentry)
281 		ipoib_warn(priv, "failed to create path debug file\n");
282 }
283 
284 void ipoib_delete_debug_files(struct ifnet *dev)
285 {
286 	struct ipoib_dev_priv *priv = dev->if_softc;
287 
288 	if (priv->mcg_dentry)
289 		debugfs_remove(priv->mcg_dentry);
290 	if (priv->path_dentry)
291 		debugfs_remove(priv->path_dentry);
292 }
293 
294 int ipoib_register_debugfs(void)
295 {
296 	ipoib_root = debugfs_create_dir("ipoib", NULL);
297 	return ipoib_root ? 0 : -ENOMEM;
298 }
299 
300 void ipoib_unregister_debugfs(void)
301 {
302 	debugfs_remove(ipoib_root);
303 }
304