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