cosm_scif_client.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 COSM Client Driver
  19. *
  20. */
  21. #include <linux/module.h>
  22. #include <linux/delay.h>
  23. #include <linux/reboot.h>
  24. #include <linux/kthread.h>
  25. #include "../cosm/cosm_main.h"
  26. #define COSM_SCIF_MAX_RETRIES 10
  27. #define COSM_HEARTBEAT_SEND_MSEC (COSM_HEARTBEAT_SEND_SEC * MSEC_PER_SEC)
  28. static struct task_struct *client_thread;
  29. static scif_epd_t client_epd;
  30. static struct scif_peer_dev *client_spdev;
  31. /*
  32. * Reboot notifier: receives shutdown status from the OS and communicates it
  33. * back to the COSM process on the host
  34. */
  35. static int cosm_reboot_event(struct notifier_block *this, unsigned long event,
  36. void *ptr)
  37. {
  38. struct cosm_msg msg = { .id = COSM_MSG_SHUTDOWN_STATUS };
  39. int rc;
  40. event = (event == SYS_RESTART) ? SYSTEM_RESTART : event;
  41. dev_info(&client_spdev->dev, "%s %d received event %ld\n",
  42. __func__, __LINE__, event);
  43. msg.shutdown_status = event;
  44. rc = scif_send(client_epd, &msg, sizeof(msg), SCIF_SEND_BLOCK);
  45. if (rc < 0)
  46. dev_err(&client_spdev->dev, "%s %d scif_send rc %d\n",
  47. __func__, __LINE__, rc);
  48. return NOTIFY_DONE;
  49. }
  50. static struct notifier_block cosm_reboot = {
  51. .notifier_call = cosm_reboot_event,
  52. };
  53. /* Set system time from timespec value received from the host */
  54. static void cosm_set_time(struct cosm_msg *msg)
  55. {
  56. int rc = do_settimeofday64(&msg->timespec);
  57. if (rc)
  58. dev_err(&client_spdev->dev, "%s: %d settimeofday rc %d\n",
  59. __func__, __LINE__, rc);
  60. }
  61. /* COSM client receive message processing */
  62. static void cosm_client_recv(void)
  63. {
  64. struct cosm_msg msg;
  65. int rc;
  66. while (1) {
  67. rc = scif_recv(client_epd, &msg, sizeof(msg), 0);
  68. if (!rc) {
  69. return;
  70. } else if (rc < 0) {
  71. dev_err(&client_spdev->dev, "%s: %d rc %d\n",
  72. __func__, __LINE__, rc);
  73. return;
  74. }
  75. dev_dbg(&client_spdev->dev, "%s: %d rc %d id 0x%llx\n",
  76. __func__, __LINE__, rc, msg.id);
  77. switch (msg.id) {
  78. case COSM_MSG_SYNC_TIME:
  79. cosm_set_time(&msg);
  80. break;
  81. case COSM_MSG_SHUTDOWN:
  82. orderly_poweroff(true);
  83. break;
  84. default:
  85. dev_err(&client_spdev->dev, "%s: %d unknown id %lld\n",
  86. __func__, __LINE__, msg.id);
  87. break;
  88. }
  89. }
  90. }
  91. /* Initiate connection to the COSM server on the host */
  92. static int cosm_scif_connect(void)
  93. {
  94. struct scif_port_id port_id;
  95. int i, rc;
  96. client_epd = scif_open();
  97. if (!client_epd) {
  98. dev_err(&client_spdev->dev, "%s %d scif_open failed\n",
  99. __func__, __LINE__);
  100. return -ENOMEM;
  101. }
  102. port_id.node = 0;
  103. port_id.port = SCIF_COSM_LISTEN_PORT;
  104. for (i = 0; i < COSM_SCIF_MAX_RETRIES; i++) {
  105. rc = scif_connect(client_epd, &port_id);
  106. if (rc < 0)
  107. msleep(1000);
  108. else
  109. break;
  110. }
  111. if (rc < 0) {
  112. dev_err(&client_spdev->dev, "%s %d scif_connect rc %d\n",
  113. __func__, __LINE__, rc);
  114. scif_close(client_epd);
  115. client_epd = NULL;
  116. }
  117. return rc < 0 ? rc : 0;
  118. }
  119. /* Close host SCIF connection */
  120. static void cosm_scif_connect_exit(void)
  121. {
  122. if (client_epd) {
  123. scif_close(client_epd);
  124. client_epd = NULL;
  125. }
  126. }
  127. /*
  128. * COSM SCIF client thread function: waits for messages from the host and sends
  129. * a heartbeat to the host
  130. */
  131. static int cosm_scif_client(void *unused)
  132. {
  133. struct cosm_msg msg = { .id = COSM_MSG_HEARTBEAT };
  134. struct scif_pollepd pollepd;
  135. int rc;
  136. allow_signal(SIGKILL);
  137. while (!kthread_should_stop()) {
  138. pollepd.epd = client_epd;
  139. pollepd.events = POLLIN;
  140. rc = scif_poll(&pollepd, 1, COSM_HEARTBEAT_SEND_MSEC);
  141. if (rc < 0) {
  142. if (-EINTR != rc)
  143. dev_err(&client_spdev->dev,
  144. "%s %d scif_poll rc %d\n",
  145. __func__, __LINE__, rc);
  146. continue;
  147. }
  148. if (pollepd.revents & POLLIN)
  149. cosm_client_recv();
  150. msg.id = COSM_MSG_HEARTBEAT;
  151. rc = scif_send(client_epd, &msg, sizeof(msg), SCIF_SEND_BLOCK);
  152. if (rc < 0)
  153. dev_err(&client_spdev->dev, "%s %d scif_send rc %d\n",
  154. __func__, __LINE__, rc);
  155. }
  156. dev_dbg(&client_spdev->dev, "%s %d Client thread stopped\n",
  157. __func__, __LINE__);
  158. return 0;
  159. }
  160. static void cosm_scif_probe(struct scif_peer_dev *spdev)
  161. {
  162. int rc;
  163. dev_dbg(&spdev->dev, "%s %d: dnode %d\n",
  164. __func__, __LINE__, spdev->dnode);
  165. /* We are only interested in the host with spdev->dnode == 0 */
  166. if (spdev->dnode)
  167. return;
  168. client_spdev = spdev;
  169. rc = cosm_scif_connect();
  170. if (rc)
  171. goto exit;
  172. rc = register_reboot_notifier(&cosm_reboot);
  173. if (rc) {
  174. dev_err(&spdev->dev,
  175. "reboot notifier registration failed rc %d\n", rc);
  176. goto connect_exit;
  177. }
  178. client_thread = kthread_run(cosm_scif_client, NULL, "cosm_client");
  179. if (IS_ERR(client_thread)) {
  180. rc = PTR_ERR(client_thread);
  181. dev_err(&spdev->dev, "%s %d kthread_run rc %d\n",
  182. __func__, __LINE__, rc);
  183. goto unreg_reboot;
  184. }
  185. return;
  186. unreg_reboot:
  187. unregister_reboot_notifier(&cosm_reboot);
  188. connect_exit:
  189. cosm_scif_connect_exit();
  190. exit:
  191. client_spdev = NULL;
  192. }
  193. static void cosm_scif_remove(struct scif_peer_dev *spdev)
  194. {
  195. int rc;
  196. dev_dbg(&spdev->dev, "%s %d: dnode %d\n",
  197. __func__, __LINE__, spdev->dnode);
  198. if (spdev->dnode)
  199. return;
  200. if (!IS_ERR_OR_NULL(client_thread)) {
  201. rc = send_sig(SIGKILL, client_thread, 0);
  202. if (rc) {
  203. pr_err("%s %d send_sig rc %d\n",
  204. __func__, __LINE__, rc);
  205. return;
  206. }
  207. kthread_stop(client_thread);
  208. }
  209. unregister_reboot_notifier(&cosm_reboot);
  210. cosm_scif_connect_exit();
  211. client_spdev = NULL;
  212. }
  213. static struct scif_client scif_client_cosm = {
  214. .name = KBUILD_MODNAME,
  215. .probe = cosm_scif_probe,
  216. .remove = cosm_scif_remove,
  217. };
  218. static int __init cosm_client_init(void)
  219. {
  220. int rc = scif_client_register(&scif_client_cosm);
  221. if (rc)
  222. pr_err("scif_client_register failed rc %d\n", rc);
  223. return rc;
  224. }
  225. static void __exit cosm_client_exit(void)
  226. {
  227. scif_client_unregister(&scif_client_cosm);
  228. }
  229. module_init(cosm_client_init);
  230. module_exit(cosm_client_exit);
  231. MODULE_AUTHOR("Intel Corporation");
  232. MODULE_DESCRIPTION("Intel(R) MIC card OS state management client driver");
  233. MODULE_LICENSE("GPL v2");