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/kstrtox.h> 27 #include <linux/rwsem.h> 28 #include <linux/errno.h> 29 #include <linux/uaccess.h> 30 #include <linux/notifier.h> 31 #include <linux/nmi.h> 32 #include <linux/reboot.h> 33 #include <linux/wait.h> 34 #include <linux/poll.h> 35 #include <linux/string.h> 36 #include <linux/ctype.h> 37 #include <linux/delay.h> 38 #include <linux/atomic.h> 39 #include <linux/sched/signal.h> 40 41 #ifdef CONFIG_X86 42 /* 43 * This is ugly, but I've determined that x86 is the only architecture 44 * that can reasonably support the IPMI NMI watchdog timeout at this 45 * time. If another architecture adds this capability somehow, it 46 * will have to be a somewhat different mechanism and I have no idea 47 * how it will work. So in the unlikely event that another 48 * architecture supports this, we can figure out a good generic 49 * mechanism for it at that time. 50 */ 51 #include <asm/kdebug.h> 52 #include <asm/nmi.h> 53 #define HAVE_DIE_NMI 54 #endif 55 56 /* 57 * The IPMI command/response information for the watchdog timer. 58 */ 59 60 /* values for byte 1 of the set command, byte 2 of the get response. */ 61 #define WDOG_DONT_LOG (1 << 7) 62 #define WDOG_DONT_STOP_ON_SET (1 << 6) 63 #define WDOG_SET_TIMER_USE(byte, use) \ 64 byte = ((byte) & 0xf8) | ((use) & 0x7) 65 #define WDOG_GET_TIMER_USE(byte) ((byte) & 0x7) 66 #define WDOG_TIMER_USE_BIOS_FRB2 1 67 #define WDOG_TIMER_USE_BIOS_POST 2 68 #define WDOG_TIMER_USE_OS_LOAD 3 69 #define WDOG_TIMER_USE_SMS_OS 4 70 #define WDOG_TIMER_USE_OEM 5 71 72 /* values for byte 2 of the set command, byte 3 of the get response. */ 73 #define WDOG_SET_PRETIMEOUT_ACT(byte, use) \ 74 byte = ((byte) & 0x8f) | (((use) & 0x7) << 4) 75 #define WDOG_GET_PRETIMEOUT_ACT(byte) (((byte) >> 4) & 0x7) 76 #define WDOG_PRETIMEOUT_NONE 0 77 #define WDOG_PRETIMEOUT_SMI 1 78 #define WDOG_PRETIMEOUT_NMI 2 79 #define WDOG_PRETIMEOUT_MSG_INT 3 80 81 /* Operations that can be performed on a pretimout. */ 82 #define WDOG_PREOP_NONE 0 83 #define WDOG_PREOP_PANIC 1 84 /* Cause data to be available to read. Doesn't work in NMI mode. */ 85 #define WDOG_PREOP_GIVE_DATA 2 86 87 /* Actions to perform on a full timeout. */ 88 #define WDOG_SET_TIMEOUT_ACT(byte, use) \ 89 byte = ((byte) & 0xf8) | ((use) & 0x7) 90 #define WDOG_GET_TIMEOUT_ACT(byte) ((byte) & 0x7) 91 #define WDOG_TIMEOUT_NONE 0 92 #define WDOG_TIMEOUT_RESET 1 93 #define WDOG_TIMEOUT_POWER_DOWN 2 94 #define WDOG_TIMEOUT_POWER_CYCLE 3 95 96 /* 97 * Byte 3 of the get command, byte 4 of the get response is the 98 * pre-timeout in seconds. 99 */ 100 101 /* Bits for setting byte 4 of the set command, byte 5 of the get response. */ 102 #define WDOG_EXPIRE_CLEAR_BIOS_FRB2 (1 << 1) 103 #define WDOG_EXPIRE_CLEAR_BIOS_POST (1 << 2) 104 #define WDOG_EXPIRE_CLEAR_OS_LOAD (1 << 3) 105 #define WDOG_EXPIRE_CLEAR_SMS_OS (1 << 4) 106 #define WDOG_EXPIRE_CLEAR_OEM (1 << 5) 107 108 /* 109 * Setting/getting the watchdog timer value. This is for bytes 5 and 110 * 6 (the timeout time) of the set command, and bytes 6 and 7 (the 111 * timeout time) and 8 and 9 (the current countdown value) of the 112 * response. The timeout value is given in seconds (in the command it 113 * is 100ms intervals). 114 */ 115 #define WDOG_SET_TIMEOUT(byte1, byte2, val) \ 116 (byte1) = (((val) * 10) & 0xff), (byte2) = (((val) * 10) >> 8) 117 #define WDOG_GET_TIMEOUT(byte1, byte2) \ 118 (((byte1) | ((byte2) << 8)) / 10) 119 120 #define IPMI_WDOG_RESET_TIMER 0x22 121 #define IPMI_WDOG_SET_TIMER 0x24 122 #define IPMI_WDOG_GET_TIMER 0x25 123 124 #define IPMI_WDOG_TIMER_NOT_INIT_RESP 0x80 125 126 static DEFINE_MUTEX(ipmi_watchdog_mutex); 127 static bool nowayout = WATCHDOG_NOWAYOUT; 128 129 static struct ipmi_user *watchdog_user; 130 static int watchdog_ifnum; 131 132 /* Default the timeout to 10 seconds. */ 133 static int timeout = 10; 134 135 /* The pre-timeout is disabled by default. */ 136 static int pretimeout; 137 138 /* Default timeout to set on panic */ 139 static int panic_wdt_timeout = 255; 140 141 /* Default action is to reset the board on a timeout. */ 142 static unsigned char action_val = WDOG_TIMEOUT_RESET; 143 144 static char action[16] = "reset"; 145 146 static unsigned char preaction_val = WDOG_PRETIMEOUT_NONE; 147 148 static char preaction[16] = "pre_none"; 149 150 static unsigned char preop_val = WDOG_PREOP_NONE; 151 152 static char preop[16] = "preop_none"; 153 static DEFINE_SPINLOCK(ipmi_read_lock); 154 static char data_to_read; 155 static DECLARE_WAIT_QUEUE_HEAD(read_q); 156 static struct fasync_struct *fasync_q; 157 static atomic_t pretimeout_since_last_heartbeat; 158 static char expect_close; 159 160 static int ifnum_to_use = -1; 161 162 /* Parameters to ipmi_set_timeout */ 163 #define IPMI_SET_TIMEOUT_NO_HB 0 164 #define IPMI_SET_TIMEOUT_HB_IF_NECESSARY 1 165 #define IPMI_SET_TIMEOUT_FORCE_HB 2 166 167 static int ipmi_set_timeout(int do_heartbeat); 168 static void ipmi_register_watchdog(int ipmi_intf); 169 static void ipmi_unregister_watchdog(int ipmi_intf); 170 171 /* 172 * If true, the driver will start running as soon as it is configured 173 * and ready. 174 */ 175 static int start_now; 176 177 static int set_param_timeout(const char *val, const struct kernel_param *kp) 178 { 179 char *endp; 180 int l; 181 int rv = 0; 182 183 if (!val) 184 return -EINVAL; 185 l = simple_strtoul(val, &endp, 0); 186 if (endp == val) 187 return -EINVAL; 188 189 *((int *)kp->arg) = l; 190 if (watchdog_user) 191 rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY); 192 193 return rv; 194 } 195 196 static const struct kernel_param_ops param_ops_timeout = { 197 .set = set_param_timeout, 198 .get = param_get_int, 199 }; 200 #define param_check_timeout param_check_int 201 202 typedef int (*action_fn)(const char *intval, char *outval); 203 204 static int action_op(const char *inval, char *outval); 205 static int preaction_op(const char *inval, char *outval); 206 static int preop_op(const char *inval, char *outval); 207 static void check_parms(void); 208 209 static int set_param_str(const char *val, const struct kernel_param *kp) 210 { 211 action_fn fn = (action_fn) kp->arg; 212 int rv = 0; 213 char valcp[16]; 214 char *s; 215 216 strscpy(valcp, val, 16); 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, 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 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); 343 static void msg_free_smi(struct ipmi_smi_msg *msg) 344 { 345 if (atomic_dec_and_test(&msg_tofree)) { 346 if (!oops_in_progress) 347 complete(&msg_wait); 348 } 349 } 350 static void msg_free_recv(struct ipmi_recv_msg *msg) 351 { 352 if (atomic_dec_and_test(&msg_tofree)) { 353 if (!oops_in_progress) 354 complete(&msg_wait); 355 } 356 } 357 static struct ipmi_smi_msg smi_msg = INIT_IPMI_SMI_MSG(msg_free_smi); 358 static struct ipmi_recv_msg recv_msg = INIT_IPMI_RECV_MSG(msg_free_recv); 359 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 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 atomic_set(&msg_tofree, 0); 439 return rv; 440 } 441 442 wait_for_completion(&msg_wait); 443 444 if ((do_heartbeat == IPMI_SET_TIMEOUT_FORCE_HB) 445 || ((send_heartbeat_now) 446 && (do_heartbeat == IPMI_SET_TIMEOUT_HB_IF_NECESSARY))) 447 rv = __ipmi_heartbeat(); 448 449 return rv; 450 } 451 452 static int ipmi_set_timeout(int do_heartbeat) 453 { 454 int rv; 455 456 mutex_lock(&ipmi_watchdog_mutex); 457 rv = _ipmi_set_timeout(do_heartbeat); 458 mutex_unlock(&ipmi_watchdog_mutex); 459 460 return rv; 461 } 462 463 static atomic_t panic_done_count = ATOMIC_INIT(0); 464 465 static void panic_smi_free(struct ipmi_smi_msg *msg) 466 { 467 atomic_dec(&panic_done_count); 468 } 469 static void panic_recv_free(struct ipmi_recv_msg *msg) 470 { 471 atomic_dec(&panic_done_count); 472 } 473 474 static struct ipmi_smi_msg panic_halt_heartbeat_smi_msg = 475 INIT_IPMI_SMI_MSG(panic_smi_free); 476 static struct ipmi_recv_msg panic_halt_heartbeat_recv_msg = 477 INIT_IPMI_RECV_MSG(panic_recv_free); 478 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(2, &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(2, &panic_done_count); 511 } 512 513 static struct ipmi_smi_msg panic_halt_smi_msg = 514 INIT_IPMI_SMI_MSG(panic_smi_free); 515 static struct ipmi_recv_msg panic_halt_recv_msg = 516 INIT_IPMI_RECV_MSG(panic_recv_free); 517 518 /* 519 * Special call, doesn't claim any locks. This is only to be called 520 * at panic or halt time, in run-to-completion mode, when the caller 521 * is the only CPU and the only thing that will be going is these IPMI 522 * calls. 523 */ 524 static void panic_halt_ipmi_set_timeout(void) 525 { 526 int send_heartbeat_now; 527 int rv; 528 529 /* Wait for the messages to be free. */ 530 while (atomic_read(&panic_done_count) != 0) 531 ipmi_poll_interface(watchdog_user); 532 atomic_add(2, &panic_done_count); 533 rv = __ipmi_set_timeout(&panic_halt_smi_msg, 534 &panic_halt_recv_msg, 535 &send_heartbeat_now); 536 if (rv) { 537 atomic_sub(2, &panic_done_count); 538 pr_warn("Unable to extend the watchdog timeout\n"); 539 } else { 540 if (send_heartbeat_now) 541 panic_halt_ipmi_heartbeat(); 542 } 543 while (atomic_read(&panic_done_count) != 0) 544 ipmi_poll_interface(watchdog_user); 545 } 546 547 static int __ipmi_heartbeat(void) 548 { 549 struct kernel_ipmi_msg msg; 550 int rv; 551 struct ipmi_system_interface_addr addr; 552 int timeout_retries = 0; 553 554 restart: 555 /* 556 * Don't reset the timer if we have the timer turned off, that 557 * re-enables the watchdog. 558 */ 559 if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE) 560 return 0; 561 562 atomic_set(&msg_tofree, 2); 563 564 addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; 565 addr.channel = IPMI_BMC_CHANNEL; 566 addr.lun = 0; 567 568 msg.netfn = 0x06; 569 msg.cmd = IPMI_WDOG_RESET_TIMER; 570 msg.data = NULL; 571 msg.data_len = 0; 572 rv = ipmi_request_supply_msgs(watchdog_user, 573 (struct ipmi_addr *) &addr, 574 0, 575 &msg, 576 NULL, 577 &smi_msg, 578 &recv_msg, 579 1); 580 if (rv) { 581 atomic_set(&msg_tofree, 0); 582 pr_warn("heartbeat send failure: %d\n", rv); 583 return rv; 584 } 585 586 /* Wait for the heartbeat to be sent. */ 587 wait_for_completion(&msg_wait); 588 589 if (recv_msg.msg.data[0] == IPMI_WDOG_TIMER_NOT_INIT_RESP) { 590 timeout_retries++; 591 if (timeout_retries > 3) { 592 pr_err("Unable to restore the IPMI watchdog's settings, giving up\n"); 593 rv = -EIO; 594 goto out; 595 } 596 597 /* 598 * The timer was not initialized, that means the BMC was 599 * probably reset and lost the watchdog information. Attempt 600 * to restore the timer's info. Note that we still hold 601 * the heartbeat lock, to keep a heartbeat from happening 602 * in this process, so must say no heartbeat to avoid a 603 * deadlock on this mutex 604 */ 605 rv = _ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB); 606 if (rv) { 607 pr_err("Unable to send the command to set the watchdog's settings, giving up\n"); 608 goto out; 609 } 610 611 /* Might need a heartbeat send, go ahead and do it. */ 612 goto restart; 613 } else if (recv_msg.msg.data[0] != 0) { 614 /* 615 * Got an error in the heartbeat response. It was already 616 * reported in ipmi_wdog_msg_handler, but we should return 617 * an error here. 618 */ 619 rv = -EINVAL; 620 } 621 622 out: 623 return rv; 624 } 625 626 static int _ipmi_heartbeat(void) 627 { 628 int rv; 629 630 if (!watchdog_user) 631 return -ENODEV; 632 633 if (ipmi_start_timer_on_heartbeat) { 634 ipmi_start_timer_on_heartbeat = 0; 635 ipmi_watchdog_state = action_val; 636 rv = _ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB); 637 } else if (atomic_cmpxchg(&pretimeout_since_last_heartbeat, 1, 0)) { 638 /* 639 * A pretimeout occurred, make sure we set the timeout. 640 * We don't want to set the action, though, we want to 641 * leave that alone (thus it can't be combined with the 642 * above operation. 643 */ 644 rv = _ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY); 645 } else { 646 rv = __ipmi_heartbeat(); 647 } 648 649 return rv; 650 } 651 652 static int ipmi_heartbeat(void) 653 { 654 int rv; 655 656 mutex_lock(&ipmi_watchdog_mutex); 657 rv = _ipmi_heartbeat(); 658 mutex_unlock(&ipmi_watchdog_mutex); 659 660 return rv; 661 } 662 663 static const struct watchdog_info ident = { 664 .options = 0, /* WDIOF_SETTIMEOUT, */ 665 .firmware_version = 1, 666 .identity = "IPMI" 667 }; 668 669 static int ipmi_ioctl(struct file *file, 670 unsigned int cmd, unsigned long arg) 671 { 672 void __user *argp = (void __user *)arg; 673 int i; 674 int val; 675 676 switch (cmd) { 677 case WDIOC_GETSUPPORT: 678 i = copy_to_user(argp, &ident, sizeof(ident)); 679 return i ? -EFAULT : 0; 680 681 case WDIOC_SETTIMEOUT: 682 i = copy_from_user(&val, argp, sizeof(int)); 683 if (i) 684 return -EFAULT; 685 timeout = val; 686 return _ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY); 687 688 case WDIOC_GETTIMEOUT: 689 i = copy_to_user(argp, &timeout, sizeof(timeout)); 690 if (i) 691 return -EFAULT; 692 return 0; 693 694 case WDIOC_SETPRETIMEOUT: 695 i = copy_from_user(&val, argp, sizeof(int)); 696 if (i) 697 return -EFAULT; 698 pretimeout = val; 699 return _ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY); 700 701 case WDIOC_GETPRETIMEOUT: 702 i = copy_to_user(argp, &pretimeout, sizeof(pretimeout)); 703 if (i) 704 return -EFAULT; 705 return 0; 706 707 case WDIOC_KEEPALIVE: 708 return _ipmi_heartbeat(); 709 710 case WDIOC_SETOPTIONS: 711 i = copy_from_user(&val, argp, sizeof(int)); 712 if (i) 713 return -EFAULT; 714 if (val & WDIOS_DISABLECARD) { 715 ipmi_watchdog_state = WDOG_TIMEOUT_NONE; 716 _ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB); 717 ipmi_start_timer_on_heartbeat = 0; 718 } 719 720 if (val & WDIOS_ENABLECARD) { 721 ipmi_watchdog_state = action_val; 722 _ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB); 723 } 724 return 0; 725 726 case WDIOC_GETSTATUS: 727 val = 0; 728 i = copy_to_user(argp, &val, sizeof(val)); 729 if (i) 730 return -EFAULT; 731 return 0; 732 733 default: 734 return -ENOIOCTLCMD; 735 } 736 } 737 738 static long ipmi_unlocked_ioctl(struct file *file, 739 unsigned int cmd, 740 unsigned long arg) 741 { 742 int ret; 743 744 mutex_lock(&ipmi_watchdog_mutex); 745 ret = ipmi_ioctl(file, cmd, arg); 746 mutex_unlock(&ipmi_watchdog_mutex); 747 748 return ret; 749 } 750 751 static ssize_t ipmi_write(struct file *file, 752 const char __user *buf, 753 size_t len, 754 loff_t *ppos) 755 { 756 int rv; 757 758 if (len) { 759 if (!nowayout) { 760 size_t i; 761 762 /* In case it was set long ago */ 763 expect_close = 0; 764 765 for (i = 0; i != len; i++) { 766 char c; 767 768 if (get_user(c, buf + i)) 769 return -EFAULT; 770 if (c == 'V') 771 expect_close = 42; 772 } 773 } 774 rv = ipmi_heartbeat(); 775 if (rv) 776 return rv; 777 } 778 return len; 779 } 780 781 static ssize_t ipmi_read(struct file *file, 782 char __user *buf, 783 size_t count, 784 loff_t *ppos) 785 { 786 int rv = 0; 787 wait_queue_entry_t wait; 788 789 if (count <= 0) 790 return 0; 791 792 /* 793 * Reading returns if the pretimeout has gone off, and it only does 794 * it once per pretimeout. 795 */ 796 spin_lock_irq(&ipmi_read_lock); 797 if (!data_to_read) { 798 if (file->f_flags & O_NONBLOCK) { 799 rv = -EAGAIN; 800 goto out; 801 } 802 803 init_waitqueue_entry(&wait, current); 804 add_wait_queue(&read_q, &wait); 805 while (!data_to_read && !signal_pending(current)) { 806 set_current_state(TASK_INTERRUPTIBLE); 807 spin_unlock_irq(&ipmi_read_lock); 808 schedule(); 809 spin_lock_irq(&ipmi_read_lock); 810 } 811 remove_wait_queue(&read_q, &wait); 812 813 if (signal_pending(current)) { 814 rv = -ERESTARTSYS; 815 goto out; 816 } 817 } 818 data_to_read = 0; 819 820 out: 821 spin_unlock_irq(&ipmi_read_lock); 822 823 if (rv == 0) { 824 if (copy_to_user(buf, &data_to_read, 1)) 825 rv = -EFAULT; 826 else 827 rv = 1; 828 } 829 830 return rv; 831 } 832 833 static int ipmi_open(struct inode *ino, struct file *filep) 834 { 835 switch (iminor(ino)) { 836 case WATCHDOG_MINOR: 837 if (test_and_set_bit(0, &ipmi_wdog_open)) 838 return -EBUSY; 839 840 841 /* 842 * Don't start the timer now, let it start on the 843 * first heartbeat. 844 */ 845 ipmi_start_timer_on_heartbeat = 1; 846 return stream_open(ino, filep); 847 848 default: 849 return (-ENODEV); 850 } 851 } 852 853 static __poll_t ipmi_poll(struct file *file, poll_table *wait) 854 { 855 __poll_t mask = 0; 856 857 poll_wait(file, &read_q, wait); 858 859 spin_lock_irq(&ipmi_read_lock); 860 if (data_to_read) 861 mask |= (EPOLLIN | EPOLLRDNORM); 862 spin_unlock_irq(&ipmi_read_lock); 863 864 return mask; 865 } 866 867 static int ipmi_fasync(int fd, struct file *file, int on) 868 { 869 int result; 870 871 result = fasync_helper(fd, file, on, &fasync_q); 872 873 return (result); 874 } 875 876 static int ipmi_close(struct inode *ino, struct file *filep) 877 { 878 if (iminor(ino) == WATCHDOG_MINOR) { 879 if (expect_close == 42) { 880 mutex_lock(&ipmi_watchdog_mutex); 881 ipmi_watchdog_state = WDOG_TIMEOUT_NONE; 882 _ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB); 883 mutex_unlock(&ipmi_watchdog_mutex); 884 } else { 885 pr_crit("Unexpected close, not stopping watchdog!\n"); 886 ipmi_heartbeat(); 887 } 888 clear_bit(0, &ipmi_wdog_open); 889 } 890 891 expect_close = 0; 892 893 return 0; 894 } 895 896 static const struct file_operations ipmi_wdog_fops = { 897 .owner = THIS_MODULE, 898 .read = ipmi_read, 899 .poll = ipmi_poll, 900 .write = ipmi_write, 901 .unlocked_ioctl = ipmi_unlocked_ioctl, 902 .compat_ioctl = compat_ptr_ioctl, 903 .open = ipmi_open, 904 .release = ipmi_close, 905 .fasync = ipmi_fasync, 906 }; 907 908 static struct miscdevice ipmi_wdog_miscdev = { 909 .minor = WATCHDOG_MINOR, 910 .name = "watchdog", 911 .fops = &ipmi_wdog_fops 912 }; 913 914 static void ipmi_wdog_msg_handler(struct ipmi_recv_msg *msg, 915 void *handler_data) 916 { 917 if (msg->msg.cmd == IPMI_WDOG_RESET_TIMER && 918 msg->msg.data[0] == IPMI_WDOG_TIMER_NOT_INIT_RESP) 919 pr_info("response: The IPMI controller appears to have been reset, will attempt to reinitialize the watchdog timer\n"); 920 else if (msg->msg.data[0] != 0) 921 pr_err("response: Error %x on cmd %x\n", 922 msg->msg.data[0], 923 msg->msg.cmd); 924 925 ipmi_free_recv_msg(msg); 926 } 927 928 static void ipmi_wdog_pretimeout_handler(void *handler_data) 929 { 930 if (preaction_val != WDOG_PRETIMEOUT_NONE) { 931 if (preop_val == WDOG_PREOP_PANIC) { 932 if (atomic_inc_and_test(&preop_panic_excl)) 933 panic("Watchdog pre-timeout"); 934 } else if (preop_val == WDOG_PREOP_GIVE_DATA) { 935 unsigned long flags; 936 937 spin_lock_irqsave(&ipmi_read_lock, flags); 938 data_to_read = 1; 939 wake_up_interruptible(&read_q); 940 kill_fasync(&fasync_q, SIGIO, POLL_IN); 941 spin_unlock_irqrestore(&ipmi_read_lock, flags); 942 } 943 } 944 945 /* 946 * On some machines, the heartbeat will give an error and not 947 * work unless we re-enable the timer. So do so. 948 */ 949 atomic_set(&pretimeout_since_last_heartbeat, 1); 950 } 951 952 static void ipmi_wdog_panic_handler(void *user_data) 953 { 954 static int panic_event_handled; 955 956 /* 957 * On a panic, if we have a panic timeout, make sure to extend 958 * the watchdog timer to a reasonable value to complete the 959 * panic, if the watchdog timer is running. Plus the 960 * pretimeout is meaningless at panic time. 961 */ 962 if (watchdog_user && !panic_event_handled && 963 ipmi_watchdog_state != WDOG_TIMEOUT_NONE) { 964 /* Make sure we do this only once. */ 965 panic_event_handled = 1; 966 967 timeout = panic_wdt_timeout; 968 pretimeout = 0; 969 panic_halt_ipmi_set_timeout(); 970 } 971 } 972 973 static const struct ipmi_user_hndl ipmi_hndlrs = { 974 .ipmi_recv_hndl = ipmi_wdog_msg_handler, 975 .ipmi_watchdog_pretimeout = ipmi_wdog_pretimeout_handler, 976 .ipmi_panic_handler = ipmi_wdog_panic_handler 977 }; 978 979 static void ipmi_register_watchdog(int ipmi_intf) 980 { 981 int rv = -EBUSY; 982 983 if (watchdog_user) 984 goto out; 985 986 if ((ifnum_to_use >= 0) && (ifnum_to_use != ipmi_intf)) 987 goto out; 988 989 watchdog_ifnum = ipmi_intf; 990 991 rv = ipmi_create_user(ipmi_intf, &ipmi_hndlrs, NULL, &watchdog_user); 992 if (rv < 0) { 993 pr_crit("Unable to register with ipmi\n"); 994 goto out; 995 } 996 997 rv = ipmi_get_version(watchdog_user, 998 &ipmi_version_major, 999 &ipmi_version_minor); 1000 if (rv) { 1001 pr_warn("Unable to get IPMI version, assuming 1.0\n"); 1002 ipmi_version_major = 1; 1003 ipmi_version_minor = 0; 1004 } 1005 1006 rv = misc_register(&ipmi_wdog_miscdev); 1007 if (rv < 0) { 1008 ipmi_destroy_user(watchdog_user); 1009 watchdog_user = NULL; 1010 pr_crit("Unable to register misc device\n"); 1011 } 1012 1013 #ifdef HAVE_DIE_NMI 1014 if (nmi_handler_registered) { 1015 int old_pretimeout = pretimeout; 1016 int old_timeout = timeout; 1017 int old_preop_val = preop_val; 1018 1019 /* 1020 * Set the pretimeout to go off in a second and give 1021 * ourselves plenty of time to stop the timer. 1022 */ 1023 ipmi_watchdog_state = WDOG_TIMEOUT_RESET; 1024 preop_val = WDOG_PREOP_NONE; /* Make sure nothing happens */ 1025 pretimeout = 99; 1026 timeout = 100; 1027 1028 testing_nmi = 1; 1029 1030 rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB); 1031 if (rv) { 1032 pr_warn("Error starting timer to test NMI: 0x%x. The NMI pretimeout will likely not work\n", 1033 rv); 1034 rv = 0; 1035 goto out_restore; 1036 } 1037 1038 msleep(1500); 1039 1040 if (testing_nmi != 2) { 1041 pr_warn("IPMI NMI didn't seem to occur. The NMI pretimeout will likely not work\n"); 1042 } 1043 out_restore: 1044 testing_nmi = 0; 1045 preop_val = old_preop_val; 1046 pretimeout = old_pretimeout; 1047 timeout = old_timeout; 1048 } 1049 #endif 1050 1051 out: 1052 if ((start_now) && (rv == 0)) { 1053 /* Run from startup, so start the timer now. */ 1054 start_now = 0; /* Disable this function after first startup. */ 1055 ipmi_watchdog_state = action_val; 1056 ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB); 1057 pr_info("Starting now!\n"); 1058 } else { 1059 /* Stop the timer now. */ 1060 ipmi_watchdog_state = WDOG_TIMEOUT_NONE; 1061 ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB); 1062 } 1063 } 1064 1065 static void ipmi_unregister_watchdog(int ipmi_intf) 1066 { 1067 int rv; 1068 struct ipmi_user *loc_user = watchdog_user; 1069 1070 if (!loc_user) 1071 return; 1072 1073 if (watchdog_ifnum != ipmi_intf) 1074 return; 1075 1076 /* Make sure no one can call us any more. */ 1077 misc_deregister(&ipmi_wdog_miscdev); 1078 1079 watchdog_user = NULL; 1080 1081 /* 1082 * Wait to make sure the message makes it out. The lower layer has 1083 * pointers to our buffers, we want to make sure they are done before 1084 * we release our memory. 1085 */ 1086 while (atomic_read(&msg_tofree)) 1087 msg_free_smi(NULL); 1088 1089 mutex_lock(&ipmi_watchdog_mutex); 1090 1091 /* Disconnect from IPMI. */ 1092 rv = ipmi_destroy_user(loc_user); 1093 if (rv) 1094 pr_warn("error unlinking from IPMI: %d\n", rv); 1095 1096 /* If it comes back, restart it properly. */ 1097 ipmi_start_timer_on_heartbeat = 1; 1098 1099 mutex_unlock(&ipmi_watchdog_mutex); 1100 } 1101 1102 #ifdef HAVE_DIE_NMI 1103 static int 1104 ipmi_nmi(unsigned int val, struct pt_regs *regs) 1105 { 1106 /* 1107 * If we get here, it's an NMI that's not a memory or I/O 1108 * error. We can't truly tell if it's from IPMI or not 1109 * without sending a message, and sending a message is almost 1110 * impossible because of locking. 1111 */ 1112 1113 if (testing_nmi) { 1114 testing_nmi = 2; 1115 return NMI_HANDLED; 1116 } 1117 1118 /* If we are not expecting a timeout, ignore it. */ 1119 if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE) 1120 return NMI_DONE; 1121 1122 if (preaction_val != WDOG_PRETIMEOUT_NMI) 1123 return NMI_DONE; 1124 1125 /* 1126 * If no one else handled the NMI, we assume it was the IPMI 1127 * watchdog. 1128 */ 1129 if (preop_val == WDOG_PREOP_PANIC) { 1130 /* On some machines, the heartbeat will give 1131 an error and not work unless we re-enable 1132 the timer. So do so. */ 1133 atomic_set(&pretimeout_since_last_heartbeat, 1); 1134 if (atomic_inc_and_test(&preop_panic_excl)) 1135 nmi_panic(regs, "pre-timeout"); 1136 } 1137 1138 return NMI_HANDLED; 1139 } 1140 #endif 1141 1142 static int wdog_reboot_handler(struct notifier_block *this, 1143 unsigned long code, 1144 void *unused) 1145 { 1146 static int reboot_event_handled; 1147 1148 if ((watchdog_user) && (!reboot_event_handled)) { 1149 /* Make sure we only do this once. */ 1150 reboot_event_handled = 1; 1151 1152 if (code == SYS_POWER_OFF || code == SYS_HALT) { 1153 /* Disable the WDT if we are shutting down. */ 1154 ipmi_watchdog_state = WDOG_TIMEOUT_NONE; 1155 ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB); 1156 } else if (ipmi_watchdog_state != WDOG_TIMEOUT_NONE) { 1157 /* Set a long timer to let the reboot happen or 1158 reset if it hangs, but only if the watchdog 1159 timer was already running. */ 1160 if (timeout < 120) 1161 timeout = 120; 1162 pretimeout = 0; 1163 ipmi_watchdog_state = WDOG_TIMEOUT_RESET; 1164 ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB); 1165 } 1166 } 1167 return NOTIFY_OK; 1168 } 1169 1170 static struct notifier_block wdog_reboot_notifier = { 1171 .notifier_call = wdog_reboot_handler, 1172 .next = NULL, 1173 .priority = 0 1174 }; 1175 1176 static void ipmi_new_smi(int if_num, struct device *device) 1177 { 1178 ipmi_register_watchdog(if_num); 1179 } 1180 1181 static void ipmi_smi_gone(int if_num) 1182 { 1183 ipmi_unregister_watchdog(if_num); 1184 } 1185 1186 static struct ipmi_smi_watcher smi_watcher = { 1187 .owner = THIS_MODULE, 1188 .new_smi = ipmi_new_smi, 1189 .smi_gone = ipmi_smi_gone 1190 }; 1191 1192 static int action_op(const char *inval, char *outval) 1193 { 1194 if (outval) 1195 strcpy(outval, action); 1196 1197 if (!inval) 1198 return 0; 1199 1200 if (strcmp(inval, "reset") == 0) 1201 action_val = WDOG_TIMEOUT_RESET; 1202 else if (strcmp(inval, "none") == 0) 1203 action_val = WDOG_TIMEOUT_NONE; 1204 else if (strcmp(inval, "power_cycle") == 0) 1205 action_val = WDOG_TIMEOUT_POWER_CYCLE; 1206 else if (strcmp(inval, "power_off") == 0) 1207 action_val = WDOG_TIMEOUT_POWER_DOWN; 1208 else 1209 return -EINVAL; 1210 strcpy(action, inval); 1211 return 0; 1212 } 1213 1214 static int preaction_op(const char *inval, char *outval) 1215 { 1216 if (outval) 1217 strcpy(outval, preaction); 1218 1219 if (!inval) 1220 return 0; 1221 1222 if (strcmp(inval, "pre_none") == 0) 1223 preaction_val = WDOG_PRETIMEOUT_NONE; 1224 else if (strcmp(inval, "pre_smi") == 0) 1225 preaction_val = WDOG_PRETIMEOUT_SMI; 1226 #ifdef HAVE_DIE_NMI 1227 else if (strcmp(inval, "pre_nmi") == 0) 1228 preaction_val = WDOG_PRETIMEOUT_NMI; 1229 #endif 1230 else if (strcmp(inval, "pre_int") == 0) 1231 preaction_val = WDOG_PRETIMEOUT_MSG_INT; 1232 else 1233 return -EINVAL; 1234 strcpy(preaction, inval); 1235 return 0; 1236 } 1237 1238 static int preop_op(const char *inval, char *outval) 1239 { 1240 if (outval) 1241 strcpy(outval, preop); 1242 1243 if (!inval) 1244 return 0; 1245 1246 if (strcmp(inval, "preop_none") == 0) 1247 preop_val = WDOG_PREOP_NONE; 1248 else if (strcmp(inval, "preop_panic") == 0) 1249 preop_val = WDOG_PREOP_PANIC; 1250 else if (strcmp(inval, "preop_give_data") == 0) 1251 preop_val = WDOG_PREOP_GIVE_DATA; 1252 else 1253 return -EINVAL; 1254 strcpy(preop, inval); 1255 return 0; 1256 } 1257 1258 static void check_parms(void) 1259 { 1260 #ifdef HAVE_DIE_NMI 1261 int do_nmi = 0; 1262 int rv; 1263 1264 if (preaction_val == WDOG_PRETIMEOUT_NMI) { 1265 do_nmi = 1; 1266 if (preop_val == WDOG_PREOP_GIVE_DATA) { 1267 pr_warn("Pretimeout op is to give data but NMI pretimeout is enabled, setting pretimeout op to none\n"); 1268 preop_op("preop_none", NULL); 1269 do_nmi = 0; 1270 } 1271 } 1272 if (do_nmi && !nmi_handler_registered) { 1273 rv = register_nmi_handler(NMI_UNKNOWN, ipmi_nmi, 0, 1274 "ipmi"); 1275 if (rv) { 1276 pr_warn("Can't register nmi handler\n"); 1277 return; 1278 } else 1279 nmi_handler_registered = 1; 1280 } else if (!do_nmi && nmi_handler_registered) { 1281 unregister_nmi_handler(NMI_UNKNOWN, "ipmi"); 1282 nmi_handler_registered = 0; 1283 } 1284 #endif 1285 } 1286 1287 static int __init ipmi_wdog_init(void) 1288 { 1289 int rv; 1290 1291 if (action_op(action, NULL)) { 1292 action_op("reset", NULL); 1293 pr_info("Unknown action '%s', defaulting to reset\n", action); 1294 } 1295 1296 if (preaction_op(preaction, NULL)) { 1297 preaction_op("pre_none", NULL); 1298 pr_info("Unknown preaction '%s', defaulting to none\n", 1299 preaction); 1300 } 1301 1302 if (preop_op(preop, NULL)) { 1303 preop_op("preop_none", NULL); 1304 pr_info("Unknown preop '%s', defaulting to none\n", preop); 1305 } 1306 1307 check_parms(); 1308 1309 register_reboot_notifier(&wdog_reboot_notifier); 1310 1311 rv = ipmi_smi_watcher_register(&smi_watcher); 1312 if (rv) { 1313 #ifdef HAVE_DIE_NMI 1314 if (nmi_handler_registered) 1315 unregister_nmi_handler(NMI_UNKNOWN, "ipmi"); 1316 #endif 1317 unregister_reboot_notifier(&wdog_reboot_notifier); 1318 pr_warn("can't register smi watcher\n"); 1319 return rv; 1320 } 1321 1322 pr_info("driver initialized\n"); 1323 1324 return 0; 1325 } 1326 1327 static void __exit ipmi_wdog_exit(void) 1328 { 1329 ipmi_smi_watcher_unregister(&smi_watcher); 1330 ipmi_unregister_watchdog(watchdog_ifnum); 1331 1332 #ifdef HAVE_DIE_NMI 1333 if (nmi_handler_registered) 1334 unregister_nmi_handler(NMI_UNKNOWN, "ipmi"); 1335 #endif 1336 1337 unregister_reboot_notifier(&wdog_reboot_notifier); 1338 } 1339 module_exit(ipmi_wdog_exit); 1340 module_init(ipmi_wdog_init); 1341 MODULE_LICENSE("GPL"); 1342 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>"); 1343 MODULE_DESCRIPTION("watchdog timer based upon the IPMI interface."); 1344