1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/kthread.h>
22#include "cosm_main.h"
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95#define COSM_SCIF_BACKLOG 16
96#define COSM_HEARTBEAT_CHECK_DELTA_SEC 10
97#define COSM_HEARTBEAT_TIMEOUT_SEC \
98 (COSM_HEARTBEAT_SEND_SEC + COSM_HEARTBEAT_CHECK_DELTA_SEC)
99#define COSM_HEARTBEAT_TIMEOUT_MSEC (COSM_HEARTBEAT_TIMEOUT_SEC * MSEC_PER_SEC)
100
101static struct task_struct *server_thread;
102static scif_epd_t listen_epd;
103
104
105static void cosm_update_mic_status(struct cosm_device *cdev)
106{
107 if (cdev->shutdown_status_int != MIC_NOP) {
108 cosm_set_shutdown_status(cdev, cdev->shutdown_status_int);
109 cdev->shutdown_status_int = MIC_NOP;
110 }
111}
112
113
114static void cosm_shutdown_status_int(struct cosm_device *cdev,
115 enum mic_status shutdown_status)
116{
117 switch (shutdown_status) {
118 case MIC_HALTED:
119 case MIC_POWER_OFF:
120 case MIC_RESTART:
121 case MIC_CRASHED:
122 break;
123 default:
124 dev_err(&cdev->dev, "%s %d Unexpected shutdown_status %d\n",
125 __func__, __LINE__, shutdown_status);
126 return;
127 };
128 cdev->shutdown_status_int = shutdown_status;
129 cdev->heartbeat_watchdog_enable = false;
130
131 if (cdev->state != MIC_SHUTTING_DOWN)
132 cosm_set_state(cdev, MIC_SHUTTING_DOWN);
133}
134
135
136static void cosm_scif_recv(struct cosm_device *cdev)
137{
138 struct cosm_msg msg;
139 int rc;
140
141 while (1) {
142 rc = scif_recv(cdev->epd, &msg, sizeof(msg), 0);
143 if (!rc) {
144 break;
145 } else if (rc < 0) {
146 dev_dbg(&cdev->dev, "%s: %d rc %d\n",
147 __func__, __LINE__, rc);
148 break;
149 }
150 dev_dbg(&cdev->dev, "%s: %d rc %d id 0x%llx\n",
151 __func__, __LINE__, rc, msg.id);
152
153 switch (msg.id) {
154 case COSM_MSG_SHUTDOWN_STATUS:
155 cosm_shutdown_status_int(cdev, msg.shutdown_status);
156 break;
157 case COSM_MSG_HEARTBEAT:
158
159 break;
160 default:
161 dev_err(&cdev->dev, "%s: %d unknown msg.id %lld\n",
162 __func__, __LINE__, msg.id);
163 break;
164 }
165 }
166}
167
168
169static void cosm_set_crashed(struct cosm_device *cdev)
170{
171 dev_err(&cdev->dev, "node alive timeout\n");
172 cosm_shutdown_status_int(cdev, MIC_CRASHED);
173 cosm_update_mic_status(cdev);
174}
175
176
177static void cosm_send_time(struct cosm_device *cdev)
178{
179 struct cosm_msg msg = { .id = COSM_MSG_SYNC_TIME };
180 int rc;
181
182 getnstimeofday64(&msg.timespec);
183 rc = scif_send(cdev->epd, &msg, sizeof(msg), SCIF_SEND_BLOCK);
184 if (rc < 0)
185 dev_err(&cdev->dev, "%s %d scif_send failed rc %d\n",
186 __func__, __LINE__, rc);
187}
188
189
190
191
192
193
194static void cosm_scif_close(struct cosm_device *cdev)
195{
196
197
198
199
200
201 cosm_update_mic_status(cdev);
202 scif_close(cdev->epd);
203 cdev->epd = NULL;
204 dev_dbg(&cdev->dev, "%s %d\n", __func__, __LINE__);
205}
206
207
208
209
210
211
212static int cosm_set_online(struct cosm_device *cdev)
213{
214 int rc = 0;
215
216 if (MIC_BOOTING == cdev->state || MIC_ONLINE == cdev->state) {
217 cdev->heartbeat_watchdog_enable = cdev->sysfs_heartbeat_enable;
218 cdev->epd = cdev->newepd;
219 if (cdev->state == MIC_BOOTING)
220 cosm_set_state(cdev, MIC_ONLINE);
221 cosm_send_time(cdev);
222 dev_dbg(&cdev->dev, "%s %d\n", __func__, __LINE__);
223 } else {
224 dev_warn(&cdev->dev, "%s %d not going online in state: %s\n",
225 __func__, __LINE__, cosm_state_string[cdev->state]);
226 rc = -EINVAL;
227 }
228
229 put_device(&cdev->dev);
230 return rc;
231}
232
233
234
235
236
237
238
239
240void cosm_scif_work(struct work_struct *work)
241{
242 struct cosm_device *cdev = container_of(work, struct cosm_device,
243 scif_work);
244 struct scif_pollepd pollepd;
245 int rc;
246
247 mutex_lock(&cdev->cosm_mutex);
248 if (cosm_set_online(cdev))
249 goto exit;
250
251 while (1) {
252 pollepd.epd = cdev->epd;
253 pollepd.events = POLLIN;
254
255
256 mutex_unlock(&cdev->cosm_mutex);
257
258 rc = scif_poll(&pollepd, 1, COSM_HEARTBEAT_TIMEOUT_MSEC);
259 mutex_lock(&cdev->cosm_mutex);
260 if (rc < 0) {
261 dev_err(&cdev->dev, "%s %d scif_poll rc %d\n",
262 __func__, __LINE__, rc);
263 continue;
264 }
265
266
267 if (pollepd.revents & POLLIN)
268 cosm_scif_recv(cdev);
269
270
271 if (pollepd.revents & POLLHUP) {
272 cosm_scif_close(cdev);
273 break;
274 }
275
276
277 if (!rc && cdev->heartbeat_watchdog_enable)
278 cosm_set_crashed(cdev);
279 }
280exit:
281 dev_dbg(&cdev->dev, "%s %d exiting\n", __func__, __LINE__);
282 mutex_unlock(&cdev->cosm_mutex);
283}
284
285
286
287
288
289
290static int cosm_scif_server(void *unused)
291{
292 struct cosm_device *cdev;
293 scif_epd_t newepd;
294 struct scif_port_id port_id;
295 int rc;
296
297 allow_signal(SIGKILL);
298
299 while (!kthread_should_stop()) {
300 rc = scif_accept(listen_epd, &port_id, &newepd,
301 SCIF_ACCEPT_SYNC);
302 if (rc < 0) {
303 if (-ERESTARTSYS != rc)
304 pr_err("%s %d rc %d\n", __func__, __LINE__, rc);
305 continue;
306 }
307
308
309
310
311
312 cdev = cosm_find_cdev_by_id(port_id.node - 1);
313 if (!cdev)
314 continue;
315 cdev->newepd = newepd;
316 schedule_work(&cdev->scif_work);
317 }
318
319 pr_debug("%s %d Server thread stopped\n", __func__, __LINE__);
320 return 0;
321}
322
323static int cosm_scif_listen(void)
324{
325 int rc;
326
327 listen_epd = scif_open();
328 if (!listen_epd) {
329 pr_err("%s %d scif_open failed\n", __func__, __LINE__);
330 return -ENOMEM;
331 }
332
333 rc = scif_bind(listen_epd, SCIF_COSM_LISTEN_PORT);
334 if (rc < 0) {
335 pr_err("%s %d scif_bind failed rc %d\n",
336 __func__, __LINE__, rc);
337 goto err;
338 }
339
340 rc = scif_listen(listen_epd, COSM_SCIF_BACKLOG);
341 if (rc < 0) {
342 pr_err("%s %d scif_listen rc %d\n", __func__, __LINE__, rc);
343 goto err;
344 }
345 pr_debug("%s %d listen_epd set up\n", __func__, __LINE__);
346 return 0;
347err:
348 scif_close(listen_epd);
349 listen_epd = NULL;
350 return rc;
351}
352
353static void cosm_scif_listen_exit(void)
354{
355 pr_debug("%s %d closing listen_epd\n", __func__, __LINE__);
356 if (listen_epd) {
357 scif_close(listen_epd);
358 listen_epd = NULL;
359 }
360}
361
362
363
364
365
366int cosm_scif_init(void)
367{
368 int rc = cosm_scif_listen();
369
370 if (rc) {
371 pr_err("%s %d cosm_scif_listen rc %d\n",
372 __func__, __LINE__, rc);
373 goto err;
374 }
375
376 server_thread = kthread_run(cosm_scif_server, NULL, "cosm_server");
377 if (IS_ERR(server_thread)) {
378 rc = PTR_ERR(server_thread);
379 pr_err("%s %d kthread_run rc %d\n", __func__, __LINE__, rc);
380 goto listen_exit;
381 }
382 return 0;
383listen_exit:
384 cosm_scif_listen_exit();
385err:
386 return rc;
387}
388
389
390void cosm_scif_exit(void)
391{
392 int rc;
393
394 if (!IS_ERR_OR_NULL(server_thread)) {
395 rc = send_sig(SIGKILL, server_thread, 0);
396 if (rc) {
397 pr_err("%s %d send_sig rc %d\n",
398 __func__, __LINE__, rc);
399 return;
400 }
401 kthread_stop(server_thread);
402 }
403
404 cosm_scif_listen_exit();
405}
406