Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux/fpc-iii.git] / drivers / infiniband / hw / qib / qib_debugfs.c
blob799a0c3bffc4a0e3bc10570e456630d1bea43f4f
1 #ifdef CONFIG_DEBUG_FS
2 /*
3 * Copyright (c) 2013 Intel Corporation. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
33 #include <linux/debugfs.h>
34 #include <linux/seq_file.h>
35 #include <linux/kernel.h>
36 #include <linux/export.h>
38 #include "qib.h"
39 #include "qib_verbs.h"
40 #include "qib_debugfs.h"
42 static struct dentry *qib_dbg_root;
44 #define DEBUGFS_FILE(name) \
45 static const struct seq_operations _##name##_seq_ops = { \
46 .start = _##name##_seq_start, \
47 .next = _##name##_seq_next, \
48 .stop = _##name##_seq_stop, \
49 .show = _##name##_seq_show \
50 }; \
51 static int _##name##_open(struct inode *inode, struct file *s) \
52 { \
53 struct seq_file *seq; \
54 int ret; \
55 ret = seq_open(s, &_##name##_seq_ops); \
56 if (ret) \
57 return ret; \
58 seq = s->private_data; \
59 seq->private = inode->i_private; \
60 return 0; \
61 } \
62 static const struct file_operations _##name##_file_ops = { \
63 .owner = THIS_MODULE, \
64 .open = _##name##_open, \
65 .read = seq_read, \
66 .llseek = seq_lseek, \
67 .release = seq_release \
70 #define DEBUGFS_FILE_CREATE(name) \
71 do { \
72 struct dentry *ent; \
73 ent = debugfs_create_file(#name , 0400, ibd->qib_ibdev_dbg, \
74 ibd, &_##name##_file_ops); \
75 if (!ent) \
76 pr_warn("create of " #name " failed\n"); \
77 } while (0)
79 static void *_opcode_stats_seq_start(struct seq_file *s, loff_t *pos)
81 struct qib_opcode_stats_perctx *opstats;
83 if (*pos >= ARRAY_SIZE(opstats->stats))
84 return NULL;
85 return pos;
88 static void *_opcode_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
90 struct qib_opcode_stats_perctx *opstats;
92 ++*pos;
93 if (*pos >= ARRAY_SIZE(opstats->stats))
94 return NULL;
95 return pos;
99 static void _opcode_stats_seq_stop(struct seq_file *s, void *v)
101 /* nothing allocated */
104 static int _opcode_stats_seq_show(struct seq_file *s, void *v)
106 loff_t *spos = v;
107 loff_t i = *spos, j;
108 u64 n_packets = 0, n_bytes = 0;
109 struct qib_ibdev *ibd = (struct qib_ibdev *)s->private;
110 struct qib_devdata *dd = dd_from_dev(ibd);
112 for (j = 0; j < dd->first_user_ctxt; j++) {
113 if (!dd->rcd[j])
114 continue;
115 n_packets += dd->rcd[j]->opstats->stats[i].n_packets;
116 n_bytes += dd->rcd[j]->opstats->stats[i].n_bytes;
118 if (!n_packets && !n_bytes)
119 return SEQ_SKIP;
120 seq_printf(s, "%02llx %llu/%llu\n", i,
121 (unsigned long long) n_packets,
122 (unsigned long long) n_bytes);
124 return 0;
127 DEBUGFS_FILE(opcode_stats)
129 static void *_ctx_stats_seq_start(struct seq_file *s, loff_t *pos)
131 struct qib_ibdev *ibd = (struct qib_ibdev *)s->private;
132 struct qib_devdata *dd = dd_from_dev(ibd);
134 if (!*pos)
135 return SEQ_START_TOKEN;
136 if (*pos >= dd->first_user_ctxt)
137 return NULL;
138 return pos;
141 static void *_ctx_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
143 struct qib_ibdev *ibd = (struct qib_ibdev *)s->private;
144 struct qib_devdata *dd = dd_from_dev(ibd);
146 if (v == SEQ_START_TOKEN)
147 return pos;
149 ++*pos;
150 if (*pos >= dd->first_user_ctxt)
151 return NULL;
152 return pos;
155 static void _ctx_stats_seq_stop(struct seq_file *s, void *v)
157 /* nothing allocated */
160 static int _ctx_stats_seq_show(struct seq_file *s, void *v)
162 loff_t *spos;
163 loff_t i, j;
164 u64 n_packets = 0;
165 struct qib_ibdev *ibd = (struct qib_ibdev *)s->private;
166 struct qib_devdata *dd = dd_from_dev(ibd);
168 if (v == SEQ_START_TOKEN) {
169 seq_puts(s, "Ctx:npkts\n");
170 return 0;
173 spos = v;
174 i = *spos;
176 if (!dd->rcd[i])
177 return SEQ_SKIP;
179 for (j = 0; j < ARRAY_SIZE(dd->rcd[i]->opstats->stats); j++)
180 n_packets += dd->rcd[i]->opstats->stats[j].n_packets;
182 if (!n_packets)
183 return SEQ_SKIP;
185 seq_printf(s, " %llu:%llu\n", i, n_packets);
186 return 0;
189 DEBUGFS_FILE(ctx_stats)
191 static void *_qp_stats_seq_start(struct seq_file *s, loff_t *pos)
193 struct qib_qp_iter *iter;
194 loff_t n = *pos;
196 iter = qib_qp_iter_init(s->private);
197 if (!iter)
198 return NULL;
200 while (n--) {
201 if (qib_qp_iter_next(iter)) {
202 kfree(iter);
203 return NULL;
207 return iter;
210 static void *_qp_stats_seq_next(struct seq_file *s, void *iter_ptr,
211 loff_t *pos)
213 struct qib_qp_iter *iter = iter_ptr;
215 (*pos)++;
217 if (qib_qp_iter_next(iter)) {
218 kfree(iter);
219 return NULL;
222 return iter;
225 static void _qp_stats_seq_stop(struct seq_file *s, void *iter_ptr)
227 /* nothing for now */
230 static int _qp_stats_seq_show(struct seq_file *s, void *iter_ptr)
232 struct qib_qp_iter *iter = iter_ptr;
234 if (!iter)
235 return 0;
237 qib_qp_iter_print(s, iter);
239 return 0;
242 DEBUGFS_FILE(qp_stats)
244 void qib_dbg_ibdev_init(struct qib_ibdev *ibd)
246 char name[10];
248 snprintf(name, sizeof(name), "qib%d", dd_from_dev(ibd)->unit);
249 ibd->qib_ibdev_dbg = debugfs_create_dir(name, qib_dbg_root);
250 if (!ibd->qib_ibdev_dbg) {
251 pr_warn("create of %s failed\n", name);
252 return;
254 DEBUGFS_FILE_CREATE(opcode_stats);
255 DEBUGFS_FILE_CREATE(ctx_stats);
256 DEBUGFS_FILE_CREATE(qp_stats);
257 return;
260 void qib_dbg_ibdev_exit(struct qib_ibdev *ibd)
262 if (!qib_dbg_root)
263 goto out;
264 debugfs_remove_recursive(ibd->qib_ibdev_dbg);
265 out:
266 ibd->qib_ibdev_dbg = NULL;
269 void qib_dbg_init(void)
271 qib_dbg_root = debugfs_create_dir(QIB_DRV_NAME, NULL);
272 if (!qib_dbg_root)
273 pr_warn("init of debugfs failed\n");
276 void qib_dbg_exit(void)
278 debugfs_remove_recursive(qib_dbg_root);
279 qib_dbg_root = NULL;
282 #endif