Linux 3.12.39
[linux/fpc-iii.git] / drivers / watchdog / wdrtas.c
blob3045debd5411e26d818309fc3a24c349af0ade4f
1 /*
2 * FIXME: add wdrtas_get_status and wdrtas_get_boot_status as soon as
3 * RTAS calls are available
4 */
6 /*
7 * RTAS watchdog driver
9 * (C) Copyright IBM Corp. 2005
10 * device driver to exploit watchdog RTAS functions
12 * Authors : Utz Bacher <utz.bacher@de.ibm.com>
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.
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.
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.
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
40 #include <linux/uaccess.h>
42 #include <asm/rtas.h>
44 #define WDRTAS_MAGIC_CHAR 42
45 #define WDRTAS_SUPPORTED_MASK (WDIOF_SETTIMEOUT | \
46 WDIOF_MAGICCLOSE)
48 MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>");
49 MODULE_DESCRIPTION("RTAS watchdog driver");
50 MODULE_LICENSE("GPL");
51 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
52 MODULE_ALIAS_MISCDEV(TEMP_MINOR);
54 static bool wdrtas_nowayout = WATCHDOG_NOWAYOUT;
55 static atomic_t wdrtas_miscdev_open = ATOMIC_INIT(0);
56 static char wdrtas_expect_close;
58 static int wdrtas_interval;
60 #define WDRTAS_THERMAL_SENSOR 3
61 static int wdrtas_token_get_sensor_state;
62 #define WDRTAS_SURVEILLANCE_IND 9000
63 static int wdrtas_token_set_indicator;
64 #define WDRTAS_SP_SPI 28
65 static int wdrtas_token_get_sp;
66 static int wdrtas_token_event_scan;
68 #define WDRTAS_DEFAULT_INTERVAL 300
70 #define WDRTAS_LOGBUFFER_LEN 128
71 static char wdrtas_logbuffer[WDRTAS_LOGBUFFER_LEN];
74 /*** watchdog access functions */
76 /**
77 * wdrtas_set_interval - sets the watchdog interval
78 * @interval: new interval
80 * returns 0 on success, <0 on failures
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.
87 static int wdrtas_set_interval(int interval)
89 long result;
90 static int print_msg = 10;
92 /* rtas uses minutes */
93 interval = (interval + 59) / 60;
95 result = rtas_call(wdrtas_token_set_indicator, 3, 1, NULL,
96 WDRTAS_SURVEILLANCE_IND, 0, interval);
97 if (result < 0 && print_msg) {
98 pr_err("setting the watchdog to %i timeout failed: %li\n",
99 interval, result);
100 print_msg--;
103 return result;
106 #define WDRTAS_SP_SPI_LEN 4
109 * wdrtas_get_interval - returns the current watchdog interval
110 * @fallback_value: value (in seconds) to use, if the RTAS call fails
112 * returns the interval
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.
118 static int wdrtas_get_interval(int fallback_value)
120 long result;
121 char value[WDRTAS_SP_SPI_LEN];
123 spin_lock(&rtas_data_buf_lock);
124 memset(rtas_data_buf, 0, WDRTAS_SP_SPI_LEN);
125 result = rtas_call(wdrtas_token_get_sp, 3, 1, NULL,
126 WDRTAS_SP_SPI, __pa(rtas_data_buf),
127 WDRTAS_SP_SPI_LEN);
129 memcpy(value, rtas_data_buf, WDRTAS_SP_SPI_LEN);
130 spin_unlock(&rtas_data_buf_lock);
132 if (value[0] != 0 || value[1] != 2 || value[3] != 0 || result < 0) {
133 pr_warn("could not get sp_spi watchdog timeout (%li). Continuing\n",
134 result);
135 return fallback_value;
138 /* rtas uses minutes */
139 return ((int)value[2]) * 60;
143 * wdrtas_timer_start - starts watchdog
145 * wdrtas_timer_start starts the watchdog by calling the RTAS function
146 * set-interval (surveillance)
148 static void wdrtas_timer_start(void)
150 wdrtas_set_interval(wdrtas_interval);
154 * wdrtas_timer_stop - stops watchdog
156 * wdrtas_timer_stop stops the watchdog timer by calling the RTAS function
157 * set-interval (surveillance)
159 static void wdrtas_timer_stop(void)
161 wdrtas_set_interval(0);
165 * wdrtas_timer_keepalive - resets watchdog timer to keep system alive
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.
171 static void wdrtas_timer_keepalive(void)
173 long result;
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)
181 pr_err("event-scan failed: %li\n", result);
182 if (result == 0)
183 print_hex_dump(KERN_INFO, "dumping event, data: ",
184 DUMP_PREFIX_OFFSET, 16, 1,
185 wdrtas_logbuffer, WDRTAS_LOGBUFFER_LEN, false);
186 } while (result == 0);
190 * wdrtas_get_temperature - returns current temperature
192 * returns temperature or <0 on failures
194 * wdrtas_get_temperature returns the current temperature in Fahrenheit. It
195 * uses the RTAS call get-sensor-state, token 3 to do so
197 static int wdrtas_get_temperature(void)
199 int result;
200 int temperature = 0;
202 result = rtas_get_sensor(WDRTAS_THERMAL_SENSOR, 0, &temperature);
204 if (result < 0)
205 pr_warn("reading the thermal sensor failed: %i\n", result);
206 else
207 temperature = ((temperature * 9) / 5) + 32; /* fahrenheit */
209 return temperature;
213 * wdrtas_get_status - returns the status of the watchdog
215 * returns a bitmask of defines WDIOF_... as defined in
216 * include/linux/watchdog.h
218 static int wdrtas_get_status(void)
220 return 0; /* TODO */
224 * wdrtas_get_boot_status - returns the reason for the last boot
226 * returns a bitmask of defines WDIOF_... as defined in
227 * include/linux/watchdog.h, indicating why the watchdog rebooted the system
229 static int wdrtas_get_boot_status(void)
231 return 0; /* TODO */
234 /*** watchdog API and operations stuff */
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
242 * returns the number of successfully processed characters, which is always
243 * the number of bytes passed to this function
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.
249 static ssize_t wdrtas_write(struct file *file, const char __user *buf,
250 size_t len, loff_t *ppos)
252 int i;
253 char c;
255 if (!len)
256 goto out;
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;
270 wdrtas_timer_keepalive();
272 out:
273 return len;
277 * wdrtas_ioctl - ioctl function for the watchdog device
278 * @file: file structure
279 * @cmd: command for ioctl
280 * @arg: argument pointer
282 * returns 0 on success, <0 on failure
284 * wdrtas_ioctl implements the watchdog API ioctls
287 static long wdrtas_ioctl(struct file *file, unsigned int cmd,
288 unsigned long arg)
290 int __user *argp = (void __user *)arg;
291 int i;
292 static const struct watchdog_info wdinfo = {
293 .options = WDRTAS_SUPPORTED_MASK,
294 .firmware_version = 0,
295 .identity = "wdrtas",
298 switch (cmd) {
299 case WDIOC_GETSUPPORT:
300 if (copy_to_user(argp, &wdinfo, sizeof(wdinfo)))
301 return -EFAULT;
302 return 0;
304 case WDIOC_GETSTATUS:
305 i = wdrtas_get_status();
306 return put_user(i, argp);
308 case WDIOC_GETBOOTSTATUS:
309 i = wdrtas_get_boot_status();
310 return put_user(i, argp);
312 case WDIOC_GETTEMP:
313 if (wdrtas_token_get_sensor_state == RTAS_UNKNOWN_SERVICE)
314 return -EOPNOTSUPP;
316 i = wdrtas_get_temperature();
317 return put_user(i, argp);
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();
328 /* not implemented. Done by H8
329 if (i & WDIOS_TEMPPANIC) {
330 } */
331 return 0;
333 case WDIOC_KEEPALIVE:
334 wdrtas_timer_keepalive();
335 return 0;
337 case WDIOC_SETTIMEOUT:
338 if (get_user(i, argp))
339 return -EFAULT;
341 if (wdrtas_set_interval(i))
342 return -EINVAL;
344 wdrtas_timer_keepalive();
346 if (wdrtas_token_get_sp == RTAS_UNKNOWN_SERVICE)
347 wdrtas_interval = i;
348 else
349 wdrtas_interval = wdrtas_get_interval(i);
350 /* fallthrough */
352 case WDIOC_GETTIMEOUT:
353 return put_user(wdrtas_interval, argp);
355 default:
356 return -ENOTTY;
361 * wdrtas_open - open function of watchdog device
362 * @inode: inode structure
363 * @file: file structure
365 * returns 0 on success, -EBUSY if the file has been opened already, <0 on
366 * other failures
368 * function called when watchdog device is opened
370 static int wdrtas_open(struct inode *inode, struct file *file)
372 /* only open once */
373 if (atomic_inc_return(&wdrtas_miscdev_open) > 1) {
374 atomic_dec(&wdrtas_miscdev_open);
375 return -EBUSY;
378 wdrtas_timer_start();
379 wdrtas_timer_keepalive();
381 return nonseekable_open(inode, file);
385 * wdrtas_close - close function of watchdog device
386 * @inode: inode structure
387 * @file: file structure
389 * returns 0 on success
391 * close function. Always succeeds
393 static int wdrtas_close(struct inode *inode, struct file *file)
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 {
399 pr_warn("got unexpected close. Watchdog not stopped.\n");
400 wdrtas_timer_keepalive();
403 wdrtas_expect_close = 0;
404 atomic_dec(&wdrtas_miscdev_open);
405 return 0;
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
415 * returns always 1 or -EFAULT in case of user space copy failures, <0 on
416 * other failures
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...
421 static ssize_t wdrtas_temp_read(struct file *file, char __user *buf,
422 size_t count, loff_t *ppos)
424 int temperature = 0;
426 temperature = wdrtas_get_temperature();
427 if (temperature < 0)
428 return temperature;
430 if (copy_to_user(buf, &temperature, 1))
431 return -EFAULT;
433 return 1;
437 * wdrtas_temp_open - open function of temperature device
438 * @inode: inode structure
439 * @file: file structure
441 * returns 0 on success, <0 on failure
443 * function called when temperature device is opened
445 static int wdrtas_temp_open(struct inode *inode, struct file *file)
447 return nonseekable_open(inode, file);
451 * wdrtas_temp_close - close function of temperature device
452 * @inode: inode structure
453 * @file: file structure
455 * returns 0 on success
457 * close function. Always succeeds
459 static int wdrtas_temp_close(struct inode *inode, struct file *file)
461 return 0;
465 * wdrtas_reboot - reboot notifier function
466 * @nb: notifier block structure
467 * @code: reboot code
468 * @ptr: unused
470 * returns NOTIFY_DONE
472 * wdrtas_reboot stops the watchdog in case of a reboot
474 static int wdrtas_reboot(struct notifier_block *this,
475 unsigned long code, void *ptr)
477 if (code == SYS_DOWN || code == SYS_HALT)
478 wdrtas_timer_stop();
480 return NOTIFY_DONE;
483 /*** initialization stuff */
485 static const struct file_operations wdrtas_fops = {
486 .owner = THIS_MODULE,
487 .llseek = no_llseek,
488 .write = wdrtas_write,
489 .unlocked_ioctl = wdrtas_ioctl,
490 .open = wdrtas_open,
491 .release = wdrtas_close,
494 static struct miscdevice wdrtas_miscdev = {
495 .minor = WATCHDOG_MINOR,
496 .name = "watchdog",
497 .fops = &wdrtas_fops,
500 static const struct file_operations wdrtas_temp_fops = {
501 .owner = THIS_MODULE,
502 .llseek = no_llseek,
503 .read = wdrtas_temp_read,
504 .open = wdrtas_temp_open,
505 .release = wdrtas_temp_close,
508 static struct miscdevice wdrtas_tempdev = {
509 .minor = TEMP_MINOR,
510 .name = "temperature",
511 .fops = &wdrtas_temp_fops,
514 static struct notifier_block wdrtas_notifier = {
515 .notifier_call = wdrtas_reboot,
519 * wdrtas_get_tokens - reads in RTAS tokens
521 * returns 0 on success, <0 on failure
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.
527 static int wdrtas_get_tokens(void)
529 wdrtas_token_get_sensor_state = rtas_token("get-sensor-state");
530 if (wdrtas_token_get_sensor_state == RTAS_UNKNOWN_SERVICE) {
531 pr_warn("couldn't get token for get-sensor-state. Trying to continue without temperature support.\n");
534 wdrtas_token_get_sp = rtas_token("ibm,get-system-parameter");
535 if (wdrtas_token_get_sp == RTAS_UNKNOWN_SERVICE) {
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);
540 wdrtas_token_set_indicator = rtas_token("set-indicator");
541 if (wdrtas_token_set_indicator == RTAS_UNKNOWN_SERVICE) {
542 pr_err("couldn't get token for set-indicator. Terminating watchdog code.\n");
543 return -EIO;
546 wdrtas_token_event_scan = rtas_token("event-scan");
547 if (wdrtas_token_event_scan == RTAS_UNKNOWN_SERVICE) {
548 pr_err("couldn't get token for event-scan. Terminating watchdog code.\n");
549 return -EIO;
552 return 0;
556 * wdrtas_unregister_devs - unregisters the misc dev handlers
558 * wdrtas_register_devs unregisters the watchdog and temperature watchdog
559 * misc devs
561 static void wdrtas_unregister_devs(void)
563 misc_deregister(&wdrtas_miscdev);
564 if (wdrtas_token_get_sensor_state != RTAS_UNKNOWN_SERVICE)
565 misc_deregister(&wdrtas_tempdev);
569 * wdrtas_register_devs - registers the misc dev handlers
571 * returns 0 on success, <0 on failure
573 * wdrtas_register_devs registers the watchdog and temperature watchdog
574 * misc devs
576 static int wdrtas_register_devs(void)
578 int result;
580 result = misc_register(&wdrtas_miscdev);
581 if (result) {
582 pr_err("couldn't register watchdog misc device. Terminating watchdog code.\n");
583 return result;
586 if (wdrtas_token_get_sensor_state != RTAS_UNKNOWN_SERVICE) {
587 result = misc_register(&wdrtas_tempdev);
588 if (result) {
589 pr_warn("couldn't register watchdog temperature misc device. Continuing without temperature support.\n");
590 wdrtas_token_get_sensor_state = RTAS_UNKNOWN_SERVICE;
594 return 0;
598 * wdrtas_init - init function of the watchdog driver
600 * returns 0 on success, <0 on failure
602 * registers the file handlers and the reboot notifier
604 static int __init wdrtas_init(void)
606 if (wdrtas_get_tokens())
607 return -ENODEV;
609 if (wdrtas_register_devs())
610 return -ENODEV;
612 if (register_reboot_notifier(&wdrtas_notifier)) {
613 pr_err("could not register reboot notifier. Terminating watchdog code.\n");
614 wdrtas_unregister_devs();
615 return -ENODEV;
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);
623 return 0;
627 * wdrtas_exit - exit function of the watchdog driver
629 * unregisters the file handlers and the reboot notifier
631 static void __exit wdrtas_exit(void)
633 if (!wdrtas_nowayout)
634 wdrtas_timer_stop();
636 wdrtas_unregister_devs();
638 unregister_reboot_notifier(&wdrtas_notifier);
641 module_init(wdrtas_init);
642 module_exit(wdrtas_exit);