2  * FIXME: add wdrtas_get_status and wdrtas_get_boot_status as soon as
 
   3  * RTAS calls are available
 
   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)
 
  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 #include <linux/config.h>
 
  31 #include <linux/init.h>
 
  32 #include <linux/kernel.h>
 
  33 #include <linux/miscdevice.h>
 
  34 #include <linux/module.h>
 
  35 #include <linux/notifier.h>
 
  36 #include <linux/reboot.h>
 
  37 #include <linux/types.h>
 
  38 #include <linux/watchdog.h>
 
  41 #include <asm/uaccess.h>
 
  43 #define WDRTAS_MAGIC_CHAR               42
 
  44 #define WDRTAS_SUPPORTED_MASK           (WDIOF_SETTIMEOUT | \
 
  47 MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>");
 
  48 MODULE_DESCRIPTION("RTAS watchdog driver");
 
  49 MODULE_LICENSE("GPL");
 
  50 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
 
  51 MODULE_ALIAS_MISCDEV(TEMP_MINOR);
 
  53 #ifdef CONFIG_WATCHDOG_NOWAYOUT
 
  54 static int wdrtas_nowayout = 1;
 
  56 static int wdrtas_nowayout = 0;
 
  59 static atomic_t wdrtas_miscdev_open = ATOMIC_INIT(0);
 
  60 static char wdrtas_expect_close = 0;
 
  62 static int wdrtas_interval;
 
  64 #define WDRTAS_THERMAL_SENSOR           3
 
  65 static int wdrtas_token_get_sensor_state;
 
  66 #define WDRTAS_SURVEILLANCE_IND         9000
 
  67 static int wdrtas_token_set_indicator;
 
  68 #define WDRTAS_SP_SPI                   28
 
  69 static int wdrtas_token_get_sp;
 
  70 static int wdrtas_token_event_scan;
 
  72 #define WDRTAS_DEFAULT_INTERVAL         300
 
  74 #define WDRTAS_LOGBUFFER_LEN            128
 
  75 static char wdrtas_logbuffer[WDRTAS_LOGBUFFER_LEN];
 
  78 /*** watchdog access functions */
 
  81  * wdrtas_set_interval - sets the watchdog interval
 
  82  * @interval: new interval
 
  84  * returns 0 on success, <0 on failures
 
  86  * wdrtas_set_interval sets the watchdog keepalive interval by calling the
 
  87  * RTAS function set-indicator (surveillance). The unit of interval is
 
  91 wdrtas_set_interval(int interval)
 
  94         static int print_msg = 10;
 
  96         /* rtas uses minutes */
 
  97         interval = (interval + 59) / 60;
 
  99         result = rtas_call(wdrtas_token_set_indicator, 3, 1, NULL,
 
 100                            WDRTAS_SURVEILLANCE_IND, 0, interval);
 
 101         if ( (result < 0) && (print_msg) ) {
 
 102                 printk(KERN_ERR "wdrtas: setting the watchdog to %i "
 
 103                        "timeout failed: %li\n", interval, result);
 
 111  * wdrtas_get_interval - returns the current watchdog interval
 
 112  * @fallback_value: value (in seconds) to use, if the RTAS call fails
 
 114  * returns the interval
 
 116  * wdrtas_get_interval returns the current watchdog keepalive interval
 
 117  * as reported by the RTAS function ibm,get-system-parameter. The unit
 
 118  * of the return value is seconds.
 
 121 wdrtas_get_interval(int fallback_value)
 
 126         result = rtas_call(wdrtas_token_get_sp, 3, 1, NULL,
 
 127                            WDRTAS_SP_SPI, (void *)__pa(&value), 4);
 
 128         if ( (value[0] != 0) || (value[1] != 2) || (value[3] != 0) ||
 
 130                 printk(KERN_WARNING "wdrtas: could not get sp_spi watchdog "
 
 131                        "timeout (%li). Continuing\n", result);
 
 132                 return fallback_value;
 
 135         /* rtas uses minutes */
 
 136         return ((int)value[2]) * 60;
 
 140  * wdrtas_timer_start - starts watchdog
 
 142  * wdrtas_timer_start starts the watchdog by calling the RTAS function
 
 143  * set-interval (surveillance)
 
 146 wdrtas_timer_start(void)
 
 148         wdrtas_set_interval(wdrtas_interval);
 
 152  * wdrtas_timer_stop - stops watchdog
 
 154  * wdrtas_timer_stop stops the watchdog timer by calling the RTAS function
 
 155  * set-interval (surveillance)
 
 158 wdrtas_timer_stop(void)
 
 160         wdrtas_set_interval(0);
 
 164  * wdrtas_log_scanned_event - logs an event we received during keepalive
 
 166  * wdrtas_log_scanned_event prints a message to the log buffer dumping
 
 167  * the results of the last event-scan call
 
 170 wdrtas_log_scanned_event(void)
 
 174         for (i = 0; i < WDRTAS_LOGBUFFER_LEN; i += 16)
 
 175                 printk(KERN_INFO "wdrtas: dumping event (line %i/%i), data = "
 
 176                        "%02x %02x %02x %02x  %02x %02x %02x %02x   "
 
 177                        "%02x %02x %02x %02x  %02x %02x %02x %02x\n",
 
 178                        (i / 16) + 1, (WDRTAS_LOGBUFFER_LEN / 16),
 
 179                        wdrtas_logbuffer[i + 0], wdrtas_logbuffer[i + 1], 
 
 180                        wdrtas_logbuffer[i + 2], wdrtas_logbuffer[i + 3], 
 
 181                        wdrtas_logbuffer[i + 4], wdrtas_logbuffer[i + 5], 
 
 182                        wdrtas_logbuffer[i + 6], wdrtas_logbuffer[i + 7], 
 
 183                        wdrtas_logbuffer[i + 8], wdrtas_logbuffer[i + 9], 
 
 184                        wdrtas_logbuffer[i + 10], wdrtas_logbuffer[i + 11], 
 
 185                        wdrtas_logbuffer[i + 12], wdrtas_logbuffer[i + 13], 
 
 186                        wdrtas_logbuffer[i + 14], wdrtas_logbuffer[i + 15]);
 
 190  * wdrtas_timer_keepalive - resets watchdog timer to keep system alive
 
 192  * wdrtas_timer_keepalive restarts the watchdog timer by calling the
 
 193  * RTAS function event-scan and repeats these calls as long as there are
 
 194  * events available. All events will be dumped.
 
 197 wdrtas_timer_keepalive(void)
 
 202                 result = rtas_call(wdrtas_token_event_scan, 4, 1, NULL,
 
 203                                    RTAS_EVENT_SCAN_ALL_EVENTS, 0,
 
 204                                    (void *)__pa(wdrtas_logbuffer),
 
 205                                    WDRTAS_LOGBUFFER_LEN);
 
 207                         printk(KERN_ERR "wdrtas: event-scan failed: %li\n",
 
 210                         wdrtas_log_scanned_event();
 
 211         } while (result == 0);
 
 215  * wdrtas_get_temperature - returns current temperature
 
 217  * returns temperature or <0 on failures
 
 219  * wdrtas_get_temperature returns the current temperature in Fahrenheit. It
 
 220  * uses the RTAS call get-sensor-state, token 3 to do so
 
 223 wdrtas_get_temperature(void)
 
 228         result = rtas_call(wdrtas_token_get_sensor_state, 2, 2,
 
 229                            (void *)__pa(&temperature),
 
 230                            WDRTAS_THERMAL_SENSOR, 0);
 
 233                 printk(KERN_WARNING "wdrtas: reading the thermal sensor "
 
 234                        "faild: %li\n", result);
 
 236                 temperature = ((temperature * 9) / 5) + 32; /* fahrenheit */
 
 242  * wdrtas_get_status - returns the status of the watchdog
 
 244  * returns a bitmask of defines WDIOF_... as defined in
 
 245  * include/linux/watchdog.h
 
 248 wdrtas_get_status(void)
 
 254  * wdrtas_get_boot_status - returns the reason for the last boot
 
 256  * returns a bitmask of defines WDIOF_... as defined in
 
 257  * include/linux/watchdog.h, indicating why the watchdog rebooted the system
 
 260 wdrtas_get_boot_status(void)
 
 265 /*** watchdog API and operations stuff */
 
 267 /* wdrtas_write - called when watchdog device is written to
 
 268  * @file: file structure
 
 269  * @buf: user buffer with data
 
 270  * @len: amount to data written
 
 271  * @ppos: position in file
 
 273  * returns the number of successfully processed characters, which is always
 
 274  * the number of bytes passed to this function
 
 276  * wdrtas_write processes all the data given to it and looks for the magic
 
 277  * character 'V'. This character allows the watchdog device to be closed
 
 281 wdrtas_write(struct file *file, const char __user *buf,
 
 282              size_t len, loff_t *ppos)
 
 290         if (!wdrtas_nowayout) {
 
 291                 wdrtas_expect_close = 0;
 
 293                 for (i = 0; i < len; i++) {
 
 294                         if (get_user(c, buf + i))
 
 296                         /* allow to close device */
 
 298                                 wdrtas_expect_close = WDRTAS_MAGIC_CHAR;
 
 302         wdrtas_timer_keepalive();
 
 309  * wdrtas_ioctl - ioctl function for the watchdog device
 
 310  * @inode: inode structure
 
 311  * @file: file structure
 
 312  * @cmd: command for ioctl
 
 313  * @arg: argument pointer
 
 315  * returns 0 on success, <0 on failure
 
 317  * wdrtas_ioctl implements the watchdog API ioctls
 
 320 wdrtas_ioctl(struct inode *inode, struct file *file,
 
 321              unsigned int cmd, unsigned long arg)
 
 323         int __user *argp = (void *)arg;
 
 325         static struct watchdog_info wdinfo = {
 
 326                 .options = WDRTAS_SUPPORTED_MASK,
 
 327                 .firmware_version = 0,
 
 332         case WDIOC_GETSUPPORT:
 
 333                 if (copy_to_user(argp, &wdinfo, sizeof(wdinfo)))
 
 337         case WDIOC_GETSTATUS:
 
 338                 i = wdrtas_get_status();
 
 339                 return put_user(i, argp);
 
 341         case WDIOC_GETBOOTSTATUS:
 
 342                 i = wdrtas_get_boot_status();
 
 343                 return put_user(i, argp);
 
 346                 if (wdrtas_token_get_sensor_state == RTAS_UNKNOWN_SERVICE)
 
 349                 i = wdrtas_get_temperature();
 
 350                 return put_user(i, argp);
 
 352         case WDIOC_SETOPTIONS:
 
 353                 if (get_user(i, argp))
 
 355                 if (i & WDIOS_DISABLECARD)
 
 357                 if (i & WDIOS_ENABLECARD) {
 
 358                         wdrtas_timer_keepalive();
 
 359                         wdrtas_timer_start();
 
 361                 if (i & WDIOS_TEMPPANIC) {
 
 362                         /* not implemented. Done by H8 */
 
 366         case WDIOC_KEEPALIVE:
 
 367                 wdrtas_timer_keepalive();
 
 370         case WDIOC_SETTIMEOUT:
 
 371                 if (get_user(i, argp))
 
 374                 if (wdrtas_set_interval(i))
 
 377                 wdrtas_timer_keepalive();
 
 379                 if (wdrtas_token_get_sp == RTAS_UNKNOWN_SERVICE)
 
 382                         wdrtas_interval = wdrtas_get_interval(i);
 
 385         case WDIOC_GETTIMEOUT:
 
 386                 return put_user(wdrtas_interval, argp);
 
 394  * wdrtas_open - open function of watchdog device
 
 395  * @inode: inode structure
 
 396  * @file: file structure
 
 398  * returns 0 on success, -EBUSY if the file has been opened already, <0 on
 
 401  * function called when watchdog device is opened
 
 404 wdrtas_open(struct inode *inode, struct file *file)
 
 407         if (atomic_inc_return(&wdrtas_miscdev_open) > 1) {
 
 408                 atomic_dec(&wdrtas_miscdev_open);
 
 412         wdrtas_timer_start();
 
 413         wdrtas_timer_keepalive();
 
 415         return nonseekable_open(inode, file);
 
 419  * wdrtas_close - close function of watchdog device
 
 420  * @inode: inode structure
 
 421  * @file: file structure
 
 423  * returns 0 on success
 
 425  * close function. Always succeeds
 
 428 wdrtas_close(struct inode *inode, struct file *file)
 
 430         /* only stop watchdog, if this was announced using 'V' before */
 
 431         if (wdrtas_expect_close == WDRTAS_MAGIC_CHAR)
 
 434                 printk(KERN_WARNING "wdrtas: got unexpected close. Watchdog "
 
 436                 wdrtas_timer_keepalive();
 
 439         wdrtas_expect_close = 0;
 
 440         atomic_dec(&wdrtas_miscdev_open);
 
 445  * wdrtas_temp_read - gives back the temperature in fahrenheit
 
 446  * @file: file structure
 
 448  * @count: number of bytes to be read
 
 449  * @ppos: position in file
 
 451  * returns always 1 or -EFAULT in case of user space copy failures, <0 on
 
 454  * wdrtas_temp_read gives the temperature to the users by copying this
 
 455  * value as one byte into the user space buffer. The unit is Fahrenheit...
 
 458 wdrtas_temp_read(struct file *file, char __user *buf,
 
 459                  size_t count, loff_t *ppos)
 
 463         temperature = wdrtas_get_temperature();
 
 467         if (copy_to_user(buf, &temperature, 1))
 
 474  * wdrtas_temp_open - open function of temperature device
 
 475  * @inode: inode structure
 
 476  * @file: file structure
 
 478  * returns 0 on success, <0 on failure
 
 480  * function called when temperature device is opened
 
 483 wdrtas_temp_open(struct inode *inode, struct file *file)
 
 485         return nonseekable_open(inode, file);
 
 489  * wdrtas_temp_close - close function of temperature device
 
 490  * @inode: inode structure
 
 491  * @file: file structure
 
 493  * returns 0 on success
 
 495  * close function. Always succeeds
 
 498 wdrtas_temp_close(struct inode *inode, struct file *file)
 
 504  * wdrtas_reboot - reboot notifier function
 
 505  * @nb: notifier block structure
 
 509  * returns NOTIFY_DONE
 
 511  * wdrtas_reboot stops the watchdog in case of a reboot
 
 514 wdrtas_reboot(struct notifier_block *this, unsigned long code, void *ptr)
 
 516         if ( (code==SYS_DOWN) || (code==SYS_HALT) )
 
 522 /*** initialization stuff */
 
 524 static struct file_operations wdrtas_fops = {
 
 525         .owner          = THIS_MODULE,
 
 527         .write          = wdrtas_write,
 
 528         .ioctl          = wdrtas_ioctl,
 
 530         .release        = wdrtas_close,
 
 533 static struct miscdevice wdrtas_miscdev = {
 
 534         .minor =        WATCHDOG_MINOR,
 
 536         .fops =         &wdrtas_fops,
 
 539 static struct file_operations wdrtas_temp_fops = {
 
 540         .owner          = THIS_MODULE,
 
 542         .read           = wdrtas_temp_read,
 
 543         .open           = wdrtas_temp_open,
 
 544         .release        = wdrtas_temp_close,
 
 547 static struct miscdevice wdrtas_tempdev = {
 
 549         .name =         "temperature",
 
 550         .fops =         &wdrtas_temp_fops,
 
 553 static struct notifier_block wdrtas_notifier = {
 
 554         .notifier_call =        wdrtas_reboot,
 
 558  * wdrtas_get_tokens - reads in RTAS tokens
 
 560  * returns 0 on succes, <0 on failure
 
 562  * wdrtas_get_tokens reads in the tokens for the RTAS calls used in
 
 563  * this watchdog driver. It tolerates, if "get-sensor-state" and
 
 564  * "ibm,get-system-parameter" are not available.
 
 567 wdrtas_get_tokens(void)
 
 569         wdrtas_token_get_sensor_state = rtas_token("get-sensor-state");
 
 570         if (wdrtas_token_get_sensor_state == RTAS_UNKNOWN_SERVICE) {
 
 571                 printk(KERN_WARNING "wdrtas: couldn't get token for "
 
 572                        "get-sensor-state. Trying to continue without "
 
 573                        "temperature support.\n");
 
 576         wdrtas_token_get_sp = rtas_token("ibm,get-system-parameter");
 
 577         if (wdrtas_token_get_sp == RTAS_UNKNOWN_SERVICE) {
 
 578                 printk(KERN_WARNING "wdrtas: couldn't get token for "
 
 579                        "ibm,get-system-parameter. Trying to continue with "
 
 580                        "a default timeout value of %i seconds.\n",
 
 581                        WDRTAS_DEFAULT_INTERVAL);
 
 584         wdrtas_token_set_indicator = rtas_token("set-indicator");
 
 585         if (wdrtas_token_set_indicator == RTAS_UNKNOWN_SERVICE) {
 
 586                 printk(KERN_ERR "wdrtas: couldn't get token for "
 
 587                        "set-indicator. Terminating watchdog code.\n");
 
 591         wdrtas_token_event_scan = rtas_token("event-scan");
 
 592         if (wdrtas_token_event_scan == RTAS_UNKNOWN_SERVICE) {
 
 593                 printk(KERN_ERR "wdrtas: couldn't get token for event-scan. "
 
 594                        "Terminating watchdog code.\n");
 
 602  * wdrtas_unregister_devs - unregisters the misc dev handlers
 
 604  * wdrtas_register_devs unregisters the watchdog and temperature watchdog
 
 608 wdrtas_unregister_devs(void)
 
 610         misc_deregister(&wdrtas_miscdev);
 
 611         if (wdrtas_token_get_sensor_state != RTAS_UNKNOWN_SERVICE)
 
 612                 misc_deregister(&wdrtas_tempdev);
 
 616  * wdrtas_register_devs - registers the misc dev handlers
 
 618  * returns 0 on succes, <0 on failure
 
 620  * wdrtas_register_devs registers the watchdog and temperature watchdog
 
 624 wdrtas_register_devs(void)
 
 628         result = misc_register(&wdrtas_miscdev);
 
 630                 printk(KERN_ERR "wdrtas: couldn't register watchdog misc "
 
 631                        "device. Terminating watchdog code.\n");
 
 635         if (wdrtas_token_get_sensor_state != RTAS_UNKNOWN_SERVICE) {
 
 636                 result = misc_register(&wdrtas_tempdev);
 
 638                         printk(KERN_WARNING "wdrtas: couldn't register "
 
 639                                "watchdog temperature misc device. Continuing "
 
 640                                "without temperature support.\n");
 
 641                         wdrtas_token_get_sensor_state = RTAS_UNKNOWN_SERVICE;
 
 649  * wdrtas_init - init function of the watchdog driver
 
 651  * returns 0 on succes, <0 on failure
 
 653  * registers the file handlers and the reboot notifier
 
 658         if (wdrtas_get_tokens())
 
 661         if (wdrtas_register_devs())
 
 664         if (register_reboot_notifier(&wdrtas_notifier)) {
 
 665                 printk(KERN_ERR "wdrtas: could not register reboot notifier. "
 
 666                        "Terminating watchdog code.\n");
 
 667                 wdrtas_unregister_devs();
 
 671         if (wdrtas_token_get_sp == RTAS_UNKNOWN_SERVICE)
 
 672                 wdrtas_interval = WDRTAS_DEFAULT_INTERVAL;
 
 674                 wdrtas_interval = wdrtas_get_interval(WDRTAS_DEFAULT_INTERVAL);
 
 680  * wdrtas_exit - exit function of the watchdog driver
 
 682  * unregisters the file handlers and the reboot notifier
 
 687         if (!wdrtas_nowayout)
 
 690         wdrtas_unregister_devs();
 
 692         unregister_reboot_notifier(&wdrtas_notifier);
 
 695 module_init(wdrtas_init);
 
 696 module_exit(wdrtas_exit);