blob: c974058e48d2a4da6976755d48d371e87b15fe90 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Watchdog implementation based on z/VM Watchdog Timer API
3 *
Christian Borntraeger58872d52009-06-16 10:30:35 +02004 * Copyright IBM Corp. 2004,2009
5 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * The user space watchdog daemon can use this driver as
7 * /dev/vmwatchdog to have z/VM execute the specified CP
8 * command when the timeout expires. The default command is
9 * "IPL", which which cause an immediate reboot.
10 */
Christian Borntraeger58872d52009-06-16 10:30:35 +020011#define KMSG_COMPONENT "vmwatchdog"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/init.h>
15#include <linux/fs.h>
16#include <linux/kernel.h>
17#include <linux/miscdevice.h>
18#include <linux/module.h>
19#include <linux/moduleparam.h>
Christian Borntraeger58872d52009-06-16 10:30:35 +020020#include <linux/suspend.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/watchdog.h>
22
23#include <asm/ebcdic.h>
24#include <asm/io.h>
25#include <asm/uaccess.h>
26
27#define MAX_CMDLEN 240
28#define MIN_INTERVAL 15
29static char vmwdt_cmd[MAX_CMDLEN] = "IPL";
30static int vmwdt_conceal;
31
Andrey Panin4bfdf372005-07-27 11:43:58 -070032static int vmwdt_nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34MODULE_LICENSE("GPL");
35MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
36MODULE_DESCRIPTION("z/VM Watchdog Timer");
37module_param_string(cmd, vmwdt_cmd, MAX_CMDLEN, 0644);
38MODULE_PARM_DESC(cmd, "CP command that is run when the watchdog triggers");
39module_param_named(conceal, vmwdt_conceal, bool, 0644);
40MODULE_PARM_DESC(conceal, "Enable the CONCEAL CP option while the watchdog "
41 " is active");
42module_param_named(nowayout, vmwdt_nowayout, bool, 0);
43MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started"
44 " (default=CONFIG_WATCHDOG_NOWAYOUT)");
45MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
46
47static unsigned int vmwdt_interval = 60;
48static unsigned long vmwdt_is_open;
49static int vmwdt_expect_close;
50
Gerald Schaeferfeb5c5a2009-12-07 12:52:19 +010051static DEFINE_MUTEX(vmwdt_mutex);
52
Christian Borntraeger58872d52009-06-16 10:30:35 +020053#define VMWDT_OPEN 0 /* devnode is open or suspend in progress */
54#define VMWDT_RUNNING 1 /* The watchdog is armed */
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056enum vmwdt_func {
57 /* function codes */
58 wdt_init = 0,
59 wdt_change = 1,
60 wdt_cancel = 2,
61 /* flags */
62 wdt_conceal = 0x80000000,
63};
64
65static int __diag288(enum vmwdt_func func, unsigned int timeout,
66 char *cmd, size_t len)
67{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020068 register unsigned long __func asm("2") = func;
69 register unsigned long __timeout asm("3") = timeout;
70 register unsigned long __cmdp asm("4") = virt_to_phys(cmd);
71 register unsigned long __cmdl asm("5") = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 int err;
73
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020074 err = -EINVAL;
75 asm volatile(
76 " diag %1,%3,0x288\n"
77 "0: la %0,0\n"
78 "1:\n"
79 EX_TABLE(0b,1b)
Heiko Carstens2b12f992007-10-12 16:11:48 +020080 : "+d" (err) : "d"(__func), "d"(__timeout),
81 "d"(__cmdp), "d"(__cmdl) : "1", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return err;
83}
84
85static int vmwdt_keepalive(void)
86{
87 /* we allocate new memory every time to avoid having
88 * to track the state. static allocation is not an
89 * option since that might not be contiguous in real
90 * storage in case of a modular build */
91 static char *ebc_cmd;
92 size_t len;
93 int ret;
94 unsigned int func;
95
96 ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL);
97 if (!ebc_cmd)
98 return -ENOMEM;
99
100 len = strlcpy(ebc_cmd, vmwdt_cmd, MAX_CMDLEN);
101 ASCEBC(ebc_cmd, MAX_CMDLEN);
102 EBC_TOUPPER(ebc_cmd, MAX_CMDLEN);
103
104 func = vmwdt_conceal ? (wdt_init | wdt_conceal) : wdt_init;
Christian Borntraeger58872d52009-06-16 10:30:35 +0200105 set_bit(VMWDT_RUNNING, &vmwdt_is_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 ret = __diag288(func, vmwdt_interval, ebc_cmd, len);
Martin Schwidefsky0d130062008-07-14 09:59:40 +0200107 WARN_ON(ret != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 kfree(ebc_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return ret;
110}
111
112static int vmwdt_disable(void)
113{
114 int ret = __diag288(wdt_cancel, 0, "", 0);
Martin Schwidefsky0d130062008-07-14 09:59:40 +0200115 WARN_ON(ret != 0);
Christian Borntraeger58872d52009-06-16 10:30:35 +0200116 clear_bit(VMWDT_RUNNING, &vmwdt_is_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return ret;
118}
119
120static int __init vmwdt_probe(void)
121{
122 /* there is no real way to see if the watchdog is supported,
123 * so we try initializing it with a NOP command ("BEGIN")
124 * that won't cause any harm even if the following disable
125 * fails for some reason */
126 static char __initdata ebc_begin[] = {
127 194, 197, 199, 201, 213
128 };
Martin Schwidefsky0d130062008-07-14 09:59:40 +0200129 if (__diag288(wdt_init, 15, ebc_begin, sizeof(ebc_begin)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return vmwdt_disable();
132}
133
134static int vmwdt_open(struct inode *i, struct file *f)
135{
136 int ret;
Gerald Schaeferfeb5c5a2009-12-07 12:52:19 +0100137 if (test_and_set_bit(VMWDT_OPEN, &vmwdt_is_open))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return -EBUSY;
139 ret = vmwdt_keepalive();
140 if (ret)
Christian Borntraeger58872d52009-06-16 10:30:35 +0200141 clear_bit(VMWDT_OPEN, &vmwdt_is_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return ret ? ret : nonseekable_open(i, f);
143}
144
145static int vmwdt_close(struct inode *i, struct file *f)
146{
147 if (vmwdt_expect_close == 42)
148 vmwdt_disable();
149 vmwdt_expect_close = 0;
Christian Borntraeger58872d52009-06-16 10:30:35 +0200150 clear_bit(VMWDT_OPEN, &vmwdt_is_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return 0;
152}
153
154static struct watchdog_info vmwdt_info = {
155 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
156 .firmware_version = 0,
157 .identity = "z/VM Watchdog Timer",
158};
159
Gerald Schaeferfeb5c5a2009-12-07 12:52:19 +0100160static int __vmwdt_ioctl(unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 switch (cmd) {
163 case WDIOC_GETSUPPORT:
164 if (copy_to_user((void __user *)arg, &vmwdt_info,
165 sizeof(vmwdt_info)))
166 return -EFAULT;
167 return 0;
168 case WDIOC_GETSTATUS:
169 case WDIOC_GETBOOTSTATUS:
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200170 return put_user(0, (int __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 case WDIOC_GETTEMP:
172 return -EINVAL;
173 case WDIOC_SETOPTIONS:
174 {
175 int options, ret;
176 if (get_user(options, (int __user *)arg))
177 return -EFAULT;
178 ret = -EINVAL;
179 if (options & WDIOS_DISABLECARD) {
180 ret = vmwdt_disable();
181 if (ret)
182 return ret;
183 }
184 if (options & WDIOS_ENABLECARD) {
185 ret = vmwdt_keepalive();
186 }
187 return ret;
188 }
189 case WDIOC_GETTIMEOUT:
190 return put_user(vmwdt_interval, (int __user *)arg);
191 case WDIOC_SETTIMEOUT:
192 {
193 int interval;
194 if (get_user(interval, (int __user *)arg))
195 return -EFAULT;
196 if (interval < MIN_INTERVAL)
197 return -EINVAL;
198 vmwdt_interval = interval;
199 }
200 return vmwdt_keepalive();
201 case WDIOC_KEEPALIVE:
202 return vmwdt_keepalive();
203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return -EINVAL;
205}
206
Gerald Schaeferfeb5c5a2009-12-07 12:52:19 +0100207static long vmwdt_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
208{
209 int rc;
210
211 mutex_lock(&vmwdt_mutex);
212 rc = __vmwdt_ioctl(cmd, arg);
213 mutex_unlock(&vmwdt_mutex);
214 return (long) rc;
215}
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217static ssize_t vmwdt_write(struct file *f, const char __user *buf,
218 size_t count, loff_t *ppos)
219{
220 if(count) {
221 if (!vmwdt_nowayout) {
222 size_t i;
223
224 /* note: just in case someone wrote the magic character
225 * five months ago... */
226 vmwdt_expect_close = 0;
227
228 for (i = 0; i != count; i++) {
229 char c;
230 if (get_user(c, buf+i))
231 return -EFAULT;
232 if (c == 'V')
233 vmwdt_expect_close = 42;
234 }
235 }
236 /* someone wrote to us, we should restart timer */
237 vmwdt_keepalive();
238 }
239 return count;
240}
241
Christian Borntraeger58872d52009-06-16 10:30:35 +0200242static int vmwdt_resume(void)
243{
244 clear_bit(VMWDT_OPEN, &vmwdt_is_open);
245 return NOTIFY_DONE;
246}
247
248/*
249 * It makes no sense to go into suspend while the watchdog is running.
250 * Depending on the memory size, the watchdog might trigger, while we
251 * are still saving the memory.
252 * We reuse the open flag to ensure that suspend and watchdog open are
253 * exclusive operations
254 */
255static int vmwdt_suspend(void)
256{
257 if (test_and_set_bit(VMWDT_OPEN, &vmwdt_is_open)) {
Christian Borntraeger2c48c4d2009-07-07 16:37:11 +0200258 pr_err("The system cannot be suspended while the watchdog"
259 " is in use\n");
Christian Borntraeger58872d52009-06-16 10:30:35 +0200260 return NOTIFY_BAD;
261 }
262 if (test_bit(VMWDT_RUNNING, &vmwdt_is_open)) {
263 clear_bit(VMWDT_OPEN, &vmwdt_is_open);
Christian Borntraeger2c48c4d2009-07-07 16:37:11 +0200264 pr_err("The system cannot be suspended while the watchdog"
265 " is running\n");
Christian Borntraeger58872d52009-06-16 10:30:35 +0200266 return NOTIFY_BAD;
267 }
268 return NOTIFY_DONE;
269}
270
271/*
272 * This function is called for suspend and resume.
273 */
274static int vmwdt_power_event(struct notifier_block *this, unsigned long event,
275 void *ptr)
276{
277 switch (event) {
278 case PM_POST_HIBERNATION:
279 case PM_POST_SUSPEND:
280 return vmwdt_resume();
281 case PM_HIBERNATION_PREPARE:
282 case PM_SUSPEND_PREPARE:
283 return vmwdt_suspend();
284 default:
285 return NOTIFY_DONE;
286 }
287}
288
289static struct notifier_block vmwdt_power_notifier = {
290 .notifier_call = vmwdt_power_event,
291};
292
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800293static const struct file_operations vmwdt_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 .open = &vmwdt_open,
295 .release = &vmwdt_close,
Gerald Schaeferfeb5c5a2009-12-07 12:52:19 +0100296 .unlocked_ioctl = &vmwdt_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 .write = &vmwdt_write,
298 .owner = THIS_MODULE,
299};
300
301static struct miscdevice vmwdt_dev = {
302 .minor = WATCHDOG_MINOR,
303 .name = "watchdog",
304 .fops = &vmwdt_fops,
305};
306
307static int __init vmwdt_init(void)
308{
309 int ret;
310
311 ret = vmwdt_probe();
312 if (ret)
313 return ret;
Christian Borntraeger58872d52009-06-16 10:30:35 +0200314 ret = register_pm_notifier(&vmwdt_power_notifier);
315 if (ret)
316 return ret;
Gerald Schaeferfeb5c5a2009-12-07 12:52:19 +0100317 /*
318 * misc_register() has to be the last action in module_init(), because
319 * file operations will be available right after this.
320 */
Christian Borntraeger58872d52009-06-16 10:30:35 +0200321 ret = misc_register(&vmwdt_dev);
322 if (ret) {
323 unregister_pm_notifier(&vmwdt_power_notifier);
324 return ret;
325 }
326 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328module_init(vmwdt_init);
329
330static void __exit vmwdt_exit(void)
331{
Christian Borntraeger58872d52009-06-16 10:30:35 +0200332 unregister_pm_notifier(&vmwdt_power_notifier);
333 misc_deregister(&vmwdt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335module_exit(vmwdt_exit);