blob: 56b3eab019cbddde76219ec3fb0bfa50406089aa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Watchdog implementation based on z/VM Watchdog Timer API
3 *
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.
8 */
9#include <linux/init.h>
10#include <linux/fs.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>
16
17#include <asm/ebcdic.h>
18#include <asm/io.h>
19#include <asm/uaccess.h>
20
21#define MAX_CMDLEN 240
22#define MIN_INTERVAL 15
23static char vmwdt_cmd[MAX_CMDLEN] = "IPL";
24static int vmwdt_conceal;
25
Andrey Panin4bfdf372005-07-27 11:43:58 -070026static int vmwdt_nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28MODULE_LICENSE("GPL");
29MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
30MODULE_DESCRIPTION("z/VM Watchdog Timer");
31module_param_string(cmd, vmwdt_cmd, MAX_CMDLEN, 0644);
32MODULE_PARM_DESC(cmd, "CP command that is run when the watchdog triggers");
33module_param_named(conceal, vmwdt_conceal, bool, 0644);
34MODULE_PARM_DESC(conceal, "Enable the CONCEAL CP option while the watchdog "
35 " is active");
36module_param_named(nowayout, vmwdt_nowayout, bool, 0);
37MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started"
38 " (default=CONFIG_WATCHDOG_NOWAYOUT)");
39MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
40
41static unsigned int vmwdt_interval = 60;
42static unsigned long vmwdt_is_open;
43static int vmwdt_expect_close;
44
45enum vmwdt_func {
46 /* function codes */
47 wdt_init = 0,
48 wdt_change = 1,
49 wdt_cancel = 2,
50 /* flags */
51 wdt_conceal = 0x80000000,
52};
53
54static int __diag288(enum vmwdt_func func, unsigned int timeout,
55 char *cmd, size_t len)
56{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020057 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 int err;
62
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020063 err = -EINVAL;
64 asm volatile(
65 " diag %1,%3,0x288\n"
66 "0: la %0,0\n"
67 "1:\n"
68 EX_TABLE(0b,1b)
Heiko Carstens2b12f992007-10-12 16:11:48 +020069 : "+d" (err) : "d"(__func), "d"(__timeout),
70 "d"(__cmdp), "d"(__cmdl) : "1", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return err;
72}
73
74static int vmwdt_keepalive(void)
75{
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 */
80 static char *ebc_cmd;
81 size_t len;
82 int ret;
83 unsigned int func;
84
85 ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL);
86 if (!ebc_cmd)
87 return -ENOMEM;
88
89 len = strlcpy(ebc_cmd, vmwdt_cmd, MAX_CMDLEN);
90 ASCEBC(ebc_cmd, MAX_CMDLEN);
91 EBC_TOUPPER(ebc_cmd, MAX_CMDLEN);
92
93 func = vmwdt_conceal ? (wdt_init | wdt_conceal) : wdt_init;
94 ret = __diag288(func, vmwdt_interval, ebc_cmd, len);
Martin Schwidefsky0d130062008-07-14 09:59:40 +020095 WARN_ON(ret != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 kfree(ebc_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return ret;
98}
99
100static int vmwdt_disable(void)
101{
102 int ret = __diag288(wdt_cancel, 0, "", 0);
Martin Schwidefsky0d130062008-07-14 09:59:40 +0200103 WARN_ON(ret != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return ret;
105}
106
107static int __init vmwdt_probe(void)
108{
109 /* there is no real way to see if the watchdog is supported,
110 * so we try initializing it with a NOP command ("BEGIN")
111 * that won't cause any harm even if the following disable
112 * fails for some reason */
113 static char __initdata ebc_begin[] = {
114 194, 197, 199, 201, 213
115 };
Martin Schwidefsky0d130062008-07-14 09:59:40 +0200116 if (__diag288(wdt_init, 15, ebc_begin, sizeof(ebc_begin)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return vmwdt_disable();
119}
120
121static int vmwdt_open(struct inode *i, struct file *f)
122{
123 int ret;
124 if (test_and_set_bit(0, &vmwdt_is_open))
125 return -EBUSY;
126 ret = vmwdt_keepalive();
127 if (ret)
128 clear_bit(0, &vmwdt_is_open);
129 return ret ? ret : nonseekable_open(i, f);
130}
131
132static int vmwdt_close(struct inode *i, struct file *f)
133{
134 if (vmwdt_expect_close == 42)
135 vmwdt_disable();
136 vmwdt_expect_close = 0;
137 clear_bit(0, &vmwdt_is_open);
138 return 0;
139}
140
141static struct watchdog_info vmwdt_info = {
142 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
143 .firmware_version = 0,
144 .identity = "z/VM Watchdog Timer",
145};
146
147static int vmwdt_ioctl(struct inode *i, struct file *f,
148 unsigned int cmd, unsigned long arg)
149{
150 switch (cmd) {
151 case WDIOC_GETSUPPORT:
152 if (copy_to_user((void __user *)arg, &vmwdt_info,
153 sizeof(vmwdt_info)))
154 return -EFAULT;
155 return 0;
156 case WDIOC_GETSTATUS:
157 case WDIOC_GETBOOTSTATUS:
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200158 return put_user(0, (int __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 case WDIOC_GETTEMP:
160 return -EINVAL;
161 case WDIOC_SETOPTIONS:
162 {
163 int options, ret;
164 if (get_user(options, (int __user *)arg))
165 return -EFAULT;
166 ret = -EINVAL;
167 if (options & WDIOS_DISABLECARD) {
168 ret = vmwdt_disable();
169 if (ret)
170 return ret;
171 }
172 if (options & WDIOS_ENABLECARD) {
173 ret = vmwdt_keepalive();
174 }
175 return ret;
176 }
177 case WDIOC_GETTIMEOUT:
178 return put_user(vmwdt_interval, (int __user *)arg);
179 case WDIOC_SETTIMEOUT:
180 {
181 int interval;
182 if (get_user(interval, (int __user *)arg))
183 return -EFAULT;
184 if (interval < MIN_INTERVAL)
185 return -EINVAL;
186 vmwdt_interval = interval;
187 }
188 return vmwdt_keepalive();
189 case WDIOC_KEEPALIVE:
190 return vmwdt_keepalive();
191 }
192
193 return -EINVAL;
194}
195
196static ssize_t vmwdt_write(struct file *f, const char __user *buf,
197 size_t count, loff_t *ppos)
198{
199 if(count) {
200 if (!vmwdt_nowayout) {
201 size_t i;
202
203 /* note: just in case someone wrote the magic character
204 * five months ago... */
205 vmwdt_expect_close = 0;
206
207 for (i = 0; i != count; i++) {
208 char c;
209 if (get_user(c, buf+i))
210 return -EFAULT;
211 if (c == 'V')
212 vmwdt_expect_close = 42;
213 }
214 }
215 /* someone wrote to us, we should restart timer */
216 vmwdt_keepalive();
217 }
218 return count;
219}
220
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800221static const struct file_operations vmwdt_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 .open = &vmwdt_open,
223 .release = &vmwdt_close,
224 .ioctl = &vmwdt_ioctl,
225 .write = &vmwdt_write,
226 .owner = THIS_MODULE,
227};
228
229static struct miscdevice vmwdt_dev = {
230 .minor = WATCHDOG_MINOR,
231 .name = "watchdog",
232 .fops = &vmwdt_fops,
233};
234
235static int __init vmwdt_init(void)
236{
237 int ret;
238
239 ret = vmwdt_probe();
240 if (ret)
241 return ret;
242 return misc_register(&vmwdt_dev);
243}
244module_init(vmwdt_init);
245
246static void __exit vmwdt_exit(void)
247{
248 WARN_ON(misc_deregister(&vmwdt_dev) != 0);
249}
250module_exit(vmwdt_exit);