2  * Watchdog implementation based on z/VM Watchdog Timer API
 
   4  * The user space watchdog daemon can use this driver as
 
   5  * /dev/vmwatchdog to have z/VM execute the specified CP
 
   6  * command when the timeout expires. The default command is
 
   7  * "IPL", which which cause an immediate reboot.
 
   9 #include <linux/init.h>
 
  11 #include <linux/kernel.h>
 
  12 #include <linux/miscdevice.h>
 
  13 #include <linux/module.h>
 
  14 #include <linux/moduleparam.h>
 
  15 #include <linux/watchdog.h>
 
  17 #include <asm/ebcdic.h>
 
  19 #include <asm/uaccess.h>
 
  21 #define MAX_CMDLEN 240
 
  22 #define MIN_INTERVAL 15
 
  23 static char vmwdt_cmd[MAX_CMDLEN] = "IPL";
 
  24 static int vmwdt_conceal;
 
  26 static int vmwdt_nowayout = WATCHDOG_NOWAYOUT;
 
  28 MODULE_LICENSE("GPL");
 
  29 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
 
  30 MODULE_DESCRIPTION("z/VM Watchdog Timer");
 
  31 module_param_string(cmd, vmwdt_cmd, MAX_CMDLEN, 0644);
 
  32 MODULE_PARM_DESC(cmd, "CP command that is run when the watchdog triggers");
 
  33 module_param_named(conceal, vmwdt_conceal, bool, 0644);
 
  34 MODULE_PARM_DESC(conceal, "Enable the CONCEAL CP option while the watchdog "
 
  36 module_param_named(nowayout, vmwdt_nowayout, bool, 0);
 
  37 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started"
 
  38                 " (default=CONFIG_WATCHDOG_NOWAYOUT)");
 
  39 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
 
  41 static unsigned int vmwdt_interval = 60;
 
  42 static unsigned long vmwdt_is_open;
 
  43 static int vmwdt_expect_close;
 
  51         wdt_conceal = 0x80000000,
 
  54 static int __diag288(enum vmwdt_func func, unsigned int timeout,
 
  55                             char *cmd, size_t len)
 
  57         register unsigned long __func asm("2") = func;
 
  58         register unsigned long __timeout asm("3") = timeout;
 
  59         register unsigned long __cmdp asm("4") = virt_to_phys(cmd);
 
  60         register unsigned long __cmdl asm("5") = len;
 
  69                 : "=d" (err) : "d"(__func), "d"(__timeout),
 
  70                   "d"(__cmdp), "d"(__cmdl), "0" (-EINVAL) : "1", "cc");
 
  74 static int vmwdt_keepalive(void)
 
  76         /* we allocate new memory every time to avoid having
 
  77          * to track the state. static allocation is not an
 
  78          * option since that might not be contiguous in real
 
  79          * storage in case of a modular build */
 
  85         ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL);
 
  89         len = strlcpy(ebc_cmd, vmwdt_cmd, MAX_CMDLEN);
 
  90         ASCEBC(ebc_cmd, MAX_CMDLEN);
 
  91         EBC_TOUPPER(ebc_cmd, MAX_CMDLEN);
 
  93         func = vmwdt_conceal ? (wdt_init | wdt_conceal) : wdt_init;
 
  94         ret = __diag288(func, vmwdt_interval, ebc_cmd, len);
 
  98                 printk(KERN_WARNING "%s: problem setting interval %d, "
 
  99                         "cmd %s\n", __FUNCTION__, vmwdt_interval,
 
 105 static int vmwdt_disable(void)
 
 107         int ret = __diag288(wdt_cancel, 0, "", 0);
 
 109                 printk(KERN_WARNING "%s: problem disabling watchdog\n",
 
 115 static int __init vmwdt_probe(void)
 
 117         /* there is no real way to see if the watchdog is supported,
 
 118          * so we try initializing it with a NOP command ("BEGIN")
 
 119          * that won't cause any harm even if the following disable
 
 120          * fails for some reason */
 
 121         static char __initdata ebc_begin[] = {
 
 122                 194, 197, 199, 201, 213
 
 124         if (__diag288(wdt_init, 15, ebc_begin, sizeof(ebc_begin)) != 0) {
 
 125                 printk(KERN_INFO "z/VM watchdog not available\n");
 
 128         return vmwdt_disable();
 
 131 static int vmwdt_open(struct inode *i, struct file *f)
 
 134         if (test_and_set_bit(0, &vmwdt_is_open))
 
 136         ret = vmwdt_keepalive();
 
 138                 clear_bit(0, &vmwdt_is_open);
 
 139         return ret ? ret : nonseekable_open(i, f);
 
 142 static int vmwdt_close(struct inode *i, struct file *f)
 
 144         if (vmwdt_expect_close == 42)
 
 146         vmwdt_expect_close = 0;
 
 147         clear_bit(0, &vmwdt_is_open);
 
 151 static struct watchdog_info vmwdt_info = {
 
 152         .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
 
 153         .firmware_version = 0,
 
 154         .identity = "z/VM Watchdog Timer",
 
 157 static int vmwdt_ioctl(struct inode *i, struct file *f,
 
 158                           unsigned int cmd, unsigned long arg)
 
 161         case WDIOC_GETSUPPORT:
 
 162                 if (copy_to_user((void __user *)arg, &vmwdt_info,
 
 166         case WDIOC_GETSTATUS:
 
 167         case WDIOC_GETBOOTSTATUS:
 
 168                 return put_user(0, (int __user *)arg);
 
 171         case WDIOC_SETOPTIONS:
 
 174                         if (get_user(options, (int __user *)arg))
 
 177                         if (options & WDIOS_DISABLECARD) {
 
 178                                 ret = vmwdt_disable();
 
 182                         if (options & WDIOS_ENABLECARD) {
 
 183                                 ret = vmwdt_keepalive();
 
 187         case WDIOC_GETTIMEOUT:
 
 188                 return put_user(vmwdt_interval, (int __user *)arg);
 
 189         case WDIOC_SETTIMEOUT:
 
 192                         if (get_user(interval, (int __user *)arg))
 
 194                         if (interval < MIN_INTERVAL)
 
 196                         vmwdt_interval = interval;
 
 198                 return vmwdt_keepalive();
 
 199         case WDIOC_KEEPALIVE:
 
 200                 return vmwdt_keepalive();
 
 206 static ssize_t vmwdt_write(struct file *f, const char __user *buf,
 
 207                                 size_t count, loff_t *ppos)
 
 210                 if (!vmwdt_nowayout) {
 
 213                         /* note: just in case someone wrote the magic character
 
 214                          * five months ago... */
 
 215                         vmwdt_expect_close = 0;
 
 217                         for (i = 0; i != count; i++) {
 
 219                                 if (get_user(c, buf+i))
 
 222                                         vmwdt_expect_close = 42;
 
 225                 /* someone wrote to us, we should restart timer */
 
 231 static struct file_operations vmwdt_fops = {
 
 233         .release = &vmwdt_close,
 
 234         .ioctl   = &vmwdt_ioctl,
 
 235         .write   = &vmwdt_write,
 
 236         .owner   = THIS_MODULE,
 
 239 static struct miscdevice vmwdt_dev = {
 
 240         .minor      = WATCHDOG_MINOR,
 
 245 static int __init vmwdt_init(void)
 
 252         return misc_register(&vmwdt_dev);
 
 254 module_init(vmwdt_init);
 
 256 static void __exit vmwdt_exit(void)
 
 258         WARN_ON(misc_deregister(&vmwdt_dev) != 0);
 
 260 module_exit(vmwdt_exit);