Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Watchdog driver for z/VM and LPAR using the diag 288 interface.
0004  *
0005  * Under z/VM, expiration of the watchdog will send a "system restart" command
0006  * to CP.
0007  *
0008  * The command can be altered using the module parameter "cmd". This is
0009  * not recommended because it's only supported on z/VM but not whith LPAR.
0010  *
0011  * On LPAR, the watchdog will always trigger a system restart. the module
0012  * paramter cmd is meaningless here.
0013  *
0014  *
0015  * Copyright IBM Corp. 2004, 2013
0016  * Author(s): Arnd Bergmann (arndb@de.ibm.com)
0017  *        Philipp Hachtmann (phacht@de.ibm.com)
0018  *
0019  */
0020 
0021 #define KMSG_COMPONENT "diag288_wdt"
0022 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
0023 
0024 #include <linux/init.h>
0025 #include <linux/kernel.h>
0026 #include <linux/module.h>
0027 #include <linux/moduleparam.h>
0028 #include <linux/slab.h>
0029 #include <linux/watchdog.h>
0030 #include <linux/suspend.h>
0031 #include <asm/ebcdic.h>
0032 #include <asm/diag.h>
0033 #include <linux/io.h>
0034 
0035 #define MAX_CMDLEN 240
0036 #define DEFAULT_CMD "SYSTEM RESTART"
0037 
0038 #define MIN_INTERVAL 15     /* Minimal time supported by diag88 */
0039 #define MAX_INTERVAL 3600   /* One hour should be enough - pure estimation */
0040 
0041 #define WDT_DEFAULT_TIMEOUT 30
0042 
0043 /* Function codes - init, change, cancel */
0044 #define WDT_FUNC_INIT 0
0045 #define WDT_FUNC_CHANGE 1
0046 #define WDT_FUNC_CANCEL 2
0047 #define WDT_FUNC_CONCEAL 0x80000000
0048 
0049 /* Action codes for LPAR watchdog */
0050 #define LPARWDT_RESTART 0
0051 
0052 static char wdt_cmd[MAX_CMDLEN] = DEFAULT_CMD;
0053 static bool conceal_on;
0054 static bool nowayout_info = WATCHDOG_NOWAYOUT;
0055 
0056 MODULE_LICENSE("GPL");
0057 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
0058 MODULE_AUTHOR("Philipp Hachtmann <phacht@de.ibm.com>");
0059 
0060 MODULE_DESCRIPTION("System z diag288  Watchdog Timer");
0061 
0062 module_param_string(cmd, wdt_cmd, MAX_CMDLEN, 0644);
0063 MODULE_PARM_DESC(cmd, "CP command that is run when the watchdog triggers (z/VM only)");
0064 
0065 module_param_named(conceal, conceal_on, bool, 0644);
0066 MODULE_PARM_DESC(conceal, "Enable the CONCEAL CP option while the watchdog is active (z/VM only)");
0067 
0068 module_param_named(nowayout, nowayout_info, bool, 0444);
0069 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default = CONFIG_WATCHDOG_NOWAYOUT)");
0070 
0071 MODULE_ALIAS("vmwatchdog");
0072 
0073 static int __diag288(unsigned int func, unsigned int timeout,
0074              unsigned long action, unsigned int len)
0075 {
0076     register unsigned long __func asm("2") = func;
0077     register unsigned long __timeout asm("3") = timeout;
0078     register unsigned long __action asm("4") = action;
0079     register unsigned long __len asm("5") = len;
0080     int err;
0081 
0082     err = -EINVAL;
0083     asm volatile(
0084         "   diag    %1, %3, 0x288\n"
0085         "0: la  %0, 0\n"
0086         "1:\n"
0087         EX_TABLE(0b, 1b)
0088         : "+d" (err) : "d"(__func), "d"(__timeout),
0089           "d"(__action), "d"(__len) : "1", "cc");
0090     return err;
0091 }
0092 
0093 static int __diag288_vm(unsigned int  func, unsigned int timeout,
0094             char *cmd, size_t len)
0095 {
0096     diag_stat_inc(DIAG_STAT_X288);
0097     return __diag288(func, timeout, virt_to_phys(cmd), len);
0098 }
0099 
0100 static int __diag288_lpar(unsigned int func, unsigned int timeout,
0101               unsigned long action)
0102 {
0103     diag_stat_inc(DIAG_STAT_X288);
0104     return __diag288(func, timeout, action, 0);
0105 }
0106 
0107 static unsigned long wdt_status;
0108 
0109 #define DIAG_WDOG_BUSY  0
0110 
0111 static int wdt_start(struct watchdog_device *dev)
0112 {
0113     char *ebc_cmd;
0114     size_t len;
0115     int ret;
0116     unsigned int func;
0117 
0118     if (test_and_set_bit(DIAG_WDOG_BUSY, &wdt_status))
0119         return -EBUSY;
0120 
0121     if (MACHINE_IS_VM) {
0122         ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL);
0123         if (!ebc_cmd) {
0124             clear_bit(DIAG_WDOG_BUSY, &wdt_status);
0125             return -ENOMEM;
0126         }
0127         len = strlcpy(ebc_cmd, wdt_cmd, MAX_CMDLEN);
0128         ASCEBC(ebc_cmd, MAX_CMDLEN);
0129         EBC_TOUPPER(ebc_cmd, MAX_CMDLEN);
0130 
0131         func = conceal_on ? (WDT_FUNC_INIT | WDT_FUNC_CONCEAL)
0132             : WDT_FUNC_INIT;
0133         ret = __diag288_vm(func, dev->timeout, ebc_cmd, len);
0134         WARN_ON(ret != 0);
0135         kfree(ebc_cmd);
0136     } else {
0137         ret = __diag288_lpar(WDT_FUNC_INIT,
0138                      dev->timeout, LPARWDT_RESTART);
0139     }
0140 
0141     if (ret) {
0142         pr_err("The watchdog cannot be activated\n");
0143         clear_bit(DIAG_WDOG_BUSY, &wdt_status);
0144         return ret;
0145     }
0146     return 0;
0147 }
0148 
0149 static int wdt_stop(struct watchdog_device *dev)
0150 {
0151     int ret;
0152 
0153     diag_stat_inc(DIAG_STAT_X288);
0154     ret = __diag288(WDT_FUNC_CANCEL, 0, 0, 0);
0155 
0156     clear_bit(DIAG_WDOG_BUSY, &wdt_status);
0157 
0158     return ret;
0159 }
0160 
0161 static int wdt_ping(struct watchdog_device *dev)
0162 {
0163     char *ebc_cmd;
0164     size_t len;
0165     int ret;
0166     unsigned int func;
0167 
0168     if (MACHINE_IS_VM) {
0169         ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL);
0170         if (!ebc_cmd)
0171             return -ENOMEM;
0172         len = strlcpy(ebc_cmd, wdt_cmd, MAX_CMDLEN);
0173         ASCEBC(ebc_cmd, MAX_CMDLEN);
0174         EBC_TOUPPER(ebc_cmd, MAX_CMDLEN);
0175 
0176         /*
0177          * It seems to be ok to z/VM to use the init function to
0178          * retrigger the watchdog. On LPAR WDT_FUNC_CHANGE must
0179          * be used when the watchdog is running.
0180          */
0181         func = conceal_on ? (WDT_FUNC_INIT | WDT_FUNC_CONCEAL)
0182             : WDT_FUNC_INIT;
0183 
0184         ret = __diag288_vm(func, dev->timeout, ebc_cmd, len);
0185         WARN_ON(ret != 0);
0186         kfree(ebc_cmd);
0187     } else {
0188         ret = __diag288_lpar(WDT_FUNC_CHANGE, dev->timeout, 0);
0189     }
0190 
0191     if (ret)
0192         pr_err("The watchdog timer cannot be started or reset\n");
0193     return ret;
0194 }
0195 
0196 static int wdt_set_timeout(struct watchdog_device * dev, unsigned int new_to)
0197 {
0198     dev->timeout = new_to;
0199     return wdt_ping(dev);
0200 }
0201 
0202 static const struct watchdog_ops wdt_ops = {
0203     .owner = THIS_MODULE,
0204     .start = wdt_start,
0205     .stop = wdt_stop,
0206     .ping = wdt_ping,
0207     .set_timeout = wdt_set_timeout,
0208 };
0209 
0210 static const struct watchdog_info wdt_info = {
0211     .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
0212     .firmware_version = 0,
0213     .identity = "z Watchdog",
0214 };
0215 
0216 static struct watchdog_device wdt_dev = {
0217     .parent = NULL,
0218     .info = &wdt_info,
0219     .ops = &wdt_ops,
0220     .bootstatus = 0,
0221     .timeout = WDT_DEFAULT_TIMEOUT,
0222     .min_timeout = MIN_INTERVAL,
0223     .max_timeout = MAX_INTERVAL,
0224 };
0225 
0226 /*
0227  * It makes no sense to go into suspend while the watchdog is running.
0228  * Depending on the memory size, the watchdog might trigger, while we
0229  * are still saving the memory.
0230  */
0231 static int wdt_suspend(void)
0232 {
0233     if (test_and_set_bit(DIAG_WDOG_BUSY, &wdt_status)) {
0234         pr_err("Linux cannot be suspended while the watchdog is in use\n");
0235         return notifier_from_errno(-EBUSY);
0236     }
0237     return NOTIFY_DONE;
0238 }
0239 
0240 static int wdt_resume(void)
0241 {
0242     clear_bit(DIAG_WDOG_BUSY, &wdt_status);
0243     return NOTIFY_DONE;
0244 }
0245 
0246 static int wdt_power_event(struct notifier_block *this, unsigned long event,
0247                void *ptr)
0248 {
0249     switch (event) {
0250     case PM_POST_HIBERNATION:
0251     case PM_POST_SUSPEND:
0252         return wdt_resume();
0253     case PM_HIBERNATION_PREPARE:
0254     case PM_SUSPEND_PREPARE:
0255         return wdt_suspend();
0256     default:
0257         return NOTIFY_DONE;
0258     }
0259 }
0260 
0261 static struct notifier_block wdt_power_notifier = {
0262     .notifier_call = wdt_power_event,
0263 };
0264 
0265 static int __init diag288_init(void)
0266 {
0267     int ret;
0268     char ebc_begin[] = {
0269         194, 197, 199, 201, 213
0270     };
0271 
0272     watchdog_set_nowayout(&wdt_dev, nowayout_info);
0273 
0274     if (MACHINE_IS_VM) {
0275         if (__diag288_vm(WDT_FUNC_INIT, 15,
0276                  ebc_begin, sizeof(ebc_begin)) != 0) {
0277             pr_err("The watchdog cannot be initialized\n");
0278             return -EINVAL;
0279         }
0280     } else {
0281         if (__diag288_lpar(WDT_FUNC_INIT, 30, LPARWDT_RESTART)) {
0282             pr_err("The watchdog cannot be initialized\n");
0283             return -EINVAL;
0284         }
0285     }
0286 
0287     if (__diag288_lpar(WDT_FUNC_CANCEL, 0, 0)) {
0288         pr_err("The watchdog cannot be deactivated\n");
0289         return -EINVAL;
0290     }
0291 
0292     ret = register_pm_notifier(&wdt_power_notifier);
0293     if (ret)
0294         return ret;
0295 
0296     ret = watchdog_register_device(&wdt_dev);
0297     if (ret)
0298         unregister_pm_notifier(&wdt_power_notifier);
0299 
0300     return ret;
0301 }
0302 
0303 static void __exit diag288_exit(void)
0304 {
0305     watchdog_unregister_device(&wdt_dev);
0306     unregister_pm_notifier(&wdt_power_notifier);
0307 }
0308 
0309 module_init(diag288_init);
0310 module_exit(diag288_exit);