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