This source file includes following definitions.
- wdrtas_set_interval
- wdrtas_get_interval
- wdrtas_timer_start
- wdrtas_timer_stop
- wdrtas_timer_keepalive
- wdrtas_get_temperature
- wdrtas_get_status
- wdrtas_get_boot_status
- wdrtas_write
- wdrtas_ioctl
- wdrtas_open
- wdrtas_close
- wdrtas_temp_read
- wdrtas_temp_open
- wdrtas_temp_close
- wdrtas_reboot
- wdrtas_get_tokens
- wdrtas_unregister_devs
- wdrtas_register_devs
- wdrtas_init
- wdrtas_exit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
18 #include <linux/fs.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/miscdevice.h>
22 #include <linux/module.h>
23 #include <linux/notifier.h>
24 #include <linux/reboot.h>
25 #include <linux/types.h>
26 #include <linux/watchdog.h>
27 #include <linux/uaccess.h>
28
29 #include <asm/rtas.h>
30
31 #define WDRTAS_MAGIC_CHAR 42
32 #define WDRTAS_SUPPORTED_MASK (WDIOF_SETTIMEOUT | \
33 WDIOF_MAGICCLOSE)
34
35 MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>");
36 MODULE_DESCRIPTION("RTAS watchdog driver");
37 MODULE_LICENSE("GPL");
38
39 static bool wdrtas_nowayout = WATCHDOG_NOWAYOUT;
40 static atomic_t wdrtas_miscdev_open = ATOMIC_INIT(0);
41 static char wdrtas_expect_close;
42
43 static int wdrtas_interval;
44
45 #define WDRTAS_THERMAL_SENSOR 3
46 static int wdrtas_token_get_sensor_state;
47 #define WDRTAS_SURVEILLANCE_IND 9000
48 static int wdrtas_token_set_indicator;
49 #define WDRTAS_SP_SPI 28
50 static int wdrtas_token_get_sp;
51 static int wdrtas_token_event_scan;
52
53 #define WDRTAS_DEFAULT_INTERVAL 300
54
55 #define WDRTAS_LOGBUFFER_LEN 128
56 static char wdrtas_logbuffer[WDRTAS_LOGBUFFER_LEN];
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 static int wdrtas_set_interval(int interval)
73 {
74 long result;
75 static int print_msg = 10;
76
77
78 interval = (interval + 59) / 60;
79
80 result = rtas_call(wdrtas_token_set_indicator, 3, 1, NULL,
81 WDRTAS_SURVEILLANCE_IND, 0, interval);
82 if (result < 0 && print_msg) {
83 pr_err("setting the watchdog to %i timeout failed: %li\n",
84 interval, result);
85 print_msg--;
86 }
87
88 return result;
89 }
90
91 #define WDRTAS_SP_SPI_LEN 4
92
93
94
95
96
97
98
99
100
101
102
103 static int wdrtas_get_interval(int fallback_value)
104 {
105 long result;
106 char value[WDRTAS_SP_SPI_LEN];
107
108 spin_lock(&rtas_data_buf_lock);
109 memset(rtas_data_buf, 0, WDRTAS_SP_SPI_LEN);
110 result = rtas_call(wdrtas_token_get_sp, 3, 1, NULL,
111 WDRTAS_SP_SPI, __pa(rtas_data_buf),
112 WDRTAS_SP_SPI_LEN);
113
114 memcpy(value, rtas_data_buf, WDRTAS_SP_SPI_LEN);
115 spin_unlock(&rtas_data_buf_lock);
116
117 if (value[0] != 0 || value[1] != 2 || value[3] != 0 || result < 0) {
118 pr_warn("could not get sp_spi watchdog timeout (%li). Continuing\n",
119 result);
120 return fallback_value;
121 }
122
123
124 return ((int)value[2]) * 60;
125 }
126
127
128
129
130
131
132
133 static void wdrtas_timer_start(void)
134 {
135 wdrtas_set_interval(wdrtas_interval);
136 }
137
138
139
140
141
142
143
144 static void wdrtas_timer_stop(void)
145 {
146 wdrtas_set_interval(0);
147 }
148
149
150
151
152
153
154
155
156 static void wdrtas_timer_keepalive(void)
157 {
158 long result;
159
160 do {
161 result = rtas_call(wdrtas_token_event_scan, 4, 1, NULL,
162 RTAS_EVENT_SCAN_ALL_EVENTS, 0,
163 (void *)__pa(wdrtas_logbuffer),
164 WDRTAS_LOGBUFFER_LEN);
165 if (result < 0)
166 pr_err("event-scan failed: %li\n", result);
167 if (result == 0)
168 print_hex_dump(KERN_INFO, "dumping event, data: ",
169 DUMP_PREFIX_OFFSET, 16, 1,
170 wdrtas_logbuffer, WDRTAS_LOGBUFFER_LEN, false);
171 } while (result == 0);
172 }
173
174
175
176
177
178
179
180
181
182 static int wdrtas_get_temperature(void)
183 {
184 int result;
185 int temperature = 0;
186
187 result = rtas_get_sensor(WDRTAS_THERMAL_SENSOR, 0, &temperature);
188
189 if (result < 0)
190 pr_warn("reading the thermal sensor failed: %i\n", result);
191 else
192 temperature = ((temperature * 9) / 5) + 32;
193
194 return temperature;
195 }
196
197
198
199
200
201
202
203 static int wdrtas_get_status(void)
204 {
205 return 0;
206 }
207
208
209
210
211
212
213
214 static int wdrtas_get_boot_status(void)
215 {
216 return 0;
217 }
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234 static ssize_t wdrtas_write(struct file *file, const char __user *buf,
235 size_t len, loff_t *ppos)
236 {
237 int i;
238 char c;
239
240 if (!len)
241 goto out;
242
243 if (!wdrtas_nowayout) {
244 wdrtas_expect_close = 0;
245
246 for (i = 0; i < len; i++) {
247 if (get_user(c, buf + i))
248 return -EFAULT;
249
250 if (c == 'V')
251 wdrtas_expect_close = WDRTAS_MAGIC_CHAR;
252 }
253 }
254
255 wdrtas_timer_keepalive();
256
257 out:
258 return len;
259 }
260
261
262
263
264
265
266
267
268
269
270
271
272 static long wdrtas_ioctl(struct file *file, unsigned int cmd,
273 unsigned long arg)
274 {
275 int __user *argp = (void __user *)arg;
276 int i;
277 static const struct watchdog_info wdinfo = {
278 .options = WDRTAS_SUPPORTED_MASK,
279 .firmware_version = 0,
280 .identity = "wdrtas",
281 };
282
283 switch (cmd) {
284 case WDIOC_GETSUPPORT:
285 if (copy_to_user(argp, &wdinfo, sizeof(wdinfo)))
286 return -EFAULT;
287 return 0;
288
289 case WDIOC_GETSTATUS:
290 i = wdrtas_get_status();
291 return put_user(i, argp);
292
293 case WDIOC_GETBOOTSTATUS:
294 i = wdrtas_get_boot_status();
295 return put_user(i, argp);
296
297 case WDIOC_GETTEMP:
298 if (wdrtas_token_get_sensor_state == RTAS_UNKNOWN_SERVICE)
299 return -EOPNOTSUPP;
300
301 i = wdrtas_get_temperature();
302 return put_user(i, argp);
303
304 case WDIOC_SETOPTIONS:
305 if (get_user(i, argp))
306 return -EFAULT;
307 if (i & WDIOS_DISABLECARD)
308 wdrtas_timer_stop();
309 if (i & WDIOS_ENABLECARD) {
310 wdrtas_timer_keepalive();
311 wdrtas_timer_start();
312 }
313
314
315
316 return 0;
317
318 case WDIOC_KEEPALIVE:
319 wdrtas_timer_keepalive();
320 return 0;
321
322 case WDIOC_SETTIMEOUT:
323 if (get_user(i, argp))
324 return -EFAULT;
325
326 if (wdrtas_set_interval(i))
327 return -EINVAL;
328
329 wdrtas_timer_keepalive();
330
331 if (wdrtas_token_get_sp == RTAS_UNKNOWN_SERVICE)
332 wdrtas_interval = i;
333 else
334 wdrtas_interval = wdrtas_get_interval(i);
335
336
337 case WDIOC_GETTIMEOUT:
338 return put_user(wdrtas_interval, argp);
339
340 default:
341 return -ENOTTY;
342 }
343 }
344
345
346
347
348
349
350
351
352
353
354
355 static int wdrtas_open(struct inode *inode, struct file *file)
356 {
357
358 if (atomic_inc_return(&wdrtas_miscdev_open) > 1) {
359 atomic_dec(&wdrtas_miscdev_open);
360 return -EBUSY;
361 }
362
363 wdrtas_timer_start();
364 wdrtas_timer_keepalive();
365
366 return stream_open(inode, file);
367 }
368
369
370
371
372
373
374
375
376
377
378 static int wdrtas_close(struct inode *inode, struct file *file)
379 {
380
381 if (wdrtas_expect_close == WDRTAS_MAGIC_CHAR)
382 wdrtas_timer_stop();
383 else {
384 pr_warn("got unexpected close. Watchdog not stopped.\n");
385 wdrtas_timer_keepalive();
386 }
387
388 wdrtas_expect_close = 0;
389 atomic_dec(&wdrtas_miscdev_open);
390 return 0;
391 }
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406 static ssize_t wdrtas_temp_read(struct file *file, char __user *buf,
407 size_t count, loff_t *ppos)
408 {
409 int temperature = 0;
410
411 temperature = wdrtas_get_temperature();
412 if (temperature < 0)
413 return temperature;
414
415 if (copy_to_user(buf, &temperature, 1))
416 return -EFAULT;
417
418 return 1;
419 }
420
421
422
423
424
425
426
427
428
429
430 static int wdrtas_temp_open(struct inode *inode, struct file *file)
431 {
432 return stream_open(inode, file);
433 }
434
435
436
437
438
439
440
441
442
443
444 static int wdrtas_temp_close(struct inode *inode, struct file *file)
445 {
446 return 0;
447 }
448
449
450
451
452
453
454
455
456
457
458
459 static int wdrtas_reboot(struct notifier_block *this,
460 unsigned long code, void *ptr)
461 {
462 if (code == SYS_DOWN || code == SYS_HALT)
463 wdrtas_timer_stop();
464
465 return NOTIFY_DONE;
466 }
467
468
469
470 static const struct file_operations wdrtas_fops = {
471 .owner = THIS_MODULE,
472 .llseek = no_llseek,
473 .write = wdrtas_write,
474 .unlocked_ioctl = wdrtas_ioctl,
475 .open = wdrtas_open,
476 .release = wdrtas_close,
477 };
478
479 static struct miscdevice wdrtas_miscdev = {
480 .minor = WATCHDOG_MINOR,
481 .name = "watchdog",
482 .fops = &wdrtas_fops,
483 };
484
485 static const struct file_operations wdrtas_temp_fops = {
486 .owner = THIS_MODULE,
487 .llseek = no_llseek,
488 .read = wdrtas_temp_read,
489 .open = wdrtas_temp_open,
490 .release = wdrtas_temp_close,
491 };
492
493 static struct miscdevice wdrtas_tempdev = {
494 .minor = TEMP_MINOR,
495 .name = "temperature",
496 .fops = &wdrtas_temp_fops,
497 };
498
499 static struct notifier_block wdrtas_notifier = {
500 .notifier_call = wdrtas_reboot,
501 };
502
503
504
505
506
507
508
509
510
511
512 static int wdrtas_get_tokens(void)
513 {
514 wdrtas_token_get_sensor_state = rtas_token("get-sensor-state");
515 if (wdrtas_token_get_sensor_state == RTAS_UNKNOWN_SERVICE) {
516 pr_warn("couldn't get token for get-sensor-state. Trying to continue without temperature support.\n");
517 }
518
519 wdrtas_token_get_sp = rtas_token("ibm,get-system-parameter");
520 if (wdrtas_token_get_sp == RTAS_UNKNOWN_SERVICE) {
521 pr_warn("couldn't get token for ibm,get-system-parameter. Trying to continue with a default timeout value of %i seconds.\n",
522 WDRTAS_DEFAULT_INTERVAL);
523 }
524
525 wdrtas_token_set_indicator = rtas_token("set-indicator");
526 if (wdrtas_token_set_indicator == RTAS_UNKNOWN_SERVICE) {
527 pr_err("couldn't get token for set-indicator. Terminating watchdog code.\n");
528 return -EIO;
529 }
530
531 wdrtas_token_event_scan = rtas_token("event-scan");
532 if (wdrtas_token_event_scan == RTAS_UNKNOWN_SERVICE) {
533 pr_err("couldn't get token for event-scan. Terminating watchdog code.\n");
534 return -EIO;
535 }
536
537 return 0;
538 }
539
540
541
542
543
544
545
546 static void wdrtas_unregister_devs(void)
547 {
548 misc_deregister(&wdrtas_miscdev);
549 if (wdrtas_token_get_sensor_state != RTAS_UNKNOWN_SERVICE)
550 misc_deregister(&wdrtas_tempdev);
551 }
552
553
554
555
556
557
558
559
560
561 static int wdrtas_register_devs(void)
562 {
563 int result;
564
565 result = misc_register(&wdrtas_miscdev);
566 if (result) {
567 pr_err("couldn't register watchdog misc device. Terminating watchdog code.\n");
568 return result;
569 }
570
571 if (wdrtas_token_get_sensor_state != RTAS_UNKNOWN_SERVICE) {
572 result = misc_register(&wdrtas_tempdev);
573 if (result) {
574 pr_warn("couldn't register watchdog temperature misc device. Continuing without temperature support.\n");
575 wdrtas_token_get_sensor_state = RTAS_UNKNOWN_SERVICE;
576 }
577 }
578
579 return 0;
580 }
581
582
583
584
585
586
587
588
589 static int __init wdrtas_init(void)
590 {
591 if (wdrtas_get_tokens())
592 return -ENODEV;
593
594 if (wdrtas_register_devs())
595 return -ENODEV;
596
597 if (register_reboot_notifier(&wdrtas_notifier)) {
598 pr_err("could not register reboot notifier. Terminating watchdog code.\n");
599 wdrtas_unregister_devs();
600 return -ENODEV;
601 }
602
603 if (wdrtas_token_get_sp == RTAS_UNKNOWN_SERVICE)
604 wdrtas_interval = WDRTAS_DEFAULT_INTERVAL;
605 else
606 wdrtas_interval = wdrtas_get_interval(WDRTAS_DEFAULT_INTERVAL);
607
608 return 0;
609 }
610
611
612
613
614
615
616 static void __exit wdrtas_exit(void)
617 {
618 if (!wdrtas_nowayout)
619 wdrtas_timer_stop();
620
621 wdrtas_unregister_devs();
622
623 unregister_reboot_notifier(&wdrtas_notifier);
624 }
625
626 module_init(wdrtas_init);
627 module_exit(wdrtas_exit);