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/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 tempsens_show(struct device *dev, struct device_attribute *attr,
  81. char *buf)
  82. {
  83. u64 tempsens;
  84. struct genwqe_dev *cd = dev_get_drvdata(dev);
  85. tempsens = __genwqe_readq(cd, IO_SLU_TEMPERATURE_SENSOR);
  86. return sprintf(buf, "%016llx\n", tempsens);
  87. }
  88. static DEVICE_ATTR_RO(tempsens);
  89. static ssize_t freerunning_timer_show(struct device *dev,
  90. struct device_attribute *attr,
  91. char *buf)
  92. {
  93. u64 t;
  94. struct genwqe_dev *cd = dev_get_drvdata(dev);
  95. t = __genwqe_readq(cd, IO_SLC_FREE_RUNNING_TIMER);
  96. return sprintf(buf, "%016llx\n", t);
  97. }
  98. static DEVICE_ATTR_RO(freerunning_timer);
  99. static ssize_t queue_working_time_show(struct device *dev,
  100. struct device_attribute *attr,
  101. char *buf)
  102. {
  103. u64 t;
  104. struct genwqe_dev *cd = dev_get_drvdata(dev);
  105. t = __genwqe_readq(cd, IO_SLC_QUEUE_WTIME);
  106. return sprintf(buf, "%016llx\n", t);
  107. }
  108. static DEVICE_ATTR_RO(queue_working_time);
  109. static ssize_t base_clock_show(struct device *dev,
  110. struct device_attribute *attr,
  111. char *buf)
  112. {
  113. u64 base_clock;
  114. struct genwqe_dev *cd = dev_get_drvdata(dev);
  115. base_clock = genwqe_base_clock_frequency(cd);
  116. return sprintf(buf, "%lld\n", base_clock);
  117. }
  118. static DEVICE_ATTR_RO(base_clock);
  119. /**
  120. * curr_bitstream_show() - Show the current bitstream id
  121. *
  122. * There is a bug in some old versions of the CPLD which selects the
  123. * bitstream, which causes the IO_SLU_BITSTREAM register to report
  124. * unreliable data in very rare cases. This makes this sysfs
  125. * unreliable up to the point were a new CPLD version is being used.
  126. *
  127. * Unfortunately there is no automatic way yet to query the CPLD
  128. * version, such that you need to manually ensure via programming
  129. * tools that you have a recent version of the CPLD software.
  130. *
  131. * The proposed circumvention is to use a special recovery bitstream
  132. * on the backup partition (0) to identify problems while loading the
  133. * image.
  134. */
  135. static ssize_t curr_bitstream_show(struct device *dev,
  136. struct device_attribute *attr, char *buf)
  137. {
  138. int curr_bitstream;
  139. struct genwqe_dev *cd = dev_get_drvdata(dev);
  140. curr_bitstream = __genwqe_readq(cd, IO_SLU_BITSTREAM) & 0x1;
  141. return sprintf(buf, "%d\n", curr_bitstream);
  142. }
  143. static DEVICE_ATTR_RO(curr_bitstream);
  144. /**
  145. * next_bitstream_show() - Show the next activated bitstream
  146. *
  147. * IO_SLC_CFGREG_SOFTRESET: This register can only be accessed by the PF.
  148. */
  149. static ssize_t next_bitstream_show(struct device *dev,
  150. struct device_attribute *attr, char *buf)
  151. {
  152. int next_bitstream;
  153. struct genwqe_dev *cd = dev_get_drvdata(dev);
  154. switch ((cd->softreset & 0xc) >> 2) {
  155. case 0x2:
  156. next_bitstream = 0;
  157. break;
  158. case 0x3:
  159. next_bitstream = 1;
  160. break;
  161. default:
  162. next_bitstream = -1;
  163. break; /* error */
  164. }
  165. return sprintf(buf, "%d\n", next_bitstream);
  166. }
  167. static ssize_t next_bitstream_store(struct device *dev,
  168. struct device_attribute *attr,
  169. const char *buf, size_t count)
  170. {
  171. int partition;
  172. struct genwqe_dev *cd = dev_get_drvdata(dev);
  173. if (kstrtoint(buf, 0, &partition) < 0)
  174. return -EINVAL;
  175. switch (partition) {
  176. case 0x0:
  177. cd->softreset = 0x78;
  178. break;
  179. case 0x1:
  180. cd->softreset = 0x7c;
  181. break;
  182. default:
  183. return -EINVAL;
  184. }
  185. __genwqe_writeq(cd, IO_SLC_CFGREG_SOFTRESET, cd->softreset);
  186. return count;
  187. }
  188. static DEVICE_ATTR_RW(next_bitstream);
  189. static ssize_t reload_bitstream_store(struct device *dev,
  190. struct device_attribute *attr,
  191. const char *buf, size_t count)
  192. {
  193. int reload;
  194. struct genwqe_dev *cd = dev_get_drvdata(dev);
  195. if (kstrtoint(buf, 0, &reload) < 0)
  196. return -EINVAL;
  197. if (reload == 0x1) {
  198. if (cd->card_state == GENWQE_CARD_UNUSED ||
  199. cd->card_state == GENWQE_CARD_USED)
  200. cd->card_state = GENWQE_CARD_RELOAD_BITSTREAM;
  201. else
  202. return -EIO;
  203. } else {
  204. return -EINVAL;
  205. }
  206. return count;
  207. }
  208. static DEVICE_ATTR_WO(reload_bitstream);
  209. /*
  210. * Create device_attribute structures / params: name, mode, show, store
  211. * additional flag if valid in VF
  212. */
  213. static struct attribute *genwqe_attributes[] = {
  214. &dev_attr_tempsens.attr,
  215. &dev_attr_next_bitstream.attr,
  216. &dev_attr_curr_bitstream.attr,
  217. &dev_attr_base_clock.attr,
  218. &dev_attr_type.attr,
  219. &dev_attr_version.attr,
  220. &dev_attr_appid.attr,
  221. &dev_attr_status.attr,
  222. &dev_attr_freerunning_timer.attr,
  223. &dev_attr_queue_working_time.attr,
  224. &dev_attr_reload_bitstream.attr,
  225. NULL,
  226. };
  227. static struct attribute *genwqe_normal_attributes[] = {
  228. &dev_attr_type.attr,
  229. &dev_attr_version.attr,
  230. &dev_attr_appid.attr,
  231. &dev_attr_status.attr,
  232. &dev_attr_freerunning_timer.attr,
  233. &dev_attr_queue_working_time.attr,
  234. NULL,
  235. };
  236. /**
  237. * genwqe_is_visible() - Determine if sysfs attribute should be visible or not
  238. *
  239. * VFs have restricted mmio capabilities, so not all sysfs entries
  240. * are allowed in VFs.
  241. */
  242. static umode_t genwqe_is_visible(struct kobject *kobj,
  243. struct attribute *attr, int n)
  244. {
  245. unsigned int j;
  246. struct device *dev = container_of(kobj, struct device, kobj);
  247. struct genwqe_dev *cd = dev_get_drvdata(dev);
  248. umode_t mode = attr->mode;
  249. if (genwqe_is_privileged(cd))
  250. return mode;
  251. for (j = 0; genwqe_normal_attributes[j] != NULL; j++)
  252. if (genwqe_normal_attributes[j] == attr)
  253. return mode;
  254. return 0;
  255. }
  256. static struct attribute_group genwqe_attribute_group = {
  257. .is_visible = genwqe_is_visible,
  258. .attrs = genwqe_attributes,
  259. };
  260. const struct attribute_group *genwqe_attribute_groups[] = {
  261. &genwqe_attribute_group,
  262. NULL,
  263. };