cosm_scif_server.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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/kthread.h>
  22. #include <linux/sched/signal.h>
  23. #include "cosm_main.h"
  24. /*
  25. * The COSM driver uses SCIF to communicate between the management node and the
  26. * MIC cards. SCIF is used to (a) Send a shutdown command to the card (b)
  27. * receive a shutdown status back from the card upon completion of shutdown and
  28. * (c) receive periodic heartbeat messages from the card used to deduce if the
  29. * card has crashed.
  30. *
  31. * A COSM server consisting of a SCIF listening endpoint waits for incoming
  32. * connections from the card. Upon acceptance of the connection, a separate
  33. * work-item is scheduled to handle SCIF message processing for that card. The
  34. * life-time of this work-item is therefore the time from which the connection
  35. * from a card is accepted to the time at which the connection is closed. A new
  36. * work-item starts each time the card boots and is alive till the card (a)
  37. * shuts down (b) is reset (c) crashes (d) cosm_client driver on the card is
  38. * unloaded.
  39. *
  40. * From the point of view of COSM interactions with SCIF during card
  41. * shutdown, reset and crash are as follows:
  42. *
  43. * Card shutdown
  44. * -------------
  45. * 1. COSM client on the card invokes orderly_poweroff() in response to SHUTDOWN
  46. * message from the host.
  47. * 2. Card driver shutdown callback invokes scif_unregister_device(..) resulting
  48. * in scif_remove(..) getting called on the card
  49. * 3. scif_remove -> scif_stop -> scif_handle_remove_node ->
  50. * scif_peer_unregister_device -> device_unregister for the host peer device
  51. * 4. During device_unregister remove(..) method of cosm_client is invoked which
  52. * closes the COSM SCIF endpoint on the card. This results in a SCIF_DISCNCT
  53. * message being sent to host SCIF. SCIF_DISCNCT message processing on the
  54. * host SCIF sets the host COSM SCIF endpoint state to DISCONNECTED and wakes
  55. * up the host COSM thread blocked in scif_poll(..) resulting in
  56. * scif_poll(..) returning POLLHUP.
  57. * 5. On the card, scif_peer_release_dev is next called which results in an
  58. * SCIF_EXIT message being sent to the host and after receiving the
  59. * SCIF_EXIT_ACK from the host the peer device teardown on the card is
  60. * complete.
  61. * 6. As part of the SCIF_EXIT message processing on the host, host sends a
  62. * SCIF_REMOVE_NODE to itself corresponding to the card being removed. This
  63. * starts a similar SCIF peer device teardown sequence on the host
  64. * corresponding to the card being shut down.
  65. *
  66. * Card reset
  67. * ----------
  68. * The case of interest here is when the card has not been previously shut down
  69. * since most of the steps below are skipped in that case:
  70. * 1. cosm_stop(..) invokes hw_ops->stop(..) method of the base PCIe driver
  71. * which unregisters the SCIF HW device resulting in scif_remove(..) being
  72. * called on the host.
  73. * 2. scif_remove(..) calls scif_disconnect_node(..) which results in a
  74. * SCIF_EXIT message being sent to the card.
  75. * 3. The card executes scif_stop() as part of SCIF_EXIT message
  76. * processing. This results in the COSM endpoint on the card being closed and
  77. * the SCIF host peer device on the card getting unregistered similar to
  78. * steps 3, 4 and 5 for the card shutdown case above. scif_poll(..) on the
  79. * host returns POLLHUP as a result.
  80. * 4. On the host, card peer device unregister and SCIF HW remove(..) also
  81. * subsequently complete.
  82. *
  83. * Card crash
  84. * ----------
  85. * If a reset is issued after the card has crashed, there is no SCIF_DISCNT
  86. * message from the card which would result in scif_poll(..) returning
  87. * POLLHUP. In this case when the host SCIF driver sends a SCIF_REMOVE_NODE
  88. * message to itself resulting in the card SCIF peer device being unregistered,
  89. * this results in a scif_peer_release_dev -> scif_cleanup_scifdev->
  90. * scif_invalidate_ep call sequence which sets the endpoint state to
  91. * DISCONNECTED and results in scif_poll(..) returning POLLHUP.
  92. */
  93. #define COSM_SCIF_BACKLOG 16
  94. #define COSM_HEARTBEAT_CHECK_DELTA_SEC 10
  95. #define COSM_HEARTBEAT_TIMEOUT_SEC \
  96. (COSM_HEARTBEAT_SEND_SEC + COSM_HEARTBEAT_CHECK_DELTA_SEC)
  97. #define COSM_HEARTBEAT_TIMEOUT_MSEC (COSM_HEARTBEAT_TIMEOUT_SEC * MSEC_PER_SEC)
  98. static struct task_struct *server_thread;
  99. static scif_epd_t listen_epd;
  100. /* Publish MIC card's shutdown status to user space MIC daemon */
  101. static void cosm_update_mic_status(struct cosm_device *cdev)
  102. {
  103. if (cdev->shutdown_status_int != MIC_NOP) {
  104. cosm_set_shutdown_status(cdev, cdev->shutdown_status_int);
  105. cdev->shutdown_status_int = MIC_NOP;
  106. }
  107. }
  108. /* Store MIC card's shutdown status internally when it is received */
  109. static void cosm_shutdown_status_int(struct cosm_device *cdev,
  110. enum mic_status shutdown_status)
  111. {
  112. switch (shutdown_status) {
  113. case MIC_HALTED:
  114. case MIC_POWER_OFF:
  115. case MIC_RESTART:
  116. case MIC_CRASHED:
  117. break;
  118. default:
  119. dev_err(&cdev->dev, "%s %d Unexpected shutdown_status %d\n",
  120. __func__, __LINE__, shutdown_status);
  121. return;
  122. };
  123. cdev->shutdown_status_int = shutdown_status;
  124. cdev->heartbeat_watchdog_enable = false;
  125. if (cdev->state != MIC_SHUTTING_DOWN)
  126. cosm_set_state(cdev, MIC_SHUTTING_DOWN);
  127. }
  128. /* Non-blocking recv. Read and process all available messages */
  129. static void cosm_scif_recv(struct cosm_device *cdev)
  130. {
  131. struct cosm_msg msg;
  132. int rc;
  133. while (1) {
  134. rc = scif_recv(cdev->epd, &msg, sizeof(msg), 0);
  135. if (!rc) {
  136. break;
  137. } else if (rc < 0) {
  138. dev_dbg(&cdev->dev, "%s: %d rc %d\n",
  139. __func__, __LINE__, rc);
  140. break;
  141. }
  142. dev_dbg(&cdev->dev, "%s: %d rc %d id 0x%llx\n",
  143. __func__, __LINE__, rc, msg.id);
  144. switch (msg.id) {
  145. case COSM_MSG_SHUTDOWN_STATUS:
  146. cosm_shutdown_status_int(cdev, msg.shutdown_status);
  147. break;
  148. case COSM_MSG_HEARTBEAT:
  149. /* Nothing to do, heartbeat only unblocks scif_poll */
  150. break;
  151. default:
  152. dev_err(&cdev->dev, "%s: %d unknown msg.id %lld\n",
  153. __func__, __LINE__, msg.id);
  154. break;
  155. }
  156. }
  157. }
  158. /* Publish crashed status for this MIC card */
  159. static void cosm_set_crashed(struct cosm_device *cdev)
  160. {
  161. dev_err(&cdev->dev, "node alive timeout\n");
  162. cosm_shutdown_status_int(cdev, MIC_CRASHED);
  163. cosm_update_mic_status(cdev);
  164. }
  165. /* Send host time to the MIC card to sync system time between host and MIC */
  166. static void cosm_send_time(struct cosm_device *cdev)
  167. {
  168. struct cosm_msg msg = { .id = COSM_MSG_SYNC_TIME };
  169. int rc;
  170. getnstimeofday64(&msg.timespec);
  171. rc = scif_send(cdev->epd, &msg, sizeof(msg), SCIF_SEND_BLOCK);
  172. if (rc < 0)
  173. dev_err(&cdev->dev, "%s %d scif_send failed rc %d\n",
  174. __func__, __LINE__, rc);
  175. }
  176. /*
  177. * Close this cosm_device's endpoint after its peer endpoint on the card has
  178. * been closed. In all cases except MIC card crash POLLHUP on the host is
  179. * triggered by the client's endpoint being closed.
  180. */
  181. static void cosm_scif_close(struct cosm_device *cdev)
  182. {
  183. /*
  184. * Because SHUTDOWN_STATUS message is sent by the MIC cards in the
  185. * reboot notifier when shutdown is still not complete, we notify mpssd
  186. * to reset the card when SCIF endpoint is closed.
  187. */
  188. cosm_update_mic_status(cdev);
  189. scif_close(cdev->epd);
  190. cdev->epd = NULL;
  191. dev_dbg(&cdev->dev, "%s %d\n", __func__, __LINE__);
  192. }
  193. /*
  194. * Set card state to ONLINE when a new SCIF connection from a MIC card is
  195. * received. Normally the state is BOOTING when the connection comes in, but can
  196. * be ONLINE if cosm_client driver on the card was unloaded and then reloaded.
  197. */
  198. static int cosm_set_online(struct cosm_device *cdev)
  199. {
  200. int rc = 0;
  201. if (MIC_BOOTING == cdev->state || MIC_ONLINE == cdev->state) {
  202. cdev->heartbeat_watchdog_enable = cdev->sysfs_heartbeat_enable;
  203. cdev->epd = cdev->newepd;
  204. if (cdev->state == MIC_BOOTING)
  205. cosm_set_state(cdev, MIC_ONLINE);
  206. cosm_send_time(cdev);
  207. dev_dbg(&cdev->dev, "%s %d\n", __func__, __LINE__);
  208. } else {
  209. dev_warn(&cdev->dev, "%s %d not going online in state: %s\n",
  210. __func__, __LINE__, cosm_state_string[cdev->state]);
  211. rc = -EINVAL;
  212. }
  213. /* Drop reference acquired by bus_find_device in the server thread */
  214. put_device(&cdev->dev);
  215. return rc;
  216. }
  217. /*
  218. * Work function for handling work for a SCIF connection from a particular MIC
  219. * card. It first sets the card state to ONLINE and then calls scif_poll to
  220. * block on activity such as incoming messages on the SCIF endpoint. When the
  221. * endpoint is closed, the work function exits, completing its life cycle, from
  222. * MIC card boot to card shutdown/reset/crash.
  223. */
  224. void cosm_scif_work(struct work_struct *work)
  225. {
  226. struct cosm_device *cdev = container_of(work, struct cosm_device,
  227. scif_work);
  228. struct scif_pollepd pollepd;
  229. int rc;
  230. mutex_lock(&cdev->cosm_mutex);
  231. if (cosm_set_online(cdev))
  232. goto exit;
  233. while (1) {
  234. pollepd.epd = cdev->epd;
  235. pollepd.events = POLLIN;
  236. /* Drop the mutex before blocking in scif_poll(..) */
  237. mutex_unlock(&cdev->cosm_mutex);
  238. /* poll(..) with timeout on our endpoint */
  239. rc = scif_poll(&pollepd, 1, COSM_HEARTBEAT_TIMEOUT_MSEC);
  240. mutex_lock(&cdev->cosm_mutex);
  241. if (rc < 0) {
  242. dev_err(&cdev->dev, "%s %d scif_poll rc %d\n",
  243. __func__, __LINE__, rc);
  244. continue;
  245. }
  246. /* There is a message from the card */
  247. if (pollepd.revents & POLLIN)
  248. cosm_scif_recv(cdev);
  249. /* The peer endpoint is closed or this endpoint disconnected */
  250. if (pollepd.revents & POLLHUP) {
  251. cosm_scif_close(cdev);
  252. break;
  253. }
  254. /* Did we timeout from poll? */
  255. if (!rc && cdev->heartbeat_watchdog_enable)
  256. cosm_set_crashed(cdev);
  257. }
  258. exit:
  259. dev_dbg(&cdev->dev, "%s %d exiting\n", __func__, __LINE__);
  260. mutex_unlock(&cdev->cosm_mutex);
  261. }
  262. /*
  263. * COSM SCIF server thread function. Accepts incoming SCIF connections from MIC
  264. * cards, finds the correct cosm_device to associate that connection with and
  265. * schedules individual work items for each MIC card.
  266. */
  267. static int cosm_scif_server(void *unused)
  268. {
  269. struct cosm_device *cdev;
  270. scif_epd_t newepd;
  271. struct scif_port_id port_id;
  272. int rc;
  273. allow_signal(SIGKILL);
  274. while (!kthread_should_stop()) {
  275. rc = scif_accept(listen_epd, &port_id, &newepd,
  276. SCIF_ACCEPT_SYNC);
  277. if (rc < 0) {
  278. if (-ERESTARTSYS != rc)
  279. pr_err("%s %d rc %d\n", __func__, __LINE__, rc);
  280. continue;
  281. }
  282. /*
  283. * Associate the incoming connection with a particular
  284. * cosm_device, COSM device ID == SCIF node ID - 1
  285. */
  286. cdev = cosm_find_cdev_by_id(port_id.node - 1);
  287. if (!cdev)
  288. continue;
  289. cdev->newepd = newepd;
  290. schedule_work(&cdev->scif_work);
  291. }
  292. pr_debug("%s %d Server thread stopped\n", __func__, __LINE__);
  293. return 0;
  294. }
  295. static int cosm_scif_listen(void)
  296. {
  297. int rc;
  298. listen_epd = scif_open();
  299. if (!listen_epd) {
  300. pr_err("%s %d scif_open failed\n", __func__, __LINE__);
  301. return -ENOMEM;
  302. }
  303. rc = scif_bind(listen_epd, SCIF_COSM_LISTEN_PORT);
  304. if (rc < 0) {
  305. pr_err("%s %d scif_bind failed rc %d\n",
  306. __func__, __LINE__, rc);
  307. goto err;
  308. }
  309. rc = scif_listen(listen_epd, COSM_SCIF_BACKLOG);
  310. if (rc < 0) {
  311. pr_err("%s %d scif_listen rc %d\n", __func__, __LINE__, rc);
  312. goto err;
  313. }
  314. pr_debug("%s %d listen_epd set up\n", __func__, __LINE__);
  315. return 0;
  316. err:
  317. scif_close(listen_epd);
  318. listen_epd = NULL;
  319. return rc;
  320. }
  321. static void cosm_scif_listen_exit(void)
  322. {
  323. pr_debug("%s %d closing listen_epd\n", __func__, __LINE__);
  324. if (listen_epd) {
  325. scif_close(listen_epd);
  326. listen_epd = NULL;
  327. }
  328. }
  329. /*
  330. * Create a listening SCIF endpoint and a server kthread which accepts incoming
  331. * SCIF connections from MIC cards
  332. */
  333. int cosm_scif_init(void)
  334. {
  335. int rc = cosm_scif_listen();
  336. if (rc) {
  337. pr_err("%s %d cosm_scif_listen rc %d\n",
  338. __func__, __LINE__, rc);
  339. goto err;
  340. }
  341. server_thread = kthread_run(cosm_scif_server, NULL, "cosm_server");
  342. if (IS_ERR(server_thread)) {
  343. rc = PTR_ERR(server_thread);
  344. pr_err("%s %d kthread_run rc %d\n", __func__, __LINE__, rc);
  345. goto listen_exit;
  346. }
  347. return 0;
  348. listen_exit:
  349. cosm_scif_listen_exit();
  350. err:
  351. return rc;
  352. }
  353. /* Stop the running server thread and close the listening SCIF endpoint */
  354. void cosm_scif_exit(void)
  355. {
  356. int rc;
  357. if (!IS_ERR_OR_NULL(server_thread)) {
  358. rc = send_sig(SIGKILL, server_thread, 0);
  359. if (rc) {
  360. pr_err("%s %d send_sig rc %d\n",
  361. __func__, __LINE__, rc);
  362. return;
  363. }
  364. kthread_stop(server_thread);
  365. }
  366. cosm_scif_listen_exit();
  367. }