fmc-fakedev.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * Copyright (C) 2012 CERN (www.cern.ch)
  3. * Author: Alessandro Rubini <rubini@gnudd.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * The software is provided "as is"; the copyright holders disclaim
  10. * all warranties and liabilities, to the extent permitted by
  11. * applicable law.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/string.h>
  16. #include <linux/device.h>
  17. #include <linux/slab.h>
  18. #include <linux/firmware.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/err.h>
  21. #include <linux/fmc.h>
  22. #define FF_EEPROM_SIZE 8192 /* The standard eeprom size */
  23. #define FF_MAX_MEZZANINES 4 /* Fakes a multi-mezzanine carrier */
  24. /* The user can pass up to 4 names of eeprom images to load */
  25. static char *ff_eeprom[FF_MAX_MEZZANINES];
  26. static int ff_nr_eeprom;
  27. module_param_array_named(eeprom, ff_eeprom, charp, &ff_nr_eeprom, 0444);
  28. /* The user can ask for a multi-mezzanine carrier, with the default eeprom */
  29. static int ff_nr_dev = 1;
  30. module_param_named(ndev, ff_nr_dev, int, 0444);
  31. /* Lazily, don't support the "standard" module parameters */
  32. /*
  33. * Eeprom built from these commands:
  34. ../fru-generator -v fake-vendor -n fake-design-for-testing \
  35. -s 01234 -p none > IPMI-FRU
  36. gensdbfs . ../fake-eeprom.bin
  37. */
  38. static char ff_eeimg[FF_MAX_MEZZANINES][FF_EEPROM_SIZE] = {
  39. {
  40. 0x01, 0x00, 0x00, 0x01, 0x00, 0x0c, 0x00, 0xf2, 0x01, 0x0b, 0x00, 0xb2,
  41. 0x86, 0x87, 0xcb, 0x66, 0x61, 0x6b, 0x65, 0x2d, 0x76, 0x65, 0x6e, 0x64,
  42. 0x6f, 0x72, 0xd7, 0x66, 0x61, 0x6b, 0x65, 0x2d, 0x64, 0x65, 0x73, 0x69,
  43. 0x67, 0x6e, 0x2d, 0x66, 0x6f, 0x72, 0x2d, 0x74, 0x65, 0x73, 0x74, 0x69,
  44. 0x6e, 0x67, 0xc5, 0x30, 0x31, 0x32, 0x33, 0x34, 0xc4, 0x6e, 0x6f, 0x6e,
  45. 0x65, 0xda, 0x32, 0x30, 0x31, 0x32, 0x2d, 0x31, 0x31, 0x2d, 0x31, 0x39,
  46. 0x20, 0x32, 0x32, 0x3a, 0x34, 0x32, 0x3a, 0x33, 0x30, 0x2e, 0x30, 0x37,
  47. 0x34, 0x30, 0x35, 0x35, 0xc1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x87,
  48. 0x02, 0x02, 0x0d, 0xf7, 0xf8, 0x02, 0xb0, 0x04, 0x74, 0x04, 0xec, 0x04,
  49. 0x00, 0x00, 0x00, 0x00, 0xe8, 0x03, 0x02, 0x02, 0x0d, 0x5c, 0x93, 0x01,
  50. 0x4a, 0x01, 0x39, 0x01, 0x5a, 0x01, 0x00, 0x00, 0x00, 0x00, 0xb8, 0x0b,
  51. 0x02, 0x02, 0x0d, 0x63, 0x8c, 0x00, 0xfa, 0x00, 0xed, 0x00, 0x06, 0x01,
  52. 0x00, 0x00, 0x00, 0x00, 0xa0, 0x0f, 0x01, 0x02, 0x0d, 0xfb, 0xf5, 0x05,
  53. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  54. 0x01, 0x02, 0x0d, 0xfc, 0xf4, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  55. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x0d, 0xfd, 0xf3, 0x03,
  56. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  57. 0xfa, 0x82, 0x0b, 0xea, 0x8f, 0xa2, 0x12, 0x00, 0x00, 0x1e, 0x44, 0x00,
  58. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61. 0x00, 0x00, 0x00, 0x00, 0x53, 0x44, 0x42, 0x2d, 0x00, 0x03, 0x01, 0x01,
  62. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  63. 0x00, 0x00, 0x01, 0xc4, 0x46, 0x69, 0x6c, 0x65, 0x44, 0x61, 0x74, 0x61,
  64. 0x2e, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  65. 0x2e, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  66. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
  67. 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc0,
  68. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc4, 0x46, 0x69, 0x6c, 0x65,
  69. 0x44, 0x61, 0x74, 0x61, 0x6e, 0x61, 0x6d, 0x65, 0x00, 0x00, 0x00, 0x01,
  70. 0x00, 0x00, 0x00, 0x00, 0x6e, 0x61, 0x6d, 0x65, 0x20, 0x20, 0x20, 0x20,
  71. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x01,
  72. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
  73. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xdf,
  74. 0x46, 0x69, 0x6c, 0x65, 0x44, 0x61, 0x74, 0x61, 0x49, 0x50, 0x4d, 0x49,
  75. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x49, 0x50, 0x4d, 0x49,
  76. 0x2d, 0x46, 0x52, 0x55, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  77. 0x20, 0x20, 0x20, 0x01, 0x66, 0x61, 0x6b, 0x65, 0x0a,
  78. },
  79. };
  80. struct ff_dev {
  81. struct fmc_device *fmc[FF_MAX_MEZZANINES];
  82. struct device dev;
  83. };
  84. static struct ff_dev *ff_current_dev; /* We have 1 carrier, 1 slot */
  85. static int ff_reprogram(struct fmc_device *fmc, struct fmc_driver *drv,
  86. char *gw)
  87. {
  88. const struct firmware *fw;
  89. int ret;
  90. if (!gw) {
  91. /* program golden: success */
  92. fmc->flags &= ~FMC_DEVICE_HAS_CUSTOM;
  93. fmc->flags |= FMC_DEVICE_HAS_GOLDEN;
  94. return 0;
  95. }
  96. dev_info(&fmc->dev, "reprogramming with %s\n", gw);
  97. ret = request_firmware(&fw, gw, &fmc->dev);
  98. if (ret < 0) {
  99. dev_warn(&fmc->dev, "request firmware \"%s\": error %i\n",
  100. gw, ret);
  101. goto out;
  102. }
  103. fmc->flags &= ~FMC_DEVICE_HAS_GOLDEN;
  104. fmc->flags |= FMC_DEVICE_HAS_CUSTOM;
  105. out:
  106. release_firmware(fw);
  107. return ret;
  108. }
  109. static int ff_irq_request(struct fmc_device *fmc, irq_handler_t handler,
  110. char *name, int flags)
  111. {
  112. return -EOPNOTSUPP;
  113. }
  114. /* FIXME: should also have some fake FMC GPIO mapping */
  115. /*
  116. * This work function is called when we changed the eeprom. It removes the
  117. * current fmc device and registers a new one, with different identifiers.
  118. */
  119. static struct ff_dev *ff_dev_create(void); /* defined later */
  120. static void ff_work_fn(struct work_struct *work)
  121. {
  122. struct ff_dev *ff = ff_current_dev;
  123. int ret;
  124. fmc_device_unregister_n(ff->fmc, ff_nr_dev);
  125. device_unregister(&ff->dev);
  126. ff_current_dev = NULL;
  127. ff = ff_dev_create();
  128. if (IS_ERR(ff)) {
  129. pr_warning("%s: can't re-create FMC devices\n", __func__);
  130. return;
  131. }
  132. ret = fmc_device_register_n(ff->fmc, ff_nr_dev);
  133. if (ret < 0) {
  134. dev_warn(&ff->dev, "can't re-register FMC devices\n");
  135. device_unregister(&ff->dev);
  136. return;
  137. }
  138. ff_current_dev = ff;
  139. }
  140. static DECLARE_DELAYED_WORK(ff_work, ff_work_fn);
  141. /* low-level i2c */
  142. static int ff_eeprom_read(struct fmc_device *fmc, uint32_t offset,
  143. void *buf, size_t size)
  144. {
  145. if (offset > FF_EEPROM_SIZE)
  146. return -EINVAL;
  147. if (offset + size > FF_EEPROM_SIZE)
  148. size = FF_EEPROM_SIZE - offset;
  149. memcpy(buf, fmc->eeprom + offset, size);
  150. return size;
  151. }
  152. static int ff_eeprom_write(struct fmc_device *fmc, uint32_t offset,
  153. const void *buf, size_t size)
  154. {
  155. if (offset > FF_EEPROM_SIZE)
  156. return -EINVAL;
  157. if (offset + size > FF_EEPROM_SIZE)
  158. size = FF_EEPROM_SIZE - offset;
  159. dev_info(&fmc->dev, "write_eeprom: offset %i, size %zi\n",
  160. (int)offset, size);
  161. memcpy(fmc->eeprom + offset, buf, size);
  162. schedule_delayed_work(&ff_work, HZ * 2); /* remove, replug, in 2s */
  163. return size;
  164. }
  165. /* i2c operations for fmc */
  166. static int ff_read_ee(struct fmc_device *fmc, int pos, void *data, int len)
  167. {
  168. if (!(fmc->flags & FMC_DEVICE_HAS_GOLDEN))
  169. return -EOPNOTSUPP;
  170. return ff_eeprom_read(fmc, pos, data, len);
  171. }
  172. static int ff_write_ee(struct fmc_device *fmc, int pos,
  173. const void *data, int len)
  174. {
  175. if (!(fmc->flags & FMC_DEVICE_HAS_GOLDEN))
  176. return -EOPNOTSUPP;
  177. return ff_eeprom_write(fmc, pos, data, len);
  178. }
  179. /* readl and writel do not do anything. Don't waste RAM with "base" */
  180. static uint32_t ff_readl(struct fmc_device *fmc, int offset)
  181. {
  182. return 0;
  183. }
  184. static void ff_writel(struct fmc_device *fmc, uint32_t value, int offset)
  185. {
  186. return;
  187. }
  188. /* validate is useful so fmc-write-eeprom will not reprogram every 2 seconds */
  189. static int ff_validate(struct fmc_device *fmc, struct fmc_driver *drv)
  190. {
  191. int i;
  192. if (!drv->busid_n)
  193. return 0; /* everyhing is valid */
  194. for (i = 0; i < drv->busid_n; i++)
  195. if (drv->busid_val[i] == fmc->device_id)
  196. return i;
  197. return -ENOENT;
  198. }
  199. static struct fmc_operations ff_fmc_operations = {
  200. .read32 = ff_readl,
  201. .write32 = ff_writel,
  202. .reprogram = ff_reprogram,
  203. .irq_request = ff_irq_request,
  204. .read_ee = ff_read_ee,
  205. .write_ee = ff_write_ee,
  206. .validate = ff_validate,
  207. };
  208. /* This device is kmalloced: release it */
  209. static void ff_dev_release(struct device *dev)
  210. {
  211. struct ff_dev *ff = container_of(dev, struct ff_dev, dev);
  212. kfree(ff);
  213. }
  214. static struct fmc_device ff_template_fmc = {
  215. .version = FMC_VERSION,
  216. .owner = THIS_MODULE,
  217. .carrier_name = "fake-fmc-carrier",
  218. .device_id = 0xf001, /* fool */
  219. .eeprom_len = sizeof(ff_eeimg[0]),
  220. .memlen = 0x1000, /* 4k, to show something */
  221. .op = &ff_fmc_operations,
  222. .hwdev = NULL, /* filled at creation time */
  223. .flags = FMC_DEVICE_HAS_GOLDEN,
  224. };
  225. static struct ff_dev *ff_dev_create(void)
  226. {
  227. struct ff_dev *ff;
  228. struct fmc_device *fmc;
  229. int i, ret;
  230. ff = kzalloc(sizeof(*ff), GFP_KERNEL);
  231. if (!ff)
  232. return ERR_PTR(-ENOMEM);
  233. dev_set_name(&ff->dev, "fake-fmc-carrier");
  234. ff->dev.release = ff_dev_release;
  235. ret = device_register(&ff->dev);
  236. if (ret < 0) {
  237. put_device(&ff->dev);
  238. return ERR_PTR(ret);
  239. }
  240. /* Create fmc structures that refer to this new "hw" device */
  241. for (i = 0; i < ff_nr_dev; i++) {
  242. fmc = kmemdup(&ff_template_fmc, sizeof(ff_template_fmc),
  243. GFP_KERNEL);
  244. fmc->hwdev = &ff->dev;
  245. fmc->carrier_data = ff;
  246. fmc->nr_slots = ff_nr_dev;
  247. /* the following fields are different for each slot */
  248. fmc->eeprom = ff_eeimg[i];
  249. fmc->eeprom_addr = 0x50 + 2 * i;
  250. fmc->slot_id = i;
  251. ff->fmc[i] = fmc;
  252. /* increment the identifier, each must be different */
  253. ff_template_fmc.device_id++;
  254. }
  255. return ff;
  256. }
  257. /* init and exit */
  258. static int ff_init(void)
  259. {
  260. struct ff_dev *ff;
  261. const struct firmware *fw;
  262. int i, len, ret = 0;
  263. /* Replicate the default eeprom for the max number of mezzanines */
  264. for (i = 1; i < FF_MAX_MEZZANINES; i++)
  265. memcpy(ff_eeimg[i], ff_eeimg[0], sizeof(ff_eeimg[0]));
  266. if (ff_nr_eeprom > ff_nr_dev)
  267. ff_nr_dev = ff_nr_eeprom;
  268. ff = ff_dev_create();
  269. if (IS_ERR(ff))
  270. return PTR_ERR(ff);
  271. /* If the user passed "eeprom=" as a parameter, fetch them */
  272. for (i = 0; i < ff_nr_eeprom; i++) {
  273. if (!strlen(ff_eeprom[i]))
  274. continue;
  275. ret = request_firmware(&fw, ff_eeprom[i], &ff->dev);
  276. if (ret < 0) {
  277. dev_err(&ff->dev, "Mezzanine %i: can't load \"%s\" "
  278. "(error %i)\n", i, ff_eeprom[i], -ret);
  279. } else {
  280. len = min_t(size_t, fw->size, (size_t)FF_EEPROM_SIZE);
  281. memcpy(ff_eeimg[i], fw->data, len);
  282. release_firmware(fw);
  283. dev_info(&ff->dev, "Mezzanine %i: eeprom \"%s\"\n", i,
  284. ff_eeprom[i]);
  285. }
  286. }
  287. ret = fmc_device_register_n(ff->fmc, ff_nr_dev);
  288. if (ret) {
  289. device_unregister(&ff->dev);
  290. return ret;
  291. }
  292. ff_current_dev = ff;
  293. return ret;
  294. }
  295. static void ff_exit(void)
  296. {
  297. if (ff_current_dev) {
  298. fmc_device_unregister_n(ff_current_dev->fmc, ff_nr_dev);
  299. device_unregister(&ff_current_dev->dev);
  300. }
  301. cancel_delayed_work_sync(&ff_work);
  302. }
  303. module_init(ff_init);
  304. module_exit(ff_exit);
  305. MODULE_LICENSE("Dual BSD/GPL");