cosm_scif_client.c 6.4 KB

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