fmc-debug.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (C) 2015 CERN (www.cern.ch)
  3. * Author: Federico Vaga <federico.vaga@cern.ch>
  4. *
  5. * Released according to the GNU GPL, version 2 or any later version.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/device.h>
  9. #include <linux/init.h>
  10. #include <linux/fs.h>
  11. #include <linux/debugfs.h>
  12. #include <linux/seq_file.h>
  13. #include <asm/byteorder.h>
  14. #include <linux/fmc.h>
  15. #include <linux/sdb.h>
  16. #include <linux/fmc-sdb.h>
  17. #define FMC_DBG_SDB_DUMP "dump_sdb"
  18. static char *__strip_trailing_space(char *buf, char *str, int len)
  19. {
  20. int i = len - 1;
  21. memcpy(buf, str, len);
  22. buf[len] = '\0';
  23. while (i >= 0 && buf[i] == ' ')
  24. buf[i--] = '\0';
  25. return buf;
  26. }
  27. #define __sdb_string(buf, field) ({ \
  28. BUILD_BUG_ON(sizeof(buf) < sizeof(field)); \
  29. __strip_trailing_space(buf, (void *)(field), sizeof(field)); \
  30. })
  31. /**
  32. * We do not check seq_printf() errors because we want to see things in any case
  33. */
  34. static void fmc_sdb_dump_recursive(struct fmc_device *fmc, struct seq_file *s,
  35. const struct sdb_array *arr)
  36. {
  37. unsigned long base = arr->baseaddr;
  38. int i, j, n = arr->len, level = arr->level;
  39. char tmp[64];
  40. for (i = 0; i < n; i++) {
  41. union sdb_record *r;
  42. struct sdb_product *p;
  43. struct sdb_component *c;
  44. r = &arr->record[i];
  45. c = &r->dev.sdb_component;
  46. p = &c->product;
  47. for (j = 0; j < level; j++)
  48. seq_printf(s, " ");
  49. switch (r->empty.record_type) {
  50. case sdb_type_interconnect:
  51. seq_printf(s, "%08llx:%08x %.19s\n",
  52. __be64_to_cpu(p->vendor_id),
  53. __be32_to_cpu(p->device_id),
  54. p->name);
  55. break;
  56. case sdb_type_device:
  57. seq_printf(s, "%08llx:%08x %.19s (%08llx-%08llx)\n",
  58. __be64_to_cpu(p->vendor_id),
  59. __be32_to_cpu(p->device_id),
  60. p->name,
  61. __be64_to_cpu(c->addr_first) + base,
  62. __be64_to_cpu(c->addr_last) + base);
  63. break;
  64. case sdb_type_bridge:
  65. seq_printf(s, "%08llx:%08x %.19s (bridge: %08llx)\n",
  66. __be64_to_cpu(p->vendor_id),
  67. __be32_to_cpu(p->device_id),
  68. p->name,
  69. __be64_to_cpu(c->addr_first) + base);
  70. if (IS_ERR(arr->subtree[i])) {
  71. seq_printf(s, "SDB: (bridge error %li)\n",
  72. PTR_ERR(arr->subtree[i]));
  73. break;
  74. }
  75. fmc_sdb_dump_recursive(fmc, s, arr->subtree[i]);
  76. break;
  77. case sdb_type_integration:
  78. seq_printf(s, "integration\n");
  79. break;
  80. case sdb_type_repo_url:
  81. seq_printf(s, "Synthesis repository: %s\n",
  82. __sdb_string(tmp, r->repo_url.repo_url));
  83. break;
  84. case sdb_type_synthesis:
  85. seq_printf(s, "Bitstream '%s' ",
  86. __sdb_string(tmp, r->synthesis.syn_name));
  87. seq_printf(s, "synthesized %08x by %s ",
  88. __be32_to_cpu(r->synthesis.date),
  89. __sdb_string(tmp, r->synthesis.user_name));
  90. seq_printf(s, "(%s version %x), ",
  91. __sdb_string(tmp, r->synthesis.tool_name),
  92. __be32_to_cpu(r->synthesis.tool_version));
  93. seq_printf(s, "commit %pm\n",
  94. r->synthesis.commit_id);
  95. break;
  96. case sdb_type_empty:
  97. seq_printf(s, "empty\n");
  98. break;
  99. default:
  100. seq_printf(s, "UNKNOWN TYPE 0x%02x\n",
  101. r->empty.record_type);
  102. break;
  103. }
  104. }
  105. }
  106. static int fmc_sdb_dump(struct seq_file *s, void *offset)
  107. {
  108. struct fmc_device *fmc = s->private;
  109. if (!fmc->sdb) {
  110. seq_printf(s, "no SDB information\n");
  111. return 0;
  112. }
  113. seq_printf(s, "FMC: %s (%s), slot %i, device %s\n", dev_name(fmc->hwdev),
  114. fmc->carrier_name, fmc->slot_id, dev_name(&fmc->dev));
  115. /* Dump SDB information */
  116. fmc_sdb_dump_recursive(fmc, s, fmc->sdb);
  117. return 0;
  118. }
  119. static int fmc_sdb_dump_open(struct inode *inode, struct file *file)
  120. {
  121. struct fmc_device *fmc = inode->i_private;
  122. return single_open(file, fmc_sdb_dump, fmc);
  123. }
  124. const struct file_operations fmc_dbgfs_sdb_dump = {
  125. .owner = THIS_MODULE,
  126. .open = fmc_sdb_dump_open,
  127. .read = seq_read,
  128. .llseek = seq_lseek,
  129. .release = single_release,
  130. };
  131. int fmc_debug_init(struct fmc_device *fmc)
  132. {
  133. fmc->dbg_dir = debugfs_create_dir(dev_name(&fmc->dev), NULL);
  134. if (IS_ERR_OR_NULL(fmc->dbg_dir)) {
  135. pr_err("FMC: Cannot create debugfs\n");
  136. return PTR_ERR(fmc->dbg_dir);
  137. }
  138. fmc->dbg_sdb_dump = debugfs_create_file(FMC_DBG_SDB_DUMP, 0444,
  139. fmc->dbg_dir, fmc,
  140. &fmc_dbgfs_sdb_dump);
  141. if (IS_ERR_OR_NULL(fmc->dbg_sdb_dump))
  142. pr_err("FMC: Cannot create debugfs file %s\n",
  143. FMC_DBG_SDB_DUMP);
  144. return 0;
  145. }
  146. void fmc_debug_exit(struct fmc_device *fmc)
  147. {
  148. if (fmc->dbg_dir)
  149. debugfs_remove_recursive(fmc->dbg_dir);
  150. }