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 
24 #include "cosm_main.h"
25 
26 /*
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
31  * card has crashed.
32  *
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
40  * unloaded.
41  *
42  * From the point of view of COSM interactions with SCIF during card
43  * shutdown, reset and crash are as follows:
44  *
45  * Card shutdown
46  * -------------
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
62  *    complete.
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.
67  *
68  * Card reset
69  * ----------
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:
72 
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
75  *    called on the host.
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.
85  *
86  * Card crash
87  * ----------
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.
95  */
96 
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)
102 
103 static struct task_struct *server_thread;
104 static scif_epd_t listen_epd;
105 
106 /* Publish MIC card's shutdown status to user space MIC daemon */
cosm_update_mic_status(struct cosm_device * cdev)107 static void cosm_update_mic_status(struct cosm_device *cdev)
108 {
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;
112 	}
113 }
114 
115 /* Store MIC card's shutdown status internally when it is received */
cosm_shutdown_status_int(struct cosm_device * cdev,enum mic_status shutdown_status)116 static void cosm_shutdown_status_int(struct cosm_device *cdev,
117 				     enum mic_status shutdown_status)
118 {
119 	switch (shutdown_status) {
120 	case MIC_HALTED:
121 	case MIC_POWER_OFF:
122 	case MIC_RESTART:
123 	case MIC_CRASHED:
124 		break;
125 	default:
126 		dev_err(&cdev->dev, "%s %d Unexpected shutdown_status %d\n",
127 			__func__, __LINE__, shutdown_status);
128 		return;
129 	};
130 	cdev->shutdown_status_int = shutdown_status;
131 	cdev->heartbeat_watchdog_enable = false;
132 
133 	if (cdev->state != MIC_SHUTTING_DOWN)
134 		cosm_set_state(cdev, MIC_SHUTTING_DOWN);
135 }
136 
137 /* Non-blocking recv. Read and process all available messages */
cosm_scif_recv(struct cosm_device * cdev)138 static void cosm_scif_recv(struct cosm_device *cdev)
139 {
140 	struct cosm_msg msg;
141 	int rc;
142 
143 	while (1) {
144 		rc = scif_recv(cdev->epd, &msg, sizeof(msg), 0);
145 		if (!rc) {
146 			break;
147 		} else if (rc < 0) {
148 			dev_dbg(&cdev->dev, "%s: %d rc %d\n",
149 				__func__, __LINE__, rc);
150 			break;
151 		}
152 		dev_dbg(&cdev->dev, "%s: %d rc %d id 0x%llx\n",
153 			__func__, __LINE__, rc, msg.id);
154 
155 		switch (msg.id) {
156 		case COSM_MSG_SHUTDOWN_STATUS:
157 			cosm_shutdown_status_int(cdev, msg.shutdown_status);
158 			break;
159 		case COSM_MSG_HEARTBEAT:
160 			/* Nothing to do, heartbeat only unblocks scif_poll */
161 			break;
162 		default:
163 			dev_err(&cdev->dev, "%s: %d unknown msg.id %lld\n",
164 				__func__, __LINE__, msg.id);
165 			break;
166 		}
167 	}
168 }
169 
170 /* Publish crashed status for this MIC card */
cosm_set_crashed(struct cosm_device * cdev)171 static void cosm_set_crashed(struct cosm_device *cdev)
172 {
173 	dev_err(&cdev->dev, "node alive timeout\n");
174 	cosm_shutdown_status_int(cdev, MIC_CRASHED);
175 	cosm_update_mic_status(cdev);
176 }
177 
178 /* Send host time to the MIC card to sync system time between host and MIC */
cosm_send_time(struct cosm_device * cdev)179 static void cosm_send_time(struct cosm_device *cdev)
180 {
181 	struct cosm_msg msg = { .id = COSM_MSG_SYNC_TIME };
182 	struct timespec64 ts;
183 	int rc;
184 
185 	ktime_get_real_ts64(&ts);
186 	msg.timespec.tv_sec = ts.tv_sec;
187 	msg.timespec.tv_nsec = ts.tv_nsec;
188 
189 	rc = scif_send(cdev->epd, &msg, sizeof(msg), SCIF_SEND_BLOCK);
190 	if (rc < 0)
191 		dev_err(&cdev->dev, "%s %d scif_send failed rc %d\n",
192 			__func__, __LINE__, rc);
193 }
194 
195 /*
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.
199  */
cosm_scif_close(struct cosm_device * cdev)200 static void cosm_scif_close(struct cosm_device *cdev)
201 {
202 	/*
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.
206 	 */
207 	cosm_update_mic_status(cdev);
208 	scif_close(cdev->epd);
209 	cdev->epd = NULL;
210 	dev_dbg(&cdev->dev, "%s %d\n", __func__, __LINE__);
211 }
212 
213 /*
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.
217  */
cosm_set_online(struct cosm_device * cdev)218 static int cosm_set_online(struct cosm_device *cdev)
219 {
220 	int rc = 0;
221 
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__);
229 	} else {
230 		dev_warn(&cdev->dev, "%s %d not going online in state: %s\n",
231 			 __func__, __LINE__, cosm_state_string[cdev->state]);
232 		rc = -EINVAL;
233 	}
234 	/* Drop reference acquired by bus_find_device in the server thread */
235 	put_device(&cdev->dev);
236 	return rc;
237 }
238 
239 /*
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.
245  */
cosm_scif_work(struct work_struct * work)246 void cosm_scif_work(struct work_struct *work)
247 {
248 	struct cosm_device *cdev = container_of(work, struct cosm_device,
249 						scif_work);
250 	struct scif_pollepd pollepd;
251 	int rc;
252 
253 	mutex_lock(&cdev->cosm_mutex);
254 	if (cosm_set_online(cdev))
255 		goto exit;
256 
257 	while (1) {
258 		pollepd.epd = cdev->epd;
259 		pollepd.events = EPOLLIN;
260 
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);
266 		if (rc < 0) {
267 			dev_err(&cdev->dev, "%s %d scif_poll rc %d\n",
268 				__func__, __LINE__, rc);
269 			continue;
270 		}
271 
272 		/* There is a message from the card */
273 		if (pollepd.revents & EPOLLIN)
274 			cosm_scif_recv(cdev);
275 
276 		/* The peer endpoint is closed or this endpoint disconnected */
277 		if (pollepd.revents & EPOLLHUP) {
278 			cosm_scif_close(cdev);
279 			break;
280 		}
281 
282 		/* Did we timeout from poll? */
283 		if (!rc && cdev->heartbeat_watchdog_enable)
284 			cosm_set_crashed(cdev);
285 	}
286 exit:
287 	dev_dbg(&cdev->dev, "%s %d exiting\n", __func__, __LINE__);
288 	mutex_unlock(&cdev->cosm_mutex);
289 }
290 
291 /*
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.
295  */
cosm_scif_server(void * unused)296 static int cosm_scif_server(void *unused)
297 {
298 	struct cosm_device *cdev;
299 	scif_epd_t newepd;
300 	struct scif_port_id port_id;
301 	int rc;
302 
303 	allow_signal(SIGKILL);
304 
305 	while (!kthread_should_stop()) {
306 		rc = scif_accept(listen_epd, &port_id, &newepd,
307 				 SCIF_ACCEPT_SYNC);
308 		if (rc < 0) {
309 			if (-ERESTARTSYS != rc)
310 				pr_err("%s %d rc %d\n", __func__, __LINE__, rc);
311 			continue;
312 		}
313 
314 		/*
315 		 * Associate the incoming connection with a particular
316 		 * cosm_device, COSM device ID == SCIF node ID - 1
317 		 */
318 		cdev = cosm_find_cdev_by_id(port_id.node - 1);
319 		if (!cdev)
320 			continue;
321 		cdev->newepd = newepd;
322 		schedule_work(&cdev->scif_work);
323 	}
324 
325 	pr_debug("%s %d Server thread stopped\n", __func__, __LINE__);
326 	return 0;
327 }
328 
cosm_scif_listen(void)329 static int cosm_scif_listen(void)
330 {
331 	int rc;
332 
333 	listen_epd = scif_open();
334 	if (!listen_epd) {
335 		pr_err("%s %d scif_open failed\n", __func__, __LINE__);
336 		return -ENOMEM;
337 	}
338 
339 	rc = scif_bind(listen_epd, SCIF_COSM_LISTEN_PORT);
340 	if (rc < 0) {
341 		pr_err("%s %d scif_bind failed rc %d\n",
342 		       __func__, __LINE__, rc);
343 		goto err;
344 	}
345 
346 	rc = scif_listen(listen_epd, COSM_SCIF_BACKLOG);
347 	if (rc < 0) {
348 		pr_err("%s %d scif_listen rc %d\n", __func__, __LINE__, rc);
349 		goto err;
350 	}
351 	pr_debug("%s %d listen_epd set up\n", __func__, __LINE__);
352 	return 0;
353 err:
354 	scif_close(listen_epd);
355 	listen_epd = NULL;
356 	return rc;
357 }
358 
cosm_scif_listen_exit(void)359 static void cosm_scif_listen_exit(void)
360 {
361 	pr_debug("%s %d closing listen_epd\n", __func__, __LINE__);
362 	if (listen_epd) {
363 		scif_close(listen_epd);
364 		listen_epd = NULL;
365 	}
366 }
367 
368 /*
369  * Create a listening SCIF endpoint and a server kthread which accepts incoming
370  * SCIF connections from MIC cards
371  */
cosm_scif_init(void)372 int cosm_scif_init(void)
373 {
374 	int rc = cosm_scif_listen();
375 
376 	if (rc) {
377 		pr_err("%s %d cosm_scif_listen rc %d\n",
378 		       __func__, __LINE__, rc);
379 		goto err;
380 	}
381 
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);
386 		goto listen_exit;
387 	}
388 	return 0;
389 listen_exit:
390 	cosm_scif_listen_exit();
391 err:
392 	return rc;
393 }
394 
395 /* Stop the running server thread and close the listening SCIF endpoint */
cosm_scif_exit(void)396 void cosm_scif_exit(void)
397 {
398 	int rc;
399 
400 	if (!IS_ERR_OR_NULL(server_thread)) {
401 		rc = send_sig(SIGKILL, server_thread, 0);
402 		if (rc) {
403 			pr_err("%s %d send_sig rc %d\n",
404 			       __func__, __LINE__, rc);
405 			return;
406 		}
407 		kthread_stop(server_thread);
408 	}
409 
410 	cosm_scif_listen_exit();
411 }
412