qib_debugfs.c 6.5 KB

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