qib_debugfs.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #ifdef CONFIG_DEBUG_FS
  2. /*
  3. * Copyright (c) 2013 Intel Corporation. All rights reserved.
  4. *
  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:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  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.
  23. *
  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.
  32. */
  33. #include <linux/debugfs.h>
  34. #include <linux/seq_file.h>
  35. #include <linux/kernel.h>
  36. #include <linux/export.h>
  37. #include "qib.h"
  38. #include "qib_verbs.h"
  39. #include "qib_debugfs.h"
  40. static struct dentry *qib_dbg_root;
  41. #define DEBUGFS_FILE(name) \
  42. static const struct seq_operations _##name##_seq_ops = { \
  43. .start = _##name##_seq_start, \
  44. .next = _##name##_seq_next, \
  45. .stop = _##name##_seq_stop, \
  46. .show = _##name##_seq_show \
  47. }; \
  48. static int _##name##_open(struct inode *inode, struct file *s) \
  49. { \
  50. struct seq_file *seq; \
  51. int ret; \
  52. ret = seq_open(s, &_##name##_seq_ops); \
  53. if (ret) \
  54. return ret; \
  55. seq = s->private_data; \
  56. seq->private = inode->i_private; \
  57. return 0; \
  58. } \
  59. static const struct file_operations _##name##_file_ops = { \
  60. .owner = THIS_MODULE, \
  61. .open = _##name##_open, \
  62. .read = seq_read, \
  63. .llseek = seq_lseek, \
  64. .release = seq_release \
  65. };
  66. #define DEBUGFS_FILE_CREATE(name) \
  67. do { \
  68. struct dentry *ent; \
  69. ent = debugfs_create_file(#name , 0400, ibd->qib_ibdev_dbg, \
  70. ibd, &_##name##_file_ops); \
  71. if (!ent) \
  72. pr_warn("create of " #name " failed\n"); \
  73. } while (0)
  74. static void *_opcode_stats_seq_start(struct seq_file *s, loff_t *pos)
  75. {
  76. struct qib_opcode_stats_perctx *opstats;
  77. if (*pos >= ARRAY_SIZE(opstats->stats))
  78. return NULL;
  79. return pos;
  80. }
  81. static void *_opcode_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
  82. {
  83. struct qib_opcode_stats_perctx *opstats;
  84. ++*pos;
  85. if (*pos >= ARRAY_SIZE(opstats->stats))
  86. return NULL;
  87. return pos;
  88. }
  89. static void _opcode_stats_seq_stop(struct seq_file *s, void *v)
  90. {
  91. /* nothing allocated */
  92. }
  93. static int _opcode_stats_seq_show(struct seq_file *s, void *v)
  94. {
  95. loff_t *spos = v;
  96. loff_t i = *spos, j;
  97. u64 n_packets = 0, n_bytes = 0;
  98. struct qib_ibdev *ibd = (struct qib_ibdev *)s->private;
  99. struct qib_devdata *dd = dd_from_dev(ibd);
  100. for (j = 0; j < dd->first_user_ctxt; j++) {
  101. if (!dd->rcd[j])
  102. continue;
  103. n_packets += dd->rcd[j]->opstats->stats[i].n_packets;
  104. n_bytes += dd->rcd[j]->opstats->stats[i].n_bytes;
  105. }
  106. if (!n_packets && !n_bytes)
  107. return SEQ_SKIP;
  108. seq_printf(s, "%02llx %llu/%llu\n", i,
  109. (unsigned long long) n_packets,
  110. (unsigned long long) n_bytes);
  111. return 0;
  112. }
  113. DEBUGFS_FILE(opcode_stats)
  114. static void *_ctx_stats_seq_start(struct seq_file *s, loff_t *pos)
  115. {
  116. struct qib_ibdev *ibd = (struct qib_ibdev *)s->private;
  117. struct qib_devdata *dd = dd_from_dev(ibd);
  118. if (!*pos)
  119. return SEQ_START_TOKEN;
  120. if (*pos >= dd->first_user_ctxt)
  121. return NULL;
  122. return pos;
  123. }
  124. static void *_ctx_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
  125. {
  126. struct qib_ibdev *ibd = (struct qib_ibdev *)s->private;
  127. struct qib_devdata *dd = dd_from_dev(ibd);
  128. if (v == SEQ_START_TOKEN)
  129. return pos;
  130. ++*pos;
  131. if (*pos >= dd->first_user_ctxt)
  132. return NULL;
  133. return pos;
  134. }
  135. static void _ctx_stats_seq_stop(struct seq_file *s, void *v)
  136. {
  137. /* nothing allocated */
  138. }
  139. static int _ctx_stats_seq_show(struct seq_file *s, void *v)
  140. {
  141. loff_t *spos;
  142. loff_t i, j;
  143. u64 n_packets = 0;
  144. struct qib_ibdev *ibd = (struct qib_ibdev *)s->private;
  145. struct qib_devdata *dd = dd_from_dev(ibd);
  146. if (v == SEQ_START_TOKEN) {
  147. seq_puts(s, "Ctx:npkts\n");
  148. return 0;
  149. }
  150. spos = v;
  151. i = *spos;
  152. if (!dd->rcd[i])
  153. return SEQ_SKIP;
  154. for (j = 0; j < ARRAY_SIZE(dd->rcd[i]->opstats->stats); j++)
  155. n_packets += dd->rcd[i]->opstats->stats[j].n_packets;
  156. if (!n_packets)
  157. return SEQ_SKIP;
  158. seq_printf(s, " %llu:%llu\n", i, n_packets);
  159. return 0;
  160. }
  161. DEBUGFS_FILE(ctx_stats)
  162. static void *_qp_stats_seq_start(struct seq_file *s, loff_t *pos)
  163. {
  164. struct qib_qp_iter *iter;
  165. loff_t n = *pos;
  166. rcu_read_lock();
  167. iter = qib_qp_iter_init(s->private);
  168. if (!iter)
  169. return NULL;
  170. while (n--) {
  171. if (qib_qp_iter_next(iter)) {
  172. kfree(iter);
  173. return NULL;
  174. }
  175. }
  176. return iter;
  177. }
  178. static void *_qp_stats_seq_next(struct seq_file *s, void *iter_ptr,
  179. loff_t *pos)
  180. {
  181. struct qib_qp_iter *iter = iter_ptr;
  182. (*pos)++;
  183. if (qib_qp_iter_next(iter)) {
  184. kfree(iter);
  185. return NULL;
  186. }
  187. return iter;
  188. }
  189. static void _qp_stats_seq_stop(struct seq_file *s, void *iter_ptr)
  190. {
  191. rcu_read_unlock();
  192. }
  193. static int _qp_stats_seq_show(struct seq_file *s, void *iter_ptr)
  194. {
  195. struct qib_qp_iter *iter = iter_ptr;
  196. if (!iter)
  197. return 0;
  198. qib_qp_iter_print(s, iter);
  199. return 0;
  200. }
  201. DEBUGFS_FILE(qp_stats)
  202. void qib_dbg_ibdev_init(struct qib_ibdev *ibd)
  203. {
  204. char name[10];
  205. snprintf(name, sizeof(name), "qib%d", dd_from_dev(ibd)->unit);
  206. ibd->qib_ibdev_dbg = debugfs_create_dir(name, qib_dbg_root);
  207. if (!ibd->qib_ibdev_dbg) {
  208. pr_warn("create of %s failed\n", name);
  209. return;
  210. }
  211. DEBUGFS_FILE_CREATE(opcode_stats);
  212. DEBUGFS_FILE_CREATE(ctx_stats);
  213. DEBUGFS_FILE_CREATE(qp_stats);
  214. }
  215. void qib_dbg_ibdev_exit(struct qib_ibdev *ibd)
  216. {
  217. if (!qib_dbg_root)
  218. goto out;
  219. debugfs_remove_recursive(ibd->qib_ibdev_dbg);
  220. out:
  221. ibd->qib_ibdev_dbg = NULL;
  222. }
  223. void qib_dbg_init(void)
  224. {
  225. qib_dbg_root = debugfs_create_dir(QIB_DRV_NAME, NULL);
  226. if (!qib_dbg_root)
  227. pr_warn("init of debugfs failed\n");
  228. }
  229. void qib_dbg_exit(void)
  230. {
  231. debugfs_remove_recursive(qib_dbg_root);
  232. qib_dbg_root = NULL;
  233. }
  234. #endif