blob: d5eac985976b42b5b5617f5f14c762b505a8ece0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Watchdog implementation based on z/VM Watchdog Timer API
3 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 2004, 2009
Christian Borntraeger58872d5f2009-06-16 10:30:35 +02005 *
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 Borntraeger58872d5f2009-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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Christian Borntraeger58872d5f2009-06-16 10:30:35 +020021#include <linux/suspend.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/watchdog.h>
23
24#include <asm/ebcdic.h>
25#include <asm/io.h>
26#include <asm/uaccess.h>
27
28#define MAX_CMDLEN 240
29#define MIN_INTERVAL 15
30static char vmwdt_cmd[MAX_CMDLEN] = "IPL";
Rusty Russell90ab5ee2012-01-13 09:32:20 +103031static bool vmwdt_conceal;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Rusty Russell90ab5ee2012-01-13 09:32:20 +103033static bool vmwdt_nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35MODULE_LICENSE("GPL");
36MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
37MODULE_DESCRIPTION("z/VM Watchdog Timer");
38module_param_string(cmd, vmwdt_cmd, MAX_CMDLEN, 0644);
39MODULE_PARM_DESC(cmd, "CP command that is run when the watchdog triggers");
40module_param_named(conceal, vmwdt_conceal, bool, 0644);
41MODULE_PARM_DESC(conceal, "Enable the CONCEAL CP option while the watchdog "
42 " is active");
43module_param_named(nowayout, vmwdt_nowayout, bool, 0);
44MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started"
45 " (default=CONFIG_WATCHDOG_NOWAYOUT)");
46MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
47
48static unsigned int vmwdt_interval = 60;
49static unsigned long vmwdt_is_open;
50static int vmwdt_expect_close;
51
Gerald Schaeferfeb5c5a2009-12-07 12:52:19 +010052static DEFINE_MUTEX(vmwdt_mutex);
53
Christian Borntraeger58872d5f2009-06-16 10:30:35 +020054#define VMWDT_OPEN 0 /* devnode is open or suspend in progress */
55#define VMWDT_RUNNING 1 /* The watchdog is armed */
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057enum vmwdt_func {
58 /* function codes */
59 wdt_init = 0,
60 wdt_change = 1,
61 wdt_cancel = 2,
62 /* flags */
63 wdt_conceal = 0x80000000,
64};
65
66static int __diag288(enum vmwdt_func func, unsigned int timeout,
67 char *cmd, size_t len)
68{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020069 register unsigned long __func asm("2") = func;
70 register unsigned long __timeout asm("3") = timeout;
71 register unsigned long __cmdp asm("4") = virt_to_phys(cmd);
72 register unsigned long __cmdl asm("5") = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 int err;
74
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020075 err = -EINVAL;
76 asm volatile(
77 " diag %1,%3,0x288\n"
78 "0: la %0,0\n"
79 "1:\n"
80 EX_TABLE(0b,1b)
Heiko Carstens2b12f992007-10-12 16:11:48 +020081 : "+d" (err) : "d"(__func), "d"(__timeout),
82 "d"(__cmdp), "d"(__cmdl) : "1", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return err;
84}
85
86static int vmwdt_keepalive(void)
87{
88 /* we allocate new memory every time to avoid having
89 * to track the state. static allocation is not an
90 * option since that might not be contiguous in real
91 * storage in case of a modular build */
92 static char *ebc_cmd;
93 size_t len;
94 int ret;
95 unsigned int func;
96
97 ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL);
98 if (!ebc_cmd)
99 return -ENOMEM;
100
101 len = strlcpy(ebc_cmd, vmwdt_cmd, MAX_CMDLEN);
102 ASCEBC(ebc_cmd, MAX_CMDLEN);
103 EBC_TOUPPER(ebc_cmd, MAX_CMDLEN);
104
105 func = vmwdt_conceal ? (wdt_init | wdt_conceal) : wdt_init;
Christian Borntraeger58872d5f2009-06-16 10:30:35 +0200106 set_bit(VMWDT_RUNNING, &vmwdt_is_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 ret = __diag288(func, vmwdt_interval, ebc_cmd, len);
Martin Schwidefsky0d130062008-07-14 09:59:40 +0200108 WARN_ON(ret != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 kfree(ebc_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return ret;
111}
112
113static int vmwdt_disable(void)
114{
Sebastian Ott739737e2013-06-25 15:34:54 +0200115 char cmd[] = {'\0'};
116 int ret = __diag288(wdt_cancel, 0, cmd, 0);
Martin Schwidefsky0d130062008-07-14 09:59:40 +0200117 WARN_ON(ret != 0);
Christian Borntraeger58872d5f2009-06-16 10:30:35 +0200118 clear_bit(VMWDT_RUNNING, &vmwdt_is_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return ret;
120}
121
122static int __init vmwdt_probe(void)
123{
124 /* there is no real way to see if the watchdog is supported,
125 * so we try initializing it with a NOP command ("BEGIN")
126 * that won't cause any harm even if the following disable
127 * fails for some reason */
Sebastian Ott739737e2013-06-25 15:34:54 +0200128 char ebc_begin[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 194, 197, 199, 201, 213
130 };
Martin Schwidefsky0d130062008-07-14 09:59:40 +0200131 if (__diag288(wdt_init, 15, ebc_begin, sizeof(ebc_begin)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return vmwdt_disable();
134}
135
136static int vmwdt_open(struct inode *i, struct file *f)
137{
138 int ret;
Gerald Schaeferfeb5c5a2009-12-07 12:52:19 +0100139 if (test_and_set_bit(VMWDT_OPEN, &vmwdt_is_open))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return -EBUSY;
141 ret = vmwdt_keepalive();
142 if (ret)
Christian Borntraeger58872d5f2009-06-16 10:30:35 +0200143 clear_bit(VMWDT_OPEN, &vmwdt_is_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return ret ? ret : nonseekable_open(i, f);
145}
146
147static int vmwdt_close(struct inode *i, struct file *f)
148{
149 if (vmwdt_expect_close == 42)
150 vmwdt_disable();
151 vmwdt_expect_close = 0;
Christian Borntraeger58872d5f2009-06-16 10:30:35 +0200152 clear_bit(VMWDT_OPEN, &vmwdt_is_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return 0;
154}
155
156static struct watchdog_info vmwdt_info = {
157 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
158 .firmware_version = 0,
159 .identity = "z/VM Watchdog Timer",
160};
161
Gerald Schaeferfeb5c5a2009-12-07 12:52:19 +0100162static int __vmwdt_ioctl(unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 switch (cmd) {
165 case WDIOC_GETSUPPORT:
166 if (copy_to_user((void __user *)arg, &vmwdt_info,
167 sizeof(vmwdt_info)))
168 return -EFAULT;
169 return 0;
170 case WDIOC_GETSTATUS:
171 case WDIOC_GETBOOTSTATUS:
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200172 return put_user(0, (int __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 case WDIOC_GETTEMP:
174 return -EINVAL;
175 case WDIOC_SETOPTIONS:
176 {
177 int options, ret;
178 if (get_user(options, (int __user *)arg))
179 return -EFAULT;
180 ret = -EINVAL;
181 if (options & WDIOS_DISABLECARD) {
182 ret = vmwdt_disable();
183 if (ret)
184 return ret;
185 }
186 if (options & WDIOS_ENABLECARD) {
187 ret = vmwdt_keepalive();
188 }
189 return ret;
190 }
191 case WDIOC_GETTIMEOUT:
192 return put_user(vmwdt_interval, (int __user *)arg);
193 case WDIOC_SETTIMEOUT:
194 {
195 int interval;
196 if (get_user(interval, (int __user *)arg))
197 return -EFAULT;
198 if (interval < MIN_INTERVAL)
199 return -EINVAL;
200 vmwdt_interval = interval;
201 }
202 return vmwdt_keepalive();
203 case WDIOC_KEEPALIVE:
204 return vmwdt_keepalive();
205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return -EINVAL;
207}
208
Gerald Schaeferfeb5c5a2009-12-07 12:52:19 +0100209static long vmwdt_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
210{
211 int rc;
212
213 mutex_lock(&vmwdt_mutex);
214 rc = __vmwdt_ioctl(cmd, arg);
215 mutex_unlock(&vmwdt_mutex);
216 return (long) rc;
217}
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219static ssize_t vmwdt_write(struct file *f, const char __user *buf,
220 size_t count, loff_t *ppos)
221{
222 if(count) {
223 if (!vmwdt_nowayout) {
224 size_t i;
225
226 /* note: just in case someone wrote the magic character
227 * five months ago... */
228 vmwdt_expect_close = 0;
229
230 for (i = 0; i != count; i++) {
231 char c;
232 if (get_user(c, buf+i))
233 return -EFAULT;
234 if (c == 'V')
235 vmwdt_expect_close = 42;
236 }
237 }
238 /* someone wrote to us, we should restart timer */
239 vmwdt_keepalive();
240 }
241 return count;
242}
243
Christian Borntraeger58872d5f2009-06-16 10:30:35 +0200244static int vmwdt_resume(void)
245{
246 clear_bit(VMWDT_OPEN, &vmwdt_is_open);
247 return NOTIFY_DONE;
248}
249
250/*
251 * It makes no sense to go into suspend while the watchdog is running.
252 * Depending on the memory size, the watchdog might trigger, while we
253 * are still saving the memory.
254 * We reuse the open flag to ensure that suspend and watchdog open are
255 * exclusive operations
256 */
257static int vmwdt_suspend(void)
258{
259 if (test_and_set_bit(VMWDT_OPEN, &vmwdt_is_open)) {
Christian Borntraeger2c48c4d2009-07-07 16:37:11 +0200260 pr_err("The system cannot be suspended while the watchdog"
261 " is in use\n");
Akinobu Mitaf0c077a2011-07-08 20:53:36 +0200262 return notifier_from_errno(-EBUSY);
Christian Borntraeger58872d5f2009-06-16 10:30:35 +0200263 }
264 if (test_bit(VMWDT_RUNNING, &vmwdt_is_open)) {
265 clear_bit(VMWDT_OPEN, &vmwdt_is_open);
Christian Borntraeger2c48c4d2009-07-07 16:37:11 +0200266 pr_err("The system cannot be suspended while the watchdog"
267 " is running\n");
Akinobu Mitaf0c077a2011-07-08 20:53:36 +0200268 return notifier_from_errno(-EBUSY);
Christian Borntraeger58872d5f2009-06-16 10:30:35 +0200269 }
270 return NOTIFY_DONE;
271}
272
273/*
274 * This function is called for suspend and resume.
275 */
276static int vmwdt_power_event(struct notifier_block *this, unsigned long event,
277 void *ptr)
278{
279 switch (event) {
280 case PM_POST_HIBERNATION:
281 case PM_POST_SUSPEND:
282 return vmwdt_resume();
283 case PM_HIBERNATION_PREPARE:
284 case PM_SUSPEND_PREPARE:
285 return vmwdt_suspend();
286 default:
287 return NOTIFY_DONE;
288 }
289}
290
291static struct notifier_block vmwdt_power_notifier = {
292 .notifier_call = vmwdt_power_event,
293};
294
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800295static const struct file_operations vmwdt_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 .open = &vmwdt_open,
297 .release = &vmwdt_close,
Gerald Schaeferfeb5c5a2009-12-07 12:52:19 +0100298 .unlocked_ioctl = &vmwdt_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 .write = &vmwdt_write,
300 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200301 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302};
303
304static struct miscdevice vmwdt_dev = {
305 .minor = WATCHDOG_MINOR,
306 .name = "watchdog",
307 .fops = &vmwdt_fops,
308};
309
310static int __init vmwdt_init(void)
311{
312 int ret;
313
314 ret = vmwdt_probe();
315 if (ret)
316 return ret;
Christian Borntraeger58872d5f2009-06-16 10:30:35 +0200317 ret = register_pm_notifier(&vmwdt_power_notifier);
318 if (ret)
319 return ret;
Gerald Schaeferfeb5c5a2009-12-07 12:52:19 +0100320 /*
321 * misc_register() has to be the last action in module_init(), because
322 * file operations will be available right after this.
323 */
Christian Borntraeger58872d5f2009-06-16 10:30:35 +0200324 ret = misc_register(&vmwdt_dev);
325 if (ret) {
326 unregister_pm_notifier(&vmwdt_power_notifier);
327 return ret;
328 }
329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331module_init(vmwdt_init);
332
333static void __exit vmwdt_exit(void)
334{
Christian Borntraeger58872d5f2009-06-16 10:30:35 +0200335 unregister_pm_notifier(&vmwdt_power_notifier);
336 misc_deregister(&vmwdt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338module_exit(vmwdt_exit);