#ifdef CONFIG_X86 /* * This is ugly, but I've determined that x86 is the only architecture * that can reasonably support the IPMI NMI watchdog timeout at this * time. If another architecture adds this capability somehow, it * will have to be a somewhat different mechanism and I have no idea * how it will work. So in the unlikely event that another * architecture supports this, we can figure out a good generic * mechanism for it at that time.
*/ #include <asm/kdebug.h> #include <asm/nmi.h> #define HAVE_DIE_NMI #endif
/* * The IPMI command/response information for the watchdog timer.
*/
/* values for byte 1 of the set command, byte 2 of the get response. */ #define WDOG_DONT_LOG (1 << 7) #define WDOG_DONT_STOP_ON_SET (1 << 6) #define WDOG_SET_TIMER_USE(byte, use) \
byte = ((byte) & 0xf8) | ((use) & 0x7) #define WDOG_GET_TIMER_USE(byte) ((byte) & 0x7) #define WDOG_TIMER_USE_BIOS_FRB2 1 #define WDOG_TIMER_USE_BIOS_POST 2 #define WDOG_TIMER_USE_OS_LOAD 3 #define WDOG_TIMER_USE_SMS_OS 4 #define WDOG_TIMER_USE_OEM 5
/* values for byte 2 of the set command, byte 3 of the get response. */ #define WDOG_SET_PRETIMEOUT_ACT(byte, use) \
byte = ((byte) & 0x8f) | (((use) & 0x7) << 4) #define WDOG_GET_PRETIMEOUT_ACT(byte) (((byte) >> 4) & 0x7) #define WDOG_PRETIMEOUT_NONE 0 #define WDOG_PRETIMEOUT_SMI 1 #define WDOG_PRETIMEOUT_NMI 2 #define WDOG_PRETIMEOUT_MSG_INT 3
/* Operations that can be performed on a pretimout. */ #define WDOG_PREOP_NONE 0 #define WDOG_PREOP_PANIC 1 /* Cause data to be available to read. Doesn't work in NMI mode. */ #define WDOG_PREOP_GIVE_DATA 2
/* Actions to perform on a full timeout. */ #define WDOG_SET_TIMEOUT_ACT(byte, use) \
byte = ((byte) & 0xf8) | ((use) & 0x7) #define WDOG_GET_TIMEOUT_ACT(byte) ((byte) & 0x7) #define WDOG_TIMEOUT_NONE 0 #define WDOG_TIMEOUT_RESET 1 #define WDOG_TIMEOUT_POWER_DOWN 2 #define WDOG_TIMEOUT_POWER_CYCLE 3
/* * Byte 3 of the get command, byte 4 of the get response is the * pre-timeout in seconds.
*/
/* Bits for setting byte 4 of the set command, byte 5 of the get response. */ #define WDOG_EXPIRE_CLEAR_BIOS_FRB2 (1 << 1) #define WDOG_EXPIRE_CLEAR_BIOS_POST (1 << 2) #define WDOG_EXPIRE_CLEAR_OS_LOAD (1 << 3) #define WDOG_EXPIRE_CLEAR_SMS_OS (1 << 4) #define WDOG_EXPIRE_CLEAR_OEM (1 << 5)
/* * Setting/getting the watchdog timer value. This is for bytes 5 and * 6 (the timeout time) of the set command, and bytes 6 and 7 (the * timeout time) and 8 and 9 (the current countdown value) of the * response. The timeout value is given in seconds (in the command it * is 100ms intervals).
*/ #define WDOG_SET_TIMEOUT(byte1, byte2, val) \
(byte1) = (((val) * 10) & 0xff), (byte2) = (((val) * 10) >> 8) #define WDOG_GET_TIMEOUT(byte1, byte2) \
(((byte1) | ((byte2) << 8)) / 10)
module_param(ifnum_to_use, wdog_ifnum, 0644);
MODULE_PARM_DESC(ifnum_to_use, "The interface number to use for the watchdog " "timer. Setting to -1 defaults to the first registered " "interface");
module_param(timeout, timeout, 0644);
MODULE_PARM_DESC(timeout, "Timeout value in seconds.");
module_param(pretimeout, timeout, 0644);
MODULE_PARM_DESC(pretimeout, "Pretimeout value in seconds.");
module_param(panic_wdt_timeout, timeout, 0644);
MODULE_PARM_DESC(panic_wdt_timeout, "Timeout value on kernel panic in seconds.");
module_param(start_now, int, 0444);
MODULE_PARM_DESC(start_now, "Set to 1 to start the watchdog as" "soon as the driver is loaded.");
module_param(nowayout, bool, 0644);
MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started " "(default=CONFIG_WATCHDOG_NOWAYOUT)");
/* Default state of the timer. */ staticunsignedchar ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
/* Is someone using the watchdog? Only one user is allowed. */ staticunsignedlong ipmi_wdog_open;
/* * If set to 1, the heartbeat command will set the state to reset and * start the timer. The timer doesn't normally run when the driver is * first opened until the heartbeat is set the first time, this * variable is used to accomplish this.
*/ staticint ipmi_start_timer_on_heartbeat;
/* IPMI version of the BMC. */ staticunsignedchar ipmi_version_major; staticunsignedchar ipmi_version_minor;
/* If a pretimeout occurs, this is used to allow only one panic to happen. */ static atomic_t preop_panic_excl = ATOMIC_INIT(-1);
/* * We use a mutex to make sure that only one thing can send a set a * message at one time. The mutex is claimed when a message is sent * and freed when both the send and receive messages are free.
*/ static atomic_t msg_tofree = ATOMIC_INIT(0); static DECLARE_COMPLETION(msg_wait); staticvoid msg_free_smi(struct ipmi_smi_msg *msg)
{ if (atomic_dec_and_test(&msg_tofree)) { if (!oops_in_progress)
complete(&msg_wait);
}
} staticvoid msg_free_recv(struct ipmi_recv_msg *msg)
{ if (atomic_dec_and_test(&msg_tofree)) { if (!oops_in_progress)
complete(&msg_wait);
}
} staticstruct ipmi_smi_msg smi_msg = INIT_IPMI_SMI_MSG(msg_free_smi); staticstruct ipmi_recv_msg recv_msg = INIT_IPMI_RECV_MSG(msg_free_recv);
staticint __ipmi_set_timeout(struct ipmi_smi_msg *smi_msg, struct ipmi_recv_msg *recv_msg, int *send_heartbeat_now)
{ struct kernel_ipmi_msg msg; unsignedchar data[6]; int rv = 0; struct ipmi_system_interface_addr addr; int hbnow = 0;
if (ipmi_watchdog_state != WDOG_TIMEOUT_NONE) { if ((ipmi_version_major > 1) ||
((ipmi_version_major == 1) && (ipmi_version_minor >= 5))) { /* This is an IPMI 1.5-only feature. */
data[0] |= WDOG_DONT_STOP_ON_SET;
} else { /* * In ipmi 1.0, setting the timer stops the watchdog, we * need to start it back up again.
*/
hbnow = 1;
}
}
/* * Special call, doesn't claim any locks. This is only to be called * at panic or halt time, in run-to-completion mode, when the caller * is the only CPU and the only thing that will be going is these IPMI * calls.
*/ staticvoid panic_halt_ipmi_set_timeout(void)
{ int send_heartbeat_now; int rv;
rv = __ipmi_set_timeout(NULL, NULL, &send_heartbeat_now); if (rv) {
pr_warn("Unable to extend the watchdog timeout\n");
} else { if (send_heartbeat_now)
panic_halt_ipmi_heartbeat();
}
}
staticint __ipmi_heartbeat(void)
{ struct kernel_ipmi_msg msg; int rv; struct ipmi_system_interface_addr addr; int timeout_retries = 0;
restart: /* * Don't reset the timer if we have the timer turned off, that * re-enables the watchdog.
*/ if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE) return 0;
/* Wait for the heartbeat to be sent. */
wait_for_completion(&msg_wait);
if (recv_msg.msg.data[0] == IPMI_WDOG_TIMER_NOT_INIT_RESP) {
timeout_retries++; if (timeout_retries > 3) {
pr_err("Unable to restore the IPMI watchdog's settings, giving up\n");
rv = -EIO; goto out;
}
/* * The timer was not initialized, that means the BMC was * probably reset and lost the watchdog information. Attempt * to restore the timer's info. Note that we still hold * the heartbeat lock, to keep a heartbeat from happening * in this process, so must say no heartbeat to avoid a * deadlock on this mutex
*/
rv = _ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB); if (rv) {
pr_err("Unable to send the command to set the watchdog's settings, giving up\n"); goto out;
}
/* Might need a heartbeat send, go ahead and do it. */ goto restart;
} elseif (recv_msg.msg.data[0] != 0) { /* * Got an error in the heartbeat response. It was already * reported in ipmi_wdog_msg_handler, but we should return * an error here.
*/
rv = -EINVAL;
}
out: return rv;
}
staticint _ipmi_heartbeat(void)
{ int rv;
if (!watchdog_user) return -ENODEV;
if (ipmi_start_timer_on_heartbeat) {
ipmi_start_timer_on_heartbeat = 0;
ipmi_watchdog_state = action_val;
rv = _ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
} elseif (atomic_cmpxchg(&pretimeout_since_last_heartbeat, 1, 0)) { /* * A pretimeout occurred, make sure we set the timeout. * We don't want to set the action, though, we want to * leave that alone (thus it can't be combined with the * above operation.
*/
rv = _ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
} else {
rv = __ipmi_heartbeat();
}
/* * Reading returns if the pretimeout has gone off, and it only does * it once per pretimeout.
*/
mutex_lock(&ipmi_read_mutex); if (!data_to_read) { if (file->f_flags & O_NONBLOCK) {
rv = -EAGAIN; goto out;
}
staticvoid ipmi_wdog_msg_handler(struct ipmi_recv_msg *msg, void *handler_data)
{ if (msg->msg.cmd == IPMI_WDOG_RESET_TIMER &&
msg->msg.data[0] == IPMI_WDOG_TIMER_NOT_INIT_RESP)
pr_info("response: The IPMI controller appears to have been reset, will attempt to reinitialize the watchdog timer\n"); elseif (msg->msg.data[0] != 0)
pr_err("response: Error %x on cmd %x\n",
msg->msg.data[0],
msg->msg.cmd);
/* * On some machines, the heartbeat will give an error and not * work unless we re-enable the timer. So do so.
*/
atomic_set(&pretimeout_since_last_heartbeat, 1);
}
/* * On a panic, if we have a panic timeout, make sure to extend * the watchdog timer to a reasonable value to complete the * panic, if the watchdog timer is running. Plus the * pretimeout is meaningless at panic time.
*/ if (watchdog_user && !panic_event_handled &&
ipmi_watchdog_state != WDOG_TIMEOUT_NONE) { /* Make sure we do this only once. */
panic_event_handled = 1;
staticvoid ipmi_register_watchdog(int ipmi_intf)
{ int rv = -EBUSY;
if (watchdog_user) goto out;
if ((ifnum_to_use >= 0) && (ifnum_to_use != ipmi_intf)) goto out;
watchdog_ifnum = ipmi_intf;
rv = ipmi_create_user(ipmi_intf, &ipmi_hndlrs, NULL, &watchdog_user); if (rv < 0) {
pr_crit("Unable to register with ipmi\n"); goto out;
}
rv = ipmi_get_version(watchdog_user,
&ipmi_version_major,
&ipmi_version_minor); if (rv) {
pr_warn("Unable to get IPMI version, assuming 1.0\n");
ipmi_version_major = 1;
ipmi_version_minor = 0;
}
rv = misc_register(&ipmi_wdog_miscdev); if (rv < 0) {
ipmi_destroy_user(watchdog_user);
watchdog_user = NULL;
pr_crit("Unable to register misc device\n");
}
#ifdef HAVE_DIE_NMI if (nmi_handler_registered) { int old_pretimeout = pretimeout; int old_timeout = timeout; int old_preop_val = preop_val;
/* * Set the pretimeout to go off in a second and give * ourselves plenty of time to stop the timer.
*/
ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
preop_val = WDOG_PREOP_NONE; /* Make sure nothing happens */
pretimeout = 99;
timeout = 100;
testing_nmi = 1;
rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB); if (rv) {
pr_warn("Error starting timer to test NMI: 0x%x. The NMI pretimeout will likely not work\n",
rv);
rv = 0; goto out_restore;
}
msleep(1500);
if (testing_nmi != 2) {
pr_warn("IPMI NMI didn't seem to occur. The NMI pretimeout will likely not work\n");
}
out_restore:
testing_nmi = 0;
preop_val = old_preop_val;
pretimeout = old_pretimeout;
timeout = old_timeout;
} #endif
out: if ((start_now) && (rv == 0)) { /* Run from startup, so start the timer now. */
start_now = 0; /* Disable this function after first startup. */
ipmi_watchdog_state = action_val;
ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
pr_info("Starting now!\n");
} else { /* Stop the timer now. */
ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
}
}
/* Make sure no one can call us any more. */
misc_deregister(&ipmi_wdog_miscdev);
watchdog_user = NULL;
/* * Wait to make sure the message makes it out. The lower layer has * pointers to our buffers, we want to make sure they are done before * we release our memory.
*/ while (atomic_read(&msg_tofree))
msg_free_smi(NULL);
mutex_lock(&ipmi_watchdog_mutex);
/* Disconnect from IPMI. */
ipmi_destroy_user(loc_user);
/* If it comes back, restart it properly. */
ipmi_start_timer_on_heartbeat = 1;
mutex_unlock(&ipmi_watchdog_mutex);
}
#ifdef HAVE_DIE_NMI staticint
ipmi_nmi(unsignedint val, struct pt_regs *regs)
{ /* * If we get here, it's an NMI that's not a memory or I/O * error. We can't truly tell if it's from IPMI or not * without sending a message, and sending a message is almost * impossible because of locking.
*/
if (testing_nmi) {
testing_nmi = 2; return NMI_HANDLED;
}
/* If we are not expecting a timeout, ignore it. */ if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE) return NMI_DONE;
if (preaction_val != WDOG_PRETIMEOUT_NMI) return NMI_DONE;
/* * If no one else handled the NMI, we assume it was the IPMI * watchdog.
*/ if (preop_val == WDOG_PREOP_PANIC) { /* On some machines, the heartbeat will give an error and not work unless we re-enable
the timer. So do so. */
atomic_set(&pretimeout_since_last_heartbeat, 1); if (atomic_inc_and_test(&preop_panic_excl))
nmi_panic(regs, "pre-timeout");
}
if ((watchdog_user) && (!reboot_event_handled)) { /* Make sure we only do this once. */
reboot_event_handled = 1;
if (code == SYS_POWER_OFF || code == SYS_HALT) { /* Disable the WDT if we are shutting down. */
ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
} elseif (ipmi_watchdog_state != WDOG_TIMEOUT_NONE) { /* Set a long timer to let the reboot happen or reset if it hangs, but only if the watchdog
timer was already running. */ if (timeout < 120)
timeout = 120;
pretimeout = 0;
ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
}
} return NOTIFY_OK;
}
#ifdef HAVE_DIE_NMI if (nmi_handler_registered)
unregister_nmi_handler(NMI_UNKNOWN, "ipmi"); #endif
unregister_reboot_notifier(&wdog_reboot_notifier);
}
module_exit(ipmi_wdog_exit);
module_init(ipmi_wdog_init);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Corey Minyard ");
MODULE_DESCRIPTION("watchdog timer based upon the IPMI interface.");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.