usnic_debugfs.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2013, Cisco Systems, Inc. All rights reserved.
  3. *
  4. * This program is free software; you may redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; version 2 of the License.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  9. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  10. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  11. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  12. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  13. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  14. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  15. * SOFTWARE.
  16. *
  17. */
  18. #include <linux/debugfs.h>
  19. #include <linux/module.h>
  20. #include "usnic.h"
  21. #include "usnic_log.h"
  22. #include "usnic_debugfs.h"
  23. #include "usnic_ib_qp_grp.h"
  24. #include "usnic_transport.h"
  25. static struct dentry *debugfs_root;
  26. static struct dentry *flows_dentry;
  27. static ssize_t usnic_debugfs_buildinfo_read(struct file *f, char __user *data,
  28. size_t count, loff_t *ppos)
  29. {
  30. char buf[500];
  31. int res;
  32. if (*ppos > 0)
  33. return 0;
  34. res = scnprintf(buf, sizeof(buf),
  35. "version: %s\n"
  36. "build date: %s\n",
  37. DRV_VERSION, DRV_RELDATE);
  38. return simple_read_from_buffer(data, count, ppos, buf, res);
  39. }
  40. static const struct file_operations usnic_debugfs_buildinfo_ops = {
  41. .owner = THIS_MODULE,
  42. .open = simple_open,
  43. .read = usnic_debugfs_buildinfo_read
  44. };
  45. static ssize_t flowinfo_read(struct file *f, char __user *data,
  46. size_t count, loff_t *ppos)
  47. {
  48. struct usnic_ib_qp_grp_flow *qp_flow;
  49. int n;
  50. int left;
  51. char *ptr;
  52. char buf[512];
  53. qp_flow = f->private_data;
  54. ptr = buf;
  55. left = count;
  56. if (*ppos > 0)
  57. return 0;
  58. spin_lock(&qp_flow->qp_grp->lock);
  59. n = scnprintf(ptr, left,
  60. "QP Grp ID: %d Transport: %s ",
  61. qp_flow->qp_grp->grp_id,
  62. usnic_transport_to_str(qp_flow->trans_type));
  63. UPDATE_PTR_LEFT(n, ptr, left);
  64. if (qp_flow->trans_type == USNIC_TRANSPORT_ROCE_CUSTOM) {
  65. n = scnprintf(ptr, left, "Port_Num:%hu\n",
  66. qp_flow->usnic_roce.port_num);
  67. UPDATE_PTR_LEFT(n, ptr, left);
  68. } else if (qp_flow->trans_type == USNIC_TRANSPORT_IPV4_UDP) {
  69. n = usnic_transport_sock_to_str(ptr, left,
  70. qp_flow->udp.sock);
  71. UPDATE_PTR_LEFT(n, ptr, left);
  72. n = scnprintf(ptr, left, "\n");
  73. UPDATE_PTR_LEFT(n, ptr, left);
  74. }
  75. spin_unlock(&qp_flow->qp_grp->lock);
  76. return simple_read_from_buffer(data, count, ppos, buf, ptr - buf);
  77. }
  78. static const struct file_operations flowinfo_ops = {
  79. .owner = THIS_MODULE,
  80. .open = simple_open,
  81. .read = flowinfo_read,
  82. };
  83. void usnic_debugfs_init(void)
  84. {
  85. debugfs_root = debugfs_create_dir(DRV_NAME, NULL);
  86. if (IS_ERR(debugfs_root)) {
  87. usnic_err("Failed to create debugfs root dir, check if debugfs is enabled in kernel configuration\n");
  88. goto out_clear_root;
  89. }
  90. flows_dentry = debugfs_create_dir("flows", debugfs_root);
  91. if (IS_ERR_OR_NULL(flows_dentry)) {
  92. usnic_err("Failed to create debugfs flow dir with err %ld\n",
  93. PTR_ERR(flows_dentry));
  94. goto out_free_root;
  95. }
  96. debugfs_create_file("build-info", S_IRUGO, debugfs_root,
  97. NULL, &usnic_debugfs_buildinfo_ops);
  98. return;
  99. out_free_root:
  100. debugfs_remove_recursive(debugfs_root);
  101. out_clear_root:
  102. debugfs_root = NULL;
  103. }
  104. void usnic_debugfs_exit(void)
  105. {
  106. if (!debugfs_root)
  107. return;
  108. debugfs_remove_recursive(debugfs_root);
  109. debugfs_root = NULL;
  110. }
  111. void usnic_debugfs_flow_add(struct usnic_ib_qp_grp_flow *qp_flow)
  112. {
  113. if (IS_ERR_OR_NULL(flows_dentry))
  114. return;
  115. scnprintf(qp_flow->dentry_name, sizeof(qp_flow->dentry_name),
  116. "%u", qp_flow->flow->flow_id);
  117. qp_flow->dbgfs_dentry = debugfs_create_file(qp_flow->dentry_name,
  118. S_IRUGO,
  119. flows_dentry,
  120. qp_flow,
  121. &flowinfo_ops);
  122. if (IS_ERR_OR_NULL(qp_flow->dbgfs_dentry)) {
  123. usnic_err("Failed to create dbg fs entry for flow %u\n",
  124. qp_flow->flow->flow_id);
  125. }
  126. }
  127. void usnic_debugfs_flow_remove(struct usnic_ib_qp_grp_flow *qp_flow)
  128. {
  129. if (!IS_ERR_OR_NULL(qp_flow->dbgfs_dentry))
  130. debugfs_remove(qp_flow->dbgfs_dentry);
  131. }