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