xref: /linux/drivers/char/ipmi/ipmi_watchdog.c (revision 13abf8130139c2ccd4962a7e5a8902be5e6cb5a7)
1 /*
2  * ipmi_watchdog.c
3  *
4  * A watchdog timer based upon the IPMI interface.
5  *
6  * Author: MontaVista Software, Inc.
7  *         Corey Minyard <minyard@mvista.com>
8  *         source@mvista.com
9  *
10  * Copyright 2002 MontaVista Software Inc.
11  *
12  *  This program is free software; you can redistribute it and/or modify it
13  *  under the terms of the GNU General Public License as published by the
14  *  Free Software Foundation; either version 2 of the License, or (at your
15  *  option) any later version.
16  *
17  *
18  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
19  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
26  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
27  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  *  You should have received a copy of the GNU General Public License along
30  *  with this program; if not, write to the Free Software Foundation, Inc.,
31  *  675 Mass Ave, Cambridge, MA 02139, USA.
32  */
33 
34 #include <linux/config.h>
35 #include <linux/module.h>
36 #include <linux/moduleparam.h>
37 #include <linux/ipmi.h>
38 #include <linux/ipmi_smi.h>
39 #include <linux/watchdog.h>
40 #include <linux/miscdevice.h>
41 #include <linux/init.h>
42 #include <linux/rwsem.h>
43 #include <linux/errno.h>
44 #include <asm/uaccess.h>
45 #include <linux/notifier.h>
46 #include <linux/nmi.h>
47 #include <linux/reboot.h>
48 #include <linux/wait.h>
49 #include <linux/poll.h>
50 #ifdef CONFIG_X86_LOCAL_APIC
51 #include <asm/apic.h>
52 #endif
53 
54 #define	PFX "IPMI Watchdog: "
55 
56 #define IPMI_WATCHDOG_VERSION "v33"
57 
58 /*
59  * The IPMI command/response information for the watchdog timer.
60  */
61 
62 /* values for byte 1 of the set command, byte 2 of the get response. */
63 #define WDOG_DONT_LOG		(1 << 7)
64 #define WDOG_DONT_STOP_ON_SET	(1 << 6)
65 #define WDOG_SET_TIMER_USE(byte, use) \
66 	byte = ((byte) & 0xf8) | ((use) & 0x7)
67 #define WDOG_GET_TIMER_USE(byte) ((byte) & 0x7)
68 #define WDOG_TIMER_USE_BIOS_FRB2	1
69 #define WDOG_TIMER_USE_BIOS_POST	2
70 #define WDOG_TIMER_USE_OS_LOAD		3
71 #define WDOG_TIMER_USE_SMS_OS		4
72 #define WDOG_TIMER_USE_OEM		5
73 
74 /* values for byte 2 of the set command, byte 3 of the get response. */
75 #define WDOG_SET_PRETIMEOUT_ACT(byte, use) \
76 	byte = ((byte) & 0x8f) | (((use) & 0x7) << 4)
77 #define WDOG_GET_PRETIMEOUT_ACT(byte) (((byte) >> 4) & 0x7)
78 #define WDOG_PRETIMEOUT_NONE		0
79 #define WDOG_PRETIMEOUT_SMI		1
80 #define WDOG_PRETIMEOUT_NMI		2
81 #define WDOG_PRETIMEOUT_MSG_INT		3
82 
83 /* Operations that can be performed on a pretimout. */
84 #define WDOG_PREOP_NONE		0
85 #define WDOG_PREOP_PANIC	1
86 #define WDOG_PREOP_GIVE_DATA	2 /* Cause data to be available to
87                                      read.  Doesn't work in NMI
88                                      mode. */
89 
90 /* Actions to perform on a full timeout. */
91 #define WDOG_SET_TIMEOUT_ACT(byte, use) \
92 	byte = ((byte) & 0xf8) | ((use) & 0x7)
93 #define WDOG_GET_TIMEOUT_ACT(byte) ((byte) & 0x7)
94 #define WDOG_TIMEOUT_NONE		0
95 #define WDOG_TIMEOUT_RESET		1
96 #define WDOG_TIMEOUT_POWER_DOWN		2
97 #define WDOG_TIMEOUT_POWER_CYCLE	3
98 
99 /* Byte 3 of the get command, byte 4 of the get response is the
100    pre-timeout in seconds. */
101 
102 /* Bits for setting byte 4 of the set command, byte 5 of the get response. */
103 #define WDOG_EXPIRE_CLEAR_BIOS_FRB2	(1 << 1)
104 #define WDOG_EXPIRE_CLEAR_BIOS_POST	(1 << 2)
105 #define WDOG_EXPIRE_CLEAR_OS_LOAD	(1 << 3)
106 #define WDOG_EXPIRE_CLEAR_SMS_OS	(1 << 4)
107 #define WDOG_EXPIRE_CLEAR_OEM		(1 << 5)
108 
109 /* Setting/getting the watchdog timer value.  This is for bytes 5 and
110    6 (the timeout time) of the set command, and bytes 6 and 7 (the
111    timeout time) and 8 and 9 (the current countdown value) of the
112    response.  The timeout value is given in seconds (in the command it
113    is 100ms intervals). */
114 #define WDOG_SET_TIMEOUT(byte1, byte2, val) \
115 	(byte1) = (((val) * 10) & 0xff), (byte2) = (((val) * 10) >> 8)
116 #define WDOG_GET_TIMEOUT(byte1, byte2) \
117 	(((byte1) | ((byte2) << 8)) / 10)
118 
119 #define IPMI_WDOG_RESET_TIMER		0x22
120 #define IPMI_WDOG_SET_TIMER		0x24
121 #define IPMI_WDOG_GET_TIMER		0x25
122 
123 /* These are here until the real ones get into the watchdog.h interface. */
124 #ifndef WDIOC_GETTIMEOUT
125 #define	WDIOC_GETTIMEOUT        _IOW(WATCHDOG_IOCTL_BASE, 20, int)
126 #endif
127 #ifndef WDIOC_SET_PRETIMEOUT
128 #define	WDIOC_SET_PRETIMEOUT     _IOW(WATCHDOG_IOCTL_BASE, 21, int)
129 #endif
130 #ifndef WDIOC_GET_PRETIMEOUT
131 #define	WDIOC_GET_PRETIMEOUT     _IOW(WATCHDOG_IOCTL_BASE, 22, int)
132 #endif
133 
134 static int nowayout = WATCHDOG_NOWAYOUT;
135 
136 static ipmi_user_t watchdog_user = NULL;
137 
138 /* Default the timeout to 10 seconds. */
139 static int timeout = 10;
140 
141 /* The pre-timeout is disabled by default. */
142 static int pretimeout = 0;
143 
144 /* Default action is to reset the board on a timeout. */
145 static unsigned char action_val = WDOG_TIMEOUT_RESET;
146 
147 static char action[16] = "reset";
148 
149 static unsigned char preaction_val = WDOG_PRETIMEOUT_NONE;
150 
151 static char preaction[16] = "pre_none";
152 
153 static unsigned char preop_val = WDOG_PREOP_NONE;
154 
155 static char preop[16] = "preop_none";
156 static DEFINE_SPINLOCK(ipmi_read_lock);
157 static char data_to_read = 0;
158 static DECLARE_WAIT_QUEUE_HEAD(read_q);
159 static struct fasync_struct *fasync_q = NULL;
160 static char pretimeout_since_last_heartbeat = 0;
161 static char expect_close;
162 
163 /* If true, the driver will start running as soon as it is configured
164    and ready. */
165 static int start_now = 0;
166 
167 module_param(timeout, int, 0);
168 MODULE_PARM_DESC(timeout, "Timeout value in seconds.");
169 module_param(pretimeout, int, 0);
170 MODULE_PARM_DESC(pretimeout, "Pretimeout value in seconds.");
171 module_param_string(action, action, sizeof(action), 0);
172 MODULE_PARM_DESC(action, "Timeout action. One of: "
173 		 "reset, none, power_cycle, power_off.");
174 module_param_string(preaction, preaction, sizeof(preaction), 0);
175 MODULE_PARM_DESC(preaction, "Pretimeout action.  One of: "
176 		 "pre_none, pre_smi, pre_nmi, pre_int.");
177 module_param_string(preop, preop, sizeof(preop), 0);
178 MODULE_PARM_DESC(preop, "Pretimeout driver operation.  One of: "
179 		 "preop_none, preop_panic, preop_give_data.");
180 module_param(start_now, int, 0);
181 MODULE_PARM_DESC(start_now, "Set to 1 to start the watchdog as"
182 		 "soon as the driver is loaded.");
183 module_param(nowayout, int, 0);
184 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
185 
186 /* Default state of the timer. */
187 static unsigned char ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
188 
189 /* If shutting down via IPMI, we ignore the heartbeat. */
190 static int ipmi_ignore_heartbeat = 0;
191 
192 /* Is someone using the watchdog?  Only one user is allowed. */
193 static unsigned long ipmi_wdog_open = 0;
194 
195 /* If set to 1, the heartbeat command will set the state to reset and
196    start the timer.  The timer doesn't normally run when the driver is
197    first opened until the heartbeat is set the first time, this
198    variable is used to accomplish this. */
199 static int ipmi_start_timer_on_heartbeat = 0;
200 
201 /* IPMI version of the BMC. */
202 static unsigned char ipmi_version_major;
203 static unsigned char ipmi_version_minor;
204 
205 
206 static int ipmi_heartbeat(void);
207 static void panic_halt_ipmi_heartbeat(void);
208 
209 
210 /* We use a semaphore to make sure that only one thing can send a set
211    timeout at one time, because we only have one copy of the data.
212    The semaphore is claimed when the set_timeout is sent and freed
213    when both messages are free. */
214 static atomic_t set_timeout_tofree = ATOMIC_INIT(0);
215 static DECLARE_MUTEX(set_timeout_lock);
216 static void set_timeout_free_smi(struct ipmi_smi_msg *msg)
217 {
218     if (atomic_dec_and_test(&set_timeout_tofree))
219 	    up(&set_timeout_lock);
220 }
221 static void set_timeout_free_recv(struct ipmi_recv_msg *msg)
222 {
223     if (atomic_dec_and_test(&set_timeout_tofree))
224 	    up(&set_timeout_lock);
225 }
226 static struct ipmi_smi_msg set_timeout_smi_msg =
227 {
228 	.done = set_timeout_free_smi
229 };
230 static struct ipmi_recv_msg set_timeout_recv_msg =
231 {
232 	.done = set_timeout_free_recv
233 };
234 
235 static int i_ipmi_set_timeout(struct ipmi_smi_msg  *smi_msg,
236 			      struct ipmi_recv_msg *recv_msg,
237 			      int                  *send_heartbeat_now)
238 {
239 	struct kernel_ipmi_msg            msg;
240 	unsigned char                     data[6];
241 	int                               rv;
242 	struct ipmi_system_interface_addr addr;
243 	int                               hbnow = 0;
244 
245 
246 	data[0] = 0;
247 	WDOG_SET_TIMER_USE(data[0], WDOG_TIMER_USE_SMS_OS);
248 
249 	if ((ipmi_version_major > 1)
250 	    || ((ipmi_version_major == 1) && (ipmi_version_minor >= 5)))
251 	{
252 		/* This is an IPMI 1.5-only feature. */
253 		data[0] |= WDOG_DONT_STOP_ON_SET;
254 	} else if (ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
255 		/* In ipmi 1.0, setting the timer stops the watchdog, we
256 		   need to start it back up again. */
257 		hbnow = 1;
258 	}
259 
260 	data[1] = 0;
261 	WDOG_SET_TIMEOUT_ACT(data[1], ipmi_watchdog_state);
262 	if (pretimeout > 0) {
263 	    WDOG_SET_PRETIMEOUT_ACT(data[1], preaction_val);
264 	    data[2] = pretimeout;
265 	} else {
266 	    WDOG_SET_PRETIMEOUT_ACT(data[1], WDOG_PRETIMEOUT_NONE);
267 	    data[2] = 0; /* No pretimeout. */
268 	}
269 	data[3] = 0;
270 	WDOG_SET_TIMEOUT(data[4], data[5], timeout);
271 
272 	addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
273 	addr.channel = IPMI_BMC_CHANNEL;
274 	addr.lun = 0;
275 
276 	msg.netfn = 0x06;
277 	msg.cmd = IPMI_WDOG_SET_TIMER;
278 	msg.data = data;
279 	msg.data_len = sizeof(data);
280 	rv = ipmi_request_supply_msgs(watchdog_user,
281 				      (struct ipmi_addr *) &addr,
282 				      0,
283 				      &msg,
284 				      NULL,
285 				      smi_msg,
286 				      recv_msg,
287 				      1);
288 	if (rv) {
289 		printk(KERN_WARNING PFX "set timeout error: %d\n",
290 		       rv);
291 	}
292 
293 	if (send_heartbeat_now)
294 	    *send_heartbeat_now = hbnow;
295 
296 	return rv;
297 }
298 
299 /* Parameters to ipmi_set_timeout */
300 #define IPMI_SET_TIMEOUT_NO_HB			0
301 #define IPMI_SET_TIMEOUT_HB_IF_NECESSARY	1
302 #define IPMI_SET_TIMEOUT_FORCE_HB		2
303 
304 static int ipmi_set_timeout(int do_heartbeat)
305 {
306 	int send_heartbeat_now;
307 	int rv;
308 
309 
310 	/* We can only send one of these at a time. */
311 	down(&set_timeout_lock);
312 
313 	atomic_set(&set_timeout_tofree, 2);
314 
315 	rv = i_ipmi_set_timeout(&set_timeout_smi_msg,
316 				&set_timeout_recv_msg,
317 				&send_heartbeat_now);
318 	if (rv) {
319 		up(&set_timeout_lock);
320 	} else {
321 		if ((do_heartbeat == IPMI_SET_TIMEOUT_FORCE_HB)
322 		    || ((send_heartbeat_now)
323 			&& (do_heartbeat == IPMI_SET_TIMEOUT_HB_IF_NECESSARY)))
324 		{
325 			rv = ipmi_heartbeat();
326 		}
327 	}
328 
329 	return rv;
330 }
331 
332 static void dummy_smi_free(struct ipmi_smi_msg *msg)
333 {
334 }
335 static void dummy_recv_free(struct ipmi_recv_msg *msg)
336 {
337 }
338 static struct ipmi_smi_msg panic_halt_smi_msg =
339 {
340 	.done = dummy_smi_free
341 };
342 static struct ipmi_recv_msg panic_halt_recv_msg =
343 {
344 	.done = dummy_recv_free
345 };
346 
347 /* Special call, doesn't claim any locks.  This is only to be called
348    at panic or halt time, in run-to-completion mode, when the caller
349    is the only CPU and the only thing that will be going is these IPMI
350    calls. */
351 static void panic_halt_ipmi_set_timeout(void)
352 {
353 	int send_heartbeat_now;
354 	int rv;
355 
356 	rv = i_ipmi_set_timeout(&panic_halt_smi_msg,
357 				&panic_halt_recv_msg,
358 				&send_heartbeat_now);
359 	if (!rv) {
360 		if (send_heartbeat_now)
361 			panic_halt_ipmi_heartbeat();
362 	}
363 }
364 
365 /* We use a semaphore to make sure that only one thing can send a
366    heartbeat at one time, because we only have one copy of the data.
367    The semaphore is claimed when the set_timeout is sent and freed
368    when both messages are free. */
369 static atomic_t heartbeat_tofree = ATOMIC_INIT(0);
370 static DECLARE_MUTEX(heartbeat_lock);
371 static DECLARE_MUTEX_LOCKED(heartbeat_wait_lock);
372 static void heartbeat_free_smi(struct ipmi_smi_msg *msg)
373 {
374     if (atomic_dec_and_test(&heartbeat_tofree))
375 	    up(&heartbeat_wait_lock);
376 }
377 static void heartbeat_free_recv(struct ipmi_recv_msg *msg)
378 {
379     if (atomic_dec_and_test(&heartbeat_tofree))
380 	    up(&heartbeat_wait_lock);
381 }
382 static struct ipmi_smi_msg heartbeat_smi_msg =
383 {
384 	.done = heartbeat_free_smi
385 };
386 static struct ipmi_recv_msg heartbeat_recv_msg =
387 {
388 	.done = heartbeat_free_recv
389 };
390 
391 static struct ipmi_smi_msg panic_halt_heartbeat_smi_msg =
392 {
393 	.done = dummy_smi_free
394 };
395 static struct ipmi_recv_msg panic_halt_heartbeat_recv_msg =
396 {
397 	.done = dummy_recv_free
398 };
399 
400 static int ipmi_heartbeat(void)
401 {
402 	struct kernel_ipmi_msg            msg;
403 	int                               rv;
404 	struct ipmi_system_interface_addr addr;
405 
406 	if (ipmi_ignore_heartbeat) {
407 		return 0;
408 	}
409 
410 	if (ipmi_start_timer_on_heartbeat) {
411 		ipmi_start_timer_on_heartbeat = 0;
412 		ipmi_watchdog_state = action_val;
413 		return ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
414 	} else if (pretimeout_since_last_heartbeat) {
415 		/* A pretimeout occurred, make sure we set the timeout.
416 		   We don't want to set the action, though, we want to
417 		   leave that alone (thus it can't be combined with the
418 		   above operation. */
419 		pretimeout_since_last_heartbeat = 0;
420 		return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
421 	}
422 
423 	down(&heartbeat_lock);
424 
425 	atomic_set(&heartbeat_tofree, 2);
426 
427 	/* Don't reset the timer if we have the timer turned off, that
428            re-enables the watchdog. */
429 	if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE) {
430 		up(&heartbeat_lock);
431 		return 0;
432 	}
433 
434 	addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
435 	addr.channel = IPMI_BMC_CHANNEL;
436 	addr.lun = 0;
437 
438 	msg.netfn = 0x06;
439 	msg.cmd = IPMI_WDOG_RESET_TIMER;
440 	msg.data = NULL;
441 	msg.data_len = 0;
442 	rv = ipmi_request_supply_msgs(watchdog_user,
443 				      (struct ipmi_addr *) &addr,
444 				      0,
445 				      &msg,
446 				      NULL,
447 				      &heartbeat_smi_msg,
448 				      &heartbeat_recv_msg,
449 				      1);
450 	if (rv) {
451 		up(&heartbeat_lock);
452 		printk(KERN_WARNING PFX "heartbeat failure: %d\n",
453 		       rv);
454 		return rv;
455 	}
456 
457 	/* Wait for the heartbeat to be sent. */
458 	down(&heartbeat_wait_lock);
459 
460 	if (heartbeat_recv_msg.msg.data[0] != 0) {
461 	    /* Got an error in the heartbeat response.  It was already
462 	       reported in ipmi_wdog_msg_handler, but we should return
463 	       an error here. */
464 	    rv = -EINVAL;
465 	}
466 
467 	up(&heartbeat_lock);
468 
469 	return rv;
470 }
471 
472 static void panic_halt_ipmi_heartbeat(void)
473 {
474 	struct kernel_ipmi_msg             msg;
475 	struct ipmi_system_interface_addr addr;
476 
477 
478 	/* Don't reset the timer if we have the timer turned off, that
479            re-enables the watchdog. */
480 	if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)
481 		return;
482 
483 	addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
484 	addr.channel = IPMI_BMC_CHANNEL;
485 	addr.lun = 0;
486 
487 	msg.netfn = 0x06;
488 	msg.cmd = IPMI_WDOG_RESET_TIMER;
489 	msg.data = NULL;
490 	msg.data_len = 0;
491 	ipmi_request_supply_msgs(watchdog_user,
492 				 (struct ipmi_addr *) &addr,
493 				 0,
494 				 &msg,
495 				 NULL,
496 				 &panic_halt_heartbeat_smi_msg,
497 				 &panic_halt_heartbeat_recv_msg,
498 				 1);
499 }
500 
501 static struct watchdog_info ident=
502 {
503 	.options	= 0,	/* WDIOF_SETTIMEOUT, */
504 	.firmware_version = 1,
505 	.identity	= "IPMI"
506 };
507 
508 static int ipmi_ioctl(struct inode *inode, struct file *file,
509 		      unsigned int cmd, unsigned long arg)
510 {
511 	void __user *argp = (void __user *)arg;
512 	int i;
513 	int val;
514 
515 	switch(cmd) {
516 	case WDIOC_GETSUPPORT:
517 		i = copy_to_user(argp, &ident, sizeof(ident));
518 		return i ? -EFAULT : 0;
519 
520 	case WDIOC_SETTIMEOUT:
521 		i = copy_from_user(&val, argp, sizeof(int));
522 		if (i)
523 			return -EFAULT;
524 		timeout = val;
525 		return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
526 
527 	case WDIOC_GETTIMEOUT:
528 		i = copy_to_user(argp, &timeout, sizeof(timeout));
529 		if (i)
530 			return -EFAULT;
531 		return 0;
532 
533 	case WDIOC_SET_PRETIMEOUT:
534 		i = copy_from_user(&val, argp, sizeof(int));
535 		if (i)
536 			return -EFAULT;
537 		pretimeout = val;
538 		return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
539 
540 	case WDIOC_GET_PRETIMEOUT:
541 		i = copy_to_user(argp, &pretimeout, sizeof(pretimeout));
542 		if (i)
543 			return -EFAULT;
544 		return 0;
545 
546 	case WDIOC_KEEPALIVE:
547 		return ipmi_heartbeat();
548 
549 	case WDIOC_SETOPTIONS:
550 		i = copy_from_user(&val, argp, sizeof(int));
551 		if (i)
552 			return -EFAULT;
553 		if (val & WDIOS_DISABLECARD)
554 		{
555 			ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
556 			ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
557 			ipmi_start_timer_on_heartbeat = 0;
558 		}
559 
560 		if (val & WDIOS_ENABLECARD)
561 		{
562 			ipmi_watchdog_state = action_val;
563 			ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
564 		}
565 		return 0;
566 
567 	case WDIOC_GETSTATUS:
568 		val = 0;
569 		i = copy_to_user(argp, &val, sizeof(val));
570 		if (i)
571 			return -EFAULT;
572 		return 0;
573 
574 	default:
575 		return -ENOIOCTLCMD;
576 	}
577 }
578 
579 static ssize_t ipmi_write(struct file *file,
580 			  const char  __user *buf,
581 			  size_t      len,
582 			  loff_t      *ppos)
583 {
584 	int rv;
585 
586 	if (len) {
587 	    	if (!nowayout) {
588 		    	size_t i;
589 
590 			/* In case it was set long ago */
591 			expect_close = 0;
592 
593     			for (i = 0; i != len; i++) {
594 				char c;
595 
596 				if (get_user(c, buf + i))
597 					return -EFAULT;
598 				if (c == 'V')
599 					expect_close = 42;
600 			}
601 		}
602 		rv = ipmi_heartbeat();
603 		if (rv)
604 			return rv;
605 		return 1;
606 	}
607 	return 0;
608 }
609 
610 static ssize_t ipmi_read(struct file *file,
611 			 char        __user *buf,
612 			 size_t      count,
613 			 loff_t      *ppos)
614 {
615 	int          rv = 0;
616 	wait_queue_t wait;
617 
618 	if (count <= 0)
619 		return 0;
620 
621 	/* Reading returns if the pretimeout has gone off, and it only does
622 	   it once per pretimeout. */
623 	spin_lock(&ipmi_read_lock);
624 	if (!data_to_read) {
625 		if (file->f_flags & O_NONBLOCK) {
626 			rv = -EAGAIN;
627 			goto out;
628 		}
629 
630 		init_waitqueue_entry(&wait, current);
631 		add_wait_queue(&read_q, &wait);
632 		while (!data_to_read) {
633 			set_current_state(TASK_INTERRUPTIBLE);
634 			spin_unlock(&ipmi_read_lock);
635 			schedule();
636 			spin_lock(&ipmi_read_lock);
637 		}
638 		remove_wait_queue(&read_q, &wait);
639 
640 		if (signal_pending(current)) {
641 			rv = -ERESTARTSYS;
642 			goto out;
643 		}
644 	}
645 	data_to_read = 0;
646 
647  out:
648 	spin_unlock(&ipmi_read_lock);
649 
650 	if (rv == 0) {
651 		if (copy_to_user(buf, &data_to_read, 1))
652 			rv = -EFAULT;
653 		else
654 			rv = 1;
655 	}
656 
657 	return rv;
658 }
659 
660 static int ipmi_open(struct inode *ino, struct file *filep)
661 {
662         switch (iminor(ino))
663         {
664                 case WATCHDOG_MINOR:
665 		    if(test_and_set_bit(0, &ipmi_wdog_open))
666                         return -EBUSY;
667 
668 		    /* Don't start the timer now, let it start on the
669 		       first heartbeat. */
670 		    ipmi_start_timer_on_heartbeat = 1;
671                     return nonseekable_open(ino, filep);
672 
673                 default:
674                     return (-ENODEV);
675         }
676 }
677 
678 static unsigned int ipmi_poll(struct file *file, poll_table *wait)
679 {
680 	unsigned int mask = 0;
681 
682 	poll_wait(file, &read_q, wait);
683 
684 	spin_lock(&ipmi_read_lock);
685 	if (data_to_read)
686 		mask |= (POLLIN | POLLRDNORM);
687 	spin_unlock(&ipmi_read_lock);
688 
689 	return mask;
690 }
691 
692 static int ipmi_fasync(int fd, struct file *file, int on)
693 {
694 	int result;
695 
696 	result = fasync_helper(fd, file, on, &fasync_q);
697 
698 	return (result);
699 }
700 
701 static int ipmi_close(struct inode *ino, struct file *filep)
702 {
703 	if (iminor(ino)==WATCHDOG_MINOR)
704 	{
705 		if (expect_close == 42) {
706 			ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
707 			ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
708 		} else {
709 			printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
710 			ipmi_heartbeat();
711 		}
712 		clear_bit(0, &ipmi_wdog_open);
713 	}
714 
715 	ipmi_fasync (-1, filep, 0);
716 	expect_close = 0;
717 
718 	return 0;
719 }
720 
721 static struct file_operations ipmi_wdog_fops = {
722 	.owner   = THIS_MODULE,
723 	.read    = ipmi_read,
724 	.poll    = ipmi_poll,
725 	.write   = ipmi_write,
726 	.ioctl   = ipmi_ioctl,
727 	.open    = ipmi_open,
728 	.release = ipmi_close,
729 	.fasync  = ipmi_fasync,
730 };
731 
732 static struct miscdevice ipmi_wdog_miscdev = {
733 	.minor		= WATCHDOG_MINOR,
734 	.name		= "watchdog",
735 	.fops		= &ipmi_wdog_fops
736 };
737 
738 static DECLARE_RWSEM(register_sem);
739 
740 static void ipmi_wdog_msg_handler(struct ipmi_recv_msg *msg,
741 				  void                 *handler_data)
742 {
743 	if (msg->msg.data[0] != 0) {
744 		printk(KERN_ERR PFX "response: Error %x on cmd %x\n",
745 		       msg->msg.data[0],
746 		       msg->msg.cmd);
747 	}
748 
749 	ipmi_free_recv_msg(msg);
750 }
751 
752 static void ipmi_wdog_pretimeout_handler(void *handler_data)
753 {
754 	if (preaction_val != WDOG_PRETIMEOUT_NONE) {
755 		if (preop_val == WDOG_PREOP_PANIC)
756 			panic("Watchdog pre-timeout");
757 		else if (preop_val == WDOG_PREOP_GIVE_DATA) {
758 			spin_lock(&ipmi_read_lock);
759 			data_to_read = 1;
760 			wake_up_interruptible(&read_q);
761 			kill_fasync(&fasync_q, SIGIO, POLL_IN);
762 
763 			spin_unlock(&ipmi_read_lock);
764 		}
765 	}
766 
767 	/* On some machines, the heartbeat will give
768 	   an error and not work unless we re-enable
769 	   the timer.   So do so. */
770 	pretimeout_since_last_heartbeat = 1;
771 }
772 
773 static struct ipmi_user_hndl ipmi_hndlrs =
774 {
775 	.ipmi_recv_hndl           = ipmi_wdog_msg_handler,
776 	.ipmi_watchdog_pretimeout = ipmi_wdog_pretimeout_handler
777 };
778 
779 static void ipmi_register_watchdog(int ipmi_intf)
780 {
781 	int rv = -EBUSY;
782 
783 	down_write(&register_sem);
784 	if (watchdog_user)
785 		goto out;
786 
787 	rv = ipmi_create_user(ipmi_intf, &ipmi_hndlrs, NULL, &watchdog_user);
788 	if (rv < 0) {
789 		printk(KERN_CRIT PFX "Unable to register with ipmi\n");
790 		goto out;
791 	}
792 
793 	ipmi_get_version(watchdog_user,
794 			 &ipmi_version_major,
795 			 &ipmi_version_minor);
796 
797 	rv = misc_register(&ipmi_wdog_miscdev);
798 	if (rv < 0) {
799 		ipmi_destroy_user(watchdog_user);
800 		watchdog_user = NULL;
801 		printk(KERN_CRIT PFX "Unable to register misc device\n");
802 	}
803 
804  out:
805 	up_write(&register_sem);
806 
807 	if ((start_now) && (rv == 0)) {
808 		/* Run from startup, so start the timer now. */
809 		start_now = 0; /* Disable this function after first startup. */
810 		ipmi_watchdog_state = action_val;
811 		ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
812 		printk(KERN_INFO PFX "Starting now!\n");
813 	}
814 }
815 
816 #ifdef HAVE_NMI_HANDLER
817 static int
818 ipmi_nmi(void *dev_id, struct pt_regs *regs, int cpu, int handled)
819 {
820 	/* If no one else handled the NMI, we assume it was the IPMI
821            watchdog. */
822 	if ((!handled) && (preop_val == WDOG_PREOP_PANIC))
823 		panic(PFX "pre-timeout");
824 
825 	/* On some machines, the heartbeat will give
826 	   an error and not work unless we re-enable
827 	   the timer.   So do so. */
828 	pretimeout_since_last_heartbeat = 1;
829 
830 	return NOTIFY_DONE;
831 }
832 
833 static struct nmi_handler ipmi_nmi_handler =
834 {
835 	.link     = LIST_HEAD_INIT(ipmi_nmi_handler.link),
836 	.dev_name = "ipmi_watchdog",
837 	.dev_id   = NULL,
838 	.handler  = ipmi_nmi,
839 	.priority = 0, /* Call us last. */
840 };
841 #endif
842 
843 static int wdog_reboot_handler(struct notifier_block *this,
844 			       unsigned long         code,
845 			       void                  *unused)
846 {
847 	static int reboot_event_handled = 0;
848 
849 	if ((watchdog_user) && (!reboot_event_handled)) {
850 		/* Make sure we only do this once. */
851 		reboot_event_handled = 1;
852 
853 		if (code == SYS_DOWN || code == SYS_HALT) {
854 			/* Disable the WDT if we are shutting down. */
855 			ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
856 			panic_halt_ipmi_set_timeout();
857 		} else {
858 			/* Set a long timer to let the reboot happens, but
859 			   reboot if it hangs. */
860 			timeout = 120;
861 			pretimeout = 0;
862 			ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
863 			panic_halt_ipmi_set_timeout();
864 		}
865 	}
866 	return NOTIFY_OK;
867 }
868 
869 static struct notifier_block wdog_reboot_notifier = {
870 	.notifier_call	= wdog_reboot_handler,
871 	.next		= NULL,
872 	.priority	= 0
873 };
874 
875 static int wdog_panic_handler(struct notifier_block *this,
876 			      unsigned long         event,
877 			      void                  *unused)
878 {
879 	static int panic_event_handled = 0;
880 
881 	/* On a panic, if we have a panic timeout, make sure that the thing
882 	   reboots, even if it hangs during that panic. */
883 	if (watchdog_user && !panic_event_handled) {
884 		/* Make sure the panic doesn't hang, and make sure we
885 		   do this only once. */
886 		panic_event_handled = 1;
887 
888 		timeout = 255;
889 		pretimeout = 0;
890 		ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
891 		panic_halt_ipmi_set_timeout();
892 	}
893 
894 	return NOTIFY_OK;
895 }
896 
897 static struct notifier_block wdog_panic_notifier = {
898 	.notifier_call	= wdog_panic_handler,
899 	.next		= NULL,
900 	.priority	= 150	/* priority: INT_MAX >= x >= 0 */
901 };
902 
903 
904 static void ipmi_new_smi(int if_num)
905 {
906 	ipmi_register_watchdog(if_num);
907 }
908 
909 static void ipmi_smi_gone(int if_num)
910 {
911 	/* This can never be called, because once the watchdog is
912 	   registered, the interface can't go away until the watchdog
913 	   is unregistered. */
914 }
915 
916 static struct ipmi_smi_watcher smi_watcher =
917 {
918 	.owner    = THIS_MODULE,
919 	.new_smi  = ipmi_new_smi,
920 	.smi_gone = ipmi_smi_gone
921 };
922 
923 static int __init ipmi_wdog_init(void)
924 {
925 	int rv;
926 
927 	printk(KERN_INFO PFX "driver version "
928 	       IPMI_WATCHDOG_VERSION "\n");
929 
930 	if (strcmp(action, "reset") == 0) {
931 		action_val = WDOG_TIMEOUT_RESET;
932 	} else if (strcmp(action, "none") == 0) {
933 		action_val = WDOG_TIMEOUT_NONE;
934 	} else if (strcmp(action, "power_cycle") == 0) {
935 		action_val = WDOG_TIMEOUT_POWER_CYCLE;
936 	} else if (strcmp(action, "power_off") == 0) {
937 		action_val = WDOG_TIMEOUT_POWER_DOWN;
938 	} else {
939 		action_val = WDOG_TIMEOUT_RESET;
940 		printk(KERN_INFO PFX "Unknown action '%s', defaulting to"
941 		       " reset\n", action);
942 	}
943 
944 	if (strcmp(preaction, "pre_none") == 0) {
945 		preaction_val = WDOG_PRETIMEOUT_NONE;
946 	} else if (strcmp(preaction, "pre_smi") == 0) {
947 		preaction_val = WDOG_PRETIMEOUT_SMI;
948 #ifdef HAVE_NMI_HANDLER
949 	} else if (strcmp(preaction, "pre_nmi") == 0) {
950 		preaction_val = WDOG_PRETIMEOUT_NMI;
951 #endif
952 	} else if (strcmp(preaction, "pre_int") == 0) {
953 		preaction_val = WDOG_PRETIMEOUT_MSG_INT;
954 	} else {
955 		preaction_val = WDOG_PRETIMEOUT_NONE;
956 		printk(KERN_INFO PFX "Unknown preaction '%s', defaulting to"
957 		       " none\n", preaction);
958 	}
959 
960 	if (strcmp(preop, "preop_none") == 0) {
961 		preop_val = WDOG_PREOP_NONE;
962 	} else if (strcmp(preop, "preop_panic") == 0) {
963 		preop_val = WDOG_PREOP_PANIC;
964 	} else if (strcmp(preop, "preop_give_data") == 0) {
965 		preop_val = WDOG_PREOP_GIVE_DATA;
966 	} else {
967 		preop_val = WDOG_PREOP_NONE;
968 		printk(KERN_INFO PFX "Unknown preop '%s', defaulting to"
969 		       " none\n", preop);
970 	}
971 
972 #ifdef HAVE_NMI_HANDLER
973 	if (preaction_val == WDOG_PRETIMEOUT_NMI) {
974 		if (preop_val == WDOG_PREOP_GIVE_DATA) {
975 			printk(KERN_WARNING PFX "Pretimeout op is to give data"
976 			       " but NMI pretimeout is enabled, setting"
977 			       " pretimeout op to none\n");
978 			preop_val = WDOG_PREOP_NONE;
979 		}
980 #ifdef CONFIG_X86_LOCAL_APIC
981 		if (nmi_watchdog == NMI_IO_APIC) {
982 			printk(KERN_WARNING PFX "nmi_watchdog is set to IO APIC"
983 			       " mode (value is %d), that is incompatible"
984 			       " with using NMI in the IPMI watchdog."
985 			       " Disabling IPMI nmi pretimeout.\n",
986 			       nmi_watchdog);
987 			preaction_val = WDOG_PRETIMEOUT_NONE;
988 		} else {
989 #endif
990 		rv = request_nmi(&ipmi_nmi_handler);
991 		if (rv) {
992 			printk(KERN_WARNING PFX "Can't register nmi handler\n");
993 			return rv;
994 		}
995 #ifdef CONFIG_X86_LOCAL_APIC
996 		}
997 #endif
998 	}
999 #endif
1000 
1001 	rv = ipmi_smi_watcher_register(&smi_watcher);
1002 	if (rv) {
1003 #ifdef HAVE_NMI_HANDLER
1004 		if (preaction_val == WDOG_PRETIMEOUT_NMI)
1005 			release_nmi(&ipmi_nmi_handler);
1006 #endif
1007 		printk(KERN_WARNING PFX "can't register smi watcher\n");
1008 		return rv;
1009 	}
1010 
1011 	register_reboot_notifier(&wdog_reboot_notifier);
1012 	notifier_chain_register(&panic_notifier_list, &wdog_panic_notifier);
1013 
1014 	return 0;
1015 }
1016 
1017 static __exit void ipmi_unregister_watchdog(void)
1018 {
1019 	int rv;
1020 
1021 	down_write(&register_sem);
1022 
1023 #ifdef HAVE_NMI_HANDLER
1024 	if (preaction_val == WDOG_PRETIMEOUT_NMI)
1025 		release_nmi(&ipmi_nmi_handler);
1026 #endif
1027 
1028 	notifier_chain_unregister(&panic_notifier_list, &wdog_panic_notifier);
1029 	unregister_reboot_notifier(&wdog_reboot_notifier);
1030 
1031 	if (! watchdog_user)
1032 		goto out;
1033 
1034 	/* Make sure no one can call us any more. */
1035 	misc_deregister(&ipmi_wdog_miscdev);
1036 
1037 	/* Wait to make sure the message makes it out.  The lower layer has
1038 	   pointers to our buffers, we want to make sure they are done before
1039 	   we release our memory. */
1040 	while (atomic_read(&set_timeout_tofree)) {
1041 		set_current_state(TASK_UNINTERRUPTIBLE);
1042 		schedule_timeout(1);
1043 	}
1044 
1045 	/* Disconnect from IPMI. */
1046 	rv = ipmi_destroy_user(watchdog_user);
1047 	if (rv) {
1048 		printk(KERN_WARNING PFX "error unlinking from IPMI: %d\n",
1049 		       rv);
1050 	}
1051 	watchdog_user = NULL;
1052 
1053  out:
1054 	up_write(&register_sem);
1055 }
1056 
1057 static void __exit ipmi_wdog_exit(void)
1058 {
1059 	ipmi_smi_watcher_unregister(&smi_watcher);
1060 	ipmi_unregister_watchdog();
1061 }
1062 module_exit(ipmi_wdog_exit);
1063 module_init(ipmi_wdog_init);
1064 MODULE_LICENSE("GPL");
1065