cosm_scif_server.c 12 KB

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