2 * Intel MIC Platform Software Stack (MPSS)
4 * Copyright(c) 2015 Intel Corporation.
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.
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.
15 * The full GNU General Public License is included in this distribution in
16 * the file called "COPYING".
18 * Intel MIC Coprocessor State Management (COSM) Driver
21 #include <linux/kthread.h>
22 #include <linux/sched/signal.h>
24 #include "cosm_main.h"
27 * The COSM driver uses SCIF to communicate between the management node and the
28 * MIC cards. SCIF is used to (a) Send a shutdown command to the card (b)
29 * receive a shutdown status back from the card upon completion of shutdown and
30 * (c) receive periodic heartbeat messages from the card used to deduce if the
33 * A COSM server consisting of a SCIF listening endpoint waits for incoming
34 * connections from the card. Upon acceptance of the connection, a separate
35 * work-item is scheduled to handle SCIF message processing for that card. The
36 * life-time of this work-item is therefore the time from which the connection
37 * from a card is accepted to the time at which the connection is closed. A new
38 * work-item starts each time the card boots and is alive till the card (a)
39 * shuts down (b) is reset (c) crashes (d) cosm_client driver on the card is
42 * From the point of view of COSM interactions with SCIF during card
43 * shutdown, reset and crash are as follows:
47 * 1. COSM client on the card invokes orderly_poweroff() in response to SHUTDOWN
48 * message from the host.
49 * 2. Card driver shutdown callback invokes scif_unregister_device(..) resulting
50 * in scif_remove(..) getting called on the card
51 * 3. scif_remove -> scif_stop -> scif_handle_remove_node ->
52 * scif_peer_unregister_device -> device_unregister for the host peer device
53 * 4. During device_unregister remove(..) method of cosm_client is invoked which
54 * closes the COSM SCIF endpoint on the card. This results in a SCIF_DISCNCT
55 * message being sent to host SCIF. SCIF_DISCNCT message processing on the
56 * host SCIF sets the host COSM SCIF endpoint state to DISCONNECTED and wakes
57 * up the host COSM thread blocked in scif_poll(..) resulting in
58 * scif_poll(..) returning EPOLLHUP.
59 * 5. On the card, scif_peer_release_dev is next called which results in an
60 * SCIF_EXIT message being sent to the host and after receiving the
61 * SCIF_EXIT_ACK from the host the peer device teardown on the card is
63 * 6. As part of the SCIF_EXIT message processing on the host, host sends a
64 * SCIF_REMOVE_NODE to itself corresponding to the card being removed. This
65 * starts a similar SCIF peer device teardown sequence on the host
66 * corresponding to the card being shut down.
70 * The case of interest here is when the card has not been previously shut down
71 * since most of the steps below are skipped in that case:
73 * 1. cosm_stop(..) invokes hw_ops->stop(..) method of the base PCIe driver
74 * which unregisters the SCIF HW device resulting in scif_remove(..) being
76 * 2. scif_remove(..) calls scif_disconnect_node(..) which results in a
77 * SCIF_EXIT message being sent to the card.
78 * 3. The card executes scif_stop() as part of SCIF_EXIT message
79 * processing. This results in the COSM endpoint on the card being closed and
80 * the SCIF host peer device on the card getting unregistered similar to
81 * steps 3, 4 and 5 for the card shutdown case above. scif_poll(..) on the
82 * host returns EPOLLHUP as a result.
83 * 4. On the host, card peer device unregister and SCIF HW remove(..) also
84 * subsequently complete.
88 * If a reset is issued after the card has crashed, there is no SCIF_DISCNT
89 * message from the card which would result in scif_poll(..) returning
90 * EPOLLHUP. In this case when the host SCIF driver sends a SCIF_REMOVE_NODE
91 * message to itself resulting in the card SCIF peer device being unregistered,
92 * this results in a scif_peer_release_dev -> scif_cleanup_scifdev->
93 * scif_invalidate_ep call sequence which sets the endpoint state to
94 * DISCONNECTED and results in scif_poll(..) returning EPOLLHUP.
97 #define COSM_SCIF_BACKLOG 16
98 #define COSM_HEARTBEAT_CHECK_DELTA_SEC 10
99 #define COSM_HEARTBEAT_TIMEOUT_SEC \
100 (COSM_HEARTBEAT_SEND_SEC + COSM_HEARTBEAT_CHECK_DELTA_SEC)
101 #define COSM_HEARTBEAT_TIMEOUT_MSEC (COSM_HEARTBEAT_TIMEOUT_SEC * MSEC_PER_SEC)
103 static struct task_struct
*server_thread
;
104 static scif_epd_t listen_epd
;
106 /* Publish MIC card's shutdown status to user space MIC daemon */
107 static void cosm_update_mic_status(struct cosm_device
*cdev
)
109 if (cdev
->shutdown_status_int
!= MIC_NOP
) {
110 cosm_set_shutdown_status(cdev
, cdev
->shutdown_status_int
);
111 cdev
->shutdown_status_int
= MIC_NOP
;
115 /* Store MIC card's shutdown status internally when it is received */
116 static void cosm_shutdown_status_int(struct cosm_device
*cdev
,
117 enum mic_status shutdown_status
)
119 switch (shutdown_status
) {
126 dev_err(&cdev
->dev
, "%s %d Unexpected shutdown_status %d\n",
127 __func__
, __LINE__
, shutdown_status
);
130 cdev
->shutdown_status_int
= shutdown_status
;
131 cdev
->heartbeat_watchdog_enable
= false;
133 if (cdev
->state
!= MIC_SHUTTING_DOWN
)
134 cosm_set_state(cdev
, MIC_SHUTTING_DOWN
);
137 /* Non-blocking recv. Read and process all available messages */
138 static void cosm_scif_recv(struct cosm_device
*cdev
)
144 rc
= scif_recv(cdev
->epd
, &msg
, sizeof(msg
), 0);
148 dev_dbg(&cdev
->dev
, "%s: %d rc %d\n",
149 __func__
, __LINE__
, rc
);
152 dev_dbg(&cdev
->dev
, "%s: %d rc %d id 0x%llx\n",
153 __func__
, __LINE__
, rc
, msg
.id
);
156 case COSM_MSG_SHUTDOWN_STATUS
:
157 cosm_shutdown_status_int(cdev
, msg
.shutdown_status
);
159 case COSM_MSG_HEARTBEAT
:
160 /* Nothing to do, heartbeat only unblocks scif_poll */
163 dev_err(&cdev
->dev
, "%s: %d unknown msg.id %lld\n",
164 __func__
, __LINE__
, msg
.id
);
170 /* Publish crashed status for this MIC card */
171 static void cosm_set_crashed(struct cosm_device
*cdev
)
173 dev_err(&cdev
->dev
, "node alive timeout\n");
174 cosm_shutdown_status_int(cdev
, MIC_CRASHED
);
175 cosm_update_mic_status(cdev
);
178 /* Send host time to the MIC card to sync system time between host and MIC */
179 static void cosm_send_time(struct cosm_device
*cdev
)
181 struct cosm_msg msg
= { .id
= COSM_MSG_SYNC_TIME
};
182 struct timespec64 ts
;
185 ktime_get_real_ts64(&ts
);
186 msg
.timespec
.tv_sec
= ts
.tv_sec
;
187 msg
.timespec
.tv_nsec
= ts
.tv_nsec
;
189 rc
= scif_send(cdev
->epd
, &msg
, sizeof(msg
), SCIF_SEND_BLOCK
);
191 dev_err(&cdev
->dev
, "%s %d scif_send failed rc %d\n",
192 __func__
, __LINE__
, rc
);
196 * Close this cosm_device's endpoint after its peer endpoint on the card has
197 * been closed. In all cases except MIC card crash EPOLLHUP on the host is
198 * triggered by the client's endpoint being closed.
200 static void cosm_scif_close(struct cosm_device
*cdev
)
203 * Because SHUTDOWN_STATUS message is sent by the MIC cards in the
204 * reboot notifier when shutdown is still not complete, we notify mpssd
205 * to reset the card when SCIF endpoint is closed.
207 cosm_update_mic_status(cdev
);
208 scif_close(cdev
->epd
);
210 dev_dbg(&cdev
->dev
, "%s %d\n", __func__
, __LINE__
);
214 * Set card state to ONLINE when a new SCIF connection from a MIC card is
215 * received. Normally the state is BOOTING when the connection comes in, but can
216 * be ONLINE if cosm_client driver on the card was unloaded and then reloaded.
218 static int cosm_set_online(struct cosm_device
*cdev
)
222 if (MIC_BOOTING
== cdev
->state
|| MIC_ONLINE
== cdev
->state
) {
223 cdev
->heartbeat_watchdog_enable
= cdev
->sysfs_heartbeat_enable
;
224 cdev
->epd
= cdev
->newepd
;
225 if (cdev
->state
== MIC_BOOTING
)
226 cosm_set_state(cdev
, MIC_ONLINE
);
227 cosm_send_time(cdev
);
228 dev_dbg(&cdev
->dev
, "%s %d\n", __func__
, __LINE__
);
230 dev_warn(&cdev
->dev
, "%s %d not going online in state: %s\n",
231 __func__
, __LINE__
, cosm_state_string
[cdev
->state
]);
234 /* Drop reference acquired by bus_find_device in the server thread */
235 put_device(&cdev
->dev
);
240 * Work function for handling work for a SCIF connection from a particular MIC
241 * card. It first sets the card state to ONLINE and then calls scif_poll to
242 * block on activity such as incoming messages on the SCIF endpoint. When the
243 * endpoint is closed, the work function exits, completing its life cycle, from
244 * MIC card boot to card shutdown/reset/crash.
246 void cosm_scif_work(struct work_struct
*work
)
248 struct cosm_device
*cdev
= container_of(work
, struct cosm_device
,
250 struct scif_pollepd pollepd
;
253 mutex_lock(&cdev
->cosm_mutex
);
254 if (cosm_set_online(cdev
))
258 pollepd
.epd
= cdev
->epd
;
259 pollepd
.events
= EPOLLIN
;
261 /* Drop the mutex before blocking in scif_poll(..) */
262 mutex_unlock(&cdev
->cosm_mutex
);
263 /* poll(..) with timeout on our endpoint */
264 rc
= scif_poll(&pollepd
, 1, COSM_HEARTBEAT_TIMEOUT_MSEC
);
265 mutex_lock(&cdev
->cosm_mutex
);
267 dev_err(&cdev
->dev
, "%s %d scif_poll rc %d\n",
268 __func__
, __LINE__
, rc
);
272 /* There is a message from the card */
273 if (pollepd
.revents
& EPOLLIN
)
274 cosm_scif_recv(cdev
);
276 /* The peer endpoint is closed or this endpoint disconnected */
277 if (pollepd
.revents
& EPOLLHUP
) {
278 cosm_scif_close(cdev
);
282 /* Did we timeout from poll? */
283 if (!rc
&& cdev
->heartbeat_watchdog_enable
)
284 cosm_set_crashed(cdev
);
287 dev_dbg(&cdev
->dev
, "%s %d exiting\n", __func__
, __LINE__
);
288 mutex_unlock(&cdev
->cosm_mutex
);
292 * COSM SCIF server thread function. Accepts incoming SCIF connections from MIC
293 * cards, finds the correct cosm_device to associate that connection with and
294 * schedules individual work items for each MIC card.
296 static int cosm_scif_server(void *unused
)
298 struct cosm_device
*cdev
;
300 struct scif_port_id port_id
;
303 allow_signal(SIGKILL
);
305 while (!kthread_should_stop()) {
306 rc
= scif_accept(listen_epd
, &port_id
, &newepd
,
309 if (-ERESTARTSYS
!= rc
)
310 pr_err("%s %d rc %d\n", __func__
, __LINE__
, rc
);
315 * Associate the incoming connection with a particular
316 * cosm_device, COSM device ID == SCIF node ID - 1
318 cdev
= cosm_find_cdev_by_id(port_id
.node
- 1);
321 cdev
->newepd
= newepd
;
322 schedule_work(&cdev
->scif_work
);
325 pr_debug("%s %d Server thread stopped\n", __func__
, __LINE__
);
329 static int cosm_scif_listen(void)
333 listen_epd
= scif_open();
335 pr_err("%s %d scif_open failed\n", __func__
, __LINE__
);
339 rc
= scif_bind(listen_epd
, SCIF_COSM_LISTEN_PORT
);
341 pr_err("%s %d scif_bind failed rc %d\n",
342 __func__
, __LINE__
, rc
);
346 rc
= scif_listen(listen_epd
, COSM_SCIF_BACKLOG
);
348 pr_err("%s %d scif_listen rc %d\n", __func__
, __LINE__
, rc
);
351 pr_debug("%s %d listen_epd set up\n", __func__
, __LINE__
);
354 scif_close(listen_epd
);
359 static void cosm_scif_listen_exit(void)
361 pr_debug("%s %d closing listen_epd\n", __func__
, __LINE__
);
363 scif_close(listen_epd
);
369 * Create a listening SCIF endpoint and a server kthread which accepts incoming
370 * SCIF connections from MIC cards
372 int cosm_scif_init(void)
374 int rc
= cosm_scif_listen();
377 pr_err("%s %d cosm_scif_listen rc %d\n",
378 __func__
, __LINE__
, rc
);
382 server_thread
= kthread_run(cosm_scif_server
, NULL
, "cosm_server");
383 if (IS_ERR(server_thread
)) {
384 rc
= PTR_ERR(server_thread
);
385 pr_err("%s %d kthread_run rc %d\n", __func__
, __LINE__
, rc
);
390 cosm_scif_listen_exit();
395 /* Stop the running server thread and close the listening SCIF endpoint */
396 void cosm_scif_exit(void)
400 if (!IS_ERR_OR_NULL(server_thread
)) {
401 rc
= send_sig(SIGKILL
, server_thread
, 0);
403 pr_err("%s %d send_sig rc %d\n",
404 __func__
, __LINE__
, rc
);
407 kthread_stop(server_thread
);
410 cosm_scif_listen_exit();