Philipp Hachtmann | f7a94db | 2014-06-05 11:01:43 +0200 | [diff] [blame] | 1 | /* |
Philipp Hachtmann | 646f919 | 2014-06-05 11:02:36 +0200 | [diff] [blame] | 2 | * Watchdog driver for z/VM and LPAR using the diag 288 interface. |
Philipp Hachtmann | f7a94db | 2014-06-05 11:01:43 +0200 | [diff] [blame] | 3 | * |
| 4 | * Under z/VM, expiration of the watchdog will send a "system restart" command |
| 5 | * to CP. |
| 6 | * |
Philipp Hachtmann | 646f919 | 2014-06-05 11:02:36 +0200 | [diff] [blame] | 7 | * The command can be altered using the module parameter "cmd". This is |
| 8 | * not recommended because it's only supported on z/VM but not whith LPAR. |
| 9 | * |
| 10 | * On LPAR, the watchdog will always trigger a system restart. the module |
| 11 | * paramter cmd is meaningless here. |
| 12 | * |
Philipp Hachtmann | f7a94db | 2014-06-05 11:01:43 +0200 | [diff] [blame] | 13 | * |
| 14 | * Copyright IBM Corp. 2004, 2013 |
| 15 | * Author(s): Arnd Bergmann (arndb@de.ibm.com) |
| 16 | * Philipp Hachtmann (phacht@de.ibm.com) |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #define KMSG_COMPONENT "diag288_wdt" |
| 21 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 22 | |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/moduleparam.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/miscdevice.h> |
| 29 | #include <linux/watchdog.h> |
| 30 | #include <linux/suspend.h> |
| 31 | #include <asm/ebcdic.h> |
| 32 | #include <linux/io.h> |
| 33 | #include <linux/uaccess.h> |
| 34 | |
| 35 | #define MAX_CMDLEN 240 |
| 36 | #define DEFAULT_CMD "SYSTEM RESTART" |
| 37 | |
| 38 | #define MIN_INTERVAL 15 /* Minimal time supported by diag88 */ |
| 39 | #define MAX_INTERVAL 3600 /* One hour should be enough - pure estimation */ |
| 40 | |
| 41 | #define WDT_DEFAULT_TIMEOUT 30 |
| 42 | |
| 43 | /* Function codes - init, change, cancel */ |
| 44 | #define WDT_FUNC_INIT 0 |
| 45 | #define WDT_FUNC_CHANGE 1 |
| 46 | #define WDT_FUNC_CANCEL 2 |
| 47 | #define WDT_FUNC_CONCEAL 0x80000000 |
| 48 | |
Philipp Hachtmann | 646f919 | 2014-06-05 11:02:36 +0200 | [diff] [blame] | 49 | /* Action codes for LPAR watchdog */ |
| 50 | #define LPARWDT_RESTART 0 |
| 51 | |
Philipp Hachtmann | f7a94db | 2014-06-05 11:01:43 +0200 | [diff] [blame] | 52 | static char wdt_cmd[MAX_CMDLEN] = DEFAULT_CMD; |
| 53 | static bool conceal_on; |
| 54 | static bool nowayout_info = WATCHDOG_NOWAYOUT; |
| 55 | |
| 56 | MODULE_LICENSE("GPL"); |
| 57 | MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>"); |
| 58 | MODULE_AUTHOR("Philipp Hachtmann <phacht@de.ibm.com>"); |
| 59 | |
| 60 | MODULE_DESCRIPTION("System z diag288 Watchdog Timer"); |
| 61 | |
| 62 | module_param_string(cmd, wdt_cmd, MAX_CMDLEN, 0644); |
| 63 | MODULE_PARM_DESC(cmd, "CP command that is run when the watchdog triggers (z/VM only)"); |
| 64 | |
| 65 | module_param_named(conceal, conceal_on, bool, 0644); |
| 66 | MODULE_PARM_DESC(conceal, "Enable the CONCEAL CP option while the watchdog is active (z/VM only)"); |
| 67 | |
| 68 | module_param_named(nowayout, nowayout_info, bool, 0444); |
| 69 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default = CONFIG_WATCHDOG_NOWAYOUT)"); |
| 70 | |
| 71 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |
| 72 | MODULE_ALIAS("vmwatchdog"); |
| 73 | |
| 74 | static int __diag288(unsigned int func, unsigned int timeout, |
| 75 | unsigned long action, unsigned int len) |
| 76 | { |
| 77 | register unsigned long __func asm("2") = func; |
| 78 | register unsigned long __timeout asm("3") = timeout; |
| 79 | register unsigned long __action asm("4") = action; |
| 80 | register unsigned long __len asm("5") = len; |
| 81 | int err; |
| 82 | |
| 83 | err = -EINVAL; |
| 84 | asm volatile( |
| 85 | " diag %1, %3, 0x288\n" |
| 86 | "0: la %0, 0\n" |
| 87 | "1:\n" |
| 88 | EX_TABLE(0b, 1b) |
| 89 | : "+d" (err) : "d"(__func), "d"(__timeout), |
| 90 | "d"(__action), "d"(__len) : "1", "cc"); |
| 91 | return err; |
| 92 | } |
| 93 | |
| 94 | static int __diag288_vm(unsigned int func, unsigned int timeout, |
| 95 | char *cmd, size_t len) |
| 96 | { |
| 97 | return __diag288(func, timeout, virt_to_phys(cmd), len); |
| 98 | } |
| 99 | |
Philipp Hachtmann | 646f919 | 2014-06-05 11:02:36 +0200 | [diff] [blame] | 100 | static int __diag288_lpar(unsigned int func, unsigned int timeout, |
| 101 | unsigned long action) |
| 102 | { |
| 103 | return __diag288(func, timeout, action, 0); |
| 104 | } |
| 105 | |
Philipp Hachtmann | f7a94db | 2014-06-05 11:01:43 +0200 | [diff] [blame] | 106 | static int wdt_start(struct watchdog_device *dev) |
| 107 | { |
| 108 | char *ebc_cmd; |
| 109 | size_t len; |
| 110 | int ret; |
| 111 | unsigned int func; |
| 112 | |
| 113 | ret = -ENODEV; |
| 114 | |
| 115 | if (MACHINE_IS_VM) { |
| 116 | ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL); |
| 117 | if (!ebc_cmd) |
| 118 | return -ENOMEM; |
| 119 | len = strlcpy(ebc_cmd, wdt_cmd, MAX_CMDLEN); |
| 120 | ASCEBC(ebc_cmd, MAX_CMDLEN); |
| 121 | EBC_TOUPPER(ebc_cmd, MAX_CMDLEN); |
| 122 | |
| 123 | func = conceal_on ? (WDT_FUNC_INIT | WDT_FUNC_CONCEAL) |
| 124 | : WDT_FUNC_INIT; |
| 125 | ret = __diag288_vm(func, dev->timeout, ebc_cmd, len); |
| 126 | WARN_ON(ret != 0); |
| 127 | kfree(ebc_cmd); |
Xu Wang | b2527d2 | 2015-03-06 16:26:30 +0800 | [diff] [blame] | 128 | } else { |
Philipp Hachtmann | 646f919 | 2014-06-05 11:02:36 +0200 | [diff] [blame] | 129 | ret = __diag288_lpar(WDT_FUNC_INIT, |
| 130 | dev->timeout, LPARWDT_RESTART); |
| 131 | } |
| 132 | |
Philipp Hachtmann | f7a94db | 2014-06-05 11:01:43 +0200 | [diff] [blame] | 133 | if (ret) { |
| 134 | pr_err("The watchdog cannot be activated\n"); |
| 135 | return ret; |
| 136 | } |
Philipp Hachtmann | f7a94db | 2014-06-05 11:01:43 +0200 | [diff] [blame] | 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static int wdt_stop(struct watchdog_device *dev) |
| 141 | { |
| 142 | int ret; |
| 143 | |
| 144 | ret = __diag288(WDT_FUNC_CANCEL, 0, 0, 0); |
Philipp Hachtmann | f7a94db | 2014-06-05 11:01:43 +0200 | [diff] [blame] | 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | static int wdt_ping(struct watchdog_device *dev) |
| 149 | { |
| 150 | char *ebc_cmd; |
| 151 | size_t len; |
| 152 | int ret; |
| 153 | unsigned int func; |
| 154 | |
| 155 | ret = -ENODEV; |
| 156 | |
| 157 | if (MACHINE_IS_VM) { |
| 158 | ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL); |
| 159 | if (!ebc_cmd) |
| 160 | return -ENOMEM; |
| 161 | len = strlcpy(ebc_cmd, wdt_cmd, MAX_CMDLEN); |
| 162 | ASCEBC(ebc_cmd, MAX_CMDLEN); |
| 163 | EBC_TOUPPER(ebc_cmd, MAX_CMDLEN); |
| 164 | |
| 165 | /* |
| 166 | * It seems to be ok to z/VM to use the init function to |
Philipp Hachtmann | 646f919 | 2014-06-05 11:02:36 +0200 | [diff] [blame] | 167 | * retrigger the watchdog. On LPAR WDT_FUNC_CHANGE must |
| 168 | * be used when the watchdog is running. |
Philipp Hachtmann | f7a94db | 2014-06-05 11:01:43 +0200 | [diff] [blame] | 169 | */ |
| 170 | func = conceal_on ? (WDT_FUNC_INIT | WDT_FUNC_CONCEAL) |
| 171 | : WDT_FUNC_INIT; |
| 172 | |
| 173 | ret = __diag288_vm(func, dev->timeout, ebc_cmd, len); |
| 174 | WARN_ON(ret != 0); |
| 175 | kfree(ebc_cmd); |
Xu Wang | b2527d2 | 2015-03-06 16:26:30 +0800 | [diff] [blame] | 176 | } else { |
Philipp Hachtmann | 646f919 | 2014-06-05 11:02:36 +0200 | [diff] [blame] | 177 | ret = __diag288_lpar(WDT_FUNC_CHANGE, dev->timeout, 0); |
Xu Wang | b2527d2 | 2015-03-06 16:26:30 +0800 | [diff] [blame] | 178 | } |
Philipp Hachtmann | 646f919 | 2014-06-05 11:02:36 +0200 | [diff] [blame] | 179 | |
Philipp Hachtmann | f7a94db | 2014-06-05 11:01:43 +0200 | [diff] [blame] | 180 | if (ret) |
| 181 | pr_err("The watchdog timer cannot be started or reset\n"); |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | static int wdt_set_timeout(struct watchdog_device * dev, unsigned int new_to) |
| 186 | { |
| 187 | dev->timeout = new_to; |
| 188 | return wdt_ping(dev); |
| 189 | } |
| 190 | |
| 191 | static struct watchdog_ops wdt_ops = { |
| 192 | .owner = THIS_MODULE, |
| 193 | .start = wdt_start, |
| 194 | .stop = wdt_stop, |
| 195 | .ping = wdt_ping, |
| 196 | .set_timeout = wdt_set_timeout, |
| 197 | }; |
| 198 | |
| 199 | static struct watchdog_info wdt_info = { |
Xu Wang | 9ec6cb8 | 2015-03-06 16:26:29 +0800 | [diff] [blame] | 200 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE, |
Philipp Hachtmann | f7a94db | 2014-06-05 11:01:43 +0200 | [diff] [blame] | 201 | .firmware_version = 0, |
| 202 | .identity = "z Watchdog", |
| 203 | }; |
| 204 | |
| 205 | static struct watchdog_device wdt_dev = { |
| 206 | .parent = NULL, |
| 207 | .info = &wdt_info, |
| 208 | .ops = &wdt_ops, |
| 209 | .bootstatus = 0, |
| 210 | .timeout = WDT_DEFAULT_TIMEOUT, |
| 211 | .min_timeout = MIN_INTERVAL, |
| 212 | .max_timeout = MAX_INTERVAL, |
| 213 | }; |
| 214 | |
| 215 | /* |
| 216 | * It makes no sense to go into suspend while the watchdog is running. |
| 217 | * Depending on the memory size, the watchdog might trigger, while we |
| 218 | * are still saving the memory. |
| 219 | * We reuse the open flag to ensure that suspend and watchdog open are |
| 220 | * exclusive operations |
| 221 | */ |
| 222 | static int wdt_suspend(void) |
| 223 | { |
| 224 | if (test_and_set_bit(WDOG_DEV_OPEN, &wdt_dev.status)) { |
| 225 | pr_err("Linux cannot be suspended while the watchdog is in use\n"); |
| 226 | return notifier_from_errno(-EBUSY); |
| 227 | } |
| 228 | if (test_bit(WDOG_ACTIVE, &wdt_dev.status)) { |
| 229 | clear_bit(WDOG_DEV_OPEN, &wdt_dev.status); |
| 230 | pr_err("Linux cannot be suspended while the watchdog is in use\n"); |
| 231 | return notifier_from_errno(-EBUSY); |
| 232 | } |
| 233 | return NOTIFY_DONE; |
| 234 | } |
| 235 | |
| 236 | static int wdt_resume(void) |
| 237 | { |
| 238 | clear_bit(WDOG_DEV_OPEN, &wdt_dev.status); |
| 239 | return NOTIFY_DONE; |
| 240 | } |
| 241 | |
| 242 | static int wdt_power_event(struct notifier_block *this, unsigned long event, |
| 243 | void *ptr) |
| 244 | { |
| 245 | switch (event) { |
| 246 | case PM_POST_HIBERNATION: |
| 247 | case PM_POST_SUSPEND: |
| 248 | return wdt_resume(); |
| 249 | case PM_HIBERNATION_PREPARE: |
| 250 | case PM_SUSPEND_PREPARE: |
| 251 | return wdt_suspend(); |
| 252 | default: |
| 253 | return NOTIFY_DONE; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | static struct notifier_block wdt_power_notifier = { |
| 258 | .notifier_call = wdt_power_event, |
| 259 | }; |
| 260 | |
| 261 | static int __init diag288_init(void) |
| 262 | { |
| 263 | int ret; |
| 264 | char ebc_begin[] = { |
| 265 | 194, 197, 199, 201, 213 |
| 266 | }; |
| 267 | |
| 268 | watchdog_set_nowayout(&wdt_dev, nowayout_info); |
| 269 | |
| 270 | if (MACHINE_IS_VM) { |
Philipp Hachtmann | f7a94db | 2014-06-05 11:01:43 +0200 | [diff] [blame] | 271 | if (__diag288_vm(WDT_FUNC_INIT, 15, |
| 272 | ebc_begin, sizeof(ebc_begin)) != 0) { |
| 273 | pr_err("The watchdog cannot be initialized\n"); |
| 274 | return -EINVAL; |
| 275 | } |
Xu Wang | b2527d2 | 2015-03-06 16:26:30 +0800 | [diff] [blame] | 276 | } else { |
Philipp Hachtmann | 646f919 | 2014-06-05 11:02:36 +0200 | [diff] [blame] | 277 | if (__diag288_lpar(WDT_FUNC_INIT, 30, LPARWDT_RESTART)) { |
| 278 | pr_err("The watchdog cannot be initialized\n"); |
| 279 | return -EINVAL; |
| 280 | } |
Philipp Hachtmann | f7a94db | 2014-06-05 11:01:43 +0200 | [diff] [blame] | 281 | } |
| 282 | |
Philipp Hachtmann | 646f919 | 2014-06-05 11:02:36 +0200 | [diff] [blame] | 283 | if (__diag288_lpar(WDT_FUNC_CANCEL, 0, 0)) { |
Philipp Hachtmann | f7a94db | 2014-06-05 11:01:43 +0200 | [diff] [blame] | 284 | pr_err("The watchdog cannot be deactivated\n"); |
| 285 | return -EINVAL; |
| 286 | } |
| 287 | |
| 288 | ret = register_pm_notifier(&wdt_power_notifier); |
| 289 | if (ret) |
| 290 | return ret; |
| 291 | |
| 292 | ret = watchdog_register_device(&wdt_dev); |
| 293 | if (ret) |
| 294 | unregister_pm_notifier(&wdt_power_notifier); |
| 295 | |
| 296 | return ret; |
| 297 | } |
| 298 | |
| 299 | static void __exit diag288_exit(void) |
| 300 | { |
| 301 | watchdog_unregister_device(&wdt_dev); |
| 302 | unregister_pm_notifier(&wdt_power_notifier); |
| 303 | } |
| 304 | |
| 305 | module_init(diag288_init); |
| 306 | module_exit(diag288_exit); |