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 COSM Client Driver
21 #include <linux/module.h>
22 #include <linux/delay.h>
23 #include <linux/reboot.h>
24 #include <linux/kthread.h>
25 #include "../cosm/cosm_main.h"
27 #define COSM_SCIF_MAX_RETRIES 10
28 #define COSM_HEARTBEAT_SEND_MSEC (COSM_HEARTBEAT_SEND_SEC * MSEC_PER_SEC)
30 static struct task_struct *client_thread;
31 static scif_epd_t client_epd;
32 static struct scif_peer_dev *client_spdev;
35 * Reboot notifier: receives shutdown status from the OS and communicates it
36 * back to the COSM process on the host
38 static int cosm_reboot_event(struct notifier_block *this, unsigned long event,
41 struct cosm_msg msg = { .id = COSM_MSG_SHUTDOWN_STATUS };
44 event = (event == SYS_RESTART) ? SYSTEM_RESTART : event;
45 dev_info(&client_spdev->dev, "%s %d received event %ld\n",
46 __func__, __LINE__, event);
48 msg.shutdown_status = event;
49 rc = scif_send(client_epd, &msg, sizeof(msg), SCIF_SEND_BLOCK);
51 dev_err(&client_spdev->dev, "%s %d scif_send rc %d\n",
52 __func__, __LINE__, rc);
57 static struct notifier_block cosm_reboot = {
58 .notifier_call = cosm_reboot_event,
61 /* Set system time from timespec value received from the host */
62 static void cosm_set_time(struct cosm_msg *msg)
64 int rc = do_settimeofday64(&msg->timespec);
67 dev_err(&client_spdev->dev, "%s: %d settimeofday rc %d\n",
68 __func__, __LINE__, rc);
71 /* COSM client receive message processing */
72 static void cosm_client_recv(void)
78 rc = scif_recv(client_epd, &msg, sizeof(msg), 0);
82 dev_err(&client_spdev->dev, "%s: %d rc %d\n",
83 __func__, __LINE__, rc);
87 dev_dbg(&client_spdev->dev, "%s: %d rc %d id 0x%llx\n",
88 __func__, __LINE__, rc, msg.id);
91 case COSM_MSG_SYNC_TIME:
94 case COSM_MSG_SHUTDOWN:
95 orderly_poweroff(true);
98 dev_err(&client_spdev->dev, "%s: %d unknown id %lld\n",
99 __func__, __LINE__, msg.id);
105 /* Initiate connection to the COSM server on the host */
106 static int cosm_scif_connect(void)
108 struct scif_port_id port_id;
111 client_epd = scif_open();
113 dev_err(&client_spdev->dev, "%s %d scif_open failed\n",
119 port_id.port = SCIF_COSM_LISTEN_PORT;
121 for (i = 0; i < COSM_SCIF_MAX_RETRIES; i++) {
122 rc = scif_connect(client_epd, &port_id);
130 dev_err(&client_spdev->dev, "%s %d scif_connect rc %d\n",
131 __func__, __LINE__, rc);
132 scif_close(client_epd);
135 return rc < 0 ? rc : 0;
138 /* Close host SCIF connection */
139 static void cosm_scif_connect_exit(void)
142 scif_close(client_epd);
148 * COSM SCIF client thread function: waits for messages from the host and sends
149 * a heartbeat to the host
151 static int cosm_scif_client(void *unused)
153 struct cosm_msg msg = { .id = COSM_MSG_HEARTBEAT };
154 struct scif_pollepd pollepd;
157 allow_signal(SIGKILL);
159 while (!kthread_should_stop()) {
160 pollepd.epd = client_epd;
161 pollepd.events = POLLIN;
163 rc = scif_poll(&pollepd, 1, COSM_HEARTBEAT_SEND_MSEC);
166 dev_err(&client_spdev->dev,
167 "%s %d scif_poll rc %d\n",
168 __func__, __LINE__, rc);
172 if (pollepd.revents & POLLIN)
175 msg.id = COSM_MSG_HEARTBEAT;
176 rc = scif_send(client_epd, &msg, sizeof(msg), SCIF_SEND_BLOCK);
178 dev_err(&client_spdev->dev, "%s %d scif_send rc %d\n",
179 __func__, __LINE__, rc);
182 dev_dbg(&client_spdev->dev, "%s %d Client thread stopped\n",
187 static void cosm_scif_probe(struct scif_peer_dev *spdev)
191 dev_dbg(&spdev->dev, "%s %d: dnode %d\n",
192 __func__, __LINE__, spdev->dnode);
194 /* We are only interested in the host with spdev->dnode == 0 */
198 client_spdev = spdev;
199 rc = cosm_scif_connect();
203 rc = register_reboot_notifier(&cosm_reboot);
206 "reboot notifier registration failed rc %d\n", rc);
210 client_thread = kthread_run(cosm_scif_client, NULL, "cosm_client");
211 if (IS_ERR(client_thread)) {
212 rc = PTR_ERR(client_thread);
213 dev_err(&spdev->dev, "%s %d kthread_run rc %d\n",
214 __func__, __LINE__, rc);
219 unregister_reboot_notifier(&cosm_reboot);
221 cosm_scif_connect_exit();
226 static void cosm_scif_remove(struct scif_peer_dev *spdev)
230 dev_dbg(&spdev->dev, "%s %d: dnode %d\n",
231 __func__, __LINE__, spdev->dnode);
236 if (!IS_ERR_OR_NULL(client_thread)) {
237 rc = send_sig(SIGKILL, client_thread, 0);
239 pr_err("%s %d send_sig rc %d\n",
240 __func__, __LINE__, rc);
243 kthread_stop(client_thread);
245 unregister_reboot_notifier(&cosm_reboot);
246 cosm_scif_connect_exit();
250 static struct scif_client scif_client_cosm = {
251 .name = KBUILD_MODNAME,
252 .probe = cosm_scif_probe,
253 .remove = cosm_scif_remove,
256 static int __init cosm_client_init(void)
258 int rc = scif_client_register(&scif_client_cosm);
261 pr_err("scif_client_register failed rc %d\n", rc);
265 static void __exit cosm_client_exit(void)
267 scif_client_unregister(&scif_client_cosm);
270 module_init(cosm_client_init);
271 module_exit(cosm_client_exit);
273 MODULE_AUTHOR("Intel Corporation");
274 MODULE_DESCRIPTION("Intel(R) MIC card OS state management client driver");
275 MODULE_LICENSE("GPL v2");