card_sysfs.c 7.5 KB

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