cosm_sysfs.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * Intel MIC Platform Software Stack (MPSS)
  3. *
  4. * Copyright(c) 2015 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License, version 2, as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * The full GNU General Public License is included in this distribution in
  16. * the file called "COPYING".
  17. *
  18. * Intel MIC Coprocessor State Management (COSM) Driver
  19. *
  20. */
  21. #include <linux/slab.h>
  22. #include "cosm_main.h"
  23. /*
  24. * A state-to-string lookup table, for exposing a human readable state
  25. * via sysfs. Always keep in sync with enum cosm_states
  26. */
  27. const char * const cosm_state_string[] = {
  28. [MIC_READY] = "ready",
  29. [MIC_BOOTING] = "booting",
  30. [MIC_ONLINE] = "online",
  31. [MIC_SHUTTING_DOWN] = "shutting_down",
  32. [MIC_RESETTING] = "resetting",
  33. [MIC_RESET_FAILED] = "reset_failed",
  34. };
  35. /*
  36. * A shutdown-status-to-string lookup table, for exposing a human
  37. * readable state via sysfs. Always keep in sync with enum cosm_shutdown_status
  38. */
  39. const char * const cosm_shutdown_status_string[] = {
  40. [MIC_NOP] = "nop",
  41. [MIC_CRASHED] = "crashed",
  42. [MIC_HALTED] = "halted",
  43. [MIC_POWER_OFF] = "poweroff",
  44. [MIC_RESTART] = "restart",
  45. };
  46. void cosm_set_shutdown_status(struct cosm_device *cdev, u8 shutdown_status)
  47. {
  48. dev_dbg(&cdev->dev, "Shutdown Status %s -> %s\n",
  49. cosm_shutdown_status_string[cdev->shutdown_status],
  50. cosm_shutdown_status_string[shutdown_status]);
  51. cdev->shutdown_status = shutdown_status;
  52. }
  53. void cosm_set_state(struct cosm_device *cdev, u8 state)
  54. {
  55. dev_dbg(&cdev->dev, "State %s -> %s\n",
  56. cosm_state_string[cdev->state],
  57. cosm_state_string[state]);
  58. cdev->state = state;
  59. sysfs_notify_dirent(cdev->state_sysfs);
  60. }
  61. static ssize_t
  62. family_show(struct device *dev, struct device_attribute *attr, char *buf)
  63. {
  64. struct cosm_device *cdev = dev_get_drvdata(dev);
  65. if (!cdev)
  66. return -EINVAL;
  67. return cdev->hw_ops->family(cdev, buf);
  68. }
  69. static DEVICE_ATTR_RO(family);
  70. static ssize_t
  71. stepping_show(struct device *dev, struct device_attribute *attr, char *buf)
  72. {
  73. struct cosm_device *cdev = dev_get_drvdata(dev);
  74. if (!cdev)
  75. return -EINVAL;
  76. return cdev->hw_ops->stepping(cdev, buf);
  77. }
  78. static DEVICE_ATTR_RO(stepping);
  79. static ssize_t
  80. state_show(struct device *dev, struct device_attribute *attr, char *buf)
  81. {
  82. struct cosm_device *cdev = dev_get_drvdata(dev);
  83. if (!cdev || cdev->state >= MIC_LAST)
  84. return -EINVAL;
  85. return scnprintf(buf, PAGE_SIZE, "%s\n",
  86. cosm_state_string[cdev->state]);
  87. }
  88. static ssize_t
  89. state_store(struct device *dev, struct device_attribute *attr,
  90. const char *buf, size_t count)
  91. {
  92. struct cosm_device *cdev = dev_get_drvdata(dev);
  93. int rc;
  94. if (!cdev)
  95. return -EINVAL;
  96. if (sysfs_streq(buf, "boot")) {
  97. rc = cosm_start(cdev);
  98. goto done;
  99. }
  100. if (sysfs_streq(buf, "reset")) {
  101. rc = cosm_reset(cdev);
  102. goto done;
  103. }
  104. if (sysfs_streq(buf, "shutdown")) {
  105. rc = cosm_shutdown(cdev);
  106. goto done;
  107. }
  108. rc = -EINVAL;
  109. done:
  110. if (rc)
  111. count = rc;
  112. return count;
  113. }
  114. static DEVICE_ATTR_RW(state);
  115. static ssize_t shutdown_status_show(struct device *dev,
  116. struct device_attribute *attr, char *buf)
  117. {
  118. struct cosm_device *cdev = dev_get_drvdata(dev);
  119. if (!cdev || cdev->shutdown_status >= MIC_STATUS_LAST)
  120. return -EINVAL;
  121. return scnprintf(buf, PAGE_SIZE, "%s\n",
  122. cosm_shutdown_status_string[cdev->shutdown_status]);
  123. }
  124. static DEVICE_ATTR_RO(shutdown_status);
  125. static ssize_t
  126. heartbeat_enable_show(struct device *dev,
  127. struct device_attribute *attr, char *buf)
  128. {
  129. struct cosm_device *cdev = dev_get_drvdata(dev);
  130. if (!cdev)
  131. return -EINVAL;
  132. return scnprintf(buf, PAGE_SIZE, "%d\n", cdev->sysfs_heartbeat_enable);
  133. }
  134. static ssize_t
  135. heartbeat_enable_store(struct device *dev,
  136. struct device_attribute *attr,
  137. const char *buf, size_t count)
  138. {
  139. struct cosm_device *cdev = dev_get_drvdata(dev);
  140. int enable;
  141. int ret;
  142. if (!cdev)
  143. return -EINVAL;
  144. mutex_lock(&cdev->cosm_mutex);
  145. ret = kstrtoint(buf, 10, &enable);
  146. if (ret)
  147. goto unlock;
  148. cdev->sysfs_heartbeat_enable = enable;
  149. /* if state is not online, cdev->heartbeat_watchdog_enable is 0 */
  150. if (cdev->state == MIC_ONLINE)
  151. cdev->heartbeat_watchdog_enable = enable;
  152. ret = count;
  153. unlock:
  154. mutex_unlock(&cdev->cosm_mutex);
  155. return ret;
  156. }
  157. static DEVICE_ATTR_RW(heartbeat_enable);
  158. static ssize_t
  159. cmdline_show(struct device *dev, struct device_attribute *attr, char *buf)
  160. {
  161. struct cosm_device *cdev = dev_get_drvdata(dev);
  162. char *cmdline;
  163. if (!cdev)
  164. return -EINVAL;
  165. cmdline = cdev->cmdline;
  166. if (cmdline)
  167. return scnprintf(buf, PAGE_SIZE, "%s\n", cmdline);
  168. return 0;
  169. }
  170. static ssize_t
  171. cmdline_store(struct device *dev, struct device_attribute *attr,
  172. const char *buf, size_t count)
  173. {
  174. struct cosm_device *cdev = dev_get_drvdata(dev);
  175. if (!cdev)
  176. return -EINVAL;
  177. mutex_lock(&cdev->cosm_mutex);
  178. kfree(cdev->cmdline);
  179. cdev->cmdline = kmalloc(count + 1, GFP_KERNEL);
  180. if (!cdev->cmdline) {
  181. count = -ENOMEM;
  182. goto unlock;
  183. }
  184. strncpy(cdev->cmdline, buf, count);
  185. if (cdev->cmdline[count - 1] == '\n')
  186. cdev->cmdline[count - 1] = '\0';
  187. else
  188. cdev->cmdline[count] = '\0';
  189. unlock:
  190. mutex_unlock(&cdev->cosm_mutex);
  191. return count;
  192. }
  193. static DEVICE_ATTR_RW(cmdline);
  194. static ssize_t
  195. firmware_show(struct device *dev, struct device_attribute *attr, char *buf)
  196. {
  197. struct cosm_device *cdev = dev_get_drvdata(dev);
  198. char *firmware;
  199. if (!cdev)
  200. return -EINVAL;
  201. firmware = cdev->firmware;
  202. if (firmware)
  203. return scnprintf(buf, PAGE_SIZE, "%s\n", firmware);
  204. return 0;
  205. }
  206. static ssize_t
  207. firmware_store(struct device *dev, struct device_attribute *attr,
  208. const char *buf, size_t count)
  209. {
  210. struct cosm_device *cdev = dev_get_drvdata(dev);
  211. if (!cdev)
  212. return -EINVAL;
  213. mutex_lock(&cdev->cosm_mutex);
  214. kfree(cdev->firmware);
  215. cdev->firmware = kmalloc(count + 1, GFP_KERNEL);
  216. if (!cdev->firmware) {
  217. count = -ENOMEM;
  218. goto unlock;
  219. }
  220. strncpy(cdev->firmware, buf, count);
  221. if (cdev->firmware[count - 1] == '\n')
  222. cdev->firmware[count - 1] = '\0';
  223. else
  224. cdev->firmware[count] = '\0';
  225. unlock:
  226. mutex_unlock(&cdev->cosm_mutex);
  227. return count;
  228. }
  229. static DEVICE_ATTR_RW(firmware);
  230. static ssize_t
  231. ramdisk_show(struct device *dev, struct device_attribute *attr, char *buf)
  232. {
  233. struct cosm_device *cdev = dev_get_drvdata(dev);
  234. char *ramdisk;
  235. if (!cdev)
  236. return -EINVAL;
  237. ramdisk = cdev->ramdisk;
  238. if (ramdisk)
  239. return scnprintf(buf, PAGE_SIZE, "%s\n", ramdisk);
  240. return 0;
  241. }
  242. static ssize_t
  243. ramdisk_store(struct device *dev, struct device_attribute *attr,
  244. const char *buf, size_t count)
  245. {
  246. struct cosm_device *cdev = dev_get_drvdata(dev);
  247. if (!cdev)
  248. return -EINVAL;
  249. mutex_lock(&cdev->cosm_mutex);
  250. kfree(cdev->ramdisk);
  251. cdev->ramdisk = kmalloc(count + 1, GFP_KERNEL);
  252. if (!cdev->ramdisk) {
  253. count = -ENOMEM;
  254. goto unlock;
  255. }
  256. strncpy(cdev->ramdisk, buf, count);
  257. if (cdev->ramdisk[count - 1] == '\n')
  258. cdev->ramdisk[count - 1] = '\0';
  259. else
  260. cdev->ramdisk[count] = '\0';
  261. unlock:
  262. mutex_unlock(&cdev->cosm_mutex);
  263. return count;
  264. }
  265. static DEVICE_ATTR_RW(ramdisk);
  266. static ssize_t
  267. bootmode_show(struct device *dev, struct device_attribute *attr, char *buf)
  268. {
  269. struct cosm_device *cdev = dev_get_drvdata(dev);
  270. char *bootmode;
  271. if (!cdev)
  272. return -EINVAL;
  273. bootmode = cdev->bootmode;
  274. if (bootmode)
  275. return scnprintf(buf, PAGE_SIZE, "%s\n", bootmode);
  276. return 0;
  277. }
  278. static ssize_t
  279. bootmode_store(struct device *dev, struct device_attribute *attr,
  280. const char *buf, size_t count)
  281. {
  282. struct cosm_device *cdev = dev_get_drvdata(dev);
  283. if (!cdev)
  284. return -EINVAL;
  285. if (!sysfs_streq(buf, "linux") && !sysfs_streq(buf, "flash"))
  286. return -EINVAL;
  287. mutex_lock(&cdev->cosm_mutex);
  288. kfree(cdev->bootmode);
  289. cdev->bootmode = kmalloc(count + 1, GFP_KERNEL);
  290. if (!cdev->bootmode) {
  291. count = -ENOMEM;
  292. goto unlock;
  293. }
  294. strncpy(cdev->bootmode, buf, count);
  295. if (cdev->bootmode[count - 1] == '\n')
  296. cdev->bootmode[count - 1] = '\0';
  297. else
  298. cdev->bootmode[count] = '\0';
  299. unlock:
  300. mutex_unlock(&cdev->cosm_mutex);
  301. return count;
  302. }
  303. static DEVICE_ATTR_RW(bootmode);
  304. static ssize_t
  305. log_buf_addr_show(struct device *dev, struct device_attribute *attr,
  306. char *buf)
  307. {
  308. struct cosm_device *cdev = dev_get_drvdata(dev);
  309. if (!cdev)
  310. return -EINVAL;
  311. return scnprintf(buf, PAGE_SIZE, "%p\n", cdev->log_buf_addr);
  312. }
  313. static ssize_t
  314. log_buf_addr_store(struct device *dev, struct device_attribute *attr,
  315. const char *buf, size_t count)
  316. {
  317. struct cosm_device *cdev = dev_get_drvdata(dev);
  318. int ret;
  319. unsigned long addr;
  320. if (!cdev)
  321. return -EINVAL;
  322. ret = kstrtoul(buf, 16, &addr);
  323. if (ret)
  324. goto exit;
  325. cdev->log_buf_addr = (void *)addr;
  326. ret = count;
  327. exit:
  328. return ret;
  329. }
  330. static DEVICE_ATTR_RW(log_buf_addr);
  331. static ssize_t
  332. log_buf_len_show(struct device *dev, struct device_attribute *attr,
  333. char *buf)
  334. {
  335. struct cosm_device *cdev = dev_get_drvdata(dev);
  336. if (!cdev)
  337. return -EINVAL;
  338. return scnprintf(buf, PAGE_SIZE, "%p\n", cdev->log_buf_len);
  339. }
  340. static ssize_t
  341. log_buf_len_store(struct device *dev, struct device_attribute *attr,
  342. const char *buf, size_t count)
  343. {
  344. struct cosm_device *cdev = dev_get_drvdata(dev);
  345. int ret;
  346. unsigned long addr;
  347. if (!cdev)
  348. return -EINVAL;
  349. ret = kstrtoul(buf, 16, &addr);
  350. if (ret)
  351. goto exit;
  352. cdev->log_buf_len = (int *)addr;
  353. ret = count;
  354. exit:
  355. return ret;
  356. }
  357. static DEVICE_ATTR_RW(log_buf_len);
  358. static struct attribute *cosm_default_attrs[] = {
  359. &dev_attr_family.attr,
  360. &dev_attr_stepping.attr,
  361. &dev_attr_state.attr,
  362. &dev_attr_shutdown_status.attr,
  363. &dev_attr_heartbeat_enable.attr,
  364. &dev_attr_cmdline.attr,
  365. &dev_attr_firmware.attr,
  366. &dev_attr_ramdisk.attr,
  367. &dev_attr_bootmode.attr,
  368. &dev_attr_log_buf_addr.attr,
  369. &dev_attr_log_buf_len.attr,
  370. NULL
  371. };
  372. ATTRIBUTE_GROUPS(cosm_default);
  373. void cosm_sysfs_init(struct cosm_device *cdev)
  374. {
  375. cdev->attr_group = cosm_default_groups;
  376. }