ipmi_watchdog.c 34.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
 * ipmi_watchdog.c
 *
 * A watchdog timer based upon the IPMI interface.
 *
 * Author: MontaVista Software, Inc.
 *         Corey Minyard <minyard@mvista.com>
 *         source@mvista.com
 *
 * Copyright 2002 MontaVista Software Inc.
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *
 *
 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/ipmi.h>
#include <linux/ipmi_smi.h>
38
#include <linux/mutex.h>
L
Linus Torvalds 已提交
39 40 41
#include <linux/watchdog.h>
#include <linux/miscdevice.h>
#include <linux/init.h>
42
#include <linux/completion.h>
43
#include <linux/kdebug.h>
L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51
#include <linux/rwsem.h>
#include <linux/errno.h>
#include <asm/uaccess.h>
#include <linux/notifier.h>
#include <linux/nmi.h>
#include <linux/reboot.h>
#include <linux/wait.h>
#include <linux/poll.h>
52 53
#include <linux/string.h>
#include <linux/ctype.h>
C
Corey Minyard 已提交
54
#include <linux/delay.h>
A
Arun Sharma 已提交
55
#include <linux/atomic.h>
56

C
Corey Minyard 已提交
57
#ifdef CONFIG_X86
58 59 60 61 62 63 64 65 66
/*
 * This is ugly, but I've determined that x86 is the only architecture
 * that can reasonably support the IPMI NMI watchdog timeout at this
 * time.  If another architecture adds this capability somehow, it
 * will have to be a somewhat different mechanism and I have no idea
 * how it will work.  So in the unlikely event that another
 * architecture supports this, we can figure out a good generic
 * mechanism for it at that time.
 */
C
Corey Minyard 已提交
67
#include <asm/kdebug.h>
68
#include <asm/nmi.h>
C
Corey Minyard 已提交
69
#define HAVE_DIE_NMI
L
Linus Torvalds 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
#endif

#define	PFX "IPMI Watchdog: "

/*
 * The IPMI command/response information for the watchdog timer.
 */

/* values for byte 1 of the set command, byte 2 of the get response. */
#define WDOG_DONT_LOG		(1 << 7)
#define WDOG_DONT_STOP_ON_SET	(1 << 6)
#define WDOG_SET_TIMER_USE(byte, use) \
	byte = ((byte) & 0xf8) | ((use) & 0x7)
#define WDOG_GET_TIMER_USE(byte) ((byte) & 0x7)
#define WDOG_TIMER_USE_BIOS_FRB2	1
#define WDOG_TIMER_USE_BIOS_POST	2
#define WDOG_TIMER_USE_OS_LOAD		3
#define WDOG_TIMER_USE_SMS_OS		4
#define WDOG_TIMER_USE_OEM		5

/* values for byte 2 of the set command, byte 3 of the get response. */
#define WDOG_SET_PRETIMEOUT_ACT(byte, use) \
	byte = ((byte) & 0x8f) | (((use) & 0x7) << 4)
#define WDOG_GET_PRETIMEOUT_ACT(byte) (((byte) >> 4) & 0x7)
#define WDOG_PRETIMEOUT_NONE		0
#define WDOG_PRETIMEOUT_SMI		1
#define WDOG_PRETIMEOUT_NMI		2
#define WDOG_PRETIMEOUT_MSG_INT		3

/* Operations that can be performed on a pretimout. */
#define WDOG_PREOP_NONE		0
#define WDOG_PREOP_PANIC	1
102 103
/* Cause data to be available to read.  Doesn't work in NMI mode. */
#define WDOG_PREOP_GIVE_DATA	2
L
Linus Torvalds 已提交
104 105 106 107 108 109 110 111 112 113

/* Actions to perform on a full timeout. */
#define WDOG_SET_TIMEOUT_ACT(byte, use) \
	byte = ((byte) & 0xf8) | ((use) & 0x7)
#define WDOG_GET_TIMEOUT_ACT(byte) ((byte) & 0x7)
#define WDOG_TIMEOUT_NONE		0
#define WDOG_TIMEOUT_RESET		1
#define WDOG_TIMEOUT_POWER_DOWN		2
#define WDOG_TIMEOUT_POWER_CYCLE	3

114 115 116 117
/*
 * Byte 3 of the get command, byte 4 of the get response is the
 * pre-timeout in seconds.
 */
L
Linus Torvalds 已提交
118 119 120 121 122 123 124 125

/* Bits for setting byte 4 of the set command, byte 5 of the get response. */
#define WDOG_EXPIRE_CLEAR_BIOS_FRB2	(1 << 1)
#define WDOG_EXPIRE_CLEAR_BIOS_POST	(1 << 2)
#define WDOG_EXPIRE_CLEAR_OS_LOAD	(1 << 3)
#define WDOG_EXPIRE_CLEAR_SMS_OS	(1 << 4)
#define WDOG_EXPIRE_CLEAR_OEM		(1 << 5)

126 127 128 129 130 131 132
/*
 * Setting/getting the watchdog timer value.  This is for bytes 5 and
 * 6 (the timeout time) of the set command, and bytes 6 and 7 (the
 * timeout time) and 8 and 9 (the current countdown value) of the
 * response.  The timeout value is given in seconds (in the command it
 * is 100ms intervals).
 */
L
Linus Torvalds 已提交
133 134 135 136 137 138 139 140 141
#define WDOG_SET_TIMEOUT(byte1, byte2, val) \
	(byte1) = (((val) * 10) & 0xff), (byte2) = (((val) * 10) >> 8)
#define WDOG_GET_TIMEOUT(byte1, byte2) \
	(((byte1) | ((byte2) << 8)) / 10)

#define IPMI_WDOG_RESET_TIMER		0x22
#define IPMI_WDOG_SET_TIMER		0x24
#define IPMI_WDOG_GET_TIMER		0x25

142 143
#define IPMI_WDOG_TIMER_NOT_INIT_RESP	0x80

L
Linus Torvalds 已提交
144 145 146 147 148 149 150 151 152 153 154
/* These are here until the real ones get into the watchdog.h interface. */
#ifndef WDIOC_GETTIMEOUT
#define	WDIOC_GETTIMEOUT        _IOW(WATCHDOG_IOCTL_BASE, 20, int)
#endif
#ifndef WDIOC_SET_PRETIMEOUT
#define	WDIOC_SET_PRETIMEOUT     _IOW(WATCHDOG_IOCTL_BASE, 21, int)
#endif
#ifndef WDIOC_GET_PRETIMEOUT
#define	WDIOC_GET_PRETIMEOUT     _IOW(WATCHDOG_IOCTL_BASE, 22, int)
#endif

155
static DEFINE_MUTEX(ipmi_watchdog_mutex);
156
static int nowayout = WATCHDOG_NOWAYOUT;
L
Linus Torvalds 已提交
157

R
Randy Dunlap 已提交
158
static ipmi_user_t watchdog_user;
159
static int watchdog_ifnum;
L
Linus Torvalds 已提交
160 161 162 163 164

/* Default the timeout to 10 seconds. */
static int timeout = 10;

/* The pre-timeout is disabled by default. */
R
Randy Dunlap 已提交
165
static int pretimeout;
L
Linus Torvalds 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179

/* Default action is to reset the board on a timeout. */
static unsigned char action_val = WDOG_TIMEOUT_RESET;

static char action[16] = "reset";

static unsigned char preaction_val = WDOG_PRETIMEOUT_NONE;

static char preaction[16] = "pre_none";

static unsigned char preop_val = WDOG_PREOP_NONE;

static char preop[16] = "preop_none";
static DEFINE_SPINLOCK(ipmi_read_lock);
R
Randy Dunlap 已提交
180
static char data_to_read;
L
Linus Torvalds 已提交
181
static DECLARE_WAIT_QUEUE_HEAD(read_q);
R
Randy Dunlap 已提交
182 183
static struct fasync_struct *fasync_q;
static char pretimeout_since_last_heartbeat;
L
Linus Torvalds 已提交
184 185
static char expect_close;

186 187
static int ifnum_to_use = -1;

188 189 190 191 192 193
/* Parameters to ipmi_set_timeout */
#define IPMI_SET_TIMEOUT_NO_HB			0
#define IPMI_SET_TIMEOUT_HB_IF_NECESSARY	1
#define IPMI_SET_TIMEOUT_FORCE_HB		2

static int ipmi_set_timeout(int do_heartbeat);
194 195
static void ipmi_register_watchdog(int ipmi_intf);
static void ipmi_unregister_watchdog(int ipmi_intf);
196

197 198 199 200
/*
 * If true, the driver will start running as soon as it is configured
 * and ready.
 */
R
Randy Dunlap 已提交
201
static int start_now;
L
Linus Torvalds 已提交
202

203
static int set_param_timeout(const char *val, const struct kernel_param *kp)
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
{
	char *endp;
	int  l;
	int  rv = 0;

	if (!val)
		return -EINVAL;
	l = simple_strtoul(val, &endp, 0);
	if (endp == val)
		return -EINVAL;

	*((int *)kp->arg) = l;
	if (watchdog_user)
		rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);

	return rv;
}

222 223 224 225 226
static struct kernel_param_ops param_ops_timeout = {
	.set = set_param_timeout,
	.get = param_get_int,
};
#define param_check_timeout param_check_int
227 228 229 230 231 232 233 234

typedef int (*action_fn)(const char *intval, char *outval);

static int action_op(const char *inval, char *outval);
static int preaction_op(const char *inval, char *outval);
static int preop_op(const char *inval, char *outval);
static void check_parms(void);

235
static int set_param_str(const char *val, const struct kernel_param *kp)
236 237 238
{
	action_fn  fn = (action_fn) kp->arg;
	int        rv = 0;
239 240
	char       valcp[16];
	char       *s;
P
Pekka Enberg 已提交
241

242 243
	strncpy(valcp, val, 16);
	valcp[15] = '\0';
P
Pekka Enberg 已提交
244

245
	s = strstrip(valcp);
246

P
Pekka Enberg 已提交
247
	rv = fn(s, NULL);
248
	if (rv)
249
		goto out;
250 251 252 253 254

	check_parms();
	if (watchdog_user)
		rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);

255
 out:
256 257 258
	return rv;
}

259
static int get_param_str(char *buffer, const struct kernel_param *kp)
260 261 262 263 264 265 266 267 268 269
{
	action_fn fn = (action_fn) kp->arg;
	int       rv;

	rv = fn(NULL, buffer);
	if (rv)
		return rv;
	return strlen(buffer);
}

270

271
static int set_param_wdog_ifnum(const char *val, const struct kernel_param *kp)
272 273 274 275 276 277 278 279 280 281 282 283
{
	int rv = param_set_int(val, kp);
	if (rv)
		return rv;
	if ((ifnum_to_use < 0) || (ifnum_to_use == watchdog_ifnum))
		return 0;

	ipmi_unregister_watchdog(watchdog_ifnum);
	ipmi_register_watchdog(ifnum_to_use);
	return 0;
}

284 285 286 287 288 289 290 291 292 293 294 295 296
static struct kernel_param_ops param_ops_wdog_ifnum = {
	.set = set_param_wdog_ifnum,
	.get = param_get_int,
};

#define param_check_wdog_ifnum param_check_int

static struct kernel_param_ops param_ops_str = {
	.set = set_param_str,
	.get = get_param_str,
};

module_param(ifnum_to_use, wdog_ifnum, 0644);
297 298 299 300
MODULE_PARM_DESC(ifnum_to_use, "The interface number to use for the watchdog "
		 "timer.  Setting to -1 defaults to the first registered "
		 "interface");

301
module_param(timeout, timeout, 0644);
L
Linus Torvalds 已提交
302
MODULE_PARM_DESC(timeout, "Timeout value in seconds.");
303

304
module_param(pretimeout, timeout, 0644);
L
Linus Torvalds 已提交
305
MODULE_PARM_DESC(pretimeout, "Pretimeout value in seconds.");
306

307
module_param_cb(action, &param_ops_str, action_op, 0644);
L
Linus Torvalds 已提交
308 309
MODULE_PARM_DESC(action, "Timeout action. One of: "
		 "reset, none, power_cycle, power_off.");
310

311
module_param_cb(preaction, &param_ops_str, preaction_op, 0644);
L
Linus Torvalds 已提交
312 313
MODULE_PARM_DESC(preaction, "Pretimeout action.  One of: "
		 "pre_none, pre_smi, pre_nmi, pre_int.");
314

315
module_param_cb(preop, &param_ops_str, preop_op, 0644);
L
Linus Torvalds 已提交
316 317
MODULE_PARM_DESC(preop, "Pretimeout driver operation.  One of: "
		 "preop_none, preop_panic, preop_give_data.");
318

319
module_param(start_now, int, 0444);
L
Linus Torvalds 已提交
320 321
MODULE_PARM_DESC(start_now, "Set to 1 to start the watchdog as"
		 "soon as the driver is loaded.");
322 323

module_param(nowayout, int, 0644);
324 325
MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
		 "(default=CONFIG_WATCHDOG_NOWAYOUT)");
L
Linus Torvalds 已提交
326 327 328 329 330

/* Default state of the timer. */
static unsigned char ipmi_watchdog_state = WDOG_TIMEOUT_NONE;

/* If shutting down via IPMI, we ignore the heartbeat. */
R
Randy Dunlap 已提交
331
static int ipmi_ignore_heartbeat;
L
Linus Torvalds 已提交
332 333

/* Is someone using the watchdog?  Only one user is allowed. */
R
Randy Dunlap 已提交
334
static unsigned long ipmi_wdog_open;
L
Linus Torvalds 已提交
335

336 337 338 339 340 341
/*
 * If set to 1, the heartbeat command will set the state to reset and
 * start the timer.  The timer doesn't normally run when the driver is
 * first opened until the heartbeat is set the first time, this
 * variable is used to accomplish this.
 */
R
Randy Dunlap 已提交
342
static int ipmi_start_timer_on_heartbeat;
L
Linus Torvalds 已提交
343 344 345 346 347

/* IPMI version of the BMC. */
static unsigned char ipmi_version_major;
static unsigned char ipmi_version_minor;

348 349
/* If a pretimeout occurs, this is used to allow only one panic to happen. */
static atomic_t preop_panic_excl = ATOMIC_INIT(-1);
L
Linus Torvalds 已提交
350

C
Corey Minyard 已提交
351 352 353 354 355
#ifdef HAVE_DIE_NMI
static int testing_nmi;
static int nmi_handler_registered;
#endif

L
Linus Torvalds 已提交
356 357
static int ipmi_heartbeat(void);

358 359 360 361 362 363
/*
 * We use a mutex to make sure that only one thing can send a set
 * timeout at one time, because we only have one copy of the data.
 * The mutex is claimed when the set_timeout is sent and freed
 * when both messages are free.
 */
L
Linus Torvalds 已提交
364
static atomic_t set_timeout_tofree = ATOMIC_INIT(0);
365 366
static DEFINE_MUTEX(set_timeout_lock);
static DECLARE_COMPLETION(set_timeout_wait);
L
Linus Torvalds 已提交
367 368 369
static void set_timeout_free_smi(struct ipmi_smi_msg *msg)
{
    if (atomic_dec_and_test(&set_timeout_tofree))
370
	    complete(&set_timeout_wait);
L
Linus Torvalds 已提交
371 372 373 374
}
static void set_timeout_free_recv(struct ipmi_recv_msg *msg)
{
    if (atomic_dec_and_test(&set_timeout_tofree))
375
	    complete(&set_timeout_wait);
L
Linus Torvalds 已提交
376
}
377
static struct ipmi_smi_msg set_timeout_smi_msg = {
L
Linus Torvalds 已提交
378 379
	.done = set_timeout_free_smi
};
380
static struct ipmi_recv_msg set_timeout_recv_msg = {
L
Linus Torvalds 已提交
381 382
	.done = set_timeout_free_recv
};
383

L
Linus Torvalds 已提交
384 385 386 387 388 389 390 391 392 393 394
static int i_ipmi_set_timeout(struct ipmi_smi_msg  *smi_msg,
			      struct ipmi_recv_msg *recv_msg,
			      int                  *send_heartbeat_now)
{
	struct kernel_ipmi_msg            msg;
	unsigned char                     data[6];
	int                               rv;
	struct ipmi_system_interface_addr addr;
	int                               hbnow = 0;


C
Corey Minyard 已提交
395 396 397
	/* These can be cleared as we are setting the timeout. */
	pretimeout_since_last_heartbeat = 0;

L
Linus Torvalds 已提交
398 399 400 401
	data[0] = 0;
	WDOG_SET_TIMER_USE(data[0], WDOG_TIMER_USE_SMS_OS);

	if ((ipmi_version_major > 1)
402
	    || ((ipmi_version_major == 1) && (ipmi_version_minor >= 5))) {
L
Linus Torvalds 已提交
403 404 405
		/* This is an IPMI 1.5-only feature. */
		data[0] |= WDOG_DONT_STOP_ON_SET;
	} else if (ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
406 407 408 409
		/*
		 * In ipmi 1.0, setting the timer stops the watchdog, we
		 * need to start it back up again.
		 */
L
Linus Torvalds 已提交
410 411 412 413 414
		hbnow = 1;
	}

	data[1] = 0;
	WDOG_SET_TIMEOUT_ACT(data[1], ipmi_watchdog_state);
415
	if ((pretimeout > 0) && (ipmi_watchdog_state != WDOG_TIMEOUT_NONE)) {
L
Linus Torvalds 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
	    WDOG_SET_PRETIMEOUT_ACT(data[1], preaction_val);
	    data[2] = pretimeout;
	} else {
	    WDOG_SET_PRETIMEOUT_ACT(data[1], WDOG_PRETIMEOUT_NONE);
	    data[2] = 0; /* No pretimeout. */
	}
	data[3] = 0;
	WDOG_SET_TIMEOUT(data[4], data[5], timeout);

	addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
	addr.channel = IPMI_BMC_CHANNEL;
	addr.lun = 0;

	msg.netfn = 0x06;
	msg.cmd = IPMI_WDOG_SET_TIMER;
	msg.data = data;
	msg.data_len = sizeof(data);
	rv = ipmi_request_supply_msgs(watchdog_user,
				      (struct ipmi_addr *) &addr,
				      0,
				      &msg,
				      NULL,
				      smi_msg,
				      recv_msg,
				      1);
	if (rv) {
		printk(KERN_WARNING PFX "set timeout error: %d\n",
		       rv);
	}

	if (send_heartbeat_now)
	    *send_heartbeat_now = hbnow;

	return rv;
}

static int ipmi_set_timeout(int do_heartbeat)
{
	int send_heartbeat_now;
	int rv;


	/* We can only send one of these at a time. */
459
	mutex_lock(&set_timeout_lock);
L
Linus Torvalds 已提交
460 461 462 463 464 465 466

	atomic_set(&set_timeout_tofree, 2);

	rv = i_ipmi_set_timeout(&set_timeout_smi_msg,
				&set_timeout_recv_msg,
				&send_heartbeat_now);
	if (rv) {
467 468 469 470 471 472
		mutex_unlock(&set_timeout_lock);
		goto out;
	}

	wait_for_completion(&set_timeout_wait);

C
Corey Minyard 已提交
473 474
	mutex_unlock(&set_timeout_lock);

475 476 477 478
	if ((do_heartbeat == IPMI_SET_TIMEOUT_FORCE_HB)
	    || ((send_heartbeat_now)
		&& (do_heartbeat == IPMI_SET_TIMEOUT_HB_IF_NECESSARY)))
		rv = ipmi_heartbeat();
L
Linus Torvalds 已提交
479

480
out:
L
Linus Torvalds 已提交
481 482 483
	return rv;
}

C
Corey Minyard 已提交
484 485 486
static atomic_t panic_done_count = ATOMIC_INIT(0);

static void panic_smi_free(struct ipmi_smi_msg *msg)
L
Linus Torvalds 已提交
487
{
C
Corey Minyard 已提交
488
	atomic_dec(&panic_done_count);
L
Linus Torvalds 已提交
489
}
C
Corey Minyard 已提交
490 491 492 493 494
static void panic_recv_free(struct ipmi_recv_msg *msg)
{
	atomic_dec(&panic_done_count);
}

495
static struct ipmi_smi_msg panic_halt_heartbeat_smi_msg = {
C
Corey Minyard 已提交
496 497
	.done = panic_smi_free
};
498
static struct ipmi_recv_msg panic_halt_heartbeat_recv_msg = {
C
Corey Minyard 已提交
499 500 501 502 503 504 505 506 507
	.done = panic_recv_free
};

static void panic_halt_ipmi_heartbeat(void)
{
	struct kernel_ipmi_msg             msg;
	struct ipmi_system_interface_addr addr;
	int rv;

508 509 510 511
	/*
	 * Don't reset the timer if we have the timer turned off, that
	 * re-enables the watchdog.
	 */
C
Corey Minyard 已提交
512 513 514 515 516 517 518 519 520 521 522
	if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)
		return;

	addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
	addr.channel = IPMI_BMC_CHANNEL;
	addr.lun = 0;

	msg.netfn = 0x06;
	msg.cmd = IPMI_WDOG_RESET_TIMER;
	msg.data = NULL;
	msg.data_len = 0;
523
	atomic_add(2, &panic_done_count);
C
Corey Minyard 已提交
524 525 526 527 528 529 530 531
	rv = ipmi_request_supply_msgs(watchdog_user,
				      (struct ipmi_addr *) &addr,
				      0,
				      &msg,
				      NULL,
				      &panic_halt_heartbeat_smi_msg,
				      &panic_halt_heartbeat_recv_msg,
				      1);
532 533
	if (rv)
		atomic_sub(2, &panic_done_count);
L
Linus Torvalds 已提交
534
}
C
Corey Minyard 已提交
535

536
static struct ipmi_smi_msg panic_halt_smi_msg = {
C
Corey Minyard 已提交
537
	.done = panic_smi_free
L
Linus Torvalds 已提交
538
};
539
static struct ipmi_recv_msg panic_halt_recv_msg = {
C
Corey Minyard 已提交
540
	.done = panic_recv_free
L
Linus Torvalds 已提交
541 542
};

543 544 545 546 547 548
/*
 * Special call, doesn't claim any locks.  This is only to be called
 * at panic or halt time, in run-to-completion mode, when the caller
 * is the only CPU and the only thing that will be going is these IPMI
 * calls.
 */
L
Linus Torvalds 已提交
549 550 551 552 553
static void panic_halt_ipmi_set_timeout(void)
{
	int send_heartbeat_now;
	int rv;

C
Corey Minyard 已提交
554 555 556
	/* Wait for the messages to be free. */
	while (atomic_read(&panic_done_count) != 0)
		ipmi_poll_interface(watchdog_user);
557
	atomic_add(2, &panic_done_count);
L
Linus Torvalds 已提交
558 559 560
	rv = i_ipmi_set_timeout(&panic_halt_smi_msg,
				&panic_halt_recv_msg,
				&send_heartbeat_now);
561 562
	if (rv) {
		atomic_sub(2, &panic_done_count);
C
Corey Minyard 已提交
563 564
		printk(KERN_WARNING PFX
		       "Unable to extend the watchdog timeout.");
565 566 567 568
	} else {
		if (send_heartbeat_now)
			panic_halt_ipmi_heartbeat();
	}
C
Corey Minyard 已提交
569 570
	while (atomic_read(&panic_done_count) != 0)
		ipmi_poll_interface(watchdog_user);
L
Linus Torvalds 已提交
571 572
}

573 574 575 576 577 578
/*
 * We use a mutex to make sure that only one thing can send a
 * heartbeat at one time, because we only have one copy of the data.
 * The semaphore is claimed when the set_timeout is sent and freed
 * when both messages are free.
 */
L
Linus Torvalds 已提交
579
static atomic_t heartbeat_tofree = ATOMIC_INIT(0);
580 581
static DEFINE_MUTEX(heartbeat_lock);
static DECLARE_COMPLETION(heartbeat_wait);
L
Linus Torvalds 已提交
582 583 584
static void heartbeat_free_smi(struct ipmi_smi_msg *msg)
{
    if (atomic_dec_and_test(&heartbeat_tofree))
585
	    complete(&heartbeat_wait);
L
Linus Torvalds 已提交
586 587 588 589
}
static void heartbeat_free_recv(struct ipmi_recv_msg *msg)
{
    if (atomic_dec_and_test(&heartbeat_tofree))
590
	    complete(&heartbeat_wait);
L
Linus Torvalds 已提交
591
}
592
static struct ipmi_smi_msg heartbeat_smi_msg = {
L
Linus Torvalds 已提交
593 594
	.done = heartbeat_free_smi
};
595
static struct ipmi_recv_msg heartbeat_recv_msg = {
L
Linus Torvalds 已提交
596 597
	.done = heartbeat_free_recv
};
598

L
Linus Torvalds 已提交
599 600 601 602 603
static int ipmi_heartbeat(void)
{
	struct kernel_ipmi_msg            msg;
	int                               rv;
	struct ipmi_system_interface_addr addr;
604
	int				  timeout_retries = 0;
L
Linus Torvalds 已提交
605

C
Corey Minyard 已提交
606
	if (ipmi_ignore_heartbeat)
L
Linus Torvalds 已提交
607 608 609
		return 0;

	if (ipmi_start_timer_on_heartbeat) {
610
		ipmi_start_timer_on_heartbeat = 0;
L
Linus Torvalds 已提交
611 612 613
		ipmi_watchdog_state = action_val;
		return ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
	} else if (pretimeout_since_last_heartbeat) {
614 615 616 617 618 619
		/*
		 * A pretimeout occurred, make sure we set the timeout.
		 * We don't want to set the action, though, we want to
		 * leave that alone (thus it can't be combined with the
		 * above operation.
		 */
L
Linus Torvalds 已提交
620 621 622
		return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
	}

623
	mutex_lock(&heartbeat_lock);
L
Linus Torvalds 已提交
624

625
restart:
L
Linus Torvalds 已提交
626 627
	atomic_set(&heartbeat_tofree, 2);

628 629 630 631
	/*
	 * Don't reset the timer if we have the timer turned off, that
	 * re-enables the watchdog.
	 */
L
Linus Torvalds 已提交
632
	if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE) {
633
		mutex_unlock(&heartbeat_lock);
L
Linus Torvalds 已提交
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
		return 0;
	}

	addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
	addr.channel = IPMI_BMC_CHANNEL;
	addr.lun = 0;

	msg.netfn = 0x06;
	msg.cmd = IPMI_WDOG_RESET_TIMER;
	msg.data = NULL;
	msg.data_len = 0;
	rv = ipmi_request_supply_msgs(watchdog_user,
				      (struct ipmi_addr *) &addr,
				      0,
				      &msg,
				      NULL,
				      &heartbeat_smi_msg,
				      &heartbeat_recv_msg,
				      1);
	if (rv) {
654
		mutex_unlock(&heartbeat_lock);
L
Linus Torvalds 已提交
655 656 657 658 659 660
		printk(KERN_WARNING PFX "heartbeat failure: %d\n",
		       rv);
		return rv;
	}

	/* Wait for the heartbeat to be sent. */
661
	wait_for_completion(&heartbeat_wait);
L
Linus Torvalds 已提交
662

663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
	if (heartbeat_recv_msg.msg.data[0] == IPMI_WDOG_TIMER_NOT_INIT_RESP)  {
		timeout_retries++;
		if (timeout_retries > 3) {
			printk(KERN_ERR PFX ": Unable to restore the IPMI"
			       " watchdog's settings, giving up.\n");
			rv = -EIO;
			goto out_unlock;
		}

		/*
		 * The timer was not initialized, that means the BMC was
		 * probably reset and lost the watchdog information.  Attempt
		 * to restore the timer's info.  Note that we still hold
		 * the heartbeat lock, to keep a heartbeat from happening
		 * in this process, so must say no heartbeat to avoid a
		 * deadlock on this mutex.
		 */
		rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
		if (rv) {
			printk(KERN_ERR PFX ": Unable to send the command to"
			       " set the watchdog's settings, giving up.\n");
			goto out_unlock;
		}

		/* We might need a new heartbeat, so do it now */
		goto restart;
	} else if (heartbeat_recv_msg.msg.data[0] != 0) {
690 691 692 693 694 695
		/*
		 * Got an error in the heartbeat response.  It was already
		 * reported in ipmi_wdog_msg_handler, but we should return
		 * an error here.
		 */
		rv = -EINVAL;
L
Linus Torvalds 已提交
696 697
	}

698
out_unlock:
699
	mutex_unlock(&heartbeat_lock);
L
Linus Torvalds 已提交
700 701 702 703

	return rv;
}

704
static struct watchdog_info ident = {
L
Linus Torvalds 已提交
705 706 707 708 709
	.options	= 0,	/* WDIOF_SETTIMEOUT, */
	.firmware_version = 1,
	.identity	= "IPMI"
};

710
static int ipmi_ioctl(struct file *file,
L
Linus Torvalds 已提交
711 712 713 714 715 716
		      unsigned int cmd, unsigned long arg)
{
	void __user *argp = (void __user *)arg;
	int i;
	int val;

717
	switch (cmd) {
L
Linus Torvalds 已提交
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
	case WDIOC_GETSUPPORT:
		i = copy_to_user(argp, &ident, sizeof(ident));
		return i ? -EFAULT : 0;

	case WDIOC_SETTIMEOUT:
		i = copy_from_user(&val, argp, sizeof(int));
		if (i)
			return -EFAULT;
		timeout = val;
		return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);

	case WDIOC_GETTIMEOUT:
		i = copy_to_user(argp, &timeout, sizeof(timeout));
		if (i)
			return -EFAULT;
		return 0;

	case WDIOC_SET_PRETIMEOUT:
736
	case WDIOC_SETPRETIMEOUT:
L
Linus Torvalds 已提交
737 738 739 740 741 742 743
		i = copy_from_user(&val, argp, sizeof(int));
		if (i)
			return -EFAULT;
		pretimeout = val;
		return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);

	case WDIOC_GET_PRETIMEOUT:
744
	case WDIOC_GETPRETIMEOUT:
L
Linus Torvalds 已提交
745 746 747 748 749 750 751 752 753 754 755 756
		i = copy_to_user(argp, &pretimeout, sizeof(pretimeout));
		if (i)
			return -EFAULT;
		return 0;

	case WDIOC_KEEPALIVE:
		return ipmi_heartbeat();

	case WDIOC_SETOPTIONS:
		i = copy_from_user(&val, argp, sizeof(int));
		if (i)
			return -EFAULT;
757
		if (val & WDIOS_DISABLECARD) {
L
Linus Torvalds 已提交
758 759 760 761 762
			ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
			ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
			ipmi_start_timer_on_heartbeat = 0;
		}

763
		if (val & WDIOS_ENABLECARD) {
L
Linus Torvalds 已提交
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
			ipmi_watchdog_state = action_val;
			ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
		}
		return 0;

	case WDIOC_GETSTATUS:
		val = 0;
		i = copy_to_user(argp, &val, sizeof(val));
		if (i)
			return -EFAULT;
		return 0;

	default:
		return -ENOIOCTLCMD;
	}
}

781 782 783 784 785 786
static long ipmi_unlocked_ioctl(struct file *file,
				unsigned int cmd,
				unsigned long arg)
{
	int ret;

787
	mutex_lock(&ipmi_watchdog_mutex);
788
	ret = ipmi_ioctl(file, cmd, arg);
789
	mutex_unlock(&ipmi_watchdog_mutex);
790 791 792 793

	return ret;
}

L
Linus Torvalds 已提交
794 795 796 797 798 799 800 801
static ssize_t ipmi_write(struct file *file,
			  const char  __user *buf,
			  size_t      len,
			  loff_t      *ppos)
{
	int rv;

	if (len) {
802 803
		if (!nowayout) {
			size_t i;
L
Linus Torvalds 已提交
804 805 806 807

			/* In case it was set long ago */
			expect_close = 0;

808
			for (i = 0; i != len; i++) {
L
Linus Torvalds 已提交
809 810 811 812 813 814 815 816 817 818 819 820
				char c;

				if (get_user(c, buf + i))
					return -EFAULT;
				if (c == 'V')
					expect_close = 42;
			}
		}
		rv = ipmi_heartbeat();
		if (rv)
			return rv;
	}
821
	return len;
L
Linus Torvalds 已提交
822 823 824 825 826 827 828 829 830 831 832 833 834
}

static ssize_t ipmi_read(struct file *file,
			 char        __user *buf,
			 size_t      count,
			 loff_t      *ppos)
{
	int          rv = 0;
	wait_queue_t wait;

	if (count <= 0)
		return 0;

835 836 837 838
	/*
	 * Reading returns if the pretimeout has gone off, and it only does
	 * it once per pretimeout.
	 */
L
Linus Torvalds 已提交
839 840 841 842 843 844
	spin_lock(&ipmi_read_lock);
	if (!data_to_read) {
		if (file->f_flags & O_NONBLOCK) {
			rv = -EAGAIN;
			goto out;
		}
845

L
Linus Torvalds 已提交
846 847 848 849 850 851 852 853 854
		init_waitqueue_entry(&wait, current);
		add_wait_queue(&read_q, &wait);
		while (!data_to_read) {
			set_current_state(TASK_INTERRUPTIBLE);
			spin_unlock(&ipmi_read_lock);
			schedule();
			spin_lock(&ipmi_read_lock);
		}
		remove_wait_queue(&read_q, &wait);
855

L
Linus Torvalds 已提交
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
		if (signal_pending(current)) {
			rv = -ERESTARTSYS;
			goto out;
		}
	}
	data_to_read = 0;

 out:
	spin_unlock(&ipmi_read_lock);

	if (rv == 0) {
		if (copy_to_user(buf, &data_to_read, 1))
			rv = -EFAULT;
		else
			rv = 1;
	}

	return rv;
}

static int ipmi_open(struct inode *ino, struct file *filep)
{
878 879
	switch (iminor(ino)) {
	case WATCHDOG_MINOR:
C
Corey Minyard 已提交
880
		if (test_and_set_bit(0, &ipmi_wdog_open))
881
			return -EBUSY;
L
Linus Torvalds 已提交
882

A
Arnd Bergmann 已提交
883

884 885 886 887
		/*
		 * Don't start the timer now, let it start on the
		 * first heartbeat.
		 */
C
Corey Minyard 已提交
888 889
		ipmi_start_timer_on_heartbeat = 1;
		return nonseekable_open(ino, filep);
L
Linus Torvalds 已提交
890

C
Corey Minyard 已提交
891 892
	default:
		return (-ENODEV);
893
	}
L
Linus Torvalds 已提交
894 895 896 897 898
}

static unsigned int ipmi_poll(struct file *file, poll_table *wait)
{
	unsigned int mask = 0;
899

L
Linus Torvalds 已提交
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
	poll_wait(file, &read_q, wait);

	spin_lock(&ipmi_read_lock);
	if (data_to_read)
		mask |= (POLLIN | POLLRDNORM);
	spin_unlock(&ipmi_read_lock);

	return mask;
}

static int ipmi_fasync(int fd, struct file *file, int on)
{
	int result;

	result = fasync_helper(fd, file, on, &fasync_q);

	return (result);
}

static int ipmi_close(struct inode *ino, struct file *filep)
{
921
	if (iminor(ino) == WATCHDOG_MINOR) {
L
Linus Torvalds 已提交
922 923 924 925
		if (expect_close == 42) {
			ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
			ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
		} else {
926 927
			printk(KERN_CRIT PFX
			       "Unexpected close, not stopping watchdog!\n");
L
Linus Torvalds 已提交
928 929
			ipmi_heartbeat();
		}
930
		clear_bit(0, &ipmi_wdog_open);
L
Linus Torvalds 已提交
931 932 933 934 935 936 937
	}

	expect_close = 0;

	return 0;
}

938
static const struct file_operations ipmi_wdog_fops = {
L
Linus Torvalds 已提交
939 940 941 942
	.owner   = THIS_MODULE,
	.read    = ipmi_read,
	.poll    = ipmi_poll,
	.write   = ipmi_write,
943
	.unlocked_ioctl = ipmi_unlocked_ioctl,
L
Linus Torvalds 已提交
944 945 946
	.open    = ipmi_open,
	.release = ipmi_close,
	.fasync  = ipmi_fasync,
947
	.llseek  = no_llseek,
L
Linus Torvalds 已提交
948 949 950 951 952 953 954 955 956 957 958
};

static struct miscdevice ipmi_wdog_miscdev = {
	.minor		= WATCHDOG_MINOR,
	.name		= "watchdog",
	.fops		= &ipmi_wdog_fops
};

static void ipmi_wdog_msg_handler(struct ipmi_recv_msg *msg,
				  void                 *handler_data)
{
959 960 961 962 963 964
	if (msg->msg.cmd == IPMI_WDOG_RESET_TIMER &&
			msg->msg.data[0] == IPMI_WDOG_TIMER_NOT_INIT_RESP)
		printk(KERN_INFO PFX "response: The IPMI controller appears"
		       " to have been reset, will attempt to reinitialize"
		       " the watchdog timer\n");
	else if (msg->msg.data[0] != 0)
L
Linus Torvalds 已提交
965 966 967
		printk(KERN_ERR PFX "response: Error %x on cmd %x\n",
		       msg->msg.data[0],
		       msg->msg.cmd);
968

L
Linus Torvalds 已提交
969 970 971 972 973 974
	ipmi_free_recv_msg(msg);
}

static void ipmi_wdog_pretimeout_handler(void *handler_data)
{
	if (preaction_val != WDOG_PRETIMEOUT_NONE) {
975 976 977 978
		if (preop_val == WDOG_PREOP_PANIC) {
			if (atomic_inc_and_test(&preop_panic_excl))
				panic("Watchdog pre-timeout");
		} else if (preop_val == WDOG_PREOP_GIVE_DATA) {
L
Linus Torvalds 已提交
979 980 981 982 983 984 985 986 987
			spin_lock(&ipmi_read_lock);
			data_to_read = 1;
			wake_up_interruptible(&read_q);
			kill_fasync(&fasync_q, SIGIO, POLL_IN);

			spin_unlock(&ipmi_read_lock);
		}
	}

988 989 990 991
	/*
	 * On some machines, the heartbeat will give an error and not
	 * work unless we re-enable the timer.  So do so.
	 */
L
Linus Torvalds 已提交
992 993 994
	pretimeout_since_last_heartbeat = 1;
}

995
static struct ipmi_user_hndl ipmi_hndlrs = {
L
Linus Torvalds 已提交
996 997 998 999 1000 1001 1002 1003 1004 1005 1006
	.ipmi_recv_hndl           = ipmi_wdog_msg_handler,
	.ipmi_watchdog_pretimeout = ipmi_wdog_pretimeout_handler
};

static void ipmi_register_watchdog(int ipmi_intf)
{
	int rv = -EBUSY;

	if (watchdog_user)
		goto out;

1007 1008 1009 1010 1011
	if ((ifnum_to_use >= 0) && (ifnum_to_use != ipmi_intf))
		goto out;

	watchdog_ifnum = ipmi_intf;

L
Linus Torvalds 已提交
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
	rv = ipmi_create_user(ipmi_intf, &ipmi_hndlrs, NULL, &watchdog_user);
	if (rv < 0) {
		printk(KERN_CRIT PFX "Unable to register with ipmi\n");
		goto out;
	}

	ipmi_get_version(watchdog_user,
			 &ipmi_version_major,
			 &ipmi_version_minor);

	rv = misc_register(&ipmi_wdog_miscdev);
	if (rv < 0) {
		ipmi_destroy_user(watchdog_user);
		watchdog_user = NULL;
		printk(KERN_CRIT PFX "Unable to register misc device\n");
	}

C
Corey Minyard 已提交
1029 1030 1031 1032 1033 1034
#ifdef HAVE_DIE_NMI
	if (nmi_handler_registered) {
		int old_pretimeout = pretimeout;
		int old_timeout = timeout;
		int old_preop_val = preop_val;

1035 1036 1037 1038
		/*
		 * Set the pretimeout to go off in a second and give
		 * ourselves plenty of time to stop the timer.
		 */
C
Corey Minyard 已提交
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
		ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
		preop_val = WDOG_PREOP_NONE; /* Make sure nothing happens */
		pretimeout = 99;
		timeout = 100;

		testing_nmi = 1;

		rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
		if (rv) {
			printk(KERN_WARNING PFX "Error starting timer to"
			       " test NMI: 0x%x.  The NMI pretimeout will"
			       " likely not work\n", rv);
			rv = 0;
			goto out_restore;
		}

		msleep(1500);

		if (testing_nmi != 2) {
			printk(KERN_WARNING PFX "IPMI NMI didn't seem to"
			       " occur.  The NMI pretimeout will"
			       " likely not work\n");
		}
1062
 out_restore:
C
Corey Minyard 已提交
1063 1064 1065 1066 1067 1068 1069
		testing_nmi = 0;
		preop_val = old_preop_val;
		pretimeout = old_pretimeout;
		timeout = old_timeout;
	}
#endif

L
Linus Torvalds 已提交
1070 1071 1072 1073 1074 1075 1076
 out:
	if ((start_now) && (rv == 0)) {
		/* Run from startup, so start the timer now. */
		start_now = 0; /* Disable this function after first startup. */
		ipmi_watchdog_state = action_val;
		ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
		printk(KERN_INFO PFX "Starting now!\n");
C
Corey Minyard 已提交
1077 1078 1079 1080
	} else {
		/* Stop the timer now. */
		ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
		ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
L
Linus Torvalds 已提交
1081 1082 1083
	}
}

1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
static void ipmi_unregister_watchdog(int ipmi_intf)
{
	int rv;

	if (!watchdog_user)
		goto out;

	if (watchdog_ifnum != ipmi_intf)
		goto out;

	/* Make sure no one can call us any more. */
	misc_deregister(&ipmi_wdog_miscdev);

1097 1098 1099 1100 1101
	/*
	 * Wait to make sure the message makes it out.  The lower layer has
	 * pointers to our buffers, we want to make sure they are done before
	 * we release our memory.
	 */
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
	while (atomic_read(&set_timeout_tofree))
		schedule_timeout_uninterruptible(1);

	/* Disconnect from IPMI. */
	rv = ipmi_destroy_user(watchdog_user);
	if (rv) {
		printk(KERN_WARNING PFX "error unlinking from IPMI: %d\n",
		       rv);
	}
	watchdog_user = NULL;

 out:
1114
	return;
1115 1116
}

C
Corey Minyard 已提交
1117
#ifdef HAVE_DIE_NMI
L
Linus Torvalds 已提交
1118
static int
1119
ipmi_nmi(unsigned int val, struct pt_regs *regs)
L
Linus Torvalds 已提交
1120
{
C
Corey Minyard 已提交
1121 1122 1123 1124 1125 1126 1127 1128 1129
	/*
	 * If we get here, it's an NMI that's not a memory or I/O
	 * error.  We can't truly tell if it's from IPMI or not
	 * without sending a message, and sending a message is almost
	 * impossible because of locking.
	 */

	if (testing_nmi) {
		testing_nmi = 2;
1130
		return NMI_HANDLED;
C
Corey Minyard 已提交
1131 1132
	}

1133
	/* If we are not expecting a timeout, ignore it. */
1134
	if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)
1135
		return NMI_DONE;
C
Corey Minyard 已提交
1136 1137

	if (preaction_val != WDOG_PRETIMEOUT_NMI)
1138
		return NMI_DONE;
1139

1140 1141 1142 1143
	/*
	 * If no one else handled the NMI, we assume it was the IPMI
	 * watchdog.
	 */
C
Corey Minyard 已提交
1144
	if (preop_val == WDOG_PREOP_PANIC) {
1145 1146 1147 1148
		/* On some machines, the heartbeat will give
		   an error and not work unless we re-enable
		   the timer.   So do so. */
		pretimeout_since_last_heartbeat = 1;
1149 1150
		if (atomic_inc_and_test(&preop_panic_excl))
			panic(PFX "pre-timeout");
1151
	}
L
Linus Torvalds 已提交
1152

1153
	return NMI_HANDLED;
L
Linus Torvalds 已提交
1154 1155 1156 1157 1158 1159 1160
}
#endif

static int wdog_reboot_handler(struct notifier_block *this,
			       unsigned long         code,
			       void                  *unused)
{
1161
	static int reboot_event_handled;
L
Linus Torvalds 已提交
1162 1163 1164 1165 1166

	if ((watchdog_user) && (!reboot_event_handled)) {
		/* Make sure we only do this once. */
		reboot_event_handled = 1;

C
Corey Minyard 已提交
1167
		if (code == SYS_POWER_OFF || code == SYS_HALT) {
L
Linus Torvalds 已提交
1168 1169 1170
			/* Disable the WDT if we are shutting down. */
			ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
			panic_halt_ipmi_set_timeout();
1171
		} else if (ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
L
Linus Torvalds 已提交
1172
			/* Set a long timer to let the reboot happens, but
1173 1174
			   reboot if it hangs, but only if the watchdog
			   timer was already running. */
L
Linus Torvalds 已提交
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
			timeout = 120;
			pretimeout = 0;
			ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
			panic_halt_ipmi_set_timeout();
		}
	}
	return NOTIFY_OK;
}

static struct notifier_block wdog_reboot_notifier = {
	.notifier_call	= wdog_reboot_handler,
	.next		= NULL,
	.priority	= 0
};

static int wdog_panic_handler(struct notifier_block *this,
			      unsigned long         event,
			      void                  *unused)
{
1194
	static int panic_event_handled;
L
Linus Torvalds 已提交
1195

1196 1197 1198 1199 1200 1201 1202
	/* On a panic, if we have a panic timeout, make sure to extend
	   the watchdog timer to a reasonable value to complete the
	   panic, if the watchdog timer is running.  Plus the
	   pretimeout is meaningless at panic time. */
	if (watchdog_user && !panic_event_handled &&
	    ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
		/* Make sure we do this only once. */
L
Linus Torvalds 已提交
1203
		panic_event_handled = 1;
1204

L
Linus Torvalds 已提交
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
		timeout = 255;
		pretimeout = 0;
		panic_halt_ipmi_set_timeout();
	}

	return NOTIFY_OK;
}

static struct notifier_block wdog_panic_notifier = {
	.notifier_call	= wdog_panic_handler,
	.next		= NULL,
	.priority	= 150	/* priority: INT_MAX >= x >= 0 */
};


1220
static void ipmi_new_smi(int if_num, struct device *device)
L
Linus Torvalds 已提交
1221 1222 1223 1224 1225 1226
{
	ipmi_register_watchdog(if_num);
}

static void ipmi_smi_gone(int if_num)
{
1227
	ipmi_unregister_watchdog(if_num);
L
Linus Torvalds 已提交
1228 1229
}

1230
static struct ipmi_smi_watcher smi_watcher = {
L
Linus Torvalds 已提交
1231 1232 1233 1234 1235
	.owner    = THIS_MODULE,
	.new_smi  = ipmi_new_smi,
	.smi_gone = ipmi_smi_gone
};

1236
static int action_op(const char *inval, char *outval)
L
Linus Torvalds 已提交
1237
{
1238 1239 1240 1241 1242
	if (outval)
		strcpy(outval, action);

	if (!inval)
		return 0;
L
Linus Torvalds 已提交
1243

1244
	if (strcmp(inval, "reset") == 0)
L
Linus Torvalds 已提交
1245
		action_val = WDOG_TIMEOUT_RESET;
1246
	else if (strcmp(inval, "none") == 0)
L
Linus Torvalds 已提交
1247
		action_val = WDOG_TIMEOUT_NONE;
1248
	else if (strcmp(inval, "power_cycle") == 0)
L
Linus Torvalds 已提交
1249
		action_val = WDOG_TIMEOUT_POWER_CYCLE;
1250
	else if (strcmp(inval, "power_off") == 0)
L
Linus Torvalds 已提交
1251
		action_val = WDOG_TIMEOUT_POWER_DOWN;
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
	else
		return -EINVAL;
	strcpy(action, inval);
	return 0;
}

static int preaction_op(const char *inval, char *outval)
{
	if (outval)
		strcpy(outval, preaction);
L
Linus Torvalds 已提交
1262

1263 1264 1265 1266
	if (!inval)
		return 0;

	if (strcmp(inval, "pre_none") == 0)
L
Linus Torvalds 已提交
1267
		preaction_val = WDOG_PRETIMEOUT_NONE;
1268
	else if (strcmp(inval, "pre_smi") == 0)
L
Linus Torvalds 已提交
1269
		preaction_val = WDOG_PRETIMEOUT_SMI;
C
Corey Minyard 已提交
1270
#ifdef HAVE_DIE_NMI
1271
	else if (strcmp(inval, "pre_nmi") == 0)
L
Linus Torvalds 已提交
1272 1273
		preaction_val = WDOG_PRETIMEOUT_NMI;
#endif
1274
	else if (strcmp(inval, "pre_int") == 0)
L
Linus Torvalds 已提交
1275
		preaction_val = WDOG_PRETIMEOUT_MSG_INT;
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
	else
		return -EINVAL;
	strcpy(preaction, inval);
	return 0;
}

static int preop_op(const char *inval, char *outval)
{
	if (outval)
		strcpy(outval, preop);
L
Linus Torvalds 已提交
1286

1287 1288 1289 1290
	if (!inval)
		return 0;

	if (strcmp(inval, "preop_none") == 0)
L
Linus Torvalds 已提交
1291
		preop_val = WDOG_PREOP_NONE;
1292
	else if (strcmp(inval, "preop_panic") == 0)
L
Linus Torvalds 已提交
1293
		preop_val = WDOG_PREOP_PANIC;
1294
	else if (strcmp(inval, "preop_give_data") == 0)
L
Linus Torvalds 已提交
1295
		preop_val = WDOG_PREOP_GIVE_DATA;
1296 1297 1298 1299 1300
	else
		return -EINVAL;
	strcpy(preop, inval);
	return 0;
}
L
Linus Torvalds 已提交
1301

1302 1303
static void check_parms(void)
{
C
Corey Minyard 已提交
1304
#ifdef HAVE_DIE_NMI
1305 1306 1307
	int do_nmi = 0;
	int rv;

L
Linus Torvalds 已提交
1308
	if (preaction_val == WDOG_PRETIMEOUT_NMI) {
1309
		do_nmi = 1;
L
Linus Torvalds 已提交
1310 1311 1312 1313
		if (preop_val == WDOG_PREOP_GIVE_DATA) {
			printk(KERN_WARNING PFX "Pretimeout op is to give data"
			       " but NMI pretimeout is enabled, setting"
			       " pretimeout op to none\n");
1314 1315
			preop_op("preop_none", NULL);
			do_nmi = 0;
L
Linus Torvalds 已提交
1316
		}
1317 1318
	}
	if (do_nmi && !nmi_handler_registered) {
1319 1320
		rv = register_nmi_handler(NMI_UNKNOWN, ipmi_nmi, 0,
						"ipmi");
L
Linus Torvalds 已提交
1321
		if (rv) {
1322 1323 1324 1325 1326 1327
			printk(KERN_WARNING PFX
			       "Can't register nmi handler\n");
			return;
		} else
			nmi_handler_registered = 1;
	} else if (!do_nmi && nmi_handler_registered) {
1328
		unregister_nmi_handler(NMI_UNKNOWN, "ipmi");
1329
		nmi_handler_registered = 0;
L
Linus Torvalds 已提交
1330 1331
	}
#endif
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
}

static int __init ipmi_wdog_init(void)
{
	int rv;

	if (action_op(action, NULL)) {
		action_op("reset", NULL);
		printk(KERN_INFO PFX "Unknown action '%s', defaulting to"
		       " reset\n", action);
	}

	if (preaction_op(preaction, NULL)) {
		preaction_op("pre_none", NULL);
		printk(KERN_INFO PFX "Unknown preaction '%s', defaulting to"
		       " none\n", preaction);
	}

	if (preop_op(preop, NULL)) {
		preop_op("preop_none", NULL);
		printk(KERN_INFO PFX "Unknown preop '%s', defaulting to"
		       " none\n", preop);
	}

	check_parms();
L
Linus Torvalds 已提交
1357

1358 1359 1360 1361
	register_reboot_notifier(&wdog_reboot_notifier);
	atomic_notifier_chain_register(&panic_notifier_list,
			&wdog_panic_notifier);

L
Linus Torvalds 已提交
1362 1363
	rv = ipmi_smi_watcher_register(&smi_watcher);
	if (rv) {
C
Corey Minyard 已提交
1364 1365
#ifdef HAVE_DIE_NMI
		if (nmi_handler_registered)
1366
			unregister_nmi_handler(NMI_UNKNOWN, "ipmi");
L
Linus Torvalds 已提交
1367
#endif
1368 1369 1370
		atomic_notifier_chain_unregister(&panic_notifier_list,
						 &wdog_panic_notifier);
		unregister_reboot_notifier(&wdog_reboot_notifier);
L
Linus Torvalds 已提交
1371 1372 1373 1374
		printk(KERN_WARNING PFX "can't register smi watcher\n");
		return rv;
	}

1375 1376
	printk(KERN_INFO PFX "driver initialized\n");

L
Linus Torvalds 已提交
1377 1378 1379
	return 0;
}

1380
static void __exit ipmi_wdog_exit(void)
L
Linus Torvalds 已提交
1381
{
1382 1383
	ipmi_smi_watcher_unregister(&smi_watcher);
	ipmi_unregister_watchdog(watchdog_ifnum);
L
Linus Torvalds 已提交
1384

C
Corey Minyard 已提交
1385
#ifdef HAVE_DIE_NMI
1386
	if (nmi_handler_registered)
1387
		unregister_nmi_handler(NMI_UNKNOWN, "ipmi");
L
Linus Torvalds 已提交
1388 1389
#endif

1390
	atomic_notifier_chain_unregister(&panic_notifier_list,
1391
					 &wdog_panic_notifier);
L
Linus Torvalds 已提交
1392 1393 1394 1395 1396
	unregister_reboot_notifier(&wdog_reboot_notifier);
}
module_exit(ipmi_wdog_exit);
module_init(ipmi_wdog_init);
MODULE_LICENSE("GPL");
1397 1398
MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
MODULE_DESCRIPTION("watchdog timer based upon the IPMI interface.");