card_sysfs.c 7.6 KB

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