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