card_sysfs.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**
  2. * IBM Accelerator Family 'GenWQE'
  3. *
  4. * (C) Copyright IBM Corp. 2013
  5. *
  6. * Author: Frank Haverkamp <haver@linux.vnet.ibm.com>
  7. * Author: Joerg-Stephan Vogt <jsvogt@de.ibm.com>
  8. * Author: Michael Jung <mijung@gmx.net>
  9. * Author: Michael Ruettger <michael@ibmra.de>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License (version 2 only)
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. /*
  21. * Sysfs interfaces for the GenWQE card. There are attributes to query
  22. * the version of the bitstream as well as some for the driver. For
  23. * debugging, please also see the debugfs interfaces of this driver.
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/types.h>
  27. #include <linux/module.h>
  28. #include <linux/pci.h>
  29. #include <linux/string.h>
  30. #include <linux/fs.h>
  31. #include <linux/sysfs.h>
  32. #include <linux/ctype.h>
  33. #include <linux/device.h>
  34. #include "card_base.h"
  35. #include "card_ddcb.h"
  36. static const char * const genwqe_types[] = {
  37. [GENWQE_TYPE_ALTERA_230] = "GenWQE4-230",
  38. [GENWQE_TYPE_ALTERA_530] = "GenWQE4-530",
  39. [GENWQE_TYPE_ALTERA_A4] = "GenWQE5-A4",
  40. [GENWQE_TYPE_ALTERA_A7] = "GenWQE5-A7",
  41. };
  42. static ssize_t status_show(struct device *dev, struct device_attribute *attr,
  43. char *buf)
  44. {
  45. struct genwqe_dev *cd = dev_get_drvdata(dev);
  46. const char *cs[GENWQE_CARD_STATE_MAX] = { "unused", "used", "error" };
  47. return sprintf(buf, "%s\n", cs[cd->card_state]);
  48. }
  49. static DEVICE_ATTR_RO(status);
  50. static ssize_t appid_show(struct device *dev, struct device_attribute *attr,
  51. char *buf)
  52. {
  53. char app_name[5];
  54. struct genwqe_dev *cd = dev_get_drvdata(dev);
  55. genwqe_read_app_id(cd, app_name, sizeof(app_name));
  56. return sprintf(buf, "%s\n", app_name);
  57. }
  58. static DEVICE_ATTR_RO(appid);
  59. static ssize_t version_show(struct device *dev, struct device_attribute *attr,
  60. char *buf)
  61. {
  62. u64 slu_id, app_id;
  63. struct genwqe_dev *cd = dev_get_drvdata(dev);
  64. slu_id = __genwqe_readq(cd, IO_SLU_UNITCFG);
  65. app_id = __genwqe_readq(cd, IO_APP_UNITCFG);
  66. return sprintf(buf, "%016llx.%016llx\n", slu_id, app_id);
  67. }
  68. static DEVICE_ATTR_RO(version);
  69. static ssize_t type_show(struct device *dev, struct device_attribute *attr,
  70. char *buf)
  71. {
  72. u8 card_type;
  73. struct genwqe_dev *cd = dev_get_drvdata(dev);
  74. card_type = genwqe_card_type(cd);
  75. return sprintf(buf, "%s\n", (card_type >= ARRAY_SIZE(genwqe_types)) ?
  76. "invalid" : genwqe_types[card_type]);
  77. }
  78. static DEVICE_ATTR_RO(type);
  79. static ssize_t tempsens_show(struct device *dev, struct device_attribute *attr,
  80. char *buf)
  81. {
  82. u64 tempsens;
  83. struct genwqe_dev *cd = dev_get_drvdata(dev);
  84. tempsens = __genwqe_readq(cd, IO_SLU_TEMPERATURE_SENSOR);
  85. return sprintf(buf, "%016llx\n", tempsens);
  86. }
  87. static DEVICE_ATTR_RO(tempsens);
  88. static ssize_t freerunning_timer_show(struct device *dev,
  89. struct device_attribute *attr,
  90. char *buf)
  91. {
  92. u64 t;
  93. struct genwqe_dev *cd = dev_get_drvdata(dev);
  94. t = __genwqe_readq(cd, IO_SLC_FREE_RUNNING_TIMER);
  95. return sprintf(buf, "%016llx\n", t);
  96. }
  97. static DEVICE_ATTR_RO(freerunning_timer);
  98. static ssize_t queue_working_time_show(struct device *dev,
  99. struct device_attribute *attr,
  100. char *buf)
  101. {
  102. u64 t;
  103. struct genwqe_dev *cd = dev_get_drvdata(dev);
  104. t = __genwqe_readq(cd, IO_SLC_QUEUE_WTIME);
  105. return sprintf(buf, "%016llx\n", t);
  106. }
  107. static DEVICE_ATTR_RO(queue_working_time);
  108. static ssize_t base_clock_show(struct device *dev,
  109. struct device_attribute *attr,
  110. char *buf)
  111. {
  112. u64 base_clock;
  113. struct genwqe_dev *cd = dev_get_drvdata(dev);
  114. base_clock = genwqe_base_clock_frequency(cd);
  115. return sprintf(buf, "%lld\n", base_clock);
  116. }
  117. static DEVICE_ATTR_RO(base_clock);
  118. /**
  119. * curr_bitstream_show() - Show the current bitstream id
  120. *
  121. * There is a bug in some old versions of the CPLD which selects the
  122. * bitstream, which causes the IO_SLU_BITSTREAM register to report
  123. * unreliable data in very rare cases. This makes this sysfs
  124. * unreliable up to the point were a new CPLD version is being used.
  125. *
  126. * Unfortunately there is no automatic way yet to query the CPLD
  127. * version, such that you need to manually ensure via programming
  128. * tools that you have a recent version of the CPLD software.
  129. *
  130. * The proposed circumvention is to use a special recovery bitstream
  131. * on the backup partition (0) to identify problems while loading the
  132. * image.
  133. */
  134. static ssize_t curr_bitstream_show(struct device *dev,
  135. struct device_attribute *attr, char *buf)
  136. {
  137. int curr_bitstream;
  138. struct genwqe_dev *cd = dev_get_drvdata(dev);
  139. curr_bitstream = __genwqe_readq(cd, IO_SLU_BITSTREAM) & 0x1;
  140. return sprintf(buf, "%d\n", curr_bitstream);
  141. }
  142. static DEVICE_ATTR_RO(curr_bitstream);
  143. /**
  144. * next_bitstream_show() - Show the next activated bitstream
  145. *
  146. * IO_SLC_CFGREG_SOFTRESET: This register can only be accessed by the PF.
  147. */
  148. static ssize_t next_bitstream_show(struct device *dev,
  149. struct device_attribute *attr, char *buf)
  150. {
  151. int next_bitstream;
  152. struct genwqe_dev *cd = dev_get_drvdata(dev);
  153. switch ((cd->softreset & 0xc) >> 2) {
  154. case 0x2:
  155. next_bitstream = 0;
  156. break;
  157. case 0x3:
  158. next_bitstream = 1;
  159. break;
  160. default:
  161. next_bitstream = -1;
  162. break; /* error */
  163. }
  164. return sprintf(buf, "%d\n", next_bitstream);
  165. }
  166. static ssize_t next_bitstream_store(struct device *dev,
  167. struct device_attribute *attr,
  168. const char *buf, size_t count)
  169. {
  170. int partition;
  171. struct genwqe_dev *cd = dev_get_drvdata(dev);
  172. if (kstrtoint(buf, 0, &partition) < 0)
  173. return -EINVAL;
  174. switch (partition) {
  175. case 0x0:
  176. cd->softreset = 0x78;
  177. break;
  178. case 0x1:
  179. cd->softreset = 0x7c;
  180. break;
  181. default:
  182. return -EINVAL;
  183. }
  184. __genwqe_writeq(cd, IO_SLC_CFGREG_SOFTRESET, cd->softreset);
  185. return count;
  186. }
  187. static DEVICE_ATTR_RW(next_bitstream);
  188. static ssize_t reload_bitstream_store(struct device *dev,
  189. struct device_attribute *attr,
  190. const char *buf, size_t count)
  191. {
  192. int reload;
  193. struct genwqe_dev *cd = dev_get_drvdata(dev);
  194. if (kstrtoint(buf, 0, &reload) < 0)
  195. return -EINVAL;
  196. if (reload == 0x1) {
  197. if (cd->card_state == GENWQE_CARD_UNUSED ||
  198. cd->card_state == GENWQE_CARD_USED)
  199. cd->card_state = GENWQE_CARD_RELOAD_BITSTREAM;
  200. else
  201. return -EIO;
  202. } else {
  203. return -EINVAL;
  204. }
  205. return count;
  206. }
  207. static DEVICE_ATTR_WO(reload_bitstream);
  208. /*
  209. * Create device_attribute structures / params: name, mode, show, store
  210. * additional flag if valid in VF
  211. */
  212. static struct attribute *genwqe_attributes[] = {
  213. &dev_attr_tempsens.attr,
  214. &dev_attr_next_bitstream.attr,
  215. &dev_attr_curr_bitstream.attr,
  216. &dev_attr_base_clock.attr,
  217. &dev_attr_type.attr,
  218. &dev_attr_version.attr,
  219. &dev_attr_appid.attr,
  220. &dev_attr_status.attr,
  221. &dev_attr_freerunning_timer.attr,
  222. &dev_attr_queue_working_time.attr,
  223. &dev_attr_reload_bitstream.attr,
  224. NULL,
  225. };
  226. static struct attribute *genwqe_normal_attributes[] = {
  227. &dev_attr_type.attr,
  228. &dev_attr_version.attr,
  229. &dev_attr_appid.attr,
  230. &dev_attr_status.attr,
  231. &dev_attr_freerunning_timer.attr,
  232. &dev_attr_queue_working_time.attr,
  233. NULL,
  234. };
  235. /**
  236. * genwqe_is_visible() - Determine if sysfs attribute should be visible or not
  237. *
  238. * VFs have restricted mmio capabilities, so not all sysfs entries
  239. * are allowed in VFs.
  240. */
  241. static umode_t genwqe_is_visible(struct kobject *kobj,
  242. struct attribute *attr, int n)
  243. {
  244. unsigned int j;
  245. struct device *dev = kobj_to_dev(kobj);
  246. struct genwqe_dev *cd = dev_get_drvdata(dev);
  247. umode_t mode = attr->mode;
  248. if (genwqe_is_privileged(cd))
  249. return mode;
  250. for (j = 0; genwqe_normal_attributes[j] != NULL; j++)
  251. if (genwqe_normal_attributes[j] == attr)
  252. return mode;
  253. return 0;
  254. }
  255. static struct attribute_group genwqe_attribute_group = {
  256. .is_visible = genwqe_is_visible,
  257. .attrs = genwqe_attributes,
  258. };
  259. const struct attribute_group *genwqe_attribute_groups[] = {
  260. &genwqe_attribute_group,
  261. NULL,
  262. };