cosm_scif_server.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 EPOLLHUP.
  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 EPOLLHUP 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. * EPOLLHUP. 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 EPOLLHUP.
  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. struct timespec64 ts;
  170. int rc;
  171. ktime_get_real_ts64(&ts);
  172. msg.timespec.tv_sec = ts.tv_sec;
  173. msg.timespec.tv_nsec = ts.tv_nsec;
  174. rc = scif_send(cdev->epd, &msg, sizeof(msg), SCIF_SEND_BLOCK);
  175. if (rc < 0)
  176. dev_err(&cdev->dev, "%s %d scif_send failed rc %d\n",
  177. __func__, __LINE__, rc);
  178. }
  179. /*
  180. * Close this cosm_device's endpoint after its peer endpoint on the card has
  181. * been closed. In all cases except MIC card crash EPOLLHUP on the host is
  182. * triggered by the client's endpoint being closed.
  183. */
  184. static void cosm_scif_close(struct cosm_device *cdev)
  185. {
  186. /*
  187. * Because SHUTDOWN_STATUS message is sent by the MIC cards in the
  188. * reboot notifier when shutdown is still not complete, we notify mpssd
  189. * to reset the card when SCIF endpoint is closed.
  190. */
  191. cosm_update_mic_status(cdev);
  192. scif_close(cdev->epd);
  193. cdev->epd = NULL;
  194. dev_dbg(&cdev->dev, "%s %d\n", __func__, __LINE__);
  195. }
  196. /*
  197. * Set card state to ONLINE when a new SCIF connection from a MIC card is
  198. * received. Normally the state is BOOTING when the connection comes in, but can
  199. * be ONLINE if cosm_client driver on the card was unloaded and then reloaded.
  200. */
  201. static int cosm_set_online(struct cosm_device *cdev)
  202. {
  203. int rc = 0;
  204. if (MIC_BOOTING == cdev->state || MIC_ONLINE == cdev->state) {
  205. cdev->heartbeat_watchdog_enable = cdev->sysfs_heartbeat_enable;
  206. cdev->epd = cdev->newepd;
  207. if (cdev->state == MIC_BOOTING)
  208. cosm_set_state(cdev, MIC_ONLINE);
  209. cosm_send_time(cdev);
  210. dev_dbg(&cdev->dev, "%s %d\n", __func__, __LINE__);
  211. } else {
  212. dev_warn(&cdev->dev, "%s %d not going online in state: %s\n",
  213. __func__, __LINE__, cosm_state_string[cdev->state]);
  214. rc = -EINVAL;
  215. }
  216. /* Drop reference acquired by bus_find_device in the server thread */
  217. put_device(&cdev->dev);
  218. return rc;
  219. }
  220. /*
  221. * Work function for handling work for a SCIF connection from a particular MIC
  222. * card. It first sets the card state to ONLINE and then calls scif_poll to
  223. * block on activity such as incoming messages on the SCIF endpoint. When the
  224. * endpoint is closed, the work function exits, completing its life cycle, from
  225. * MIC card boot to card shutdown/reset/crash.
  226. */
  227. void cosm_scif_work(struct work_struct *work)
  228. {
  229. struct cosm_device *cdev = container_of(work, struct cosm_device,
  230. scif_work);
  231. struct scif_pollepd pollepd;
  232. int rc;
  233. mutex_lock(&cdev->cosm_mutex);
  234. if (cosm_set_online(cdev))
  235. goto exit;
  236. while (1) {
  237. pollepd.epd = cdev->epd;
  238. pollepd.events = EPOLLIN;
  239. /* Drop the mutex before blocking in scif_poll(..) */
  240. mutex_unlock(&cdev->cosm_mutex);
  241. /* poll(..) with timeout on our endpoint */
  242. rc = scif_poll(&pollepd, 1, COSM_HEARTBEAT_TIMEOUT_MSEC);
  243. mutex_lock(&cdev->cosm_mutex);
  244. if (rc < 0) {
  245. dev_err(&cdev->dev, "%s %d scif_poll rc %d\n",
  246. __func__, __LINE__, rc);
  247. continue;
  248. }
  249. /* There is a message from the card */
  250. if (pollepd.revents & EPOLLIN)
  251. cosm_scif_recv(cdev);
  252. /* The peer endpoint is closed or this endpoint disconnected */
  253. if (pollepd.revents & EPOLLHUP) {
  254. cosm_scif_close(cdev);
  255. break;
  256. }
  257. /* Did we timeout from poll? */
  258. if (!rc && cdev->heartbeat_watchdog_enable)
  259. cosm_set_crashed(cdev);
  260. }
  261. exit:
  262. dev_dbg(&cdev->dev, "%s %d exiting\n", __func__, __LINE__);
  263. mutex_unlock(&cdev->cosm_mutex);
  264. }
  265. /*
  266. * COSM SCIF server thread function. Accepts incoming SCIF connections from MIC
  267. * cards, finds the correct cosm_device to associate that connection with and
  268. * schedules individual work items for each MIC card.
  269. */
  270. static int cosm_scif_server(void *unused)
  271. {
  272. struct cosm_device *cdev;
  273. scif_epd_t newepd;
  274. struct scif_port_id port_id;
  275. int rc;
  276. allow_signal(SIGKILL);
  277. while (!kthread_should_stop()) {
  278. rc = scif_accept(listen_epd, &port_id, &newepd,
  279. SCIF_ACCEPT_SYNC);
  280. if (rc < 0) {
  281. if (-ERESTARTSYS != rc)
  282. pr_err("%s %d rc %d\n", __func__, __LINE__, rc);
  283. continue;
  284. }
  285. /*
  286. * Associate the incoming connection with a particular
  287. * cosm_device, COSM device ID == SCIF node ID - 1
  288. */
  289. cdev = cosm_find_cdev_by_id(port_id.node - 1);
  290. if (!cdev)
  291. continue;
  292. cdev->newepd = newepd;
  293. schedule_work(&cdev->scif_work);
  294. }
  295. pr_debug("%s %d Server thread stopped\n", __func__, __LINE__);
  296. return 0;
  297. }
  298. static int cosm_scif_listen(void)
  299. {
  300. int rc;
  301. listen_epd = scif_open();
  302. if (!listen_epd) {
  303. pr_err("%s %d scif_open failed\n", __func__, __LINE__);
  304. return -ENOMEM;
  305. }
  306. rc = scif_bind(listen_epd, SCIF_COSM_LISTEN_PORT);
  307. if (rc < 0) {
  308. pr_err("%s %d scif_bind failed rc %d\n",
  309. __func__, __LINE__, rc);
  310. goto err;
  311. }
  312. rc = scif_listen(listen_epd, COSM_SCIF_BACKLOG);
  313. if (rc < 0) {
  314. pr_err("%s %d scif_listen rc %d\n", __func__, __LINE__, rc);
  315. goto err;
  316. }
  317. pr_debug("%s %d listen_epd set up\n", __func__, __LINE__);
  318. return 0;
  319. err:
  320. scif_close(listen_epd);
  321. listen_epd = NULL;
  322. return rc;
  323. }
  324. static void cosm_scif_listen_exit(void)
  325. {
  326. pr_debug("%s %d closing listen_epd\n", __func__, __LINE__);
  327. if (listen_epd) {
  328. scif_close(listen_epd);
  329. listen_epd = NULL;
  330. }
  331. }
  332. /*
  333. * Create a listening SCIF endpoint and a server kthread which accepts incoming
  334. * SCIF connections from MIC cards
  335. */
  336. int cosm_scif_init(void)
  337. {
  338. int rc = cosm_scif_listen();
  339. if (rc) {
  340. pr_err("%s %d cosm_scif_listen rc %d\n",
  341. __func__, __LINE__, rc);
  342. goto err;
  343. }
  344. server_thread = kthread_run(cosm_scif_server, NULL, "cosm_server");
  345. if (IS_ERR(server_thread)) {
  346. rc = PTR_ERR(server_thread);
  347. pr_err("%s %d kthread_run rc %d\n", __func__, __LINE__, rc);
  348. goto listen_exit;
  349. }
  350. return 0;
  351. listen_exit:
  352. cosm_scif_listen_exit();
  353. err:
  354. return rc;
  355. }
  356. /* Stop the running server thread and close the listening SCIF endpoint */
  357. void cosm_scif_exit(void)
  358. {
  359. int rc;
  360. if (!IS_ERR_OR_NULL(server_thread)) {
  361. rc = send_sig(SIGKILL, server_thread, 0);
  362. if (rc) {
  363. pr_err("%s %d send_sig rc %d\n",
  364. __func__, __LINE__, rc);
  365. return;
  366. }
  367. kthread_stop(server_thread);
  368. }
  369. cosm_scif_listen_exit();
  370. }