[PATCH] Fix IPMI watchdog set_param_str() using kstrdup
[linux-2.6/verdex.git] / drivers / char / ipmi / ipmi_watchdog.c
blob6b634e8d95191369649538f55ae9b666cc7acf73
1 /*
2 * ipmi_watchdog.c
4 * A watchdog timer based upon the IPMI interface.
6 * Author: MontaVista Software, Inc.
7 * Corey Minyard <minyard@mvista.com>
8 * source@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
54 #include <asm/apic.h>
55 #endif
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
89 mode. */
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)
127 #endif
128 #ifndef WDIOC_SET_PRETIMEOUT
129 #define WDIOC_SET_PRETIMEOUT _IOW(WATCHDOG_IOCTL_BASE, 21, int)
130 #endif
131 #ifndef WDIOC_GET_PRETIMEOUT
132 #define WDIOC_GET_PRETIMEOUT _IOW(WATCHDOG_IOCTL_BASE, 22, int)
133 #endif
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
179 and ready. */
180 static int start_now;
182 static int set_param_int(const char *val, struct kernel_param *kp)
184 char *endp;
185 int l;
186 int rv = 0;
188 if (!val)
189 return -EINVAL;
190 l = simple_strtoul(val, &endp, 0);
191 if (endp == val)
192 return -EINVAL;
194 down_read(&register_sem);
195 *((int *)kp->arg) = l;
196 if (watchdog_user)
197 rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
198 up_read(&register_sem);
200 return rv;
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;
218 int rv = 0;
219 char valcp[16];
220 char *s;
222 strncpy(valcp, val, 16);
223 valcp[15] = '\0';
225 s = strstrip(valcp);
227 down_read(&register_sem);
228 rv = fn(s, NULL);
229 if (rv)
230 goto out_unlock;
232 check_parms();
233 if (watchdog_user)
234 rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
236 out_unlock:
237 up_read(&register_sem);
238 return rv;
241 static int get_param_str(char *buffer, struct kernel_param *kp)
243 action_fn fn = (action_fn) kp->arg;
244 int rv;
246 rv = fn(NULL, buffer);
247 if (rv)
248 return rv;
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);
256 if (rv)
257 return rv;
258 if ((ifnum_to_use < 0) || (ifnum_to_use == watchdog_ifnum))
259 return 0;
261 ipmi_unregister_watchdog(watchdog_ifnum);
262 ipmi_register_watchdog(ifnum_to_use);
263 return 0;
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 "
270 "interface");
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];
356 int rv;
357 struct ipmi_system_interface_addr addr;
358 int hbnow = 0;
361 data[0] = 0;
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. */
372 hbnow = 1;
375 data[1] = 0;
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;
380 } else {
381 WDOG_SET_PRETIMEOUT_ACT(data[1], WDOG_PRETIMEOUT_NONE);
382 data[2] = 0; /* No pretimeout. */
384 data[3] = 0;
385 WDOG_SET_TIMEOUT(data[4], data[5], timeout);
387 addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
388 addr.channel = IPMI_BMC_CHANNEL;
389 addr.lun = 0;
391 msg.netfn = 0x06;
392 msg.cmd = IPMI_WDOG_SET_TIMER;
393 msg.data = data;
394 msg.data_len = sizeof(data);
395 rv = ipmi_request_supply_msgs(watchdog_user,
396 (struct ipmi_addr *) &addr,
398 &msg,
399 NULL,
400 smi_msg,
401 recv_msg,
403 if (rv) {
404 printk(KERN_WARNING PFX "set timeout error: %d\n",
405 rv);
408 if (send_heartbeat_now)
409 *send_heartbeat_now = hbnow;
411 return rv;
414 static int ipmi_set_timeout(int do_heartbeat)
416 int send_heartbeat_now;
417 int rv;
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);
428 if (rv) {
429 mutex_unlock(&set_timeout_lock);
430 goto out;
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);
443 out:
444 return rv;
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
465 calls. */
466 static void panic_halt_ipmi_set_timeout(void)
468 int send_heartbeat_now;
469 int rv;
471 rv = i_ipmi_set_timeout(&panic_halt_smi_msg,
472 &panic_halt_recv_msg,
473 &send_heartbeat_now);
474 if (!rv) {
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;
518 int rv;
519 struct ipmi_system_interface_addr addr;
521 if (ipmi_ignore_heartbeat) {
522 return 0;
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
533 above operation. */
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);
546 return 0;
549 addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
550 addr.channel = IPMI_BMC_CHANNEL;
551 addr.lun = 0;
553 msg.netfn = 0x06;
554 msg.cmd = IPMI_WDOG_RESET_TIMER;
555 msg.data = NULL;
556 msg.data_len = 0;
557 rv = ipmi_request_supply_msgs(watchdog_user,
558 (struct ipmi_addr *) &addr,
560 &msg,
561 NULL,
562 &heartbeat_smi_msg,
563 &heartbeat_recv_msg,
565 if (rv) {
566 mutex_unlock(&heartbeat_lock);
567 printk(KERN_WARNING PFX "heartbeat failure: %d\n",
568 rv);
569 return rv;
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
578 an error here. */
579 rv = -EINVAL;
582 mutex_unlock(&heartbeat_lock);
584 return rv;
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)
596 return;
598 addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
599 addr.channel = IPMI_BMC_CHANNEL;
600 addr.lun = 0;
602 msg.netfn = 0x06;
603 msg.cmd = IPMI_WDOG_RESET_TIMER;
604 msg.data = NULL;
605 msg.data_len = 0;
606 ipmi_request_supply_msgs(watchdog_user,
607 (struct ipmi_addr *) &addr,
609 &msg,
610 NULL,
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,
620 .identity = "IPMI"
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;
627 int i;
628 int val;
630 switch(cmd) {
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));
637 if (i)
638 return -EFAULT;
639 timeout = val;
640 return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
642 case WDIOC_GETTIMEOUT:
643 i = copy_to_user(argp, &timeout, sizeof(timeout));
644 if (i)
645 return -EFAULT;
646 return 0;
648 case WDIOC_SET_PRETIMEOUT:
649 i = copy_from_user(&val, argp, sizeof(int));
650 if (i)
651 return -EFAULT;
652 pretimeout = val;
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));
657 if (i)
658 return -EFAULT;
659 return 0;
661 case WDIOC_KEEPALIVE:
662 return ipmi_heartbeat();
664 case WDIOC_SETOPTIONS:
665 i = copy_from_user(&val, argp, sizeof(int));
666 if (i)
667 return -EFAULT;
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);
680 return 0;
682 case WDIOC_GETSTATUS:
683 val = 0;
684 i = copy_to_user(argp, &val, sizeof(val));
685 if (i)
686 return -EFAULT;
687 return 0;
689 default:
690 return -ENOIOCTLCMD;
694 static ssize_t ipmi_write(struct file *file,
695 const char __user *buf,
696 size_t len,
697 loff_t *ppos)
699 int rv;
701 if (len) {
702 if (!nowayout) {
703 size_t i;
705 /* In case it was set long ago */
706 expect_close = 0;
708 for (i = 0; i != len; i++) {
709 char c;
711 if (get_user(c, buf + i))
712 return -EFAULT;
713 if (c == 'V')
714 expect_close = 42;
717 rv = ipmi_heartbeat();
718 if (rv)
719 return rv;
720 return 1;
722 return 0;
725 static ssize_t ipmi_read(struct file *file,
726 char __user *buf,
727 size_t count,
728 loff_t *ppos)
730 int rv = 0;
731 wait_queue_t wait;
733 if (count <= 0)
734 return 0;
736 /* Reading returns if the pretimeout has gone off, and it only does
737 it once per pretimeout. */
738 spin_lock(&ipmi_read_lock);
739 if (!data_to_read) {
740 if (file->f_flags & O_NONBLOCK) {
741 rv = -EAGAIN;
742 goto out;
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);
750 schedule();
751 spin_lock(&ipmi_read_lock);
753 remove_wait_queue(&read_q, &wait);
755 if (signal_pending(current)) {
756 rv = -ERESTARTSYS;
757 goto out;
760 data_to_read = 0;
762 out:
763 spin_unlock(&ipmi_read_lock);
765 if (rv == 0) {
766 if (copy_to_user(buf, &data_to_read, 1))
767 rv = -EFAULT;
768 else
769 rv = 1;
772 return rv;
775 static int ipmi_open(struct inode *ino, struct file *filep)
777 switch (iminor(ino)) {
778 case WATCHDOG_MINOR:
779 if (test_and_set_bit(0, &ipmi_wdog_open))
780 return -EBUSY;
782 /* Don't start the timer now, let it start on the
783 first heartbeat. */
784 ipmi_start_timer_on_heartbeat = 1;
785 return nonseekable_open(ino, filep);
787 default:
788 return (-ENODEV);
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);
799 if (data_to_read)
800 mask |= (POLLIN | POLLRDNORM);
801 spin_unlock(&ipmi_read_lock);
803 return mask;
806 static int ipmi_fasync(int fd, struct file *file, int on)
808 int result;
810 result = fasync_helper(fd, file, on, &fasync_q);
812 return (result);
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);
821 } else {
822 printk(KERN_CRIT PFX
823 "Unexpected close, not stopping watchdog!\n");
824 ipmi_heartbeat();
826 clear_bit(0, &ipmi_wdog_open);
829 ipmi_fasync (-1, filep, 0);
830 expect_close = 0;
832 return 0;
835 static const struct file_operations ipmi_wdog_fops = {
836 .owner = THIS_MODULE,
837 .read = ipmi_read,
838 .poll = ipmi_poll,
839 .write = ipmi_write,
840 .ioctl = ipmi_ioctl,
841 .open = ipmi_open,
842 .release = ipmi_close,
843 .fasync = ipmi_fasync,
846 static struct miscdevice ipmi_wdog_miscdev = {
847 .minor = WATCHDOG_MINOR,
848 .name = "watchdog",
849 .fops = &ipmi_wdog_fops
852 static void ipmi_wdog_msg_handler(struct ipmi_recv_msg *msg,
853 void *handler_data)
855 if (msg->msg.data[0] != 0) {
856 printk(KERN_ERR PFX "response: Error %x on cmd %x\n",
857 msg->msg.data[0],
858 msg->msg.cmd);
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);
872 data_to_read = 1;
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)
894 int rv = -EBUSY;
896 down_write(&register_sem);
897 if (watchdog_user)
898 goto out;
900 if ((ifnum_to_use >= 0) && (ifnum_to_use != ipmi_intf))
901 goto out;
903 watchdog_ifnum = ipmi_intf;
905 rv = ipmi_create_user(ipmi_intf, &ipmi_hndlrs, NULL, &watchdog_user);
906 if (rv < 0) {
907 printk(KERN_CRIT PFX "Unable to register with ipmi\n");
908 goto out;
911 ipmi_get_version(watchdog_user,
912 &ipmi_version_major,
913 &ipmi_version_minor);
915 rv = misc_register(&ipmi_wdog_miscdev);
916 if (rv < 0) {
917 ipmi_destroy_user(watchdog_user);
918 watchdog_user = NULL;
919 printk(KERN_CRIT PFX "Unable to register misc device\n");
922 out:
923 up_write(&register_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)
936 int rv;
938 down_write(&register_sem);
940 if (!watchdog_user)
941 goto out;
943 if (watchdog_ifnum != ipmi_intf)
944 goto out;
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);
957 if (rv) {
958 printk(KERN_WARNING PFX "error unlinking from IPMI: %d\n",
959 rv);
961 watchdog_user = NULL;
963 out:
964 up_write(&register_sem);
967 #ifdef HAVE_NMI_HANDLER
968 static int
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)
973 return NOTIFY_DONE;
975 /* If no one else handled the NMI, we assume it was the IPMI
976 watchdog. */
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");
986 return NOTIFY_DONE;
989 static struct nmi_handler ipmi_nmi_handler =
991 .link = LIST_HEAD_INIT(ipmi_nmi_handler.link),
992 .dev_name = "ipmi_watchdog",
993 .dev_id = NULL,
994 .handler = ipmi_nmi,
995 .priority = 0, /* Call us last. */
997 int nmi_handler_registered;
998 #endif
1000 static int wdog_reboot_handler(struct notifier_block *this,
1001 unsigned long code,
1002 void *unused)
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. */
1018 timeout = 120;
1019 pretimeout = 0;
1020 ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
1021 panic_halt_ipmi_set_timeout();
1024 return NOTIFY_OK;
1027 static struct notifier_block wdog_reboot_notifier = {
1028 .notifier_call = wdog_reboot_handler,
1029 .next = NULL,
1030 .priority = 0
1033 static int wdog_panic_handler(struct notifier_block *this,
1034 unsigned long event,
1035 void *unused)
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;
1048 timeout = 255;
1049 pretimeout = 0;
1050 panic_halt_ipmi_set_timeout();
1053 return NOTIFY_OK;
1056 static struct notifier_block wdog_panic_notifier = {
1057 .notifier_call = wdog_panic_handler,
1058 .next = NULL,
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)
1082 if (outval)
1083 strcpy(outval, action);
1085 if (!inval)
1086 return 0;
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;
1096 else
1097 return -EINVAL;
1098 strcpy(action, inval);
1099 return 0;
1102 static int preaction_op(const char *inval, char *outval)
1104 if (outval)
1105 strcpy(outval, preaction);
1107 if (!inval)
1108 return 0;
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;
1117 #endif
1118 else if (strcmp(inval, "pre_int") == 0)
1119 preaction_val = WDOG_PRETIMEOUT_MSG_INT;
1120 else
1121 return -EINVAL;
1122 strcpy(preaction, inval);
1123 return 0;
1126 static int preop_op(const char *inval, char *outval)
1128 if (outval)
1129 strcpy(outval, preop);
1131 if (!inval)
1132 return 0;
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;
1140 else
1141 return -EINVAL;
1142 strcpy(preop, inval);
1143 return 0;
1146 static void check_parms(void)
1148 #ifdef HAVE_NMI_HANDLER
1149 int do_nmi = 0;
1150 int rv;
1152 if (preaction_val == WDOG_PRETIMEOUT_NMI) {
1153 do_nmi = 1;
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);
1159 do_nmi = 0;
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",
1167 nmi_watchdog);
1168 preaction_val = WDOG_PRETIMEOUT_NONE;
1169 do_nmi = 0;
1171 #endif
1173 if (do_nmi && !nmi_handler_registered) {
1174 rv = request_nmi(&ipmi_nmi_handler);
1175 if (rv) {
1176 printk(KERN_WARNING PFX
1177 "Can't register nmi handler\n");
1178 return;
1179 } else
1180 nmi_handler_registered = 1;
1181 } else if (!do_nmi && nmi_handler_registered) {
1182 release_nmi(&ipmi_nmi_handler);
1183 nmi_handler_registered = 0;
1185 #endif
1188 static int __init ipmi_wdog_init(void)
1190 int rv;
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"
1207 " none\n", preop);
1210 check_parms();
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);
1217 if (rv) {
1218 #ifdef HAVE_NMI_HANDLER
1219 if (preaction_val == WDOG_PRETIMEOUT_NMI)
1220 release_nmi(&ipmi_nmi_handler);
1221 #endif
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");
1226 return rv;
1229 printk(KERN_INFO PFX "driver initialized\n");
1231 return 0;
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);
1242 #endif
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.");