qib_debugfs.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. void qib_dbg_ibdev_init(struct qib_ibdev *ibd)
  115. {
  116. char name[10];
  117. snprintf(name, sizeof(name), "qib%d", dd_from_dev(ibd)->unit);
  118. ibd->qib_ibdev_dbg = debugfs_create_dir(name, qib_dbg_root);
  119. if (!ibd->qib_ibdev_dbg) {
  120. pr_warn("create of %s failed\n", name);
  121. return;
  122. }
  123. DEBUGFS_FILE_CREATE(opcode_stats);
  124. return;
  125. }
  126. void qib_dbg_ibdev_exit(struct qib_ibdev *ibd)
  127. {
  128. if (!qib_dbg_root)
  129. goto out;
  130. debugfs_remove_recursive(ibd->qib_ibdev_dbg);
  131. out:
  132. ibd->qib_ibdev_dbg = NULL;
  133. }
  134. void qib_dbg_init(void)
  135. {
  136. qib_dbg_root = debugfs_create_dir(QIB_DRV_NAME, NULL);
  137. if (!qib_dbg_root)
  138. pr_warn("init of debugfs failed\n");
  139. }
  140. void qib_dbg_exit(void)
  141. {
  142. debugfs_remove_recursive(qib_dbg_root);
  143. qib_dbg_root = NULL;
  144. }
  145. #endif