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