cosm_scif_client.c 6.3 KB

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