Ashutosh Dixit | 7cb46d9 | 2015-09-29 18:13:03 -0700 | [diff] [blame] | 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 COSM Client Driver |
| 19 | * |
| 20 | */ |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/reboot.h> |
| 24 | #include <linux/kthread.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 25 | #include <linux/sched/signal.h> |
| 26 | |
Ashutosh Dixit | 7cb46d9 | 2015-09-29 18:13:03 -0700 | [diff] [blame] | 27 | #include "../cosm/cosm_main.h" |
| 28 | |
| 29 | #define COSM_SCIF_MAX_RETRIES 10 |
| 30 | #define COSM_HEARTBEAT_SEND_MSEC (COSM_HEARTBEAT_SEND_SEC * MSEC_PER_SEC) |
| 31 | |
| 32 | static struct task_struct *client_thread; |
| 33 | static scif_epd_t client_epd; |
| 34 | static struct scif_peer_dev *client_spdev; |
| 35 | |
| 36 | /* |
| 37 | * Reboot notifier: receives shutdown status from the OS and communicates it |
| 38 | * back to the COSM process on the host |
| 39 | */ |
| 40 | static int cosm_reboot_event(struct notifier_block *this, unsigned long event, |
| 41 | void *ptr) |
| 42 | { |
| 43 | struct cosm_msg msg = { .id = COSM_MSG_SHUTDOWN_STATUS }; |
| 44 | int rc; |
| 45 | |
| 46 | event = (event == SYS_RESTART) ? SYSTEM_RESTART : event; |
| 47 | dev_info(&client_spdev->dev, "%s %d received event %ld\n", |
| 48 | __func__, __LINE__, event); |
| 49 | |
| 50 | msg.shutdown_status = event; |
| 51 | rc = scif_send(client_epd, &msg, sizeof(msg), SCIF_SEND_BLOCK); |
| 52 | if (rc < 0) |
| 53 | dev_err(&client_spdev->dev, "%s %d scif_send rc %d\n", |
| 54 | __func__, __LINE__, rc); |
| 55 | |
| 56 | return NOTIFY_DONE; |
| 57 | } |
| 58 | |
| 59 | static struct notifier_block cosm_reboot = { |
| 60 | .notifier_call = cosm_reboot_event, |
| 61 | }; |
| 62 | |
| 63 | /* Set system time from timespec value received from the host */ |
| 64 | static void cosm_set_time(struct cosm_msg *msg) |
| 65 | { |
Arnd Bergmann | 6051e79 | 2018-06-18 16:25:51 +0200 | [diff] [blame] | 66 | struct timespec64 ts = { |
| 67 | .tv_sec = msg->timespec.tv_sec, |
| 68 | .tv_nsec = msg->timespec.tv_nsec, |
| 69 | }; |
| 70 | int rc = do_settimeofday64(&ts); |
Ashutosh Dixit | 7cb46d9 | 2015-09-29 18:13:03 -0700 | [diff] [blame] | 71 | |
| 72 | if (rc) |
| 73 | dev_err(&client_spdev->dev, "%s: %d settimeofday rc %d\n", |
| 74 | __func__, __LINE__, rc); |
| 75 | } |
| 76 | |
| 77 | /* COSM client receive message processing */ |
| 78 | static void cosm_client_recv(void) |
| 79 | { |
| 80 | struct cosm_msg msg; |
| 81 | int rc; |
| 82 | |
| 83 | while (1) { |
| 84 | rc = scif_recv(client_epd, &msg, sizeof(msg), 0); |
| 85 | if (!rc) { |
| 86 | return; |
| 87 | } else if (rc < 0) { |
| 88 | dev_err(&client_spdev->dev, "%s: %d rc %d\n", |
| 89 | __func__, __LINE__, rc); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | dev_dbg(&client_spdev->dev, "%s: %d rc %d id 0x%llx\n", |
| 94 | __func__, __LINE__, rc, msg.id); |
| 95 | |
| 96 | switch (msg.id) { |
| 97 | case COSM_MSG_SYNC_TIME: |
| 98 | cosm_set_time(&msg); |
| 99 | break; |
| 100 | case COSM_MSG_SHUTDOWN: |
| 101 | orderly_poweroff(true); |
| 102 | break; |
| 103 | default: |
| 104 | dev_err(&client_spdev->dev, "%s: %d unknown id %lld\n", |
| 105 | __func__, __LINE__, msg.id); |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /* Initiate connection to the COSM server on the host */ |
| 112 | static int cosm_scif_connect(void) |
| 113 | { |
| 114 | struct scif_port_id port_id; |
| 115 | int i, rc; |
| 116 | |
| 117 | client_epd = scif_open(); |
| 118 | if (!client_epd) { |
| 119 | dev_err(&client_spdev->dev, "%s %d scif_open failed\n", |
| 120 | __func__, __LINE__); |
| 121 | return -ENOMEM; |
| 122 | } |
| 123 | |
| 124 | port_id.node = 0; |
| 125 | port_id.port = SCIF_COSM_LISTEN_PORT; |
| 126 | |
| 127 | for (i = 0; i < COSM_SCIF_MAX_RETRIES; i++) { |
| 128 | rc = scif_connect(client_epd, &port_id); |
| 129 | if (rc < 0) |
| 130 | msleep(1000); |
| 131 | else |
| 132 | break; |
| 133 | } |
| 134 | |
| 135 | if (rc < 0) { |
| 136 | dev_err(&client_spdev->dev, "%s %d scif_connect rc %d\n", |
| 137 | __func__, __LINE__, rc); |
| 138 | scif_close(client_epd); |
| 139 | client_epd = NULL; |
| 140 | } |
| 141 | return rc < 0 ? rc : 0; |
| 142 | } |
| 143 | |
| 144 | /* Close host SCIF connection */ |
| 145 | static void cosm_scif_connect_exit(void) |
| 146 | { |
| 147 | if (client_epd) { |
| 148 | scif_close(client_epd); |
| 149 | client_epd = NULL; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * COSM SCIF client thread function: waits for messages from the host and sends |
| 155 | * a heartbeat to the host |
| 156 | */ |
| 157 | static int cosm_scif_client(void *unused) |
| 158 | { |
| 159 | struct cosm_msg msg = { .id = COSM_MSG_HEARTBEAT }; |
| 160 | struct scif_pollepd pollepd; |
| 161 | int rc; |
| 162 | |
| 163 | allow_signal(SIGKILL); |
| 164 | |
| 165 | while (!kthread_should_stop()) { |
| 166 | pollepd.epd = client_epd; |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 167 | pollepd.events = EPOLLIN; |
Ashutosh Dixit | 7cb46d9 | 2015-09-29 18:13:03 -0700 | [diff] [blame] | 168 | |
| 169 | rc = scif_poll(&pollepd, 1, COSM_HEARTBEAT_SEND_MSEC); |
| 170 | if (rc < 0) { |
| 171 | if (-EINTR != rc) |
| 172 | dev_err(&client_spdev->dev, |
| 173 | "%s %d scif_poll rc %d\n", |
| 174 | __func__, __LINE__, rc); |
| 175 | continue; |
| 176 | } |
| 177 | |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 178 | if (pollepd.revents & EPOLLIN) |
Ashutosh Dixit | 7cb46d9 | 2015-09-29 18:13:03 -0700 | [diff] [blame] | 179 | cosm_client_recv(); |
| 180 | |
| 181 | msg.id = COSM_MSG_HEARTBEAT; |
| 182 | rc = scif_send(client_epd, &msg, sizeof(msg), SCIF_SEND_BLOCK); |
| 183 | if (rc < 0) |
| 184 | dev_err(&client_spdev->dev, "%s %d scif_send rc %d\n", |
| 185 | __func__, __LINE__, rc); |
| 186 | } |
| 187 | |
| 188 | dev_dbg(&client_spdev->dev, "%s %d Client thread stopped\n", |
| 189 | __func__, __LINE__); |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static void cosm_scif_probe(struct scif_peer_dev *spdev) |
| 194 | { |
| 195 | int rc; |
| 196 | |
| 197 | dev_dbg(&spdev->dev, "%s %d: dnode %d\n", |
| 198 | __func__, __LINE__, spdev->dnode); |
| 199 | |
| 200 | /* We are only interested in the host with spdev->dnode == 0 */ |
| 201 | if (spdev->dnode) |
| 202 | return; |
| 203 | |
| 204 | client_spdev = spdev; |
| 205 | rc = cosm_scif_connect(); |
| 206 | if (rc) |
| 207 | goto exit; |
| 208 | |
| 209 | rc = register_reboot_notifier(&cosm_reboot); |
| 210 | if (rc) { |
| 211 | dev_err(&spdev->dev, |
| 212 | "reboot notifier registration failed rc %d\n", rc); |
| 213 | goto connect_exit; |
| 214 | } |
| 215 | |
| 216 | client_thread = kthread_run(cosm_scif_client, NULL, "cosm_client"); |
| 217 | if (IS_ERR(client_thread)) { |
| 218 | rc = PTR_ERR(client_thread); |
| 219 | dev_err(&spdev->dev, "%s %d kthread_run rc %d\n", |
| 220 | __func__, __LINE__, rc); |
| 221 | goto unreg_reboot; |
| 222 | } |
| 223 | return; |
| 224 | unreg_reboot: |
| 225 | unregister_reboot_notifier(&cosm_reboot); |
| 226 | connect_exit: |
| 227 | cosm_scif_connect_exit(); |
| 228 | exit: |
| 229 | client_spdev = NULL; |
| 230 | } |
| 231 | |
| 232 | static void cosm_scif_remove(struct scif_peer_dev *spdev) |
| 233 | { |
| 234 | int rc; |
| 235 | |
| 236 | dev_dbg(&spdev->dev, "%s %d: dnode %d\n", |
| 237 | __func__, __LINE__, spdev->dnode); |
| 238 | |
| 239 | if (spdev->dnode) |
| 240 | return; |
| 241 | |
| 242 | if (!IS_ERR_OR_NULL(client_thread)) { |
| 243 | rc = send_sig(SIGKILL, client_thread, 0); |
| 244 | if (rc) { |
| 245 | pr_err("%s %d send_sig rc %d\n", |
| 246 | __func__, __LINE__, rc); |
| 247 | return; |
| 248 | } |
| 249 | kthread_stop(client_thread); |
| 250 | } |
| 251 | unregister_reboot_notifier(&cosm_reboot); |
| 252 | cosm_scif_connect_exit(); |
| 253 | client_spdev = NULL; |
| 254 | } |
| 255 | |
| 256 | static struct scif_client scif_client_cosm = { |
| 257 | .name = KBUILD_MODNAME, |
| 258 | .probe = cosm_scif_probe, |
| 259 | .remove = cosm_scif_remove, |
| 260 | }; |
| 261 | |
| 262 | static int __init cosm_client_init(void) |
| 263 | { |
| 264 | int rc = scif_client_register(&scif_client_cosm); |
| 265 | |
| 266 | if (rc) |
| 267 | pr_err("scif_client_register failed rc %d\n", rc); |
| 268 | return rc; |
| 269 | } |
| 270 | |
| 271 | static void __exit cosm_client_exit(void) |
| 272 | { |
| 273 | scif_client_unregister(&scif_client_cosm); |
| 274 | } |
| 275 | |
| 276 | module_init(cosm_client_init); |
| 277 | module_exit(cosm_client_exit); |
| 278 | |
| 279 | MODULE_AUTHOR("Intel Corporation"); |
| 280 | MODULE_DESCRIPTION("Intel(R) MIC card OS state management client driver"); |
| 281 | MODULE_LICENSE("GPL v2"); |