pci_debug.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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_sw_names[] = {
  43. "Allocated pages",
  44. "Mapped pages",
  45. "Unmapped pages",
  46. };
  47. static void pci_fmb_show(struct seq_file *m, char *name[], int length,
  48. u64 *data)
  49. {
  50. int i;
  51. for (i = 0; i < length; i++, data++)
  52. seq_printf(m, "%26s:\t%llu\n", name[i], *data);
  53. }
  54. static void pci_sw_counter_show(struct seq_file *m)
  55. {
  56. struct zpci_dev *zdev = m->private;
  57. atomic64_t *counter = &zdev->allocated_pages;
  58. int i;
  59. for (i = 0; i < ARRAY_SIZE(pci_sw_names); i++, counter++)
  60. seq_printf(m, "%26s:\t%lu\n", pci_sw_names[i],
  61. atomic64_read(counter));
  62. }
  63. static int pci_perf_show(struct seq_file *m, void *v)
  64. {
  65. struct zpci_dev *zdev = m->private;
  66. if (!zdev)
  67. return 0;
  68. mutex_lock(&zdev->lock);
  69. if (!zdev->fmb) {
  70. mutex_unlock(&zdev->lock);
  71. seq_puts(m, "FMB statistics disabled\n");
  72. return 0;
  73. }
  74. /* header */
  75. seq_printf(m, "FMB @ %p\n", zdev->fmb);
  76. seq_printf(m, "Update interval: %u ms\n", zdev->fmb_update);
  77. seq_printf(m, "Samples: %u\n", zdev->fmb->samples);
  78. seq_printf(m, "Last update TOD: %Lx\n", zdev->fmb->last_update);
  79. pci_fmb_show(m, pci_common_names, ARRAY_SIZE(pci_common_names),
  80. &zdev->fmb->ld_ops);
  81. switch (zdev->fmb->format) {
  82. case 0:
  83. if (!(zdev->fmb->fmt_ind & ZPCI_FMB_DMA_COUNTER_VALID))
  84. break;
  85. pci_fmb_show(m, pci_fmt0_names, ARRAY_SIZE(pci_fmt0_names),
  86. &zdev->fmb->fmt0.dma_rbytes);
  87. break;
  88. case 1:
  89. pci_fmb_show(m, pci_fmt1_names, ARRAY_SIZE(pci_fmt1_names),
  90. &zdev->fmb->fmt1.rx_bytes);
  91. break;
  92. case 2:
  93. pci_fmb_show(m, pci_fmt2_names, ARRAY_SIZE(pci_fmt2_names),
  94. &zdev->fmb->fmt2.consumed_work_units);
  95. break;
  96. default:
  97. seq_puts(m, "Unknown format\n");
  98. }
  99. pci_sw_counter_show(m);
  100. mutex_unlock(&zdev->lock);
  101. return 0;
  102. }
  103. static ssize_t pci_perf_seq_write(struct file *file, const char __user *ubuf,
  104. size_t count, loff_t *off)
  105. {
  106. struct zpci_dev *zdev = ((struct seq_file *) file->private_data)->private;
  107. unsigned long val;
  108. int rc;
  109. if (!zdev)
  110. return 0;
  111. rc = kstrtoul_from_user(ubuf, count, 10, &val);
  112. if (rc)
  113. return rc;
  114. mutex_lock(&zdev->lock);
  115. switch (val) {
  116. case 0:
  117. rc = zpci_fmb_disable_device(zdev);
  118. break;
  119. case 1:
  120. rc = zpci_fmb_enable_device(zdev);
  121. break;
  122. }
  123. mutex_unlock(&zdev->lock);
  124. return rc ? rc : count;
  125. }
  126. static int pci_perf_seq_open(struct inode *inode, struct file *filp)
  127. {
  128. return single_open(filp, pci_perf_show,
  129. file_inode(filp)->i_private);
  130. }
  131. static const struct file_operations debugfs_pci_perf_fops = {
  132. .open = pci_perf_seq_open,
  133. .read = seq_read,
  134. .write = pci_perf_seq_write,
  135. .llseek = seq_lseek,
  136. .release = single_release,
  137. };
  138. void zpci_debug_init_device(struct zpci_dev *zdev, const char *name)
  139. {
  140. zdev->debugfs_dev = debugfs_create_dir(name, debugfs_root);
  141. if (IS_ERR(zdev->debugfs_dev))
  142. zdev->debugfs_dev = NULL;
  143. zdev->debugfs_perf = debugfs_create_file("statistics",
  144. S_IFREG | S_IRUGO | S_IWUSR,
  145. zdev->debugfs_dev, zdev,
  146. &debugfs_pci_perf_fops);
  147. if (IS_ERR(zdev->debugfs_perf))
  148. zdev->debugfs_perf = NULL;
  149. }
  150. void zpci_debug_exit_device(struct zpci_dev *zdev)
  151. {
  152. debugfs_remove(zdev->debugfs_perf);
  153. debugfs_remove(zdev->debugfs_dev);
  154. }
  155. int __init zpci_debug_init(void)
  156. {
  157. /* event trace buffer */
  158. pci_debug_msg_id = debug_register("pci_msg", 8, 1, 8 * sizeof(long));
  159. if (!pci_debug_msg_id)
  160. return -EINVAL;
  161. debug_register_view(pci_debug_msg_id, &debug_sprintf_view);
  162. debug_set_level(pci_debug_msg_id, 3);
  163. /* error log */
  164. pci_debug_err_id = debug_register("pci_error", 2, 1, 16);
  165. if (!pci_debug_err_id)
  166. return -EINVAL;
  167. debug_register_view(pci_debug_err_id, &debug_hex_ascii_view);
  168. debug_set_level(pci_debug_err_id, 6);
  169. debugfs_root = debugfs_create_dir("pci", NULL);
  170. return 0;
  171. }
  172. void zpci_debug_exit(void)
  173. {
  174. debug_unregister(pci_debug_msg_id);
  175. debug_unregister(pci_debug_err_id);
  176. debugfs_remove(debugfs_root);
  177. }