blob: 6b634e8d95191369649538f55ae9b666cc7acf73 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#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>
Corey Minyardd6dfd132006-03-31 02:30:41 -080041#include <linux/completion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#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>
Corey Minyardb3856762005-11-07 01:00:05 -080052#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#ifdef CONFIG_X86_LOCAL_APIC
54#include <asm/apic.h>
55#endif
56
57#define PFX "IPMI Watchdog: "
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/*
60 * The IPMI command/response information for the watchdog timer.
61 */
62
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
74
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
83
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. */
90
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
99
100/* Byte 3 of the get command, byte 4 of the get response is the
101 pre-timeout in seconds. */
102
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)
109
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)
119
120#define IPMI_WDOG_RESET_TIMER 0x22
121#define IPMI_WDOG_SET_TIMER 0x24
122#define IPMI_WDOG_GET_TIMER 0x25
123
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
134
Andrey Panin4bfdf372005-07-27 11:43:58 -0700135static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Randy Dunlap0c8204b2006-12-10 02:19:06 -0800137static ipmi_user_t watchdog_user;
Corey Minyardb2c03942006-12-06 20:41:00 -0800138static int watchdog_ifnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140/* Default the timeout to 10 seconds. */
141static int timeout = 10;
142
143/* The pre-timeout is disabled by default. */
Randy Dunlap0c8204b2006-12-10 02:19:06 -0800144static int pretimeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146/* Default action is to reset the board on a timeout. */
147static unsigned char action_val = WDOG_TIMEOUT_RESET;
148
149static char action[16] = "reset";
150
151static unsigned char preaction_val = WDOG_PRETIMEOUT_NONE;
152
153static char preaction[16] = "pre_none";
154
155static unsigned char preop_val = WDOG_PREOP_NONE;
156
157static char preop[16] = "preop_none";
158static DEFINE_SPINLOCK(ipmi_read_lock);
Randy Dunlap0c8204b2006-12-10 02:19:06 -0800159static char data_to_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160static DECLARE_WAIT_QUEUE_HEAD(read_q);
Randy Dunlap0c8204b2006-12-10 02:19:06 -0800161static struct fasync_struct *fasync_q;
162static char pretimeout_since_last_heartbeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163static char expect_close;
164
Corey Minyardb2c03942006-12-06 20:41:00 -0800165static int ifnum_to_use = -1;
166
Corey Minyardcc4673e2005-11-07 00:59:57 -0800167static DECLARE_RWSEM(register_sem);
168
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
173
174static int ipmi_set_timeout(int do_heartbeat);
Corey Minyardb2c03942006-12-06 20:41:00 -0800175static void ipmi_register_watchdog(int ipmi_intf);
176static void ipmi_unregister_watchdog(int ipmi_intf);
Corey Minyardcc4673e2005-11-07 00:59:57 -0800177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/* If true, the driver will start running as soon as it is configured
179 and ready. */
Randy Dunlap0c8204b2006-12-10 02:19:06 -0800180static int start_now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Corey Minyardcc4673e2005-11-07 00:59:57 -0800182static int set_param_int(const char *val, struct kernel_param *kp)
183{
184 char *endp;
185 int l;
186 int rv = 0;
187
188 if (!val)
189 return -EINVAL;
190 l = simple_strtoul(val, &endp, 0);
191 if (endp == val)
192 return -EINVAL;
193
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);
199
200 return rv;
201}
202
203static int get_param_int(char *buffer, struct kernel_param *kp)
204{
205 return sprintf(buffer, "%i", *((int *)kp->arg));
206}
207
208typedef int (*action_fn)(const char *intval, char *outval);
209
210static int action_op(const char *inval, char *outval);
211static int preaction_op(const char *inval, char *outval);
212static int preop_op(const char *inval, char *outval);
213static void check_parms(void);
214
215static int set_param_str(const char *val, struct kernel_param *kp)
216{
217 action_fn fn = (action_fn) kp->arg;
218 int rv = 0;
Sebastien Dugué43cdff92006-12-29 16:46:53 -0800219 char valcp[16];
220 char *s;
Corey Minyardcc4673e2005-11-07 00:59:57 -0800221
Sebastien Dugué43cdff92006-12-29 16:46:53 -0800222 strncpy(valcp, val, 16);
223 valcp[15] = '\0';
Pekka Enberg66f969d2006-06-23 02:05:45 -0700224
Sebastien Dugué43cdff92006-12-29 16:46:53 -0800225 s = strstrip(valcp);
Corey Minyardcc4673e2005-11-07 00:59:57 -0800226
227 down_read(&register_sem);
Pekka Enberg66f969d2006-06-23 02:05:45 -0700228 rv = fn(s, NULL);
Corey Minyardcc4673e2005-11-07 00:59:57 -0800229 if (rv)
230 goto out_unlock;
231
232 check_parms();
233 if (watchdog_user)
234 rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
235
236 out_unlock:
237 up_read(&register_sem);
238 return rv;
239}
240
241static int get_param_str(char *buffer, struct kernel_param *kp)
242{
243 action_fn fn = (action_fn) kp->arg;
244 int rv;
245
246 rv = fn(NULL, buffer);
247 if (rv)
248 return rv;
249 return strlen(buffer);
250}
251
Corey Minyardb2c03942006-12-06 20:41:00 -0800252
253static int set_param_wdog_ifnum(const char *val, struct kernel_param *kp)
254{
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;
260
261 ipmi_unregister_watchdog(watchdog_ifnum);
262 ipmi_register_watchdog(ifnum_to_use);
263 return 0;
264}
265
266module_param_call(ifnum_to_use, set_param_wdog_ifnum, get_param_int,
267 &ifnum_to_use, 0644);
268MODULE_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");
271
Corey Minyardcc4673e2005-11-07 00:59:57 -0800272module_param_call(timeout, set_param_int, get_param_int, &timeout, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273MODULE_PARM_DESC(timeout, "Timeout value in seconds.");
Corey Minyardcc4673e2005-11-07 00:59:57 -0800274
275module_param_call(pretimeout, set_param_int, get_param_int, &pretimeout, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276MODULE_PARM_DESC(pretimeout, "Pretimeout value in seconds.");
Corey Minyardcc4673e2005-11-07 00:59:57 -0800277
278module_param_call(action, set_param_str, get_param_str, action_op, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279MODULE_PARM_DESC(action, "Timeout action. One of: "
280 "reset, none, power_cycle, power_off.");
Corey Minyardcc4673e2005-11-07 00:59:57 -0800281
282module_param_call(preaction, set_param_str, get_param_str, preaction_op, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283MODULE_PARM_DESC(preaction, "Pretimeout action. One of: "
284 "pre_none, pre_smi, pre_nmi, pre_int.");
Corey Minyardcc4673e2005-11-07 00:59:57 -0800285
286module_param_call(preop, set_param_str, get_param_str, preop_op, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287MODULE_PARM_DESC(preop, "Pretimeout driver operation. One of: "
288 "preop_none, preop_panic, preop_give_data.");
Corey Minyardcc4673e2005-11-07 00:59:57 -0800289
Corey Minyardb2c03942006-12-06 20:41:00 -0800290module_param(start_now, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291MODULE_PARM_DESC(start_now, "Set to 1 to start the watchdog as"
292 "soon as the driver is loaded.");
Corey Minyardcc4673e2005-11-07 00:59:57 -0800293
294module_param(nowayout, int, 0644);
Corey Minyardb2c03942006-12-06 20:41:00 -0800295MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
296 "(default=CONFIG_WATCHDOG_NOWAYOUT)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298/* Default state of the timer. */
299static unsigned char ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
300
301/* If shutting down via IPMI, we ignore the heartbeat. */
Randy Dunlap0c8204b2006-12-10 02:19:06 -0800302static int ipmi_ignore_heartbeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304/* Is someone using the watchdog? Only one user is allowed. */
Randy Dunlap0c8204b2006-12-10 02:19:06 -0800305static unsigned long ipmi_wdog_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
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. */
Randy Dunlap0c8204b2006-12-10 02:19:06 -0800311static int ipmi_start_timer_on_heartbeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313/* IPMI version of the BMC. */
314static unsigned char ipmi_version_major;
315static unsigned char ipmi_version_minor;
316
Corey Minyardb3856762005-11-07 01:00:05 -0800317/* If a pretimeout occurs, this is used to allow only one panic to happen. */
318static atomic_t preop_panic_excl = ATOMIC_INIT(-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320static int ipmi_heartbeat(void);
321static void panic_halt_ipmi_heartbeat(void);
322
323
Corey Minyardd6dfd132006-03-31 02:30:41 -0800324/* We use a mutex to make sure that only one thing can send a set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 timeout at one time, because we only have one copy of the data.
Corey Minyardd6dfd132006-03-31 02:30:41 -0800326 The mutex is claimed when the set_timeout is sent and freed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 when both messages are free. */
328static atomic_t set_timeout_tofree = ATOMIC_INIT(0);
Corey Minyardd6dfd132006-03-31 02:30:41 -0800329static DEFINE_MUTEX(set_timeout_lock);
330static DECLARE_COMPLETION(set_timeout_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331static void set_timeout_free_smi(struct ipmi_smi_msg *msg)
332{
333 if (atomic_dec_and_test(&set_timeout_tofree))
Corey Minyardd6dfd132006-03-31 02:30:41 -0800334 complete(&set_timeout_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336static void set_timeout_free_recv(struct ipmi_recv_msg *msg)
337{
338 if (atomic_dec_and_test(&set_timeout_tofree))
Corey Minyardd6dfd132006-03-31 02:30:41 -0800339 complete(&set_timeout_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341static struct ipmi_smi_msg set_timeout_smi_msg =
342{
343 .done = set_timeout_free_smi
344};
345static struct ipmi_recv_msg set_timeout_recv_msg =
346{
347 .done = set_timeout_free_recv
348};
349
350static int i_ipmi_set_timeout(struct ipmi_smi_msg *smi_msg,
351 struct ipmi_recv_msg *recv_msg,
352 int *send_heartbeat_now)
353{
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;
359
360
361 data[0] = 0;
362 WDOG_SET_TIMER_USE(data[0], WDOG_TIMER_USE_SMS_OS);
363
364 if ((ipmi_version_major > 1)
365 || ((ipmi_version_major == 1) && (ipmi_version_minor >= 5)))
366 {
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;
373 }
374
375 data[1] = 0;
376 WDOG_SET_TIMEOUT_ACT(data[1], ipmi_watchdog_state);
Corey Minyard8f05ee92005-09-06 15:18:39 -0700377 if ((pretimeout > 0) && (ipmi_watchdog_state != WDOG_TIMEOUT_NONE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 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. */
383 }
384 data[3] = 0;
385 WDOG_SET_TIMEOUT(data[4], data[5], timeout);
386
387 addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
388 addr.channel = IPMI_BMC_CHANNEL;
389 addr.lun = 0;
390
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,
397 0,
398 &msg,
399 NULL,
400 smi_msg,
401 recv_msg,
402 1);
403 if (rv) {
404 printk(KERN_WARNING PFX "set timeout error: %d\n",
405 rv);
406 }
407
408 if (send_heartbeat_now)
409 *send_heartbeat_now = hbnow;
410
411 return rv;
412}
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414static int ipmi_set_timeout(int do_heartbeat)
415{
416 int send_heartbeat_now;
417 int rv;
418
419
420 /* We can only send one of these at a time. */
Corey Minyardd6dfd132006-03-31 02:30:41 -0800421 mutex_lock(&set_timeout_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 atomic_set(&set_timeout_tofree, 2);
424
425 rv = i_ipmi_set_timeout(&set_timeout_smi_msg,
426 &set_timeout_recv_msg,
427 &send_heartbeat_now);
428 if (rv) {
Corey Minyardd6dfd132006-03-31 02:30:41 -0800429 mutex_unlock(&set_timeout_lock);
430 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432
Corey Minyardd6dfd132006-03-31 02:30:41 -0800433 wait_for_completion(&set_timeout_wait);
434
435 if ((do_heartbeat == IPMI_SET_TIMEOUT_FORCE_HB)
436 || ((send_heartbeat_now)
437 && (do_heartbeat == IPMI_SET_TIMEOUT_HB_IF_NECESSARY)))
438 {
439 rv = ipmi_heartbeat();
440 }
441 mutex_unlock(&set_timeout_lock);
442
443out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return rv;
445}
446
447static void dummy_smi_free(struct ipmi_smi_msg *msg)
448{
449}
450static void dummy_recv_free(struct ipmi_recv_msg *msg)
451{
452}
453static struct ipmi_smi_msg panic_halt_smi_msg =
454{
455 .done = dummy_smi_free
456};
457static struct ipmi_recv_msg panic_halt_recv_msg =
458{
459 .done = dummy_recv_free
460};
461
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. */
466static void panic_halt_ipmi_set_timeout(void)
467{
468 int send_heartbeat_now;
469 int rv;
470
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();
477 }
478}
479
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. */
484static atomic_t heartbeat_tofree = ATOMIC_INIT(0);
Corey Minyardd6dfd132006-03-31 02:30:41 -0800485static DEFINE_MUTEX(heartbeat_lock);
486static DECLARE_COMPLETION(heartbeat_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487static void heartbeat_free_smi(struct ipmi_smi_msg *msg)
488{
489 if (atomic_dec_and_test(&heartbeat_tofree))
Corey Minyardd6dfd132006-03-31 02:30:41 -0800490 complete(&heartbeat_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492static void heartbeat_free_recv(struct ipmi_recv_msg *msg)
493{
494 if (atomic_dec_and_test(&heartbeat_tofree))
Corey Minyardd6dfd132006-03-31 02:30:41 -0800495 complete(&heartbeat_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497static struct ipmi_smi_msg heartbeat_smi_msg =
498{
499 .done = heartbeat_free_smi
500};
501static struct ipmi_recv_msg heartbeat_recv_msg =
502{
503 .done = heartbeat_free_recv
504};
505
506static struct ipmi_smi_msg panic_halt_heartbeat_smi_msg =
507{
508 .done = dummy_smi_free
509};
510static struct ipmi_recv_msg panic_halt_heartbeat_recv_msg =
511{
512 .done = dummy_recv_free
513};
514
515static int ipmi_heartbeat(void)
516{
517 struct kernel_ipmi_msg msg;
518 int rv;
519 struct ipmi_system_interface_addr addr;
520
521 if (ipmi_ignore_heartbeat) {
522 return 0;
523 }
524
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);
536 }
537
Corey Minyardd6dfd132006-03-31 02:30:41 -0800538 mutex_lock(&heartbeat_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 atomic_set(&heartbeat_tofree, 2);
541
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) {
Corey Minyardd6dfd132006-03-31 02:30:41 -0800545 mutex_unlock(&heartbeat_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return 0;
547 }
548
549 addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
550 addr.channel = IPMI_BMC_CHANNEL;
551 addr.lun = 0;
552
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,
559 0,
560 &msg,
561 NULL,
562 &heartbeat_smi_msg,
563 &heartbeat_recv_msg,
564 1);
565 if (rv) {
Corey Minyardd6dfd132006-03-31 02:30:41 -0800566 mutex_unlock(&heartbeat_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 printk(KERN_WARNING PFX "heartbeat failure: %d\n",
568 rv);
569 return rv;
570 }
571
572 /* Wait for the heartbeat to be sent. */
Corey Minyardd6dfd132006-03-31 02:30:41 -0800573 wait_for_completion(&heartbeat_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
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;
580 }
581
Corey Minyardd6dfd132006-03-31 02:30:41 -0800582 mutex_unlock(&heartbeat_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 return rv;
585}
586
587static void panic_halt_ipmi_heartbeat(void)
588{
589 struct kernel_ipmi_msg msg;
590 struct ipmi_system_interface_addr addr;
591
592
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;
597
598 addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
599 addr.channel = IPMI_BMC_CHANNEL;
600 addr.lun = 0;
601
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,
608 0,
609 &msg,
610 NULL,
611 &panic_halt_heartbeat_smi_msg,
612 &panic_halt_heartbeat_recv_msg,
613 1);
614}
615
Corey Minyard8a3628d2006-03-31 02:30:40 -0800616static struct watchdog_info ident =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 .options = 0, /* WDIOF_SETTIMEOUT, */
619 .firmware_version = 1,
620 .identity = "IPMI"
621};
622
623static int ipmi_ioctl(struct inode *inode, struct file *file,
624 unsigned int cmd, unsigned long arg)
625{
626 void __user *argp = (void __user *)arg;
627 int i;
628 int val;
629
630 switch(cmd) {
631 case WDIOC_GETSUPPORT:
632 i = copy_to_user(argp, &ident, sizeof(ident));
633 return i ? -EFAULT : 0;
634
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);
641
642 case WDIOC_GETTIMEOUT:
643 i = copy_to_user(argp, &timeout, sizeof(timeout));
644 if (i)
645 return -EFAULT;
646 return 0;
647
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);
654
655 case WDIOC_GET_PRETIMEOUT:
656 i = copy_to_user(argp, &pretimeout, sizeof(pretimeout));
657 if (i)
658 return -EFAULT;
659 return 0;
660
661 case WDIOC_KEEPALIVE:
662 return ipmi_heartbeat();
663
664 case WDIOC_SETOPTIONS:
665 i = copy_from_user(&val, argp, sizeof(int));
666 if (i)
667 return -EFAULT;
668 if (val & WDIOS_DISABLECARD)
669 {
670 ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
671 ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
672 ipmi_start_timer_on_heartbeat = 0;
673 }
674
675 if (val & WDIOS_ENABLECARD)
676 {
677 ipmi_watchdog_state = action_val;
678 ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
679 }
680 return 0;
681
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;
688
689 default:
690 return -ENOIOCTLCMD;
691 }
692}
693
694static ssize_t ipmi_write(struct file *file,
695 const char __user *buf,
696 size_t len,
697 loff_t *ppos)
698{
699 int rv;
700
701 if (len) {
702 if (!nowayout) {
703 size_t i;
704
705 /* In case it was set long ago */
706 expect_close = 0;
707
708 for (i = 0; i != len; i++) {
709 char c;
710
711 if (get_user(c, buf + i))
712 return -EFAULT;
713 if (c == 'V')
714 expect_close = 42;
715 }
716 }
717 rv = ipmi_heartbeat();
718 if (rv)
719 return rv;
720 return 1;
721 }
722 return 0;
723}
724
725static ssize_t ipmi_read(struct file *file,
726 char __user *buf,
727 size_t count,
728 loff_t *ppos)
729{
730 int rv = 0;
731 wait_queue_t wait;
732
733 if (count <= 0)
734 return 0;
735
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;
743 }
744
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);
752 }
753 remove_wait_queue(&read_q, &wait);
754
755 if (signal_pending(current)) {
756 rv = -ERESTARTSYS;
757 goto out;
758 }
759 }
760 data_to_read = 0;
761
762 out:
763 spin_unlock(&ipmi_read_lock);
764
765 if (rv == 0) {
766 if (copy_to_user(buf, &data_to_read, 1))
767 rv = -EFAULT;
768 else
769 rv = 1;
770 }
771
772 return rv;
773}
774
775static int ipmi_open(struct inode *ino, struct file *filep)
776{
Corey Minyarde8b33612005-09-06 15:18:45 -0700777 switch (iminor(ino)) {
778 case WATCHDOG_MINOR:
779 if (test_and_set_bit(0, &ipmi_wdog_open))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return -EBUSY;
781
Corey Minyarde8b33612005-09-06 15:18:45 -0700782 /* 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Corey Minyarde8b33612005-09-06 15:18:45 -0700787 default:
788 return (-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790}
791
792static unsigned int ipmi_poll(struct file *file, poll_table *wait)
793{
794 unsigned int mask = 0;
795
796 poll_wait(file, &read_q, wait);
797
798 spin_lock(&ipmi_read_lock);
799 if (data_to_read)
800 mask |= (POLLIN | POLLRDNORM);
801 spin_unlock(&ipmi_read_lock);
802
803 return mask;
804}
805
806static int ipmi_fasync(int fd, struct file *file, int on)
807{
808 int result;
809
810 result = fasync_helper(fd, file, on, &fasync_q);
811
812 return (result);
813}
814
815static int ipmi_close(struct inode *ino, struct file *filep)
816{
Corey Minyard8a3628d2006-03-31 02:30:40 -0800817 if (iminor(ino) == WATCHDOG_MINOR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (expect_close == 42) {
819 ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
820 ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 } else {
Corey Minyard8a3628d2006-03-31 02:30:40 -0800822 printk(KERN_CRIT PFX
823 "Unexpected close, not stopping watchdog!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 ipmi_heartbeat();
825 }
Corey Minyardec26d792005-05-01 08:59:11 -0700826 clear_bit(0, &ipmi_wdog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828
829 ipmi_fasync (-1, filep, 0);
830 expect_close = 0;
831
832 return 0;
833}
834
Arjan van de Ven62322d22006-07-03 00:24:21 -0700835static const struct file_operations ipmi_wdog_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 .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,
844};
845
846static struct miscdevice ipmi_wdog_miscdev = {
847 .minor = WATCHDOG_MINOR,
848 .name = "watchdog",
849 .fops = &ipmi_wdog_fops
850};
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852static void ipmi_wdog_msg_handler(struct ipmi_recv_msg *msg,
853 void *handler_data)
854{
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);
859 }
860
861 ipmi_free_recv_msg(msg);
862}
863
864static void ipmi_wdog_pretimeout_handler(void *handler_data)
865{
866 if (preaction_val != WDOG_PRETIMEOUT_NONE) {
Corey Minyardb3856762005-11-07 01:00:05 -0800867 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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 spin_lock(&ipmi_read_lock);
872 data_to_read = 1;
873 wake_up_interruptible(&read_q);
874 kill_fasync(&fasync_q, SIGIO, POLL_IN);
875
876 spin_unlock(&ipmi_read_lock);
877 }
878 }
879
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;
884}
885
886static struct ipmi_user_hndl ipmi_hndlrs =
887{
888 .ipmi_recv_hndl = ipmi_wdog_msg_handler,
889 .ipmi_watchdog_pretimeout = ipmi_wdog_pretimeout_handler
890};
891
892static void ipmi_register_watchdog(int ipmi_intf)
893{
894 int rv = -EBUSY;
895
896 down_write(&register_sem);
897 if (watchdog_user)
898 goto out;
899
Corey Minyardb2c03942006-12-06 20:41:00 -0800900 if ((ifnum_to_use >= 0) && (ifnum_to_use != ipmi_intf))
901 goto out;
902
903 watchdog_ifnum = ipmi_intf;
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 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;
909 }
910
911 ipmi_get_version(watchdog_user,
912 &ipmi_version_major,
913 &ipmi_version_minor);
914
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");
920 }
921
922 out:
923 up_write(&register_sem);
924
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");
931 }
932}
933
Corey Minyardb2c03942006-12-06 20:41:00 -0800934static void ipmi_unregister_watchdog(int ipmi_intf)
935{
936 int rv;
937
938 down_write(&register_sem);
939
940 if (!watchdog_user)
941 goto out;
942
943 if (watchdog_ifnum != ipmi_intf)
944 goto out;
945
946 /* Make sure no one can call us any more. */
947 misc_deregister(&ipmi_wdog_miscdev);
948
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);
954
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);
960 }
961 watchdog_user = NULL;
962
963 out:
964 up_write(&register_sem);
965}
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967#ifdef HAVE_NMI_HANDLER
968static int
David Howells7d12e782006-10-05 14:55:46 +0100969ipmi_nmi(void *dev_id, int cpu, int handled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Corey Minyard8f05ee92005-09-06 15:18:39 -0700971 /* If we are not expecting a timeout, ignore it. */
972 if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)
973 return NOTIFY_DONE;
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 /* If no one else handled the NMI, we assume it was the IPMI
976 watchdog. */
Corey Minyard8f05ee92005-09-06 15:18:39 -0700977 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;
Corey Minyardb3856762005-11-07 01:00:05 -0800982 if (atomic_inc_and_test(&preop_panic_excl))
983 panic(PFX "pre-timeout");
Corey Minyard8f05ee92005-09-06 15:18:39 -0700984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 return NOTIFY_DONE;
987}
988
989static struct nmi_handler ipmi_nmi_handler =
990{
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. */
996};
Corey Minyardcc4673e2005-11-07 00:59:57 -0800997int nmi_handler_registered;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998#endif
999
1000static int wdog_reboot_handler(struct notifier_block *this,
1001 unsigned long code,
1002 void *unused)
1003{
1004 static int reboot_event_handled = 0;
1005
1006 if ((watchdog_user) && (!reboot_event_handled)) {
1007 /* Make sure we only do this once. */
1008 reboot_event_handled = 1;
1009
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();
Corey Minyard96febe92006-06-28 04:26:55 -07001014 } else if (ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 /* Set a long timer to let the reboot happens, but
Corey Minyard96febe92006-06-28 04:26:55 -07001016 reboot if it hangs, but only if the watchdog
1017 timer was already running. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 timeout = 120;
1019 pretimeout = 0;
1020 ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
1021 panic_halt_ipmi_set_timeout();
1022 }
1023 }
1024 return NOTIFY_OK;
1025}
1026
1027static struct notifier_block wdog_reboot_notifier = {
1028 .notifier_call = wdog_reboot_handler,
1029 .next = NULL,
1030 .priority = 0
1031};
1032
1033static int wdog_panic_handler(struct notifier_block *this,
1034 unsigned long event,
1035 void *unused)
1036{
1037 static int panic_event_handled = 0;
1038
Corey Minyard96febe92006-06-28 04:26:55 -07001039 /* 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. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 panic_event_handled = 1;
1047
1048 timeout = 255;
1049 pretimeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 panic_halt_ipmi_set_timeout();
1051 }
1052
1053 return NOTIFY_OK;
1054}
1055
1056static struct notifier_block wdog_panic_notifier = {
1057 .notifier_call = wdog_panic_handler,
1058 .next = NULL,
1059 .priority = 150 /* priority: INT_MAX >= x >= 0 */
1060};
1061
1062
Corey Minyard50c812b2006-03-26 01:37:21 -08001063static void ipmi_new_smi(int if_num, struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 ipmi_register_watchdog(if_num);
1066}
1067
1068static void ipmi_smi_gone(int if_num)
1069{
Corey Minyardb2c03942006-12-06 20:41:00 -08001070 ipmi_unregister_watchdog(if_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
1073static struct ipmi_smi_watcher smi_watcher =
1074{
1075 .owner = THIS_MODULE,
1076 .new_smi = ipmi_new_smi,
1077 .smi_gone = ipmi_smi_gone
1078};
1079
Corey Minyardcc4673e2005-11-07 00:59:57 -08001080static int action_op(const char *inval, char *outval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Corey Minyardcc4673e2005-11-07 00:59:57 -08001082 if (outval)
1083 strcpy(outval, action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Corey Minyardcc4673e2005-11-07 00:59:57 -08001085 if (!inval)
1086 return 0;
1087
1088 if (strcmp(inval, "reset") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 action_val = WDOG_TIMEOUT_RESET;
Corey Minyardcc4673e2005-11-07 00:59:57 -08001090 else if (strcmp(inval, "none") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 action_val = WDOG_TIMEOUT_NONE;
Corey Minyardcc4673e2005-11-07 00:59:57 -08001092 else if (strcmp(inval, "power_cycle") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 action_val = WDOG_TIMEOUT_POWER_CYCLE;
Corey Minyardcc4673e2005-11-07 00:59:57 -08001094 else if (strcmp(inval, "power_off") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 action_val = WDOG_TIMEOUT_POWER_DOWN;
Corey Minyardcc4673e2005-11-07 00:59:57 -08001096 else
1097 return -EINVAL;
1098 strcpy(action, inval);
1099 return 0;
1100}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Corey Minyardcc4673e2005-11-07 00:59:57 -08001102static int preaction_op(const char *inval, char *outval)
1103{
1104 if (outval)
1105 strcpy(outval, preaction);
1106
1107 if (!inval)
1108 return 0;
1109
1110 if (strcmp(inval, "pre_none") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 preaction_val = WDOG_PRETIMEOUT_NONE;
Corey Minyardcc4673e2005-11-07 00:59:57 -08001112 else if (strcmp(inval, "pre_smi") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 preaction_val = WDOG_PRETIMEOUT_SMI;
1114#ifdef HAVE_NMI_HANDLER
Corey Minyardcc4673e2005-11-07 00:59:57 -08001115 else if (strcmp(inval, "pre_nmi") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 preaction_val = WDOG_PRETIMEOUT_NMI;
1117#endif
Corey Minyardcc4673e2005-11-07 00:59:57 -08001118 else if (strcmp(inval, "pre_int") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 preaction_val = WDOG_PRETIMEOUT_MSG_INT;
Corey Minyardcc4673e2005-11-07 00:59:57 -08001120 else
1121 return -EINVAL;
1122 strcpy(preaction, inval);
1123 return 0;
1124}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Corey Minyardcc4673e2005-11-07 00:59:57 -08001126static int preop_op(const char *inval, char *outval)
1127{
1128 if (outval)
1129 strcpy(outval, preop);
1130
1131 if (!inval)
1132 return 0;
1133
1134 if (strcmp(inval, "preop_none") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 preop_val = WDOG_PREOP_NONE;
Corey Minyardcc4673e2005-11-07 00:59:57 -08001136 else if (strcmp(inval, "preop_panic") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 preop_val = WDOG_PREOP_PANIC;
Corey Minyardcc4673e2005-11-07 00:59:57 -08001138 else if (strcmp(inval, "preop_give_data") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 preop_val = WDOG_PREOP_GIVE_DATA;
Corey Minyardcc4673e2005-11-07 00:59:57 -08001140 else
1141 return -EINVAL;
1142 strcpy(preop, inval);
1143 return 0;
1144}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Corey Minyardcc4673e2005-11-07 00:59:57 -08001146static void check_parms(void)
1147{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148#ifdef HAVE_NMI_HANDLER
Corey Minyardcc4673e2005-11-07 00:59:57 -08001149 int do_nmi = 0;
1150 int rv;
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (preaction_val == WDOG_PRETIMEOUT_NMI) {
Corey Minyardcc4673e2005-11-07 00:59:57 -08001153 do_nmi = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 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");
Corey Minyardcc4673e2005-11-07 00:59:57 -08001158 preop_op("preop_none", NULL);
1159 do_nmi = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
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;
Corey Minyardcc4673e2005-11-07 00:59:57 -08001169 do_nmi = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 }
1171#endif
1172 }
Corey Minyardcc4673e2005-11-07 00:59:57 -08001173 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;
1184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185#endif
Corey Minyardcc4673e2005-11-07 00:59:57 -08001186}
1187
1188static int __init ipmi_wdog_init(void)
1189{
1190 int rv;
1191
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);
1196 }
1197
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);
1202 }
1203
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);
1208 }
1209
1210 check_parms();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Corey Minyardb2c03942006-12-06 20:41:00 -08001212 register_reboot_notifier(&wdog_reboot_notifier);
1213 atomic_notifier_chain_register(&panic_notifier_list,
1214 &wdog_panic_notifier);
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 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
Corey Minyardb2c03942006-12-06 20:41:00 -08001222 atomic_notifier_chain_unregister(&panic_notifier_list,
1223 &wdog_panic_notifier);
1224 unregister_reboot_notifier(&wdog_reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 printk(KERN_WARNING PFX "can't register smi watcher\n");
1226 return rv;
1227 }
1228
Corey Minyard1fdd75b2005-09-06 15:18:42 -07001229 printk(KERN_INFO PFX "driver initialized\n");
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return 0;
1232}
1233
Corey Minyardb2c03942006-12-06 20:41:00 -08001234static void __exit ipmi_wdog_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Corey Minyardb2c03942006-12-06 20:41:00 -08001236 ipmi_smi_watcher_unregister(&smi_watcher);
1237 ipmi_unregister_watchdog(watchdog_ifnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239#ifdef HAVE_NMI_HANDLER
Corey Minyardcc4673e2005-11-07 00:59:57 -08001240 if (nmi_handler_registered)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 release_nmi(&ipmi_nmi_handler);
1242#endif
1243
Alan Sterne041c682006-03-27 01:16:30 -08001244 atomic_notifier_chain_unregister(&panic_notifier_list,
Corey Minyardb2c03942006-12-06 20:41:00 -08001245 &wdog_panic_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 unregister_reboot_notifier(&wdog_reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248module_exit(ipmi_wdog_exit);
1249module_init(ipmi_wdog_init);
1250MODULE_LICENSE("GPL");
Corey Minyard1fdd75b2005-09-06 15:18:42 -07001251MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
1252MODULE_DESCRIPTION("watchdog timer based upon the IPMI interface.");