1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * ipmi_watchdog.c
4  *
5  * A watchdog timer based upon the IPMI interface.
6  *
7  * Author: MontaVista Software, Inc.
8  *         Corey Minyard <minyard@mvista.com>
9  *         source@mvista.com
10  *
11  * Copyright 2002 MontaVista Software Inc.
12  */
13 
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/ipmi.h>
17 #include <linux/ipmi_smi.h>
18 #include <linux/mutex.h>
19 #include <linux/watchdog.h>
20 #include <linux/miscdevice.h>
21 #include <linux/init.h>
22 #include <linux/completion.h>
23 #include <linux/kdebug.h>
24 #include <linux/rwsem.h>
25 #include <linux/errno.h>
26 #include <linux/uaccess.h>
27 #include <linux/notifier.h>
28 #include <linux/nmi.h>
29 #include <linux/reboot.h>
30 #include <linux/wait.h>
31 #include <linux/poll.h>
32 #include <linux/string.h>
33 #include <linux/ctype.h>
34 #include <linux/delay.h>
35 #include <linux/atomic.h>
36 #include <linux/sched/signal.h>
37 
38 #ifdef CONFIG_X86
39 /*
40  * This is ugly, but I've determined that x86 is the only architecture
41  * that can reasonably support the IPMI NMI watchdog timeout at this
42  * time.  If another architecture adds this capability somehow, it
43  * will have to be a somewhat different mechanism and I have no idea
44  * how it will work.  So in the unlikely event that another
45  * architecture supports this, we can figure out a good generic
46  * mechanism for it at that time.
47  */
48 #include <asm/kdebug.h>
49 #include <asm/nmi.h>
50 #define HAVE_DIE_NMI
51 #endif
52 
53 #define	PFX "IPMI Watchdog: "
54 
55 /*
56  * The IPMI command/response information for the watchdog timer.
57  */
58 
59 /* values for byte 1 of the set command, byte 2 of the get response. */
60 #define WDOG_DONT_LOG		(1 << 7)
61 #define WDOG_DONT_STOP_ON_SET	(1 << 6)
62 #define WDOG_SET_TIMER_USE(byte, use) \
63 	byte = ((byte) & 0xf8) | ((use) & 0x7)
64 #define WDOG_GET_TIMER_USE(byte) ((byte) & 0x7)
65 #define WDOG_TIMER_USE_BIOS_FRB2	1
66 #define WDOG_TIMER_USE_BIOS_POST	2
67 #define WDOG_TIMER_USE_OS_LOAD		3
68 #define WDOG_TIMER_USE_SMS_OS		4
69 #define WDOG_TIMER_USE_OEM		5
70 
71 /* values for byte 2 of the set command, byte 3 of the get response. */
72 #define WDOG_SET_PRETIMEOUT_ACT(byte, use) \
73 	byte = ((byte) & 0x8f) | (((use) & 0x7) << 4)
74 #define WDOG_GET_PRETIMEOUT_ACT(byte) (((byte) >> 4) & 0x7)
75 #define WDOG_PRETIMEOUT_NONE		0
76 #define WDOG_PRETIMEOUT_SMI		1
77 #define WDOG_PRETIMEOUT_NMI		2
78 #define WDOG_PRETIMEOUT_MSG_INT		3
79 
80 /* Operations that can be performed on a pretimout. */
81 #define WDOG_PREOP_NONE		0
82 #define WDOG_PREOP_PANIC	1
83 /* Cause data to be available to read.  Doesn't work in NMI mode. */
84 #define WDOG_PREOP_GIVE_DATA	2
85 
86 /* Actions to perform on a full timeout. */
87 #define WDOG_SET_TIMEOUT_ACT(byte, use) \
88 	byte = ((byte) & 0xf8) | ((use) & 0x7)
89 #define WDOG_GET_TIMEOUT_ACT(byte) ((byte) & 0x7)
90 #define WDOG_TIMEOUT_NONE		0
91 #define WDOG_TIMEOUT_RESET		1
92 #define WDOG_TIMEOUT_POWER_DOWN		2
93 #define WDOG_TIMEOUT_POWER_CYCLE	3
94 
95 /*
96  * Byte 3 of the get command, byte 4 of the get response is the
97  * pre-timeout in seconds.
98  */
99 
100 /* Bits for setting byte 4 of the set command, byte 5 of the get response. */
101 #define WDOG_EXPIRE_CLEAR_BIOS_FRB2	(1 << 1)
102 #define WDOG_EXPIRE_CLEAR_BIOS_POST	(1 << 2)
103 #define WDOG_EXPIRE_CLEAR_OS_LOAD	(1 << 3)
104 #define WDOG_EXPIRE_CLEAR_SMS_OS	(1 << 4)
105 #define WDOG_EXPIRE_CLEAR_OEM		(1 << 5)
106 
107 /*
108  * Setting/getting the watchdog timer value.  This is for bytes 5 and
109  * 6 (the timeout time) of the set command, and bytes 6 and 7 (the
110  * timeout time) and 8 and 9 (the current countdown value) of the
111  * response.  The timeout value is given in seconds (in the command it
112  * is 100ms intervals).
113  */
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 #define IPMI_WDOG_TIMER_NOT_INIT_RESP	0x80
124 
125 static DEFINE_MUTEX(ipmi_watchdog_mutex);
126 static bool nowayout = WATCHDOG_NOWAYOUT;
127 
128 static ipmi_user_t watchdog_user;
129 static int watchdog_ifnum;
130 
131 /* Default the timeout to 10 seconds. */
132 static int timeout = 10;
133 
134 /* The pre-timeout is disabled by default. */
135 static int pretimeout;
136 
137 /* Default timeout to set on panic */
138 static int panic_wdt_timeout = 255;
139 
140 /* Default action is to reset the board on a timeout. */
141 static unsigned char action_val = WDOG_TIMEOUT_RESET;
142 
143 static char action[16] = "reset";
144 
145 static unsigned char preaction_val = WDOG_PRETIMEOUT_NONE;
146 
147 static char preaction[16] = "pre_none";
148 
149 static unsigned char preop_val = WDOG_PREOP_NONE;
150 
151 static char preop[16] = "preop_none";
152 static DEFINE_SPINLOCK(ipmi_read_lock);
153 static char data_to_read;
154 static DECLARE_WAIT_QUEUE_HEAD(read_q);
155 static struct fasync_struct *fasync_q;
156 static char pretimeout_since_last_heartbeat;
157 static char expect_close;
158 
159 static int ifnum_to_use = -1;
160 
161 /* Parameters to ipmi_set_timeout */
162 #define IPMI_SET_TIMEOUT_NO_HB			0
163 #define IPMI_SET_TIMEOUT_HB_IF_NECESSARY	1
164 #define IPMI_SET_TIMEOUT_FORCE_HB		2
165 
166 static int ipmi_set_timeout(int do_heartbeat);
167 static void ipmi_register_watchdog(int ipmi_intf);
168 static void ipmi_unregister_watchdog(int ipmi_intf);
169 
170 /*
171  * If true, the driver will start running as soon as it is configured
172  * and ready.
173  */
174 static int start_now;
175 
176 static int set_param_timeout(const char *val, const 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 	*((int *)kp->arg) = l;
189 	if (watchdog_user)
190 		rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
191 
192 	return rv;
193 }
194 
195 static const struct kernel_param_ops param_ops_timeout = {
196 	.set = set_param_timeout,
197 	.get = param_get_int,
198 };
199 #define param_check_timeout param_check_int
200 
201 typedef int (*action_fn)(const char *intval, char *outval);
202 
203 static int action_op(const char *inval, char *outval);
204 static int preaction_op(const char *inval, char *outval);
205 static int preop_op(const char *inval, char *outval);
206 static void check_parms(void);
207 
208 static int set_param_str(const char *val, const struct kernel_param *kp)
209 {
210 	action_fn  fn = (action_fn) kp->arg;
211 	int        rv = 0;
212 	char       valcp[16];
213 	char       *s;
214 
215 	strncpy(valcp, val, 15);
216 	valcp[15] = '\0';
217 
218 	s = strstrip(valcp);
219 
220 	rv = fn(s, NULL);
221 	if (rv)
222 		goto out;
223 
224 	check_parms();
225 	if (watchdog_user)
226 		rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
227 
228  out:
229 	return rv;
230 }
231 
232 static int get_param_str(char *buffer, const struct kernel_param *kp)
233 {
234 	action_fn fn = (action_fn) kp->arg;
235 	int       rv;
236 
237 	rv = fn(NULL, buffer);
238 	if (rv)
239 		return rv;
240 	return strlen(buffer);
241 }
242 
243 
244 static int set_param_wdog_ifnum(const char *val, const struct kernel_param *kp)
245 {
246 	int rv = param_set_int(val, kp);
247 	if (rv)
248 		return rv;
249 	if ((ifnum_to_use < 0) || (ifnum_to_use == watchdog_ifnum))
250 		return 0;
251 
252 	ipmi_unregister_watchdog(watchdog_ifnum);
253 	ipmi_register_watchdog(ifnum_to_use);
254 	return 0;
255 }
256 
257 static const struct kernel_param_ops param_ops_wdog_ifnum = {
258 	.set = set_param_wdog_ifnum,
259 	.get = param_get_int,
260 };
261 
262 #define param_check_wdog_ifnum param_check_int
263 
264 static const struct kernel_param_ops param_ops_str = {
265 	.set = set_param_str,
266 	.get = get_param_str,
267 };
268 
269 module_param(ifnum_to_use, wdog_ifnum, 0644);
270 MODULE_PARM_DESC(ifnum_to_use, "The interface number to use for the watchdog "
271 		 "timer.  Setting to -1 defaults to the first registered "
272 		 "interface");
273 
274 module_param(timeout, timeout, 0644);
275 MODULE_PARM_DESC(timeout, "Timeout value in seconds.");
276 
277 module_param(pretimeout, timeout, 0644);
278 MODULE_PARM_DESC(pretimeout, "Pretimeout value in seconds.");
279 
280 module_param(panic_wdt_timeout, timeout, 0644);
281 MODULE_PARM_DESC(panic_wdt_timeout, "Timeout value on kernel panic in seconds.");
282 
283 module_param_cb(action, &param_ops_str, action_op, 0644);
284 MODULE_PARM_DESC(action, "Timeout action. One of: "
285 		 "reset, none, power_cycle, power_off.");
286 
287 module_param_cb(preaction, &param_ops_str, preaction_op, 0644);
288 MODULE_PARM_DESC(preaction, "Pretimeout action.  One of: "
289 		 "pre_none, pre_smi, pre_nmi, pre_int.");
290 
291 module_param_cb(preop, &param_ops_str, preop_op, 0644);
292 MODULE_PARM_DESC(preop, "Pretimeout driver operation.  One of: "
293 		 "preop_none, preop_panic, preop_give_data.");
294 
295 module_param(start_now, int, 0444);
296 MODULE_PARM_DESC(start_now, "Set to 1 to start the watchdog as"
297 		 "soon as the driver is loaded.");
298 
299 module_param(nowayout, bool, 0644);
300 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
301 		 "(default=CONFIG_WATCHDOG_NOWAYOUT)");
302 
303 /* Default state of the timer. */
304 static unsigned char ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
305 
306 /* If shutting down via IPMI, we ignore the heartbeat. */
307 static int ipmi_ignore_heartbeat;
308 
309 /* Is someone using the watchdog?  Only one user is allowed. */
310 static unsigned long ipmi_wdog_open;
311 
312 /*
313  * If set to 1, the heartbeat command will set the state to reset and
314  * start the timer.  The timer doesn't normally run when the driver is
315  * first opened until the heartbeat is set the first time, this
316  * variable is used to accomplish this.
317  */
318 static int ipmi_start_timer_on_heartbeat;
319 
320 /* IPMI version of the BMC. */
321 static unsigned char ipmi_version_major;
322 static unsigned char ipmi_version_minor;
323 
324 /* If a pretimeout occurs, this is used to allow only one panic to happen. */
325 static atomic_t preop_panic_excl = ATOMIC_INIT(-1);
326 
327 #ifdef HAVE_DIE_NMI
328 static int testing_nmi;
329 static int nmi_handler_registered;
330 #endif
331 
332 static int ipmi_heartbeat(void);
333 
334 /*
335  * We use a mutex to make sure that only one thing can send a set
336  * timeout at one time, because we only have one copy of the data.
337  * The mutex is claimed when the set_timeout is sent and freed
338  * when both messages are free.
339  */
340 static atomic_t set_timeout_tofree = ATOMIC_INIT(0);
341 static DEFINE_MUTEX(set_timeout_lock);
342 static DECLARE_COMPLETION(set_timeout_wait);
343 static void set_timeout_free_smi(struct ipmi_smi_msg *msg)
344 {
345     if (atomic_dec_and_test(&set_timeout_tofree))
346 	    complete(&set_timeout_wait);
347 }
348 static void set_timeout_free_recv(struct ipmi_recv_msg *msg)
349 {
350     if (atomic_dec_and_test(&set_timeout_tofree))
351 	    complete(&set_timeout_wait);
352 }
353 static struct ipmi_smi_msg set_timeout_smi_msg = {
354 	.done = set_timeout_free_smi
355 };
356 static struct ipmi_recv_msg set_timeout_recv_msg = {
357 	.done = set_timeout_free_recv
358 };
359 
360 static int i_ipmi_set_timeout(struct ipmi_smi_msg  *smi_msg,
361 			      struct ipmi_recv_msg *recv_msg,
362 			      int                  *send_heartbeat_now)
363 {
364 	struct kernel_ipmi_msg            msg;
365 	unsigned char                     data[6];
366 	int                               rv;
367 	struct ipmi_system_interface_addr addr;
368 	int                               hbnow = 0;
369 
370 
371 	/* These can be cleared as we are setting the timeout. */
372 	pretimeout_since_last_heartbeat = 0;
373 
374 	data[0] = 0;
375 	WDOG_SET_TIMER_USE(data[0], WDOG_TIMER_USE_SMS_OS);
376 
377 	if ((ipmi_version_major > 1)
378 	    || ((ipmi_version_major == 1) && (ipmi_version_minor >= 5))) {
379 		/* This is an IPMI 1.5-only feature. */
380 		data[0] |= WDOG_DONT_STOP_ON_SET;
381 	} else if (ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
382 		/*
383 		 * In ipmi 1.0, setting the timer stops the watchdog, we
384 		 * need to start it back up again.
385 		 */
386 		hbnow = 1;
387 	}
388 
389 	data[1] = 0;
390 	WDOG_SET_TIMEOUT_ACT(data[1], ipmi_watchdog_state);
391 	if ((pretimeout > 0) && (ipmi_watchdog_state != WDOG_TIMEOUT_NONE)) {
392 	    WDOG_SET_PRETIMEOUT_ACT(data[1], preaction_val);
393 	    data[2] = pretimeout;
394 	} else {
395 	    WDOG_SET_PRETIMEOUT_ACT(data[1], WDOG_PRETIMEOUT_NONE);
396 	    data[2] = 0; /* No pretimeout. */
397 	}
398 	data[3] = 0;
399 	WDOG_SET_TIMEOUT(data[4], data[5], timeout);
400 
401 	addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
402 	addr.channel = IPMI_BMC_CHANNEL;
403 	addr.lun = 0;
404 
405 	msg.netfn = 0x06;
406 	msg.cmd = IPMI_WDOG_SET_TIMER;
407 	msg.data = data;
408 	msg.data_len = sizeof(data);
409 	rv = ipmi_request_supply_msgs(watchdog_user,
410 				      (struct ipmi_addr *) &addr,
411 				      0,
412 				      &msg,
413 				      NULL,
414 				      smi_msg,
415 				      recv_msg,
416 				      1);
417 	if (rv) {
418 		printk(KERN_WARNING PFX "set timeout error: %d\n",
419 		       rv);
420 	}
421 
422 	if (send_heartbeat_now)
423 	    *send_heartbeat_now = hbnow;
424 
425 	return rv;
426 }
427 
428 static int ipmi_set_timeout(int do_heartbeat)
429 {
430 	int send_heartbeat_now;
431 	int rv;
432 
433 
434 	/* We can only send one of these at a time. */
435 	mutex_lock(&set_timeout_lock);
436 
437 	atomic_set(&set_timeout_tofree, 2);
438 
439 	rv = i_ipmi_set_timeout(&set_timeout_smi_msg,
440 				&set_timeout_recv_msg,
441 				&send_heartbeat_now);
442 	if (rv) {
443 		mutex_unlock(&set_timeout_lock);
444 		goto out;
445 	}
446 
447 	wait_for_completion(&set_timeout_wait);
448 
449 	mutex_unlock(&set_timeout_lock);
450 
451 	if ((do_heartbeat == IPMI_SET_TIMEOUT_FORCE_HB)
452 	    || ((send_heartbeat_now)
453 		&& (do_heartbeat == IPMI_SET_TIMEOUT_HB_IF_NECESSARY)))
454 		rv = ipmi_heartbeat();
455 
456 out:
457 	return rv;
458 }
459 
460 static atomic_t panic_done_count = ATOMIC_INIT(0);
461 
462 static void panic_smi_free(struct ipmi_smi_msg *msg)
463 {
464 	atomic_dec(&panic_done_count);
465 }
466 static void panic_recv_free(struct ipmi_recv_msg *msg)
467 {
468 	atomic_dec(&panic_done_count);
469 }
470 
471 static struct ipmi_smi_msg panic_halt_heartbeat_smi_msg = {
472 	.done = panic_smi_free
473 };
474 static struct ipmi_recv_msg panic_halt_heartbeat_recv_msg = {
475 	.done = panic_recv_free
476 };
477 
478 static void panic_halt_ipmi_heartbeat(void)
479 {
480 	struct kernel_ipmi_msg             msg;
481 	struct ipmi_system_interface_addr addr;
482 	int rv;
483 
484 	/*
485 	 * Don't reset the timer if we have the timer turned off, that
486 	 * re-enables the watchdog.
487 	 */
488 	if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)
489 		return;
490 
491 	addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
492 	addr.channel = IPMI_BMC_CHANNEL;
493 	addr.lun = 0;
494 
495 	msg.netfn = 0x06;
496 	msg.cmd = IPMI_WDOG_RESET_TIMER;
497 	msg.data = NULL;
498 	msg.data_len = 0;
499 	atomic_add(1, &panic_done_count);
500 	rv = ipmi_request_supply_msgs(watchdog_user,
501 				      (struct ipmi_addr *) &addr,
502 				      0,
503 				      &msg,
504 				      NULL,
505 				      &panic_halt_heartbeat_smi_msg,
506 				      &panic_halt_heartbeat_recv_msg,
507 				      1);
508 	if (rv)
509 		atomic_sub(1, &panic_done_count);
510 }
511 
512 static struct ipmi_smi_msg panic_halt_smi_msg = {
513 	.done = panic_smi_free
514 };
515 static struct ipmi_recv_msg panic_halt_recv_msg = {
516 	.done = panic_recv_free
517 };
518 
519 /*
520  * Special call, doesn't claim any locks.  This is only to be called
521  * at panic or halt time, in run-to-completion mode, when the caller
522  * is the only CPU and the only thing that will be going is these IPMI
523  * calls.
524  */
525 static void panic_halt_ipmi_set_timeout(void)
526 {
527 	int send_heartbeat_now;
528 	int rv;
529 
530 	/* Wait for the messages to be free. */
531 	while (atomic_read(&panic_done_count) != 0)
532 		ipmi_poll_interface(watchdog_user);
533 	atomic_add(1, &panic_done_count);
534 	rv = i_ipmi_set_timeout(&panic_halt_smi_msg,
535 				&panic_halt_recv_msg,
536 				&send_heartbeat_now);
537 	if (rv) {
538 		atomic_sub(1, &panic_done_count);
539 		printk(KERN_WARNING PFX
540 		       "Unable to extend the watchdog timeout.");
541 	} else {
542 		if (send_heartbeat_now)
543 			panic_halt_ipmi_heartbeat();
544 	}
545 	while (atomic_read(&panic_done_count) != 0)
546 		ipmi_poll_interface(watchdog_user);
547 }
548 
549 /*
550  * We use a mutex to make sure that only one thing can send a
551  * heartbeat at one time, because we only have one copy of the data.
552  * The semaphore is claimed when the set_timeout is sent and freed
553  * when both messages are free.
554  */
555 static atomic_t heartbeat_tofree = ATOMIC_INIT(0);
556 static DEFINE_MUTEX(heartbeat_lock);
557 static DECLARE_COMPLETION(heartbeat_wait);
558 static void heartbeat_free_smi(struct ipmi_smi_msg *msg)
559 {
560     if (atomic_dec_and_test(&heartbeat_tofree))
561 	    complete(&heartbeat_wait);
562 }
563 static void heartbeat_free_recv(struct ipmi_recv_msg *msg)
564 {
565     if (atomic_dec_and_test(&heartbeat_tofree))
566 	    complete(&heartbeat_wait);
567 }
568 static struct ipmi_smi_msg heartbeat_smi_msg = {
569 	.done = heartbeat_free_smi
570 };
571 static struct ipmi_recv_msg heartbeat_recv_msg = {
572 	.done = heartbeat_free_recv
573 };
574 
575 static int ipmi_heartbeat(void)
576 {
577 	struct kernel_ipmi_msg            msg;
578 	int                               rv;
579 	struct ipmi_system_interface_addr addr;
580 	int				  timeout_retries = 0;
581 
582 	if (ipmi_ignore_heartbeat)
583 		return 0;
584 
585 	if (ipmi_start_timer_on_heartbeat) {
586 		ipmi_start_timer_on_heartbeat = 0;
587 		ipmi_watchdog_state = action_val;
588 		return ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
589 	} else if (pretimeout_since_last_heartbeat) {
590 		/*
591 		 * A pretimeout occurred, make sure we set the timeout.
592 		 * We don't want to set the action, though, we want to
593 		 * leave that alone (thus it can't be combined with the
594 		 * above operation.
595 		 */
596 		return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
597 	}
598 
599 	mutex_lock(&heartbeat_lock);
600 
601 restart:
602 	atomic_set(&heartbeat_tofree, 2);
603 
604 	/*
605 	 * Don't reset the timer if we have the timer turned off, that
606 	 * re-enables the watchdog.
607 	 */
608 	if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE) {
609 		mutex_unlock(&heartbeat_lock);
610 		return 0;
611 	}
612 
613 	addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
614 	addr.channel = IPMI_BMC_CHANNEL;
615 	addr.lun = 0;
616 
617 	msg.netfn = 0x06;
618 	msg.cmd = IPMI_WDOG_RESET_TIMER;
619 	msg.data = NULL;
620 	msg.data_len = 0;
621 	rv = ipmi_request_supply_msgs(watchdog_user,
622 				      (struct ipmi_addr *) &addr,
623 				      0,
624 				      &msg,
625 				      NULL,
626 				      &heartbeat_smi_msg,
627 				      &heartbeat_recv_msg,
628 				      1);
629 	if (rv) {
630 		mutex_unlock(&heartbeat_lock);
631 		printk(KERN_WARNING PFX "heartbeat failure: %d\n",
632 		       rv);
633 		return rv;
634 	}
635 
636 	/* Wait for the heartbeat to be sent. */
637 	wait_for_completion(&heartbeat_wait);
638 
639 	if (heartbeat_recv_msg.msg.data[0] == IPMI_WDOG_TIMER_NOT_INIT_RESP)  {
640 		timeout_retries++;
641 		if (timeout_retries > 3) {
642 			printk(KERN_ERR PFX ": Unable to restore the IPMI"
643 			       " watchdog's settings, giving up.\n");
644 			rv = -EIO;
645 			goto out_unlock;
646 		}
647 
648 		/*
649 		 * The timer was not initialized, that means the BMC was
650 		 * probably reset and lost the watchdog information.  Attempt
651 		 * to restore the timer's info.  Note that we still hold
652 		 * the heartbeat lock, to keep a heartbeat from happening
653 		 * in this process, so must say no heartbeat to avoid a
654 		 * deadlock on this mutex.
655 		 */
656 		rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
657 		if (rv) {
658 			printk(KERN_ERR PFX ": Unable to send the command to"
659 			       " set the watchdog's settings, giving up.\n");
660 			goto out_unlock;
661 		}
662 
663 		/* We might need a new heartbeat, so do it now */
664 		goto restart;
665 	} else if (heartbeat_recv_msg.msg.data[0] != 0) {
666 		/*
667 		 * Got an error in the heartbeat response.  It was already
668 		 * reported in ipmi_wdog_msg_handler, but we should return
669 		 * an error here.
670 		 */
671 		rv = -EINVAL;
672 	}
673 
674 out_unlock:
675 	mutex_unlock(&heartbeat_lock);
676 
677 	return rv;
678 }
679 
680 static struct watchdog_info ident = {
681 	.options	= 0,	/* WDIOF_SETTIMEOUT, */
682 	.firmware_version = 1,
683 	.identity	= "IPMI"
684 };
685 
686 static int ipmi_ioctl(struct file *file,
687 		      unsigned int cmd, unsigned long arg)
688 {
689 	void __user *argp = (void __user *)arg;
690 	int i;
691 	int val;
692 
693 	switch (cmd) {
694 	case WDIOC_GETSUPPORT:
695 		i = copy_to_user(argp, &ident, sizeof(ident));
696 		return i ? -EFAULT : 0;
697 
698 	case WDIOC_SETTIMEOUT:
699 		i = copy_from_user(&val, argp, sizeof(int));
700 		if (i)
701 			return -EFAULT;
702 		timeout = val;
703 		return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
704 
705 	case WDIOC_GETTIMEOUT:
706 		i = copy_to_user(argp, &timeout, sizeof(timeout));
707 		if (i)
708 			return -EFAULT;
709 		return 0;
710 
711 	case WDIOC_SETPRETIMEOUT:
712 		i = copy_from_user(&val, argp, sizeof(int));
713 		if (i)
714 			return -EFAULT;
715 		pretimeout = val;
716 		return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
717 
718 	case WDIOC_GETPRETIMEOUT:
719 		i = copy_to_user(argp, &pretimeout, sizeof(pretimeout));
720 		if (i)
721 			return -EFAULT;
722 		return 0;
723 
724 	case WDIOC_KEEPALIVE:
725 		return ipmi_heartbeat();
726 
727 	case WDIOC_SETOPTIONS:
728 		i = copy_from_user(&val, argp, sizeof(int));
729 		if (i)
730 			return -EFAULT;
731 		if (val & WDIOS_DISABLECARD) {
732 			ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
733 			ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
734 			ipmi_start_timer_on_heartbeat = 0;
735 		}
736 
737 		if (val & WDIOS_ENABLECARD) {
738 			ipmi_watchdog_state = action_val;
739 			ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
740 		}
741 		return 0;
742 
743 	case WDIOC_GETSTATUS:
744 		val = 0;
745 		i = copy_to_user(argp, &val, sizeof(val));
746 		if (i)
747 			return -EFAULT;
748 		return 0;
749 
750 	default:
751 		return -ENOIOCTLCMD;
752 	}
753 }
754 
755 static long ipmi_unlocked_ioctl(struct file *file,
756 				unsigned int cmd,
757 				unsigned long arg)
758 {
759 	int ret;
760 
761 	mutex_lock(&ipmi_watchdog_mutex);
762 	ret = ipmi_ioctl(file, cmd, arg);
763 	mutex_unlock(&ipmi_watchdog_mutex);
764 
765 	return ret;
766 }
767 
768 static ssize_t ipmi_write(struct file *file,
769 			  const char  __user *buf,
770 			  size_t      len,
771 			  loff_t      *ppos)
772 {
773 	int rv;
774 
775 	if (len) {
776 		if (!nowayout) {
777 			size_t i;
778 
779 			/* In case it was set long ago */
780 			expect_close = 0;
781 
782 			for (i = 0; i != len; i++) {
783 				char c;
784 
785 				if (get_user(c, buf + i))
786 					return -EFAULT;
787 				if (c == 'V')
788 					expect_close = 42;
789 			}
790 		}
791 		rv = ipmi_heartbeat();
792 		if (rv)
793 			return rv;
794 	}
795 	return len;
796 }
797 
798 static ssize_t ipmi_read(struct file *file,
799 			 char        __user *buf,
800 			 size_t      count,
801 			 loff_t      *ppos)
802 {
803 	int          rv = 0;
804 	wait_queue_entry_t wait;
805 
806 	if (count <= 0)
807 		return 0;
808 
809 	/*
810 	 * Reading returns if the pretimeout has gone off, and it only does
811 	 * it once per pretimeout.
812 	 */
813 	spin_lock(&ipmi_read_lock);
814 	if (!data_to_read) {
815 		if (file->f_flags & O_NONBLOCK) {
816 			rv = -EAGAIN;
817 			goto out;
818 		}
819 
820 		init_waitqueue_entry(&wait, current);
821 		add_wait_queue(&read_q, &wait);
822 		while (!data_to_read) {
823 			set_current_state(TASK_INTERRUPTIBLE);
824 			spin_unlock(&ipmi_read_lock);
825 			schedule();
826 			spin_lock(&ipmi_read_lock);
827 		}
828 		remove_wait_queue(&read_q, &wait);
829 
830 		if (signal_pending(current)) {
831 			rv = -ERESTARTSYS;
832 			goto out;
833 		}
834 	}
835 	data_to_read = 0;
836 
837  out:
838 	spin_unlock(&ipmi_read_lock);
839 
840 	if (rv == 0) {
841 		if (copy_to_user(buf, &data_to_read, 1))
842 			rv = -EFAULT;
843 		else
844 			rv = 1;
845 	}
846 
847 	return rv;
848 }
849 
850 static int ipmi_open(struct inode *ino, struct file *filep)
851 {
852 	switch (iminor(ino)) {
853 	case WATCHDOG_MINOR:
854 		if (test_and_set_bit(0, &ipmi_wdog_open))
855 			return -EBUSY;
856 
857 
858 		/*
859 		 * Don't start the timer now, let it start on the
860 		 * first heartbeat.
861 		 */
862 		ipmi_start_timer_on_heartbeat = 1;
863 		return nonseekable_open(ino, filep);
864 
865 	default:
866 		return (-ENODEV);
867 	}
868 }
869 
870 static __poll_t ipmi_poll(struct file *file, poll_table *wait)
871 {
872 	__poll_t mask = 0;
873 
874 	poll_wait(file, &read_q, wait);
875 
876 	spin_lock(&ipmi_read_lock);
877 	if (data_to_read)
878 		mask |= (EPOLLIN | EPOLLRDNORM);
879 	spin_unlock(&ipmi_read_lock);
880 
881 	return mask;
882 }
883 
884 static int ipmi_fasync(int fd, struct file *file, int on)
885 {
886 	int result;
887 
888 	result = fasync_helper(fd, file, on, &fasync_q);
889 
890 	return (result);
891 }
892 
893 static int ipmi_close(struct inode *ino, struct file *filep)
894 {
895 	if (iminor(ino) == WATCHDOG_MINOR) {
896 		if (expect_close == 42) {
897 			ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
898 			ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
899 		} else {
900 			printk(KERN_CRIT PFX
901 			       "Unexpected close, not stopping watchdog!\n");
902 			ipmi_heartbeat();
903 		}
904 		clear_bit(0, &ipmi_wdog_open);
905 	}
906 
907 	expect_close = 0;
908 
909 	return 0;
910 }
911 
912 static const struct file_operations ipmi_wdog_fops = {
913 	.owner   = THIS_MODULE,
914 	.read    = ipmi_read,
915 	.poll    = ipmi_poll,
916 	.write   = ipmi_write,
917 	.unlocked_ioctl = ipmi_unlocked_ioctl,
918 	.open    = ipmi_open,
919 	.release = ipmi_close,
920 	.fasync  = ipmi_fasync,
921 	.llseek  = no_llseek,
922 };
923 
924 static struct miscdevice ipmi_wdog_miscdev = {
925 	.minor		= WATCHDOG_MINOR,
926 	.name		= "watchdog",
927 	.fops		= &ipmi_wdog_fops
928 };
929 
930 static void ipmi_wdog_msg_handler(struct ipmi_recv_msg *msg,
931 				  void                 *handler_data)
932 {
933 	if (msg->msg.cmd == IPMI_WDOG_RESET_TIMER &&
934 			msg->msg.data[0] == IPMI_WDOG_TIMER_NOT_INIT_RESP)
935 		printk(KERN_INFO PFX "response: The IPMI controller appears"
936 		       " to have been reset, will attempt to reinitialize"
937 		       " the watchdog timer\n");
938 	else if (msg->msg.data[0] != 0)
939 		printk(KERN_ERR PFX "response: Error %x on cmd %x\n",
940 		       msg->msg.data[0],
941 		       msg->msg.cmd);
942 
943 	ipmi_free_recv_msg(msg);
944 }
945 
946 static void ipmi_wdog_pretimeout_handler(void *handler_data)
947 {
948 	if (preaction_val != WDOG_PRETIMEOUT_NONE) {
949 		if (preop_val == WDOG_PREOP_PANIC) {
950 			if (atomic_inc_and_test(&preop_panic_excl))
951 				panic("Watchdog pre-timeout");
952 		} else if (preop_val == WDOG_PREOP_GIVE_DATA) {
953 			spin_lock(&ipmi_read_lock);
954 			data_to_read = 1;
955 			wake_up_interruptible(&read_q);
956 			kill_fasync(&fasync_q, SIGIO, POLL_IN);
957 
958 			spin_unlock(&ipmi_read_lock);
959 		}
960 	}
961 
962 	/*
963 	 * On some machines, the heartbeat will give an error and not
964 	 * work unless we re-enable the timer.  So do so.
965 	 */
966 	pretimeout_since_last_heartbeat = 1;
967 }
968 
969 static const struct ipmi_user_hndl ipmi_hndlrs = {
970 	.ipmi_recv_hndl           = ipmi_wdog_msg_handler,
971 	.ipmi_watchdog_pretimeout = ipmi_wdog_pretimeout_handler
972 };
973 
974 static void ipmi_register_watchdog(int ipmi_intf)
975 {
976 	int rv = -EBUSY;
977 
978 	if (watchdog_user)
979 		goto out;
980 
981 	if ((ifnum_to_use >= 0) && (ifnum_to_use != ipmi_intf))
982 		goto out;
983 
984 	watchdog_ifnum = ipmi_intf;
985 
986 	rv = ipmi_create_user(ipmi_intf, &ipmi_hndlrs, NULL, &watchdog_user);
987 	if (rv < 0) {
988 		printk(KERN_CRIT PFX "Unable to register with ipmi\n");
989 		goto out;
990 	}
991 
992 	rv = ipmi_get_version(watchdog_user,
993 			      &ipmi_version_major,
994 			      &ipmi_version_minor);
995 	if (rv) {
996 		pr_warn(PFX "Unable to get IPMI version, assuming 1.0\n");
997 		ipmi_version_major = 1;
998 		ipmi_version_minor = 0;
999 	}
1000 
1001 	rv = misc_register(&ipmi_wdog_miscdev);
1002 	if (rv < 0) {
1003 		ipmi_destroy_user(watchdog_user);
1004 		watchdog_user = NULL;
1005 		printk(KERN_CRIT PFX "Unable to register misc device\n");
1006 	}
1007 
1008 #ifdef HAVE_DIE_NMI
1009 	if (nmi_handler_registered) {
1010 		int old_pretimeout = pretimeout;
1011 		int old_timeout = timeout;
1012 		int old_preop_val = preop_val;
1013 
1014 		/*
1015 		 * Set the pretimeout to go off in a second and give
1016 		 * ourselves plenty of time to stop the timer.
1017 		 */
1018 		ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
1019 		preop_val = WDOG_PREOP_NONE; /* Make sure nothing happens */
1020 		pretimeout = 99;
1021 		timeout = 100;
1022 
1023 		testing_nmi = 1;
1024 
1025 		rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
1026 		if (rv) {
1027 			printk(KERN_WARNING PFX "Error starting timer to"
1028 			       " test NMI: 0x%x.  The NMI pretimeout will"
1029 			       " likely not work\n", rv);
1030 			rv = 0;
1031 			goto out_restore;
1032 		}
1033 
1034 		msleep(1500);
1035 
1036 		if (testing_nmi != 2) {
1037 			printk(KERN_WARNING PFX "IPMI NMI didn't seem to"
1038 			       " occur.  The NMI pretimeout will"
1039 			       " likely not work\n");
1040 		}
1041  out_restore:
1042 		testing_nmi = 0;
1043 		preop_val = old_preop_val;
1044 		pretimeout = old_pretimeout;
1045 		timeout = old_timeout;
1046 	}
1047 #endif
1048 
1049  out:
1050 	if ((start_now) && (rv == 0)) {
1051 		/* Run from startup, so start the timer now. */
1052 		start_now = 0; /* Disable this function after first startup. */
1053 		ipmi_watchdog_state = action_val;
1054 		ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
1055 		printk(KERN_INFO PFX "Starting now!\n");
1056 	} else {
1057 		/* Stop the timer now. */
1058 		ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
1059 		ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
1060 	}
1061 }
1062 
1063 static void ipmi_unregister_watchdog(int ipmi_intf)
1064 {
1065 	int rv;
1066 
1067 	if (!watchdog_user)
1068 		goto out;
1069 
1070 	if (watchdog_ifnum != ipmi_intf)
1071 		goto out;
1072 
1073 	/* Make sure no one can call us any more. */
1074 	misc_deregister(&ipmi_wdog_miscdev);
1075 
1076 	/*
1077 	 * Wait to make sure the message makes it out.  The lower layer has
1078 	 * pointers to our buffers, we want to make sure they are done before
1079 	 * we release our memory.
1080 	 */
1081 	while (atomic_read(&set_timeout_tofree))
1082 		schedule_timeout_uninterruptible(1);
1083 
1084 	/* Disconnect from IPMI. */
1085 	rv = ipmi_destroy_user(watchdog_user);
1086 	if (rv) {
1087 		printk(KERN_WARNING PFX "error unlinking from IPMI: %d\n",
1088 		       rv);
1089 	}
1090 	watchdog_user = NULL;
1091 
1092  out:
1093 	return;
1094 }
1095 
1096 #ifdef HAVE_DIE_NMI
1097 static int
1098 ipmi_nmi(unsigned int val, struct pt_regs *regs)
1099 {
1100 	/*
1101 	 * If we get here, it's an NMI that's not a memory or I/O
1102 	 * error.  We can't truly tell if it's from IPMI or not
1103 	 * without sending a message, and sending a message is almost
1104 	 * impossible because of locking.
1105 	 */
1106 
1107 	if (testing_nmi) {
1108 		testing_nmi = 2;
1109 		return NMI_HANDLED;
1110 	}
1111 
1112 	/* If we are not expecting a timeout, ignore it. */
1113 	if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)
1114 		return NMI_DONE;
1115 
1116 	if (preaction_val != WDOG_PRETIMEOUT_NMI)
1117 		return NMI_DONE;
1118 
1119 	/*
1120 	 * If no one else handled the NMI, we assume it was the IPMI
1121 	 * watchdog.
1122 	 */
1123 	if (preop_val == WDOG_PREOP_PANIC) {
1124 		/* On some machines, the heartbeat will give
1125 		   an error and not work unless we re-enable
1126 		   the timer.   So do so. */
1127 		pretimeout_since_last_heartbeat = 1;
1128 		if (atomic_inc_and_test(&preop_panic_excl))
1129 			nmi_panic(regs, PFX "pre-timeout");
1130 	}
1131 
1132 	return NMI_HANDLED;
1133 }
1134 #endif
1135 
1136 static int wdog_reboot_handler(struct notifier_block *this,
1137 			       unsigned long         code,
1138 			       void                  *unused)
1139 {
1140 	static int reboot_event_handled;
1141 
1142 	if ((watchdog_user) && (!reboot_event_handled)) {
1143 		/* Make sure we only do this once. */
1144 		reboot_event_handled = 1;
1145 
1146 		if (code == SYS_POWER_OFF || code == SYS_HALT) {
1147 			/* Disable the WDT if we are shutting down. */
1148 			ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
1149 			ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
1150 		} else if (ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
1151 			/* Set a long timer to let the reboot happen or
1152 			   reset if it hangs, but only if the watchdog
1153 			   timer was already running. */
1154 			if (timeout < 120)
1155 				timeout = 120;
1156 			pretimeout = 0;
1157 			ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
1158 			ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
1159 		}
1160 	}
1161 	return NOTIFY_OK;
1162 }
1163 
1164 static struct notifier_block wdog_reboot_notifier = {
1165 	.notifier_call	= wdog_reboot_handler,
1166 	.next		= NULL,
1167 	.priority	= 0
1168 };
1169 
1170 static int wdog_panic_handler(struct notifier_block *this,
1171 			      unsigned long         event,
1172 			      void                  *unused)
1173 {
1174 	static int panic_event_handled;
1175 
1176 	/* On a panic, if we have a panic timeout, make sure to extend
1177 	   the watchdog timer to a reasonable value to complete the
1178 	   panic, if the watchdog timer is running.  Plus the
1179 	   pretimeout is meaningless at panic time. */
1180 	if (watchdog_user && !panic_event_handled &&
1181 	    ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
1182 		/* Make sure we do this only once. */
1183 		panic_event_handled = 1;
1184 
1185 		timeout = panic_wdt_timeout;
1186 		pretimeout = 0;
1187 		panic_halt_ipmi_set_timeout();
1188 	}
1189 
1190 	return NOTIFY_OK;
1191 }
1192 
1193 static struct notifier_block wdog_panic_notifier = {
1194 	.notifier_call	= wdog_panic_handler,
1195 	.next		= NULL,
1196 	.priority	= 150	/* priority: INT_MAX >= x >= 0 */
1197 };
1198 
1199 
1200 static void ipmi_new_smi(int if_num, struct device *device)
1201 {
1202 	ipmi_register_watchdog(if_num);
1203 }
1204 
1205 static void ipmi_smi_gone(int if_num)
1206 {
1207 	ipmi_unregister_watchdog(if_num);
1208 }
1209 
1210 static struct ipmi_smi_watcher smi_watcher = {
1211 	.owner    = THIS_MODULE,
1212 	.new_smi  = ipmi_new_smi,
1213 	.smi_gone = ipmi_smi_gone
1214 };
1215 
1216 static int action_op(const char *inval, char *outval)
1217 {
1218 	if (outval)
1219 		strcpy(outval, action);
1220 
1221 	if (!inval)
1222 		return 0;
1223 
1224 	if (strcmp(inval, "reset") == 0)
1225 		action_val = WDOG_TIMEOUT_RESET;
1226 	else if (strcmp(inval, "none") == 0)
1227 		action_val = WDOG_TIMEOUT_NONE;
1228 	else if (strcmp(inval, "power_cycle") == 0)
1229 		action_val = WDOG_TIMEOUT_POWER_CYCLE;
1230 	else if (strcmp(inval, "power_off") == 0)
1231 		action_val = WDOG_TIMEOUT_POWER_DOWN;
1232 	else
1233 		return -EINVAL;
1234 	strcpy(action, inval);
1235 	return 0;
1236 }
1237 
1238 static int preaction_op(const char *inval, char *outval)
1239 {
1240 	if (outval)
1241 		strcpy(outval, preaction);
1242 
1243 	if (!inval)
1244 		return 0;
1245 
1246 	if (strcmp(inval, "pre_none") == 0)
1247 		preaction_val = WDOG_PRETIMEOUT_NONE;
1248 	else if (strcmp(inval, "pre_smi") == 0)
1249 		preaction_val = WDOG_PRETIMEOUT_SMI;
1250 #ifdef HAVE_DIE_NMI
1251 	else if (strcmp(inval, "pre_nmi") == 0)
1252 		preaction_val = WDOG_PRETIMEOUT_NMI;
1253 #endif
1254 	else if (strcmp(inval, "pre_int") == 0)
1255 		preaction_val = WDOG_PRETIMEOUT_MSG_INT;
1256 	else
1257 		return -EINVAL;
1258 	strcpy(preaction, inval);
1259 	return 0;
1260 }
1261 
1262 static int preop_op(const char *inval, char *outval)
1263 {
1264 	if (outval)
1265 		strcpy(outval, preop);
1266 
1267 	if (!inval)
1268 		return 0;
1269 
1270 	if (strcmp(inval, "preop_none") == 0)
1271 		preop_val = WDOG_PREOP_NONE;
1272 	else if (strcmp(inval, "preop_panic") == 0)
1273 		preop_val = WDOG_PREOP_PANIC;
1274 	else if (strcmp(inval, "preop_give_data") == 0)
1275 		preop_val = WDOG_PREOP_GIVE_DATA;
1276 	else
1277 		return -EINVAL;
1278 	strcpy(preop, inval);
1279 	return 0;
1280 }
1281 
1282 static void check_parms(void)
1283 {
1284 #ifdef HAVE_DIE_NMI
1285 	int do_nmi = 0;
1286 	int rv;
1287 
1288 	if (preaction_val == WDOG_PRETIMEOUT_NMI) {
1289 		do_nmi = 1;
1290 		if (preop_val == WDOG_PREOP_GIVE_DATA) {
1291 			printk(KERN_WARNING PFX "Pretimeout op is to give data"
1292 			       " but NMI pretimeout is enabled, setting"
1293 			       " pretimeout op to none\n");
1294 			preop_op("preop_none", NULL);
1295 			do_nmi = 0;
1296 		}
1297 	}
1298 	if (do_nmi && !nmi_handler_registered) {
1299 		rv = register_nmi_handler(NMI_UNKNOWN, ipmi_nmi, 0,
1300 						"ipmi");
1301 		if (rv) {
1302 			printk(KERN_WARNING PFX
1303 			       "Can't register nmi handler\n");
1304 			return;
1305 		} else
1306 			nmi_handler_registered = 1;
1307 	} else if (!do_nmi && nmi_handler_registered) {
1308 		unregister_nmi_handler(NMI_UNKNOWN, "ipmi");
1309 		nmi_handler_registered = 0;
1310 	}
1311 #endif
1312 }
1313 
1314 static int __init ipmi_wdog_init(void)
1315 {
1316 	int rv;
1317 
1318 	if (action_op(action, NULL)) {
1319 		action_op("reset", NULL);
1320 		printk(KERN_INFO PFX "Unknown action '%s', defaulting to"
1321 		       " reset\n", action);
1322 	}
1323 
1324 	if (preaction_op(preaction, NULL)) {
1325 		preaction_op("pre_none", NULL);
1326 		printk(KERN_INFO PFX "Unknown preaction '%s', defaulting to"
1327 		       " none\n", preaction);
1328 	}
1329 
1330 	if (preop_op(preop, NULL)) {
1331 		preop_op("preop_none", NULL);
1332 		printk(KERN_INFO PFX "Unknown preop '%s', defaulting to"
1333 		       " none\n", preop);
1334 	}
1335 
1336 	check_parms();
1337 
1338 	register_reboot_notifier(&wdog_reboot_notifier);
1339 	atomic_notifier_chain_register(&panic_notifier_list,
1340 			&wdog_panic_notifier);
1341 
1342 	rv = ipmi_smi_watcher_register(&smi_watcher);
1343 	if (rv) {
1344 #ifdef HAVE_DIE_NMI
1345 		if (nmi_handler_registered)
1346 			unregister_nmi_handler(NMI_UNKNOWN, "ipmi");
1347 #endif
1348 		atomic_notifier_chain_unregister(&panic_notifier_list,
1349 						 &wdog_panic_notifier);
1350 		unregister_reboot_notifier(&wdog_reboot_notifier);
1351 		printk(KERN_WARNING PFX "can't register smi watcher\n");
1352 		return rv;
1353 	}
1354 
1355 	printk(KERN_INFO PFX "driver initialized\n");
1356 
1357 	return 0;
1358 }
1359 
1360 static void __exit ipmi_wdog_exit(void)
1361 {
1362 	ipmi_smi_watcher_unregister(&smi_watcher);
1363 	ipmi_unregister_watchdog(watchdog_ifnum);
1364 
1365 #ifdef HAVE_DIE_NMI
1366 	if (nmi_handler_registered)
1367 		unregister_nmi_handler(NMI_UNKNOWN, "ipmi");
1368 #endif
1369 
1370 	atomic_notifier_chain_unregister(&panic_notifier_list,
1371 					 &wdog_panic_notifier);
1372 	unregister_reboot_notifier(&wdog_reboot_notifier);
1373 }
1374 module_exit(ipmi_wdog_exit);
1375 module_init(ipmi_wdog_init);
1376 MODULE_LICENSE("GPL");
1377 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
1378 MODULE_DESCRIPTION("watchdog timer based upon the IPMI interface.");
1379