pci_debug.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 2012,2015
  4. *
  5. * Author(s):
  6. * Jan Glauber <jang@linux.vnet.ibm.com>
  7. */
  8. #define KMSG_COMPONENT "zpci"
  9. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  10. #include <linux/kernel.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/debugfs.h>
  13. #include <linux/export.h>
  14. #include <linux/pci.h>
  15. #include <asm/debug.h>
  16. #include <asm/pci_dma.h>
  17. static struct dentry *debugfs_root;
  18. debug_info_t *pci_debug_msg_id;
  19. EXPORT_SYMBOL_GPL(pci_debug_msg_id);
  20. debug_info_t *pci_debug_err_id;
  21. EXPORT_SYMBOL_GPL(pci_debug_err_id);
  22. static char *pci_common_names[] = {
  23. "Load operations",
  24. "Store operations",
  25. "Store block operations",
  26. "Refresh operations",
  27. };
  28. static char *pci_fmt0_names[] = {
  29. "DMA read bytes",
  30. "DMA write bytes",
  31. };
  32. static char *pci_fmt1_names[] = {
  33. "Received bytes",
  34. "Received packets",
  35. "Transmitted bytes",
  36. "Transmitted packets",
  37. };
  38. static char *pci_fmt2_names[] = {
  39. "Consumed work units",
  40. "Maximum work units",
  41. };
  42. static char *pci_fmt3_names[] = {
  43. "Transmitted bytes",
  44. };
  45. static char *pci_sw_names[] = {
  46. "Allocated pages",
  47. "Mapped pages",
  48. "Unmapped pages",
  49. };
  50. static void pci_fmb_show(struct seq_file *m, char *name[], int length,
  51. u64 *data)
  52. {
  53. int i;
  54. for (i = 0; i < length; i++, data++)
  55. seq_printf(m, "%26s:\t%llu\n", name[i], *data);
  56. }
  57. static void pci_sw_counter_show(struct seq_file *m)
  58. {
  59. struct zpci_dev *zdev = m->private;
  60. atomic64_t *counter = &zdev->allocated_pages;
  61. int i;
  62. for (i = 0; i < ARRAY_SIZE(pci_sw_names); i++, counter++)
  63. seq_printf(m, "%26s:\t%lu\n", pci_sw_names[i],
  64. atomic64_read(counter));
  65. }
  66. static int pci_perf_show(struct seq_file *m, void *v)
  67. {
  68. struct zpci_dev *zdev = m->private;
  69. if (!zdev)
  70. return 0;
  71. mutex_lock(&zdev->lock);
  72. if (!zdev->fmb) {
  73. mutex_unlock(&zdev->lock);
  74. seq_puts(m, "FMB statistics disabled\n");
  75. return 0;
  76. }
  77. /* header */
  78. seq_printf(m, "Update interval: %u ms\n", zdev->fmb_update);
  79. seq_printf(m, "Samples: %u\n", zdev->fmb->samples);
  80. seq_printf(m, "Last update TOD: %Lx\n", zdev->fmb->last_update);
  81. pci_fmb_show(m, pci_common_names, ARRAY_SIZE(pci_common_names),
  82. &zdev->fmb->ld_ops);
  83. switch (zdev->fmb->format) {
  84. case 0:
  85. if (!(zdev->fmb->fmt_ind & ZPCI_FMB_DMA_COUNTER_VALID))
  86. break;
  87. pci_fmb_show(m, pci_fmt0_names, ARRAY_SIZE(pci_fmt0_names),
  88. &zdev->fmb->fmt0.dma_rbytes);
  89. break;
  90. case 1:
  91. pci_fmb_show(m, pci_fmt1_names, ARRAY_SIZE(pci_fmt1_names),
  92. &zdev->fmb->fmt1.rx_bytes);
  93. break;
  94. case 2:
  95. pci_fmb_show(m, pci_fmt2_names, ARRAY_SIZE(pci_fmt2_names),
  96. &zdev->fmb->fmt2.consumed_work_units);
  97. break;
  98. case 3:
  99. pci_fmb_show(m, pci_fmt3_names, ARRAY_SIZE(pci_fmt3_names),
  100. &zdev->fmb->fmt3.tx_bytes);
  101. break;
  102. default:
  103. seq_puts(m, "Unknown format\n");
  104. }
  105. pci_sw_counter_show(m);
  106. mutex_unlock(&zdev->lock);
  107. return 0;
  108. }
  109. static ssize_t pci_perf_seq_write(struct file *file, const char __user *ubuf,
  110. size_t count, loff_t *off)
  111. {
  112. struct zpci_dev *zdev = ((struct seq_file *) file->private_data)->private;
  113. unsigned long val;
  114. int rc;
  115. if (!zdev)
  116. return 0;
  117. rc = kstrtoul_from_user(ubuf, count, 10, &val);
  118. if (rc)
  119. return rc;
  120. mutex_lock(&zdev->lock);
  121. switch (val) {
  122. case 0:
  123. rc = zpci_fmb_disable_device(zdev);
  124. break;
  125. case 1:
  126. rc = zpci_fmb_enable_device(zdev);
  127. break;
  128. }
  129. mutex_unlock(&zdev->lock);
  130. return rc ? rc : count;
  131. }
  132. static int pci_perf_seq_open(struct inode *inode, struct file *filp)
  133. {
  134. return single_open(filp, pci_perf_show,
  135. file_inode(filp)->i_private);
  136. }
  137. static const struct file_operations debugfs_pci_perf_fops = {
  138. .open = pci_perf_seq_open,
  139. .read = seq_read,
  140. .write = pci_perf_seq_write,
  141. .llseek = seq_lseek,
  142. .release = single_release,
  143. };
  144. void zpci_debug_init_device(struct zpci_dev *zdev, const char *name)
  145. {
  146. zdev->debugfs_dev = debugfs_create_dir(name, debugfs_root);
  147. if (IS_ERR(zdev->debugfs_dev))
  148. zdev->debugfs_dev = NULL;
  149. zdev->debugfs_perf = debugfs_create_file("statistics",
  150. S_IFREG | S_IRUGO | S_IWUSR,
  151. zdev->debugfs_dev, zdev,
  152. &debugfs_pci_perf_fops);
  153. if (IS_ERR(zdev->debugfs_perf))
  154. zdev->debugfs_perf = NULL;
  155. }
  156. void zpci_debug_exit_device(struct zpci_dev *zdev)
  157. {
  158. debugfs_remove(zdev->debugfs_perf);
  159. debugfs_remove(zdev->debugfs_dev);
  160. }
  161. int __init zpci_debug_init(void)
  162. {
  163. /* event trace buffer */
  164. pci_debug_msg_id = debug_register("pci_msg", 8, 1, 8 * sizeof(long));
  165. if (!pci_debug_msg_id)
  166. return -EINVAL;
  167. debug_register_view(pci_debug_msg_id, &debug_sprintf_view);
  168. debug_set_level(pci_debug_msg_id, 3);
  169. /* error log */
  170. pci_debug_err_id = debug_register("pci_error", 2, 1, 16);
  171. if (!pci_debug_err_id)
  172. return -EINVAL;
  173. debug_register_view(pci_debug_err_id, &debug_hex_ascii_view);
  174. debug_set_level(pci_debug_err_id, 6);
  175. debugfs_root = debugfs_create_dir("pci", NULL);
  176. return 0;
  177. }
  178. void zpci_debug_exit(void)
  179. {
  180. debug_unregister(pci_debug_msg_id);
  181. debug_unregister(pci_debug_err_id);
  182. debugfs_remove(debugfs_root);
  183. }