usnic_debugfs.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) 2013, Cisco Systems, Inc. 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. * 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. */
  33. #include <linux/debugfs.h>
  34. #include <linux/module.h>
  35. #include "usnic.h"
  36. #include "usnic_log.h"
  37. #include "usnic_debugfs.h"
  38. #include "usnic_ib_qp_grp.h"
  39. #include "usnic_transport.h"
  40. static struct dentry *debugfs_root;
  41. static struct dentry *flows_dentry;
  42. static ssize_t usnic_debugfs_buildinfo_read(struct file *f, char __user *data,
  43. size_t count, loff_t *ppos)
  44. {
  45. char buf[500];
  46. int res;
  47. if (*ppos > 0)
  48. return 0;
  49. res = scnprintf(buf, sizeof(buf),
  50. "version: %s\n"
  51. "build date: %s\n",
  52. DRV_VERSION, DRV_RELDATE);
  53. return simple_read_from_buffer(data, count, ppos, buf, res);
  54. }
  55. static const struct file_operations usnic_debugfs_buildinfo_ops = {
  56. .owner = THIS_MODULE,
  57. .open = simple_open,
  58. .read = usnic_debugfs_buildinfo_read
  59. };
  60. static ssize_t flowinfo_read(struct file *f, char __user *data,
  61. size_t count, loff_t *ppos)
  62. {
  63. struct usnic_ib_qp_grp_flow *qp_flow;
  64. int n;
  65. int left;
  66. char *ptr;
  67. char buf[512];
  68. qp_flow = f->private_data;
  69. ptr = buf;
  70. left = count;
  71. if (*ppos > 0)
  72. return 0;
  73. spin_lock(&qp_flow->qp_grp->lock);
  74. n = scnprintf(ptr, left,
  75. "QP Grp ID: %d Transport: %s ",
  76. qp_flow->qp_grp->grp_id,
  77. usnic_transport_to_str(qp_flow->trans_type));
  78. UPDATE_PTR_LEFT(n, ptr, left);
  79. if (qp_flow->trans_type == USNIC_TRANSPORT_ROCE_CUSTOM) {
  80. n = scnprintf(ptr, left, "Port_Num:%hu\n",
  81. qp_flow->usnic_roce.port_num);
  82. UPDATE_PTR_LEFT(n, ptr, left);
  83. } else if (qp_flow->trans_type == USNIC_TRANSPORT_IPV4_UDP) {
  84. n = usnic_transport_sock_to_str(ptr, left,
  85. qp_flow->udp.sock);
  86. UPDATE_PTR_LEFT(n, ptr, left);
  87. n = scnprintf(ptr, left, "\n");
  88. UPDATE_PTR_LEFT(n, ptr, left);
  89. }
  90. spin_unlock(&qp_flow->qp_grp->lock);
  91. return simple_read_from_buffer(data, count, ppos, buf, ptr - buf);
  92. }
  93. static const struct file_operations flowinfo_ops = {
  94. .owner = THIS_MODULE,
  95. .open = simple_open,
  96. .read = flowinfo_read,
  97. };
  98. void usnic_debugfs_init(void)
  99. {
  100. debugfs_root = debugfs_create_dir(DRV_NAME, NULL);
  101. if (IS_ERR(debugfs_root)) {
  102. usnic_err("Failed to create debugfs root dir, check if debugfs is enabled in kernel configuration\n");
  103. goto out_clear_root;
  104. }
  105. flows_dentry = debugfs_create_dir("flows", debugfs_root);
  106. if (IS_ERR_OR_NULL(flows_dentry)) {
  107. usnic_err("Failed to create debugfs flow dir with err %ld\n",
  108. PTR_ERR(flows_dentry));
  109. goto out_free_root;
  110. }
  111. debugfs_create_file("build-info", S_IRUGO, debugfs_root,
  112. NULL, &usnic_debugfs_buildinfo_ops);
  113. return;
  114. out_free_root:
  115. debugfs_remove_recursive(debugfs_root);
  116. out_clear_root:
  117. debugfs_root = NULL;
  118. }
  119. void usnic_debugfs_exit(void)
  120. {
  121. if (!debugfs_root)
  122. return;
  123. debugfs_remove_recursive(debugfs_root);
  124. debugfs_root = NULL;
  125. }
  126. void usnic_debugfs_flow_add(struct usnic_ib_qp_grp_flow *qp_flow)
  127. {
  128. if (IS_ERR_OR_NULL(flows_dentry))
  129. return;
  130. scnprintf(qp_flow->dentry_name, sizeof(qp_flow->dentry_name),
  131. "%u", qp_flow->flow->flow_id);
  132. qp_flow->dbgfs_dentry = debugfs_create_file(qp_flow->dentry_name,
  133. S_IRUGO,
  134. flows_dentry,
  135. qp_flow,
  136. &flowinfo_ops);
  137. if (IS_ERR_OR_NULL(qp_flow->dbgfs_dentry)) {
  138. usnic_err("Failed to create dbg fs entry for flow %u with error %ld\n",
  139. qp_flow->flow->flow_id,
  140. PTR_ERR(qp_flow->dbgfs_dentry));
  141. }
  142. }
  143. void usnic_debugfs_flow_remove(struct usnic_ib_qp_grp_flow *qp_flow)
  144. {
  145. if (!IS_ERR_OR_NULL(qp_flow->dbgfs_dentry))
  146. debugfs_remove(qp_flow->dbgfs_dentry);
  147. }