Merge branch 'x86-reboot-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[deliverable/linux.git] / drivers / watchdog / wdrtas.c
CommitLineData
031f7ede
UB
1/*
2 * FIXME: add wdrtas_get_status and wdrtas_get_boot_status as soon as
3 * RTAS calls are available
4 */
5
6/*
7 * RTAS watchdog driver
8 *
9 * (C) Copyright IBM Corp. 2005
10 * device driver to exploit watchdog RTAS functions
11 *
12 * Authors : Utz Bacher <utz.bacher@de.ibm.com>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
17 * any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28
27c766aa
JP
29#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
031f7ede
UB
31#include <linux/fs.h>
32#include <linux/init.h>
33#include <linux/kernel.h>
34#include <linux/miscdevice.h>
35#include <linux/module.h>
36#include <linux/notifier.h>
37#include <linux/reboot.h>
38#include <linux/types.h>
39#include <linux/watchdog.h>
dae67a28 40#include <linux/uaccess.h>
031f7ede
UB
41
42#include <asm/rtas.h>
031f7ede
UB
43
44#define WDRTAS_MAGIC_CHAR 42
45#define WDRTAS_SUPPORTED_MASK (WDIOF_SETTIMEOUT | \
46 WDIOF_MAGICCLOSE)
47
48MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>");
49MODULE_DESCRIPTION("RTAS watchdog driver");
50MODULE_LICENSE("GPL");
51MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
52MODULE_ALIAS_MISCDEV(TEMP_MINOR);
53
86a1e189 54static bool wdrtas_nowayout = WATCHDOG_NOWAYOUT;
031f7ede 55static atomic_t wdrtas_miscdev_open = ATOMIC_INIT(0);
dae67a28 56static char wdrtas_expect_close;
031f7ede
UB
57
58static int wdrtas_interval;
59
60#define WDRTAS_THERMAL_SENSOR 3
61static int wdrtas_token_get_sensor_state;
62#define WDRTAS_SURVEILLANCE_IND 9000
63static int wdrtas_token_set_indicator;
64#define WDRTAS_SP_SPI 28
65static int wdrtas_token_get_sp;
66static int wdrtas_token_event_scan;
67
68#define WDRTAS_DEFAULT_INTERVAL 300
69
70#define WDRTAS_LOGBUFFER_LEN 128
71static char wdrtas_logbuffer[WDRTAS_LOGBUFFER_LEN];
72
73
74/*** watchdog access functions */
75
76/**
77 * wdrtas_set_interval - sets the watchdog interval
78 * @interval: new interval
79 *
80 * returns 0 on success, <0 on failures
81 *
82 * wdrtas_set_interval sets the watchdog keepalive interval by calling the
83 * RTAS function set-indicator (surveillance). The unit of interval is
84 * seconds.
85 */
dae67a28
AC
86
87static int wdrtas_set_interval(int interval)
031f7ede
UB
88{
89 long result;
90 static int print_msg = 10;
91
92 /* rtas uses minutes */
93 interval = (interval + 59) / 60;
94
95 result = rtas_call(wdrtas_token_set_indicator, 3, 1, NULL,
96 WDRTAS_SURVEILLANCE_IND, 0, interval);
dae67a28 97 if (result < 0 && print_msg) {
27c766aa
JP
98 pr_err("setting the watchdog to %i timeout failed: %li\n",
99 interval, result);
031f7ede
UB
100 print_msg--;
101 }
102
103 return result;
104}
105
b6966b1b
MN
106#define WDRTAS_SP_SPI_LEN 4
107
031f7ede
UB
108/**
109 * wdrtas_get_interval - returns the current watchdog interval
110 * @fallback_value: value (in seconds) to use, if the RTAS call fails
111 *
112 * returns the interval
113 *
114 * wdrtas_get_interval returns the current watchdog keepalive interval
115 * as reported by the RTAS function ibm,get-system-parameter. The unit
116 * of the return value is seconds.
117 */
dae67a28 118static int wdrtas_get_interval(int fallback_value)
031f7ede
UB
119{
120 long result;
b6966b1b 121 char value[WDRTAS_SP_SPI_LEN];
031f7ede 122
b6966b1b
MN
123 spin_lock(&rtas_data_buf_lock);
124 memset(rtas_data_buf, 0, WDRTAS_SP_SPI_LEN);
031f7ede 125 result = rtas_call(wdrtas_token_get_sp, 3, 1, NULL,
b6966b1b
MN
126 WDRTAS_SP_SPI, __pa(rtas_data_buf),
127 WDRTAS_SP_SPI_LEN);
128
129 memcpy(value, rtas_data_buf, WDRTAS_SP_SPI_LEN);
130 spin_unlock(&rtas_data_buf_lock);
131
dae67a28 132 if (value[0] != 0 || value[1] != 2 || value[3] != 0 || result < 0) {
27c766aa
JP
133 pr_warn("could not get sp_spi watchdog timeout (%li). Continuing\n",
134 result);
031f7ede
UB
135 return fallback_value;
136 }
137
138 /* rtas uses minutes */
139 return ((int)value[2]) * 60;
140}
141
142/**
143 * wdrtas_timer_start - starts watchdog
144 *
145 * wdrtas_timer_start starts the watchdog by calling the RTAS function
146 * set-interval (surveillance)
147 */
dae67a28 148static void wdrtas_timer_start(void)
031f7ede
UB
149{
150 wdrtas_set_interval(wdrtas_interval);
151}
152
153/**
154 * wdrtas_timer_stop - stops watchdog
155 *
156 * wdrtas_timer_stop stops the watchdog timer by calling the RTAS function
157 * set-interval (surveillance)
158 */
dae67a28 159static void wdrtas_timer_stop(void)
031f7ede
UB
160{
161 wdrtas_set_interval(0);
162}
163
031f7ede
UB
164/**
165 * wdrtas_timer_keepalive - resets watchdog timer to keep system alive
166 *
167 * wdrtas_timer_keepalive restarts the watchdog timer by calling the
168 * RTAS function event-scan and repeats these calls as long as there are
169 * events available. All events will be dumped.
170 */
dae67a28 171static void wdrtas_timer_keepalive(void)
031f7ede
UB
172{
173 long result;
174
175 do {
176 result = rtas_call(wdrtas_token_event_scan, 4, 1, NULL,
177 RTAS_EVENT_SCAN_ALL_EVENTS, 0,
178 (void *)__pa(wdrtas_logbuffer),
179 WDRTAS_LOGBUFFER_LEN);
180 if (result < 0)
27c766aa 181 pr_err("event-scan failed: %li\n", result);
031f7ede 182 if (result == 0)
48388069
AS
183 print_hex_dump(KERN_INFO, "dumping event, data: ",
184 DUMP_PREFIX_OFFSET, 16, 1,
185 wdrtas_logbuffer, WDRTAS_LOGBUFFER_LEN, false);
031f7ede
UB
186 } while (result == 0);
187}
188
189/**
190 * wdrtas_get_temperature - returns current temperature
191 *
192 * returns temperature or <0 on failures
193 *
194 * wdrtas_get_temperature returns the current temperature in Fahrenheit. It
195 * uses the RTAS call get-sensor-state, token 3 to do so
196 */
dae67a28 197static int wdrtas_get_temperature(void)
031f7ede 198{
5ba762c9 199 int result;
031f7ede
UB
200 int temperature = 0;
201
5ba762c9 202 result = rtas_get_sensor(WDRTAS_THERMAL_SENSOR, 0, &temperature);
031f7ede
UB
203
204 if (result < 0)
27c766aa 205 pr_warn("reading the thermal sensor failed: %i\n", result);
031f7ede
UB
206 else
207 temperature = ((temperature * 9) / 5) + 32; /* fahrenheit */
208
209 return temperature;
210}
211
212/**
213 * wdrtas_get_status - returns the status of the watchdog
214 *
215 * returns a bitmask of defines WDIOF_... as defined in
216 * include/linux/watchdog.h
217 */
dae67a28 218static int wdrtas_get_status(void)
031f7ede
UB
219{
220 return 0; /* TODO */
221}
222
223/**
224 * wdrtas_get_boot_status - returns the reason for the last boot
225 *
226 * returns a bitmask of defines WDIOF_... as defined in
227 * include/linux/watchdog.h, indicating why the watchdog rebooted the system
228 */
dae67a28 229static int wdrtas_get_boot_status(void)
031f7ede
UB
230{
231 return 0; /* TODO */
232}
233
234/*** watchdog API and operations stuff */
235
236/* wdrtas_write - called when watchdog device is written to
237 * @file: file structure
238 * @buf: user buffer with data
239 * @len: amount to data written
240 * @ppos: position in file
241 *
242 * returns the number of successfully processed characters, which is always
243 * the number of bytes passed to this function
244 *
245 * wdrtas_write processes all the data given to it and looks for the magic
246 * character 'V'. This character allows the watchdog device to be closed
247 * properly.
248 */
dae67a28 249static ssize_t wdrtas_write(struct file *file, const char __user *buf,
031f7ede
UB
250 size_t len, loff_t *ppos)
251{
252 int i;
253 char c;
254
255 if (!len)
256 goto out;
257
258 if (!wdrtas_nowayout) {
259 wdrtas_expect_close = 0;
260 /* look for 'V' */
261 for (i = 0; i < len; i++) {
262 if (get_user(c, buf + i))
263 return -EFAULT;
264 /* allow to close device */
265 if (c == 'V')
266 wdrtas_expect_close = WDRTAS_MAGIC_CHAR;
267 }
268 }
269
270 wdrtas_timer_keepalive();
271
272out:
273 return len;
274}
275
276/**
277 * wdrtas_ioctl - ioctl function for the watchdog device
031f7ede
UB
278 * @file: file structure
279 * @cmd: command for ioctl
280 * @arg: argument pointer
281 *
282 * returns 0 on success, <0 on failure
283 *
284 * wdrtas_ioctl implements the watchdog API ioctls
285 */
dae67a28
AC
286
287static long wdrtas_ioctl(struct file *file, unsigned int cmd,
288 unsigned long arg)
031f7ede 289{
e896fd98 290 int __user *argp = (void __user *)arg;
031f7ede 291 int i;
42747d71 292 static const struct watchdog_info wdinfo = {
031f7ede
UB
293 .options = WDRTAS_SUPPORTED_MASK,
294 .firmware_version = 0,
7944d3a5 295 .identity = "wdrtas",
031f7ede
UB
296 };
297
298 switch (cmd) {
299 case WDIOC_GETSUPPORT:
300 if (copy_to_user(argp, &wdinfo, sizeof(wdinfo)))
301 return -EFAULT;
302 return 0;
303
304 case WDIOC_GETSTATUS:
305 i = wdrtas_get_status();
306 return put_user(i, argp);
307
308 case WDIOC_GETBOOTSTATUS:
309 i = wdrtas_get_boot_status();
310 return put_user(i, argp);
311
312 case WDIOC_GETTEMP:
313 if (wdrtas_token_get_sensor_state == RTAS_UNKNOWN_SERVICE)
314 return -EOPNOTSUPP;
315
316 i = wdrtas_get_temperature();
317 return put_user(i, argp);
318
319 case WDIOC_SETOPTIONS:
320 if (get_user(i, argp))
321 return -EFAULT;
322 if (i & WDIOS_DISABLECARD)
323 wdrtas_timer_stop();
324 if (i & WDIOS_ENABLECARD) {
325 wdrtas_timer_keepalive();
326 wdrtas_timer_start();
327 }
dae67a28 328 /* not implemented. Done by H8
031f7ede 329 if (i & WDIOS_TEMPPANIC) {
dae67a28 330 } */
031f7ede
UB
331 return 0;
332
333 case WDIOC_KEEPALIVE:
334 wdrtas_timer_keepalive();
335 return 0;
336
337 case WDIOC_SETTIMEOUT:
338 if (get_user(i, argp))
339 return -EFAULT;
340
341 if (wdrtas_set_interval(i))
342 return -EINVAL;
343
344 wdrtas_timer_keepalive();
345
346 if (wdrtas_token_get_sp == RTAS_UNKNOWN_SERVICE)
347 wdrtas_interval = i;
348 else
349 wdrtas_interval = wdrtas_get_interval(i);
350 /* fallthrough */
351
352 case WDIOC_GETTIMEOUT:
353 return put_user(wdrtas_interval, argp);
354
355 default:
795b89d2 356 return -ENOTTY;
031f7ede
UB
357 }
358}
359
360/**
361 * wdrtas_open - open function of watchdog device
362 * @inode: inode structure
363 * @file: file structure
364 *
365 * returns 0 on success, -EBUSY if the file has been opened already, <0 on
366 * other failures
367 *
368 * function called when watchdog device is opened
369 */
dae67a28 370static int wdrtas_open(struct inode *inode, struct file *file)
031f7ede
UB
371{
372 /* only open once */
373 if (atomic_inc_return(&wdrtas_miscdev_open) > 1) {
374 atomic_dec(&wdrtas_miscdev_open);
375 return -EBUSY;
376 }
377
378 wdrtas_timer_start();
379 wdrtas_timer_keepalive();
380
381 return nonseekable_open(inode, file);
382}
383
384/**
385 * wdrtas_close - close function of watchdog device
386 * @inode: inode structure
387 * @file: file structure
388 *
389 * returns 0 on success
390 *
391 * close function. Always succeeds
392 */
dae67a28 393static int wdrtas_close(struct inode *inode, struct file *file)
031f7ede
UB
394{
395 /* only stop watchdog, if this was announced using 'V' before */
396 if (wdrtas_expect_close == WDRTAS_MAGIC_CHAR)
397 wdrtas_timer_stop();
398 else {
27c766aa 399 pr_warn("got unexpected close. Watchdog not stopped.\n");
031f7ede
UB
400 wdrtas_timer_keepalive();
401 }
402
403 wdrtas_expect_close = 0;
404 atomic_dec(&wdrtas_miscdev_open);
405 return 0;
406}
407
408/**
409 * wdrtas_temp_read - gives back the temperature in fahrenheit
410 * @file: file structure
411 * @buf: user buffer
412 * @count: number of bytes to be read
413 * @ppos: position in file
414 *
415 * returns always 1 or -EFAULT in case of user space copy failures, <0 on
416 * other failures
417 *
418 * wdrtas_temp_read gives the temperature to the users by copying this
419 * value as one byte into the user space buffer. The unit is Fahrenheit...
420 */
dae67a28 421static ssize_t wdrtas_temp_read(struct file *file, char __user *buf,
031f7ede
UB
422 size_t count, loff_t *ppos)
423{
424 int temperature = 0;
425
426 temperature = wdrtas_get_temperature();
427 if (temperature < 0)
428 return temperature;
429
430 if (copy_to_user(buf, &temperature, 1))
431 return -EFAULT;
432
433 return 1;
434}
435
436/**
437 * wdrtas_temp_open - open function of temperature device
438 * @inode: inode structure
439 * @file: file structure
440 *
441 * returns 0 on success, <0 on failure
442 *
443 * function called when temperature device is opened
444 */
dae67a28 445static int wdrtas_temp_open(struct inode *inode, struct file *file)
031f7ede
UB
446{
447 return nonseekable_open(inode, file);
448}
449
450/**
451 * wdrtas_temp_close - close function of temperature device
452 * @inode: inode structure
453 * @file: file structure
454 *
455 * returns 0 on success
456 *
457 * close function. Always succeeds
458 */
dae67a28 459static int wdrtas_temp_close(struct inode *inode, struct file *file)
031f7ede
UB
460{
461 return 0;
462}
463
464/**
465 * wdrtas_reboot - reboot notifier function
466 * @nb: notifier block structure
467 * @code: reboot code
468 * @ptr: unused
469 *
470 * returns NOTIFY_DONE
471 *
472 * wdrtas_reboot stops the watchdog in case of a reboot
473 */
dae67a28
AC
474static int wdrtas_reboot(struct notifier_block *this,
475 unsigned long code, void *ptr)
031f7ede 476{
dae67a28 477 if (code == SYS_DOWN || code == SYS_HALT)
031f7ede
UB
478 wdrtas_timer_stop();
479
480 return NOTIFY_DONE;
481}
482
483/*** initialization stuff */
484
62322d25 485static const struct file_operations wdrtas_fops = {
031f7ede
UB
486 .owner = THIS_MODULE,
487 .llseek = no_llseek,
488 .write = wdrtas_write,
dae67a28 489 .unlocked_ioctl = wdrtas_ioctl,
031f7ede
UB
490 .open = wdrtas_open,
491 .release = wdrtas_close,
492};
493
494static struct miscdevice wdrtas_miscdev = {
495 .minor = WATCHDOG_MINOR,
496 .name = "watchdog",
497 .fops = &wdrtas_fops,
498};
499
62322d25 500static const struct file_operations wdrtas_temp_fops = {
031f7ede
UB
501 .owner = THIS_MODULE,
502 .llseek = no_llseek,
503 .read = wdrtas_temp_read,
504 .open = wdrtas_temp_open,
505 .release = wdrtas_temp_close,
506};
507
508static struct miscdevice wdrtas_tempdev = {
509 .minor = TEMP_MINOR,
510 .name = "temperature",
511 .fops = &wdrtas_temp_fops,
512};
513
514static struct notifier_block wdrtas_notifier = {
515 .notifier_call = wdrtas_reboot,
516};
517
518/**
519 * wdrtas_get_tokens - reads in RTAS tokens
520 *
af901ca1 521 * returns 0 on success, <0 on failure
031f7ede
UB
522 *
523 * wdrtas_get_tokens reads in the tokens for the RTAS calls used in
524 * this watchdog driver. It tolerates, if "get-sensor-state" and
525 * "ibm,get-system-parameter" are not available.
526 */
dae67a28 527static int wdrtas_get_tokens(void)
031f7ede
UB
528{
529 wdrtas_token_get_sensor_state = rtas_token("get-sensor-state");
530 if (wdrtas_token_get_sensor_state == RTAS_UNKNOWN_SERVICE) {
27c766aa 531 pr_warn("couldn't get token for get-sensor-state. Trying to continue without temperature support.\n");
031f7ede
UB
532 }
533
534 wdrtas_token_get_sp = rtas_token("ibm,get-system-parameter");
535 if (wdrtas_token_get_sp == RTAS_UNKNOWN_SERVICE) {
27c766aa
JP
536 pr_warn("couldn't get token for ibm,get-system-parameter. Trying to continue with a default timeout value of %i seconds.\n",
537 WDRTAS_DEFAULT_INTERVAL);
031f7ede
UB
538 }
539
540 wdrtas_token_set_indicator = rtas_token("set-indicator");
541 if (wdrtas_token_set_indicator == RTAS_UNKNOWN_SERVICE) {
27c766aa 542 pr_err("couldn't get token for set-indicator. Terminating watchdog code.\n");
031f7ede
UB
543 return -EIO;
544 }
545
546 wdrtas_token_event_scan = rtas_token("event-scan");
547 if (wdrtas_token_event_scan == RTAS_UNKNOWN_SERVICE) {
27c766aa 548 pr_err("couldn't get token for event-scan. Terminating watchdog code.\n");
031f7ede
UB
549 return -EIO;
550 }
551
552 return 0;
553}
554
555/**
556 * wdrtas_unregister_devs - unregisters the misc dev handlers
557 *
558 * wdrtas_register_devs unregisters the watchdog and temperature watchdog
559 * misc devs
560 */
dae67a28 561static void wdrtas_unregister_devs(void)
031f7ede
UB
562{
563 misc_deregister(&wdrtas_miscdev);
564 if (wdrtas_token_get_sensor_state != RTAS_UNKNOWN_SERVICE)
565 misc_deregister(&wdrtas_tempdev);
566}
567
568/**
569 * wdrtas_register_devs - registers the misc dev handlers
570 *
af901ca1 571 * returns 0 on success, <0 on failure
031f7ede
UB
572 *
573 * wdrtas_register_devs registers the watchdog and temperature watchdog
574 * misc devs
575 */
dae67a28 576static int wdrtas_register_devs(void)
031f7ede
UB
577{
578 int result;
579
580 result = misc_register(&wdrtas_miscdev);
581 if (result) {
27c766aa 582 pr_err("couldn't register watchdog misc device. Terminating watchdog code.\n");
031f7ede
UB
583 return result;
584 }
585
586 if (wdrtas_token_get_sensor_state != RTAS_UNKNOWN_SERVICE) {
587 result = misc_register(&wdrtas_tempdev);
588 if (result) {
27c766aa 589 pr_warn("couldn't register watchdog temperature misc device. Continuing without temperature support.\n");
031f7ede
UB
590 wdrtas_token_get_sensor_state = RTAS_UNKNOWN_SERVICE;
591 }
592 }
593
594 return 0;
595}
596
597/**
598 * wdrtas_init - init function of the watchdog driver
599 *
af901ca1 600 * returns 0 on success, <0 on failure
031f7ede
UB
601 *
602 * registers the file handlers and the reboot notifier
603 */
dae67a28 604static int __init wdrtas_init(void)
031f7ede
UB
605{
606 if (wdrtas_get_tokens())
607 return -ENODEV;
608
609 if (wdrtas_register_devs())
610 return -ENODEV;
611
612 if (register_reboot_notifier(&wdrtas_notifier)) {
27c766aa 613 pr_err("could not register reboot notifier. Terminating watchdog code.\n");
031f7ede
UB
614 wdrtas_unregister_devs();
615 return -ENODEV;
616 }
617
618 if (wdrtas_token_get_sp == RTAS_UNKNOWN_SERVICE)
619 wdrtas_interval = WDRTAS_DEFAULT_INTERVAL;
620 else
621 wdrtas_interval = wdrtas_get_interval(WDRTAS_DEFAULT_INTERVAL);
622
623 return 0;
624}
625
626/**
627 * wdrtas_exit - exit function of the watchdog driver
628 *
629 * unregisters the file handlers and the reboot notifier
630 */
dae67a28 631static void __exit wdrtas_exit(void)
031f7ede
UB
632{
633 if (!wdrtas_nowayout)
634 wdrtas_timer_stop();
635
636 wdrtas_unregister_devs();
637
638 unregister_reboot_notifier(&wdrtas_notifier);
639}
640
641module_init(wdrtas_init);
642module_exit(wdrtas_exit);
This page took 1.062758 seconds and 5 git commands to generate.