4 * A watchdog timer based upon the IPMI interface.
6 * Author: MontaVista Software, Inc.
7 * Corey Minyard <minyard@mvista.com>
10 * Copyright 2002 MontaVista Software Inc.
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.
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.
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.
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/ipmi.h>
37 #include <linux/ipmi_smi.h>
38 #include <linux/watchdog.h>
39 #include <linux/miscdevice.h>
40 #include <linux/init.h>
41 #include <linux/completion.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 #include <linux/string.h>
51 #include <linux/ctype.h>
52 #include <asm/atomic.h>
53 #ifdef CONFIG_X86_LOCAL_APIC
57 #define PFX "IPMI Watchdog: "
60 * The IPMI command/response information for the watchdog timer.
63 /* values for byte 1 of the set command, byte 2 of the get response. */
64 #define WDOG_DONT_LOG (1 << 7)
65 #define WDOG_DONT_STOP_ON_SET (1 << 6)
66 #define WDOG_SET_TIMER_USE(byte, use) \
67 byte = ((byte) & 0xf8) | ((use) & 0x7)
68 #define WDOG_GET_TIMER_USE(byte) ((byte) & 0x7)
69 #define WDOG_TIMER_USE_BIOS_FRB2 1
70 #define WDOG_TIMER_USE_BIOS_POST 2
71 #define WDOG_TIMER_USE_OS_LOAD 3
72 #define WDOG_TIMER_USE_SMS_OS 4
73 #define WDOG_TIMER_USE_OEM 5
75 /* values for byte 2 of the set command, byte 3 of the get response. */
76 #define WDOG_SET_PRETIMEOUT_ACT(byte, use) \
77 byte = ((byte) & 0x8f) | (((use) & 0x7) << 4)
78 #define WDOG_GET_PRETIMEOUT_ACT(byte) (((byte) >> 4) & 0x7)
79 #define WDOG_PRETIMEOUT_NONE 0
80 #define WDOG_PRETIMEOUT_SMI 1
81 #define WDOG_PRETIMEOUT_NMI 2
82 #define WDOG_PRETIMEOUT_MSG_INT 3
84 /* Operations that can be performed on a pretimout. */
85 #define WDOG_PREOP_NONE 0
86 #define WDOG_PREOP_PANIC 1
87 #define WDOG_PREOP_GIVE_DATA 2 /* Cause data to be available to
88 read. Doesn't work in NMI
91 /* Actions to perform on a full timeout. */
92 #define WDOG_SET_TIMEOUT_ACT(byte, use) \
93 byte = ((byte) & 0xf8) | ((use) & 0x7)
94 #define WDOG_GET_TIMEOUT_ACT(byte) ((byte) & 0x7)
95 #define WDOG_TIMEOUT_NONE 0
96 #define WDOG_TIMEOUT_RESET 1
97 #define WDOG_TIMEOUT_POWER_DOWN 2
98 #define WDOG_TIMEOUT_POWER_CYCLE 3
100 /* Byte 3 of the get command, byte 4 of the get response is the
101 pre-timeout in seconds. */
103 /* Bits for setting byte 4 of the set command, byte 5 of the get response. */
104 #define WDOG_EXPIRE_CLEAR_BIOS_FRB2 (1 << 1)
105 #define WDOG_EXPIRE_CLEAR_BIOS_POST (1 << 2)
106 #define WDOG_EXPIRE_CLEAR_OS_LOAD (1 << 3)
107 #define WDOG_EXPIRE_CLEAR_SMS_OS (1 << 4)
108 #define WDOG_EXPIRE_CLEAR_OEM (1 << 5)
110 /* Setting/getting the watchdog timer value. This is for bytes 5 and
111 6 (the timeout time) of the set command, and bytes 6 and 7 (the
112 timeout time) and 8 and 9 (the current countdown value) of the
113 response. The timeout value is given in seconds (in the command it
114 is 100ms intervals). */
115 #define WDOG_SET_TIMEOUT(byte1, byte2, val) \
116 (byte1) = (((val) * 10) & 0xff), (byte2) = (((val) * 10) >> 8)
117 #define WDOG_GET_TIMEOUT(byte1, byte2) \
118 (((byte1) | ((byte2) << 8)) / 10)
120 #define IPMI_WDOG_RESET_TIMER 0x22
121 #define IPMI_WDOG_SET_TIMER 0x24
122 #define IPMI_WDOG_GET_TIMER 0x25
124 /* These are here until the real ones get into the watchdog.h interface. */
125 #ifndef WDIOC_GETTIMEOUT
126 #define WDIOC_GETTIMEOUT _IOW(WATCHDOG_IOCTL_BASE, 20, int)
128 #ifndef WDIOC_SET_PRETIMEOUT
129 #define WDIOC_SET_PRETIMEOUT _IOW(WATCHDOG_IOCTL_BASE, 21, int)
131 #ifndef WDIOC_GET_PRETIMEOUT
132 #define WDIOC_GET_PRETIMEOUT _IOW(WATCHDOG_IOCTL_BASE, 22, int)
135 static int nowayout = WATCHDOG_NOWAYOUT;
137 static ipmi_user_t watchdog_user;
138 static int watchdog_ifnum;
140 /* Default the timeout to 10 seconds. */
141 static int timeout = 10;
143 /* The pre-timeout is disabled by default. */
144 static int pretimeout;
146 /* Default action is to reset the board on a timeout. */
147 static unsigned char action_val = WDOG_TIMEOUT_RESET;
149 static char action[16] = "reset";
151 static unsigned char preaction_val = WDOG_PRETIMEOUT_NONE;
153 static char preaction[16] = "pre_none";
155 static unsigned char preop_val = WDOG_PREOP_NONE;
157 static char preop[16] = "preop_none";
158 static DEFINE_SPINLOCK(ipmi_read_lock);
159 static char data_to_read;
160 static DECLARE_WAIT_QUEUE_HEAD(read_q);
161 static struct fasync_struct *fasync_q;
162 static char pretimeout_since_last_heartbeat;
163 static char expect_close;
165 static int ifnum_to_use = -1;
167 static DECLARE_RWSEM(register_sem);
169 /* Parameters to ipmi_set_timeout */
170 #define IPMI_SET_TIMEOUT_NO_HB 0
171 #define IPMI_SET_TIMEOUT_HB_IF_NECESSARY 1
172 #define IPMI_SET_TIMEOUT_FORCE_HB 2
174 static int ipmi_set_timeout(int do_heartbeat);
175 static void ipmi_register_watchdog(int ipmi_intf);
176 static void ipmi_unregister_watchdog(int ipmi_intf);
178 /* If true, the driver will start running as soon as it is configured
180 static int start_now;
182 static int set_param_int(const char *val, struct kernel_param *kp)
190 l = simple_strtoul(val, &endp, 0);
194 down_read(®ister_sem);
195 *((int *)kp->arg) = l;
197 rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
198 up_read(®ister_sem);
203 static int get_param_int(char *buffer, struct kernel_param *kp)
205 return sprintf(buffer, "%i", *((int *)kp->arg));
208 typedef int (*action_fn)(const char *intval, char *outval);
210 static int action_op(const char *inval, char *outval);
211 static int preaction_op(const char *inval, char *outval);
212 static int preop_op(const char *inval, char *outval);
213 static void check_parms(void);
215 static int set_param_str(const char *val, struct kernel_param *kp)
217 action_fn fn = (action_fn) kp->arg;
222 strncpy(valcp, val, 16);
227 down_read(®ister_sem);
234 rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
237 up_read(®ister_sem);
241 static int get_param_str(char *buffer, struct kernel_param *kp)
243 action_fn fn = (action_fn) kp->arg;
246 rv = fn(NULL, buffer);
249 return strlen(buffer);
253 static int set_param_wdog_ifnum(const char *val, struct kernel_param *kp)
255 int rv = param_set_int(val, kp);
258 if ((ifnum_to_use < 0) || (ifnum_to_use == watchdog_ifnum))
261 ipmi_unregister_watchdog(watchdog_ifnum);
262 ipmi_register_watchdog(ifnum_to_use);
266 module_param_call(ifnum_to_use, set_param_wdog_ifnum, get_param_int,
267 &ifnum_to_use, 0644);
268 MODULE_PARM_DESC(ifnum_to_use, "The interface number to use for the watchdog "
269 "timer. Setting to -1 defaults to the first registered "
272 module_param_call(timeout, set_param_int, get_param_int, &timeout, 0644);
273 MODULE_PARM_DESC(timeout, "Timeout value in seconds.");
275 module_param_call(pretimeout, set_param_int, get_param_int, &pretimeout, 0644);
276 MODULE_PARM_DESC(pretimeout, "Pretimeout value in seconds.");
278 module_param_call(action, set_param_str, get_param_str, action_op, 0644);
279 MODULE_PARM_DESC(action, "Timeout action. One of: "
280 "reset, none, power_cycle, power_off.");
282 module_param_call(preaction, set_param_str, get_param_str, preaction_op, 0644);
283 MODULE_PARM_DESC(preaction, "Pretimeout action. One of: "
284 "pre_none, pre_smi, pre_nmi, pre_int.");
286 module_param_call(preop, set_param_str, get_param_str, preop_op, 0644);
287 MODULE_PARM_DESC(preop, "Pretimeout driver operation. One of: "
288 "preop_none, preop_panic, preop_give_data.");
290 module_param(start_now, int, 0444);
291 MODULE_PARM_DESC(start_now, "Set to 1 to start the watchdog as"
292 "soon as the driver is loaded.");
294 module_param(nowayout, int, 0644);
295 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
296 "(default=CONFIG_WATCHDOG_NOWAYOUT)");
298 /* Default state of the timer. */
299 static unsigned char ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
301 /* If shutting down via IPMI, we ignore the heartbeat. */
302 static int ipmi_ignore_heartbeat;
304 /* Is someone using the watchdog? Only one user is allowed. */
305 static unsigned long ipmi_wdog_open;
307 /* If set to 1, the heartbeat command will set the state to reset and
308 start the timer. The timer doesn't normally run when the driver is
309 first opened until the heartbeat is set the first time, this
310 variable is used to accomplish this. */
311 static int ipmi_start_timer_on_heartbeat;
313 /* IPMI version of the BMC. */
314 static unsigned char ipmi_version_major;
315 static unsigned char ipmi_version_minor;
317 /* If a pretimeout occurs, this is used to allow only one panic to happen. */
318 static atomic_t preop_panic_excl = ATOMIC_INIT(-1);
320 static int ipmi_heartbeat(void);
321 static void panic_halt_ipmi_heartbeat(void);
324 /* We use a mutex to make sure that only one thing can send a set
325 timeout at one time, because we only have one copy of the data.
326 The mutex is claimed when the set_timeout is sent and freed
327 when both messages are free. */
328 static atomic_t set_timeout_tofree = ATOMIC_INIT(0);
329 static DEFINE_MUTEX(set_timeout_lock);
330 static DECLARE_COMPLETION(set_timeout_wait);
331 static void set_timeout_free_smi(struct ipmi_smi_msg *msg)
333 if (atomic_dec_and_test(&set_timeout_tofree))
334 complete(&set_timeout_wait);
336 static void set_timeout_free_recv(struct ipmi_recv_msg *msg)
338 if (atomic_dec_and_test(&set_timeout_tofree))
339 complete(&set_timeout_wait);
341 static struct ipmi_smi_msg set_timeout_smi_msg =
343 .done = set_timeout_free_smi
345 static struct ipmi_recv_msg set_timeout_recv_msg =
347 .done = set_timeout_free_recv
350 static int i_ipmi_set_timeout(struct ipmi_smi_msg *smi_msg,
351 struct ipmi_recv_msg *recv_msg,
352 int *send_heartbeat_now)
354 struct kernel_ipmi_msg msg;
355 unsigned char data[6];
357 struct ipmi_system_interface_addr addr;
362 WDOG_SET_TIMER_USE(data[0], WDOG_TIMER_USE_SMS_OS);
364 if ((ipmi_version_major > 1)
365 || ((ipmi_version_major == 1) && (ipmi_version_minor >= 5)))
367 /* This is an IPMI 1.5-only feature. */
368 data[0] |= WDOG_DONT_STOP_ON_SET;
369 } else if (ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
370 /* In ipmi 1.0, setting the timer stops the watchdog, we
371 need to start it back up again. */
376 WDOG_SET_TIMEOUT_ACT(data[1], ipmi_watchdog_state);
377 if ((pretimeout > 0) && (ipmi_watchdog_state != WDOG_TIMEOUT_NONE)) {
378 WDOG_SET_PRETIMEOUT_ACT(data[1], preaction_val);
379 data[2] = pretimeout;
381 WDOG_SET_PRETIMEOUT_ACT(data[1], WDOG_PRETIMEOUT_NONE);
382 data[2] = 0; /* No pretimeout. */
385 WDOG_SET_TIMEOUT(data[4], data[5], timeout);
387 addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
388 addr.channel = IPMI_BMC_CHANNEL;
392 msg.cmd = IPMI_WDOG_SET_TIMER;
394 msg.data_len = sizeof(data);
395 rv = ipmi_request_supply_msgs(watchdog_user,
396 (struct ipmi_addr *) &addr,
404 printk(KERN_WARNING PFX "set timeout error: %d\n",
408 if (send_heartbeat_now)
409 *send_heartbeat_now = hbnow;
414 static int ipmi_set_timeout(int do_heartbeat)
416 int send_heartbeat_now;
420 /* We can only send one of these at a time. */
421 mutex_lock(&set_timeout_lock);
423 atomic_set(&set_timeout_tofree, 2);
425 rv = i_ipmi_set_timeout(&set_timeout_smi_msg,
426 &set_timeout_recv_msg,
427 &send_heartbeat_now);
429 mutex_unlock(&set_timeout_lock);
433 wait_for_completion(&set_timeout_wait);
435 if ((do_heartbeat == IPMI_SET_TIMEOUT_FORCE_HB)
436 || ((send_heartbeat_now)
437 && (do_heartbeat == IPMI_SET_TIMEOUT_HB_IF_NECESSARY)))
439 rv = ipmi_heartbeat();
441 mutex_unlock(&set_timeout_lock);
447 static void dummy_smi_free(struct ipmi_smi_msg *msg)
450 static void dummy_recv_free(struct ipmi_recv_msg *msg)
453 static struct ipmi_smi_msg panic_halt_smi_msg =
455 .done = dummy_smi_free
457 static struct ipmi_recv_msg panic_halt_recv_msg =
459 .done = dummy_recv_free
462 /* Special call, doesn't claim any locks. This is only to be called
463 at panic or halt time, in run-to-completion mode, when the caller
464 is the only CPU and the only thing that will be going is these IPMI
466 static void panic_halt_ipmi_set_timeout(void)
468 int send_heartbeat_now;
471 rv = i_ipmi_set_timeout(&panic_halt_smi_msg,
472 &panic_halt_recv_msg,
473 &send_heartbeat_now);
475 if (send_heartbeat_now)
476 panic_halt_ipmi_heartbeat();
480 /* We use a semaphore to make sure that only one thing can send a
481 heartbeat at one time, because we only have one copy of the data.
482 The semaphore is claimed when the set_timeout is sent and freed
483 when both messages are free. */
484 static atomic_t heartbeat_tofree = ATOMIC_INIT(0);
485 static DEFINE_MUTEX(heartbeat_lock);
486 static DECLARE_COMPLETION(heartbeat_wait);
487 static void heartbeat_free_smi(struct ipmi_smi_msg *msg)
489 if (atomic_dec_and_test(&heartbeat_tofree))
490 complete(&heartbeat_wait);
492 static void heartbeat_free_recv(struct ipmi_recv_msg *msg)
494 if (atomic_dec_and_test(&heartbeat_tofree))
495 complete(&heartbeat_wait);
497 static struct ipmi_smi_msg heartbeat_smi_msg =
499 .done = heartbeat_free_smi
501 static struct ipmi_recv_msg heartbeat_recv_msg =
503 .done = heartbeat_free_recv
506 static struct ipmi_smi_msg panic_halt_heartbeat_smi_msg =
508 .done = dummy_smi_free
510 static struct ipmi_recv_msg panic_halt_heartbeat_recv_msg =
512 .done = dummy_recv_free
515 static int ipmi_heartbeat(void)
517 struct kernel_ipmi_msg msg;
519 struct ipmi_system_interface_addr addr;
521 if (ipmi_ignore_heartbeat) {
525 if (ipmi_start_timer_on_heartbeat) {
526 ipmi_start_timer_on_heartbeat = 0;
527 ipmi_watchdog_state = action_val;
528 return ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
529 } else if (pretimeout_since_last_heartbeat) {
530 /* A pretimeout occurred, make sure we set the timeout.
531 We don't want to set the action, though, we want to
532 leave that alone (thus it can't be combined with the
534 pretimeout_since_last_heartbeat = 0;
535 return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
538 mutex_lock(&heartbeat_lock);
540 atomic_set(&heartbeat_tofree, 2);
542 /* Don't reset the timer if we have the timer turned off, that
543 re-enables the watchdog. */
544 if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE) {
545 mutex_unlock(&heartbeat_lock);
549 addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
550 addr.channel = IPMI_BMC_CHANNEL;
554 msg.cmd = IPMI_WDOG_RESET_TIMER;
557 rv = ipmi_request_supply_msgs(watchdog_user,
558 (struct ipmi_addr *) &addr,
566 mutex_unlock(&heartbeat_lock);
567 printk(KERN_WARNING PFX "heartbeat failure: %d\n",
572 /* Wait for the heartbeat to be sent. */
573 wait_for_completion(&heartbeat_wait);
575 if (heartbeat_recv_msg.msg.data[0] != 0) {
576 /* Got an error in the heartbeat response. It was already
577 reported in ipmi_wdog_msg_handler, but we should return
582 mutex_unlock(&heartbeat_lock);
587 static void panic_halt_ipmi_heartbeat(void)
589 struct kernel_ipmi_msg msg;
590 struct ipmi_system_interface_addr addr;
593 /* Don't reset the timer if we have the timer turned off, that
594 re-enables the watchdog. */
595 if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)
598 addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
599 addr.channel = IPMI_BMC_CHANNEL;
603 msg.cmd = IPMI_WDOG_RESET_TIMER;
606 ipmi_request_supply_msgs(watchdog_user,
607 (struct ipmi_addr *) &addr,
611 &panic_halt_heartbeat_smi_msg,
612 &panic_halt_heartbeat_recv_msg,
616 static struct watchdog_info ident =
618 .options = 0, /* WDIOF_SETTIMEOUT, */
619 .firmware_version = 1,
623 static int ipmi_ioctl(struct inode *inode, struct file *file,
624 unsigned int cmd, unsigned long arg)
626 void __user *argp = (void __user *)arg;
631 case WDIOC_GETSUPPORT:
632 i = copy_to_user(argp, &ident, sizeof(ident));
633 return i ? -EFAULT : 0;
635 case WDIOC_SETTIMEOUT:
636 i = copy_from_user(&val, argp, sizeof(int));
640 return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
642 case WDIOC_GETTIMEOUT:
643 i = copy_to_user(argp, &timeout, sizeof(timeout));
648 case WDIOC_SET_PRETIMEOUT:
649 i = copy_from_user(&val, argp, sizeof(int));
653 return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
655 case WDIOC_GET_PRETIMEOUT:
656 i = copy_to_user(argp, &pretimeout, sizeof(pretimeout));
661 case WDIOC_KEEPALIVE:
662 return ipmi_heartbeat();
664 case WDIOC_SETOPTIONS:
665 i = copy_from_user(&val, argp, sizeof(int));
668 if (val & WDIOS_DISABLECARD)
670 ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
671 ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
672 ipmi_start_timer_on_heartbeat = 0;
675 if (val & WDIOS_ENABLECARD)
677 ipmi_watchdog_state = action_val;
678 ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
682 case WDIOC_GETSTATUS:
684 i = copy_to_user(argp, &val, sizeof(val));
694 static ssize_t ipmi_write(struct file *file,
695 const char __user *buf,
705 /* In case it was set long ago */
708 for (i = 0; i != len; i++) {
711 if (get_user(c, buf + i))
717 rv = ipmi_heartbeat();
725 static ssize_t ipmi_read(struct file *file,
736 /* Reading returns if the pretimeout has gone off, and it only does
737 it once per pretimeout. */
738 spin_lock(&ipmi_read_lock);
740 if (file->f_flags & O_NONBLOCK) {
745 init_waitqueue_entry(&wait, current);
746 add_wait_queue(&read_q, &wait);
747 while (!data_to_read) {
748 set_current_state(TASK_INTERRUPTIBLE);
749 spin_unlock(&ipmi_read_lock);
751 spin_lock(&ipmi_read_lock);
753 remove_wait_queue(&read_q, &wait);
755 if (signal_pending(current)) {
763 spin_unlock(&ipmi_read_lock);
766 if (copy_to_user(buf, &data_to_read, 1))
775 static int ipmi_open(struct inode *ino, struct file *filep)
777 switch (iminor(ino)) {
779 if (test_and_set_bit(0, &ipmi_wdog_open))
782 /* Don't start the timer now, let it start on the
784 ipmi_start_timer_on_heartbeat = 1;
785 return nonseekable_open(ino, filep);
792 static unsigned int ipmi_poll(struct file *file, poll_table *wait)
794 unsigned int mask = 0;
796 poll_wait(file, &read_q, wait);
798 spin_lock(&ipmi_read_lock);
800 mask |= (POLLIN | POLLRDNORM);
801 spin_unlock(&ipmi_read_lock);
806 static int ipmi_fasync(int fd, struct file *file, int on)
810 result = fasync_helper(fd, file, on, &fasync_q);
815 static int ipmi_close(struct inode *ino, struct file *filep)
817 if (iminor(ino) == WATCHDOG_MINOR) {
818 if (expect_close == 42) {
819 ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
820 ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
823 "Unexpected close, not stopping watchdog!\n");
826 clear_bit(0, &ipmi_wdog_open);
829 ipmi_fasync (-1, filep, 0);
835 static const struct file_operations ipmi_wdog_fops = {
836 .owner = THIS_MODULE,
842 .release = ipmi_close,
843 .fasync = ipmi_fasync,
846 static struct miscdevice ipmi_wdog_miscdev = {
847 .minor = WATCHDOG_MINOR,
849 .fops = &ipmi_wdog_fops
852 static void ipmi_wdog_msg_handler(struct ipmi_recv_msg *msg,
855 if (msg->msg.data[0] != 0) {
856 printk(KERN_ERR PFX "response: Error %x on cmd %x\n",
861 ipmi_free_recv_msg(msg);
864 static void ipmi_wdog_pretimeout_handler(void *handler_data)
866 if (preaction_val != WDOG_PRETIMEOUT_NONE) {
867 if (preop_val == WDOG_PREOP_PANIC) {
868 if (atomic_inc_and_test(&preop_panic_excl))
869 panic("Watchdog pre-timeout");
870 } else if (preop_val == WDOG_PREOP_GIVE_DATA) {
871 spin_lock(&ipmi_read_lock);
873 wake_up_interruptible(&read_q);
874 kill_fasync(&fasync_q, SIGIO, POLL_IN);
876 spin_unlock(&ipmi_read_lock);
880 /* On some machines, the heartbeat will give
881 an error and not work unless we re-enable
882 the timer. So do so. */
883 pretimeout_since_last_heartbeat = 1;
886 static struct ipmi_user_hndl ipmi_hndlrs =
888 .ipmi_recv_hndl = ipmi_wdog_msg_handler,
889 .ipmi_watchdog_pretimeout = ipmi_wdog_pretimeout_handler
892 static void ipmi_register_watchdog(int ipmi_intf)
896 down_write(®ister_sem);
900 if ((ifnum_to_use >= 0) && (ifnum_to_use != ipmi_intf))
903 watchdog_ifnum = ipmi_intf;
905 rv = ipmi_create_user(ipmi_intf, &ipmi_hndlrs, NULL, &watchdog_user);
907 printk(KERN_CRIT PFX "Unable to register with ipmi\n");
911 ipmi_get_version(watchdog_user,
913 &ipmi_version_minor);
915 rv = misc_register(&ipmi_wdog_miscdev);
917 ipmi_destroy_user(watchdog_user);
918 watchdog_user = NULL;
919 printk(KERN_CRIT PFX "Unable to register misc device\n");
923 up_write(®ister_sem);
925 if ((start_now) && (rv == 0)) {
926 /* Run from startup, so start the timer now. */
927 start_now = 0; /* Disable this function after first startup. */
928 ipmi_watchdog_state = action_val;
929 ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
930 printk(KERN_INFO PFX "Starting now!\n");
934 static void ipmi_unregister_watchdog(int ipmi_intf)
938 down_write(®ister_sem);
943 if (watchdog_ifnum != ipmi_intf)
946 /* Make sure no one can call us any more. */
947 misc_deregister(&ipmi_wdog_miscdev);
949 /* Wait to make sure the message makes it out. The lower layer has
950 pointers to our buffers, we want to make sure they are done before
951 we release our memory. */
952 while (atomic_read(&set_timeout_tofree))
953 schedule_timeout_uninterruptible(1);
955 /* Disconnect from IPMI. */
956 rv = ipmi_destroy_user(watchdog_user);
958 printk(KERN_WARNING PFX "error unlinking from IPMI: %d\n",
961 watchdog_user = NULL;
964 up_write(®ister_sem);
967 #ifdef HAVE_NMI_HANDLER
969 ipmi_nmi(void *dev_id, int cpu, int handled)
971 /* If we are not expecting a timeout, ignore it. */
972 if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)
975 /* If no one else handled the NMI, we assume it was the IPMI
977 if ((!handled) && (preop_val == WDOG_PREOP_PANIC)) {
978 /* On some machines, the heartbeat will give
979 an error and not work unless we re-enable
980 the timer. So do so. */
981 pretimeout_since_last_heartbeat = 1;
982 if (atomic_inc_and_test(&preop_panic_excl))
983 panic(PFX "pre-timeout");
989 static struct nmi_handler ipmi_nmi_handler =
991 .link = LIST_HEAD_INIT(ipmi_nmi_handler.link),
992 .dev_name = "ipmi_watchdog",
995 .priority = 0, /* Call us last. */
997 int nmi_handler_registered;
1000 static int wdog_reboot_handler(struct notifier_block *this,
1004 static int reboot_event_handled = 0;
1006 if ((watchdog_user) && (!reboot_event_handled)) {
1007 /* Make sure we only do this once. */
1008 reboot_event_handled = 1;
1010 if (code == SYS_DOWN || code == SYS_HALT) {
1011 /* Disable the WDT if we are shutting down. */
1012 ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
1013 panic_halt_ipmi_set_timeout();
1014 } else if (ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
1015 /* Set a long timer to let the reboot happens, but
1016 reboot if it hangs, but only if the watchdog
1017 timer was already running. */
1020 ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
1021 panic_halt_ipmi_set_timeout();
1027 static struct notifier_block wdog_reboot_notifier = {
1028 .notifier_call = wdog_reboot_handler,
1033 static int wdog_panic_handler(struct notifier_block *this,
1034 unsigned long event,
1037 static int panic_event_handled = 0;
1039 /* On a panic, if we have a panic timeout, make sure to extend
1040 the watchdog timer to a reasonable value to complete the
1041 panic, if the watchdog timer is running. Plus the
1042 pretimeout is meaningless at panic time. */
1043 if (watchdog_user && !panic_event_handled &&
1044 ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
1045 /* Make sure we do this only once. */
1046 panic_event_handled = 1;
1050 panic_halt_ipmi_set_timeout();
1056 static struct notifier_block wdog_panic_notifier = {
1057 .notifier_call = wdog_panic_handler,
1059 .priority = 150 /* priority: INT_MAX >= x >= 0 */
1063 static void ipmi_new_smi(int if_num, struct device *device)
1065 ipmi_register_watchdog(if_num);
1068 static void ipmi_smi_gone(int if_num)
1070 ipmi_unregister_watchdog(if_num);
1073 static struct ipmi_smi_watcher smi_watcher =
1075 .owner = THIS_MODULE,
1076 .new_smi = ipmi_new_smi,
1077 .smi_gone = ipmi_smi_gone
1080 static int action_op(const char *inval, char *outval)
1083 strcpy(outval, action);
1088 if (strcmp(inval, "reset") == 0)
1089 action_val = WDOG_TIMEOUT_RESET;
1090 else if (strcmp(inval, "none") == 0)
1091 action_val = WDOG_TIMEOUT_NONE;
1092 else if (strcmp(inval, "power_cycle") == 0)
1093 action_val = WDOG_TIMEOUT_POWER_CYCLE;
1094 else if (strcmp(inval, "power_off") == 0)
1095 action_val = WDOG_TIMEOUT_POWER_DOWN;
1098 strcpy(action, inval);
1102 static int preaction_op(const char *inval, char *outval)
1105 strcpy(outval, preaction);
1110 if (strcmp(inval, "pre_none") == 0)
1111 preaction_val = WDOG_PRETIMEOUT_NONE;
1112 else if (strcmp(inval, "pre_smi") == 0)
1113 preaction_val = WDOG_PRETIMEOUT_SMI;
1114 #ifdef HAVE_NMI_HANDLER
1115 else if (strcmp(inval, "pre_nmi") == 0)
1116 preaction_val = WDOG_PRETIMEOUT_NMI;
1118 else if (strcmp(inval, "pre_int") == 0)
1119 preaction_val = WDOG_PRETIMEOUT_MSG_INT;
1122 strcpy(preaction, inval);
1126 static int preop_op(const char *inval, char *outval)
1129 strcpy(outval, preop);
1134 if (strcmp(inval, "preop_none") == 0)
1135 preop_val = WDOG_PREOP_NONE;
1136 else if (strcmp(inval, "preop_panic") == 0)
1137 preop_val = WDOG_PREOP_PANIC;
1138 else if (strcmp(inval, "preop_give_data") == 0)
1139 preop_val = WDOG_PREOP_GIVE_DATA;
1142 strcpy(preop, inval);
1146 static void check_parms(void)
1148 #ifdef HAVE_NMI_HANDLER
1152 if (preaction_val == WDOG_PRETIMEOUT_NMI) {
1154 if (preop_val == WDOG_PREOP_GIVE_DATA) {
1155 printk(KERN_WARNING PFX "Pretimeout op is to give data"
1156 " but NMI pretimeout is enabled, setting"
1157 " pretimeout op to none\n");
1158 preop_op("preop_none", NULL);
1161 #ifdef CONFIG_X86_LOCAL_APIC
1162 if (nmi_watchdog == NMI_IO_APIC) {
1163 printk(KERN_WARNING PFX "nmi_watchdog is set to IO APIC"
1164 " mode (value is %d), that is incompatible"
1165 " with using NMI in the IPMI watchdog."
1166 " Disabling IPMI nmi pretimeout.\n",
1168 preaction_val = WDOG_PRETIMEOUT_NONE;
1173 if (do_nmi && !nmi_handler_registered) {
1174 rv = request_nmi(&ipmi_nmi_handler);
1176 printk(KERN_WARNING PFX
1177 "Can't register nmi handler\n");
1180 nmi_handler_registered = 1;
1181 } else if (!do_nmi && nmi_handler_registered) {
1182 release_nmi(&ipmi_nmi_handler);
1183 nmi_handler_registered = 0;
1188 static int __init ipmi_wdog_init(void)
1192 if (action_op(action, NULL)) {
1193 action_op("reset", NULL);
1194 printk(KERN_INFO PFX "Unknown action '%s', defaulting to"
1195 " reset\n", action);
1198 if (preaction_op(preaction, NULL)) {
1199 preaction_op("pre_none", NULL);
1200 printk(KERN_INFO PFX "Unknown preaction '%s', defaulting to"
1201 " none\n", preaction);
1204 if (preop_op(preop, NULL)) {
1205 preop_op("preop_none", NULL);
1206 printk(KERN_INFO PFX "Unknown preop '%s', defaulting to"
1212 register_reboot_notifier(&wdog_reboot_notifier);
1213 atomic_notifier_chain_register(&panic_notifier_list,
1214 &wdog_panic_notifier);
1216 rv = ipmi_smi_watcher_register(&smi_watcher);
1218 #ifdef HAVE_NMI_HANDLER
1219 if (preaction_val == WDOG_PRETIMEOUT_NMI)
1220 release_nmi(&ipmi_nmi_handler);
1222 atomic_notifier_chain_unregister(&panic_notifier_list,
1223 &wdog_panic_notifier);
1224 unregister_reboot_notifier(&wdog_reboot_notifier);
1225 printk(KERN_WARNING PFX "can't register smi watcher\n");
1229 printk(KERN_INFO PFX "driver initialized\n");
1234 static void __exit ipmi_wdog_exit(void)
1236 ipmi_smi_watcher_unregister(&smi_watcher);
1237 ipmi_unregister_watchdog(watchdog_ifnum);
1239 #ifdef HAVE_NMI_HANDLER
1240 if (nmi_handler_registered)
1241 release_nmi(&ipmi_nmi_handler);
1244 atomic_notifier_chain_unregister(&panic_notifier_list,
1245 &wdog_panic_notifier);
1246 unregister_reboot_notifier(&wdog_reboot_notifier);
1248 module_exit(ipmi_wdog_exit);
1249 module_init(ipmi_wdog_init);
1250 MODULE_LICENSE("GPL");
1251 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
1252 MODULE_DESCRIPTION("watchdog timer based upon the IPMI interface.");