[S390] monwriter: remove lock_kernel() from open() function
[deliverable/linux.git] / drivers / s390 / char / vmwatchdog.c
CommitLineData
1da177e4
LT
1/*
2 * Watchdog implementation based on z/VM Watchdog Timer API
3 *
58872d5f
CB
4 * Copyright IBM Corp. 2004,2009
5 *
1da177e4
LT
6 * 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 */
58872d5f
CB
11#define KMSG_COMPONENT "vmwatchdog"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
1da177e4
LT
14#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>
58872d5f 20#include <linux/suspend.h>
1da177e4 21#include <linux/watchdog.h>
3e0420f0 22#include <linux/smp_lock.h>
1da177e4
LT
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";
31static int vmwdt_conceal;
32
4bfdf378 33static int vmwdt_nowayout = WATCHDOG_NOWAYOUT;
1da177e4
LT
34
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
58872d5f
CB
52#define VMWDT_OPEN 0 /* devnode is open or suspend in progress */
53#define VMWDT_RUNNING 1 /* The watchdog is armed */
54
1da177e4
LT
55enum vmwdt_func {
56 /* function codes */
57 wdt_init = 0,
58 wdt_change = 1,
59 wdt_cancel = 2,
60 /* flags */
61 wdt_conceal = 0x80000000,
62};
63
64static int __diag288(enum vmwdt_func func, unsigned int timeout,
65 char *cmd, size_t len)
66{
94c12cc7
MS
67 register unsigned long __func asm("2") = func;
68 register unsigned long __timeout asm("3") = timeout;
69 register unsigned long __cmdp asm("4") = virt_to_phys(cmd);
70 register unsigned long __cmdl asm("5") = len;
1da177e4
LT
71 int err;
72
94c12cc7
MS
73 err = -EINVAL;
74 asm volatile(
75 " diag %1,%3,0x288\n"
76 "0: la %0,0\n"
77 "1:\n"
78 EX_TABLE(0b,1b)
2b12f996
HC
79 : "+d" (err) : "d"(__func), "d"(__timeout),
80 "d"(__cmdp), "d"(__cmdl) : "1", "cc");
1da177e4
LT
81 return err;
82}
83
84static int vmwdt_keepalive(void)
85{
86 /* we allocate new memory every time to avoid having
87 * to track the state. static allocation is not an
88 * option since that might not be contiguous in real
89 * storage in case of a modular build */
90 static char *ebc_cmd;
91 size_t len;
92 int ret;
93 unsigned int func;
94
95 ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL);
96 if (!ebc_cmd)
97 return -ENOMEM;
98
99 len = strlcpy(ebc_cmd, vmwdt_cmd, MAX_CMDLEN);
100 ASCEBC(ebc_cmd, MAX_CMDLEN);
101 EBC_TOUPPER(ebc_cmd, MAX_CMDLEN);
102
103 func = vmwdt_conceal ? (wdt_init | wdt_conceal) : wdt_init;
58872d5f 104 set_bit(VMWDT_RUNNING, &vmwdt_is_open);
1da177e4 105 ret = __diag288(func, vmwdt_interval, ebc_cmd, len);
0d130066 106 WARN_ON(ret != 0);
1da177e4 107 kfree(ebc_cmd);
1da177e4
LT
108 return ret;
109}
110
111static int vmwdt_disable(void)
112{
113 int ret = __diag288(wdt_cancel, 0, "", 0);
0d130066 114 WARN_ON(ret != 0);
58872d5f 115 clear_bit(VMWDT_RUNNING, &vmwdt_is_open);
1da177e4
LT
116 return ret;
117}
118
119static int __init vmwdt_probe(void)
120{
121 /* there is no real way to see if the watchdog is supported,
122 * so we try initializing it with a NOP command ("BEGIN")
123 * that won't cause any harm even if the following disable
124 * fails for some reason */
125 static char __initdata ebc_begin[] = {
126 194, 197, 199, 201, 213
127 };
0d130066 128 if (__diag288(wdt_init, 15, ebc_begin, sizeof(ebc_begin)) != 0)
1da177e4 129 return -EINVAL;
1da177e4
LT
130 return vmwdt_disable();
131}
132
133static int vmwdt_open(struct inode *i, struct file *f)
134{
135 int ret;
3e0420f0 136 lock_kernel();
58872d5f 137 if (test_and_set_bit(VMWDT_OPEN, &vmwdt_is_open)) {
3e0420f0 138 unlock_kernel();
1da177e4 139 return -EBUSY;
3e0420f0 140 }
1da177e4
LT
141 ret = vmwdt_keepalive();
142 if (ret)
58872d5f 143 clear_bit(VMWDT_OPEN, &vmwdt_is_open);
3e0420f0 144 unlock_kernel();
1da177e4
LT
145 return ret ? ret : nonseekable_open(i, f);
146}
147
148static int vmwdt_close(struct inode *i, struct file *f)
149{
150 if (vmwdt_expect_close == 42)
151 vmwdt_disable();
152 vmwdt_expect_close = 0;
58872d5f 153 clear_bit(VMWDT_OPEN, &vmwdt_is_open);
1da177e4
LT
154 return 0;
155}
156
157static struct watchdog_info vmwdt_info = {
158 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
159 .firmware_version = 0,
160 .identity = "z/VM Watchdog Timer",
161};
162
163static int vmwdt_ioctl(struct inode *i, struct file *f,
164 unsigned int cmd, unsigned long arg)
165{
166 switch (cmd) {
167 case WDIOC_GETSUPPORT:
168 if (copy_to_user((void __user *)arg, &vmwdt_info,
169 sizeof(vmwdt_info)))
170 return -EFAULT;
171 return 0;
172 case WDIOC_GETSTATUS:
173 case WDIOC_GETBOOTSTATUS:
d2c993d8 174 return put_user(0, (int __user *)arg);
1da177e4
LT
175 case WDIOC_GETTEMP:
176 return -EINVAL;
177 case WDIOC_SETOPTIONS:
178 {
179 int options, ret;
180 if (get_user(options, (int __user *)arg))
181 return -EFAULT;
182 ret = -EINVAL;
183 if (options & WDIOS_DISABLECARD) {
184 ret = vmwdt_disable();
185 if (ret)
186 return ret;
187 }
188 if (options & WDIOS_ENABLECARD) {
189 ret = vmwdt_keepalive();
190 }
191 return ret;
192 }
193 case WDIOC_GETTIMEOUT:
194 return put_user(vmwdt_interval, (int __user *)arg);
195 case WDIOC_SETTIMEOUT:
196 {
197 int interval;
198 if (get_user(interval, (int __user *)arg))
199 return -EFAULT;
200 if (interval < MIN_INTERVAL)
201 return -EINVAL;
202 vmwdt_interval = interval;
203 }
204 return vmwdt_keepalive();
205 case WDIOC_KEEPALIVE:
206 return vmwdt_keepalive();
207 }
208
209 return -EINVAL;
210}
211
212static ssize_t vmwdt_write(struct file *f, const char __user *buf,
213 size_t count, loff_t *ppos)
214{
215 if(count) {
216 if (!vmwdt_nowayout) {
217 size_t i;
218
219 /* note: just in case someone wrote the magic character
220 * five months ago... */
221 vmwdt_expect_close = 0;
222
223 for (i = 0; i != count; i++) {
224 char c;
225 if (get_user(c, buf+i))
226 return -EFAULT;
227 if (c == 'V')
228 vmwdt_expect_close = 42;
229 }
230 }
231 /* someone wrote to us, we should restart timer */
232 vmwdt_keepalive();
233 }
234 return count;
235}
236
58872d5f
CB
237static int vmwdt_resume(void)
238{
239 clear_bit(VMWDT_OPEN, &vmwdt_is_open);
240 return NOTIFY_DONE;
241}
242
243/*
244 * It makes no sense to go into suspend while the watchdog is running.
245 * Depending on the memory size, the watchdog might trigger, while we
246 * are still saving the memory.
247 * We reuse the open flag to ensure that suspend and watchdog open are
248 * exclusive operations
249 */
250static int vmwdt_suspend(void)
251{
252 if (test_and_set_bit(VMWDT_OPEN, &vmwdt_is_open)) {
2c48c4d6
CB
253 pr_err("The system cannot be suspended while the watchdog"
254 " is in use\n");
58872d5f
CB
255 return NOTIFY_BAD;
256 }
257 if (test_bit(VMWDT_RUNNING, &vmwdt_is_open)) {
258 clear_bit(VMWDT_OPEN, &vmwdt_is_open);
2c48c4d6
CB
259 pr_err("The system cannot be suspended while the watchdog"
260 " is running\n");
58872d5f
CB
261 return NOTIFY_BAD;
262 }
263 return NOTIFY_DONE;
264}
265
266/*
267 * This function is called for suspend and resume.
268 */
269static int vmwdt_power_event(struct notifier_block *this, unsigned long event,
270 void *ptr)
271{
272 switch (event) {
273 case PM_POST_HIBERNATION:
274 case PM_POST_SUSPEND:
275 return vmwdt_resume();
276 case PM_HIBERNATION_PREPARE:
277 case PM_SUSPEND_PREPARE:
278 return vmwdt_suspend();
279 default:
280 return NOTIFY_DONE;
281 }
282}
283
284static struct notifier_block vmwdt_power_notifier = {
285 .notifier_call = vmwdt_power_event,
286};
287
d54b1fdb 288static const struct file_operations vmwdt_fops = {
1da177e4
LT
289 .open = &vmwdt_open,
290 .release = &vmwdt_close,
291 .ioctl = &vmwdt_ioctl,
292 .write = &vmwdt_write,
293 .owner = THIS_MODULE,
294};
295
296static struct miscdevice vmwdt_dev = {
297 .minor = WATCHDOG_MINOR,
298 .name = "watchdog",
299 .fops = &vmwdt_fops,
300};
301
302static int __init vmwdt_init(void)
303{
304 int ret;
305
306 ret = vmwdt_probe();
307 if (ret)
308 return ret;
58872d5f
CB
309 ret = register_pm_notifier(&vmwdt_power_notifier);
310 if (ret)
311 return ret;
312 ret = misc_register(&vmwdt_dev);
313 if (ret) {
314 unregister_pm_notifier(&vmwdt_power_notifier);
315 return ret;
316 }
317 return 0;
1da177e4
LT
318}
319module_init(vmwdt_init);
320
321static void __exit vmwdt_exit(void)
322{
58872d5f
CB
323 unregister_pm_notifier(&vmwdt_power_notifier);
324 misc_deregister(&vmwdt_dev);
1da177e4
LT
325}
326module_exit(vmwdt_exit);
This page took 0.58756 seconds and 5 git commands to generate.