pci_debug.c 4.7 KB

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