fmc-sdb.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (C) 2012 CERN (www.cern.ch)
  3. * Author: Alessandro Rubini <rubini@gnudd.com>
  4. *
  5. * Released according to the GNU GPL, version 2 or any later version.
  6. *
  7. * This work is part of the White Rabbit project, a research effort led
  8. * by CERN, the European Institute for Nuclear Research.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/fmc.h>
  13. #include <linux/sdb.h>
  14. #include <linux/err.h>
  15. #include <linux/fmc-sdb.h>
  16. #include <asm/byteorder.h>
  17. static uint32_t __sdb_rd(struct fmc_device *fmc, unsigned long address,
  18. int convert)
  19. {
  20. uint32_t res = fmc_readl(fmc, address);
  21. if (convert)
  22. return __be32_to_cpu(res);
  23. return res;
  24. }
  25. static struct sdb_array *__fmc_scan_sdb_tree(struct fmc_device *fmc,
  26. unsigned long sdb_addr,
  27. unsigned long reg_base, int level)
  28. {
  29. uint32_t onew;
  30. int i, j, n, convert = 0;
  31. struct sdb_array *arr, *sub;
  32. onew = fmc_readl(fmc, sdb_addr);
  33. if (onew == SDB_MAGIC) {
  34. /* Uh! If we are little-endian, we must convert */
  35. if (SDB_MAGIC != __be32_to_cpu(SDB_MAGIC))
  36. convert = 1;
  37. } else if (onew == __be32_to_cpu(SDB_MAGIC)) {
  38. /* ok, don't convert */
  39. } else {
  40. return ERR_PTR(-ENOENT);
  41. }
  42. /* So, the magic was there: get the count from offset 4*/
  43. onew = __sdb_rd(fmc, sdb_addr + 4, convert);
  44. n = __be16_to_cpu(*(uint16_t *)&onew);
  45. arr = kzalloc(sizeof(*arr), GFP_KERNEL);
  46. if (!arr)
  47. return ERR_PTR(-ENOMEM);
  48. arr->record = kzalloc(sizeof(arr->record[0]) * n, GFP_KERNEL);
  49. arr->subtree = kzalloc(sizeof(arr->subtree[0]) * n, GFP_KERNEL);
  50. if (!arr->record || !arr->subtree) {
  51. kfree(arr->record);
  52. kfree(arr->subtree);
  53. kfree(arr);
  54. return ERR_PTR(-ENOMEM);
  55. }
  56. arr->len = n;
  57. arr->level = level;
  58. arr->fmc = fmc;
  59. for (i = 0; i < n; i++) {
  60. union sdb_record *r;
  61. for (j = 0; j < sizeof(arr->record[0]); j += 4) {
  62. *(uint32_t *)((void *)(arr->record + i) + j) =
  63. __sdb_rd(fmc, sdb_addr + (i * 64) + j, convert);
  64. }
  65. r = &arr->record[i];
  66. arr->subtree[i] = ERR_PTR(-ENODEV);
  67. if (r->empty.record_type == sdb_type_bridge) {
  68. struct sdb_component *c = &r->bridge.sdb_component;
  69. uint64_t subaddr = __be64_to_cpu(r->bridge.sdb_child);
  70. uint64_t newbase = __be64_to_cpu(c->addr_first);
  71. subaddr += reg_base;
  72. newbase += reg_base;
  73. sub = __fmc_scan_sdb_tree(fmc, subaddr, newbase,
  74. level + 1);
  75. arr->subtree[i] = sub; /* may be error */
  76. if (IS_ERR(sub))
  77. continue;
  78. sub->parent = arr;
  79. sub->baseaddr = newbase;
  80. }
  81. }
  82. return arr;
  83. }
  84. int fmc_scan_sdb_tree(struct fmc_device *fmc, unsigned long address)
  85. {
  86. struct sdb_array *ret;
  87. if (fmc->sdb)
  88. return -EBUSY;
  89. ret = __fmc_scan_sdb_tree(fmc, address, 0 /* regs */, 0);
  90. if (IS_ERR(ret))
  91. return PTR_ERR(ret);
  92. fmc->sdb = ret;
  93. return 0;
  94. }
  95. EXPORT_SYMBOL(fmc_scan_sdb_tree);
  96. static void __fmc_sdb_free(struct sdb_array *arr)
  97. {
  98. int i, n;
  99. if (!arr)
  100. return;
  101. n = arr->len;
  102. for (i = 0; i < n; i++) {
  103. if (IS_ERR(arr->subtree[i]))
  104. continue;
  105. __fmc_sdb_free(arr->subtree[i]);
  106. }
  107. kfree(arr->record);
  108. kfree(arr->subtree);
  109. kfree(arr);
  110. }
  111. int fmc_free_sdb_tree(struct fmc_device *fmc)
  112. {
  113. __fmc_sdb_free(fmc->sdb);
  114. fmc->sdb = NULL;
  115. return 0;
  116. }
  117. EXPORT_SYMBOL(fmc_free_sdb_tree);
  118. /* This helper calls reprogram and inizialized sdb as well */
  119. int fmc_reprogram(struct fmc_device *fmc, struct fmc_driver *d, char *gw,
  120. int sdb_entry)
  121. {
  122. int ret;
  123. ret = fmc->op->reprogram(fmc, d, gw);
  124. if (ret < 0)
  125. return ret;
  126. if (sdb_entry < 0)
  127. return ret;
  128. /* We are required to find SDB at a given offset */
  129. ret = fmc_scan_sdb_tree(fmc, sdb_entry);
  130. if (ret < 0) {
  131. dev_err(&fmc->dev, "Can't find SDB at address 0x%x\n",
  132. sdb_entry);
  133. return -ENODEV;
  134. }
  135. fmc_dump_sdb(fmc);
  136. return 0;
  137. }
  138. EXPORT_SYMBOL(fmc_reprogram);
  139. static void __fmc_show_sdb_tree(const struct fmc_device *fmc,
  140. const struct sdb_array *arr)
  141. {
  142. int i, j, n = arr->len, level = arr->level;
  143. const struct sdb_array *ap;
  144. for (i = 0; i < n; i++) {
  145. unsigned long base;
  146. union sdb_record *r;
  147. struct sdb_product *p;
  148. struct sdb_component *c;
  149. r = &arr->record[i];
  150. c = &r->dev.sdb_component;
  151. p = &c->product;
  152. base = 0;
  153. for (ap = arr; ap; ap = ap->parent)
  154. base += ap->baseaddr;
  155. dev_info(&fmc->dev, "SDB: ");
  156. for (j = 0; j < level; j++)
  157. printk(KERN_CONT " ");
  158. switch (r->empty.record_type) {
  159. case sdb_type_interconnect:
  160. printk(KERN_CONT "%08llx:%08x %.19s\n",
  161. __be64_to_cpu(p->vendor_id),
  162. __be32_to_cpu(p->device_id),
  163. p->name);
  164. break;
  165. case sdb_type_device:
  166. printk(KERN_CONT "%08llx:%08x %.19s (%08llx-%08llx)\n",
  167. __be64_to_cpu(p->vendor_id),
  168. __be32_to_cpu(p->device_id),
  169. p->name,
  170. __be64_to_cpu(c->addr_first) + base,
  171. __be64_to_cpu(c->addr_last) + base);
  172. break;
  173. case sdb_type_bridge:
  174. printk(KERN_CONT "%08llx:%08x %.19s (bridge: %08llx)\n",
  175. __be64_to_cpu(p->vendor_id),
  176. __be32_to_cpu(p->device_id),
  177. p->name,
  178. __be64_to_cpu(c->addr_first) + base);
  179. if (IS_ERR(arr->subtree[i])) {
  180. printk(KERN_CONT "(bridge error %li)\n",
  181. PTR_ERR(arr->subtree[i]));
  182. break;
  183. }
  184. __fmc_show_sdb_tree(fmc, arr->subtree[i]);
  185. break;
  186. case sdb_type_integration:
  187. printk(KERN_CONT "integration\n");
  188. break;
  189. case sdb_type_repo_url:
  190. printk(KERN_CONT "repo-url\n");
  191. break;
  192. case sdb_type_synthesis:
  193. printk(KERN_CONT "synthesis-info\n");
  194. break;
  195. case sdb_type_empty:
  196. printk(KERN_CONT "empty\n");
  197. break;
  198. default:
  199. printk(KERN_CONT "UNKNOWN TYPE 0x%02x\n",
  200. r->empty.record_type);
  201. break;
  202. }
  203. }
  204. }
  205. void fmc_show_sdb_tree(const struct fmc_device *fmc)
  206. {
  207. if (!fmc->sdb)
  208. return;
  209. __fmc_show_sdb_tree(fmc, fmc->sdb);
  210. }
  211. EXPORT_SYMBOL(fmc_show_sdb_tree);
  212. signed long fmc_find_sdb_device(struct sdb_array *tree,
  213. uint64_t vid, uint32_t did, unsigned long *sz)
  214. {
  215. signed long res = -ENODEV;
  216. union sdb_record *r;
  217. struct sdb_product *p;
  218. struct sdb_component *c;
  219. int i, n = tree->len;
  220. uint64_t last, first;
  221. /* FIXME: what if the first interconnect is not at zero? */
  222. for (i = 0; i < n; i++) {
  223. r = &tree->record[i];
  224. c = &r->dev.sdb_component;
  225. p = &c->product;
  226. if (!IS_ERR(tree->subtree[i]))
  227. res = fmc_find_sdb_device(tree->subtree[i],
  228. vid, did, sz);
  229. if (res >= 0)
  230. return res + tree->baseaddr;
  231. if (r->empty.record_type != sdb_type_device)
  232. continue;
  233. if (__be64_to_cpu(p->vendor_id) != vid)
  234. continue;
  235. if (__be32_to_cpu(p->device_id) != did)
  236. continue;
  237. /* found */
  238. last = __be64_to_cpu(c->addr_last);
  239. first = __be64_to_cpu(c->addr_first);
  240. if (sz)
  241. *sz = (typeof(*sz))(last + 1 - first);
  242. return first + tree->baseaddr;
  243. }
  244. return res;
  245. }
  246. EXPORT_SYMBOL(fmc_find_sdb_device);