2018-02-20 21:30:22 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* ipmi_watchdog.c
|
|
|
|
*
|
|
|
|
* A watchdog timer based upon the IPMI interface.
|
|
|
|
*
|
|
|
|
* Author: MontaVista Software, Inc.
|
|
|
|
* Corey Minyard <minyard@mvista.com>
|
|
|
|
* source@mvista.com
|
|
|
|
*
|
|
|
|
* Copyright 2002 MontaVista Software Inc.
|
|
|
|
*/
|
|
|
|
|
2018-05-09 23:15:48 +08:00
|
|
|
#define pr_fmt(fmt) "IPMI Watchdog: " fmt
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/moduleparam.h>
|
|
|
|
#include <linux/ipmi.h>
|
|
|
|
#include <linux/ipmi_smi.h>
|
2010-06-02 20:28:52 +08:00
|
|
|
#include <linux/mutex.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/watchdog.h>
|
|
|
|
#include <linux/miscdevice.h>
|
|
|
|
#include <linux/init.h>
|
2006-03-31 18:30:41 +08:00
|
|
|
#include <linux/completion.h>
|
2007-05-08 15:27:03 +08:00
|
|
|
#include <linux/kdebug.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/rwsem.h>
|
|
|
|
#include <linux/errno.h>
|
2016-12-25 03:46:01 +08:00
|
|
|
#include <linux/uaccess.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/notifier.h>
|
|
|
|
#include <linux/nmi.h>
|
|
|
|
#include <linux/reboot.h>
|
|
|
|
#include <linux/wait.h>
|
|
|
|
#include <linux/poll.h>
|
2005-11-07 16:59:57 +08:00
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/ctype.h>
|
2007-10-18 18:07:10 +08:00
|
|
|
#include <linux/delay.h>
|
2011-07-27 07:09:06 +08:00
|
|
|
#include <linux/atomic.h>
|
2017-02-03 02:15:33 +08:00
|
|
|
#include <linux/sched/signal.h>
|
2007-05-08 15:23:58 +08:00
|
|
|
|
2007-10-18 18:07:10 +08:00
|
|
|
#ifdef CONFIG_X86
|
2008-04-29 16:01:12 +08:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2007-10-18 18:07:10 +08:00
|
|
|
#include <asm/kdebug.h>
|
2011-10-01 03:06:21 +08:00
|
|
|
#include <asm/nmi.h>
|
2007-10-18 18:07:10 +08:00
|
|
|
#define HAVE_DIE_NMI
|
2005-04-17 06:20:36 +08:00
|
|
|
#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
|
2008-04-29 16:01:12 +08:00
|
|
|
/* Cause data to be available to read. Doesn't work in NMI mode. */
|
|
|
|
#define WDOG_PREOP_GIVE_DATA 2
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
2008-04-29 16:01:12 +08:00
|
|
|
/*
|
|
|
|
* Byte 3 of the get command, byte 4 of the get response is the
|
|
|
|
* pre-timeout in seconds.
|
|
|
|
*/
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* 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)
|
|
|
|
|
2008-04-29 16:01:12 +08:00
|
|
|
/*
|
|
|
|
* 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).
|
|
|
|
*/
|
2005-04-17 06:20:36 +08:00
|
|
|
#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)
|
|
|
|
|
|
|
|
#define IPMI_WDOG_RESET_TIMER 0x22
|
|
|
|
#define IPMI_WDOG_SET_TIMER 0x24
|
|
|
|
#define IPMI_WDOG_GET_TIMER 0x25
|
|
|
|
|
2011-12-20 09:12:02 +08:00
|
|
|
#define IPMI_WDOG_TIMER_NOT_INIT_RESP 0x80
|
|
|
|
|
2010-06-02 20:28:52 +08:00
|
|
|
static DEFINE_MUTEX(ipmi_watchdog_mutex);
|
2012-03-05 23:51:11 +08:00
|
|
|
static bool nowayout = WATCHDOG_NOWAYOUT;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2018-04-06 09:50:48 +08:00
|
|
|
static struct ipmi_user *watchdog_user;
|
2006-12-07 12:41:00 +08:00
|
|
|
static int watchdog_ifnum;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* Default the timeout to 10 seconds. */
|
|
|
|
static int timeout = 10;
|
|
|
|
|
|
|
|
/* The pre-timeout is disabled by default. */
|
2006-12-10 18:19:06 +08:00
|
|
|
static int pretimeout;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2015-09-29 17:39:19 +08:00
|
|
|
/* Default timeout to set on panic */
|
|
|
|
static int panic_wdt_timeout = 255;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Default action is to reset the board on a timeout. */
|
|
|
|
static unsigned char action_val = WDOG_TIMEOUT_RESET;
|
|
|
|
|
|
|
|
static char action[16] = "reset";
|
|
|
|
|
|
|
|
static unsigned char preaction_val = WDOG_PRETIMEOUT_NONE;
|
|
|
|
|
|
|
|
static char preaction[16] = "pre_none";
|
|
|
|
|
|
|
|
static unsigned char preop_val = WDOG_PREOP_NONE;
|
|
|
|
|
|
|
|
static char preop[16] = "preop_none";
|
|
|
|
static DEFINE_SPINLOCK(ipmi_read_lock);
|
2006-12-10 18:19:06 +08:00
|
|
|
static char data_to_read;
|
2005-04-17 06:20:36 +08:00
|
|
|
static DECLARE_WAIT_QUEUE_HEAD(read_q);
|
2006-12-10 18:19:06 +08:00
|
|
|
static struct fasync_struct *fasync_q;
|
2018-03-29 00:35:47 +08:00
|
|
|
static atomic_t pretimeout_since_last_heartbeat;
|
2005-04-17 06:20:36 +08:00
|
|
|
static char expect_close;
|
|
|
|
|
2006-12-07 12:41:00 +08:00
|
|
|
static int ifnum_to_use = -1;
|
|
|
|
|
2005-11-07 16:59:57 +08:00
|
|
|
/* Parameters to ipmi_set_timeout */
|
|
|
|
#define IPMI_SET_TIMEOUT_NO_HB 0
|
|
|
|
#define IPMI_SET_TIMEOUT_HB_IF_NECESSARY 1
|
|
|
|
#define IPMI_SET_TIMEOUT_FORCE_HB 2
|
|
|
|
|
|
|
|
static int ipmi_set_timeout(int do_heartbeat);
|
2006-12-07 12:41:00 +08:00
|
|
|
static void ipmi_register_watchdog(int ipmi_intf);
|
|
|
|
static void ipmi_unregister_watchdog(int ipmi_intf);
|
2005-11-07 16:59:57 +08:00
|
|
|
|
2008-04-29 16:01:12 +08:00
|
|
|
/*
|
|
|
|
* If true, the driver will start running as soon as it is configured
|
|
|
|
* and ready.
|
|
|
|
*/
|
2006-12-10 18:19:06 +08:00
|
|
|
static int start_now;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-08-12 13:04:37 +08:00
|
|
|
static int set_param_timeout(const char *val, const struct kernel_param *kp)
|
2005-11-07 16:59:57 +08:00
|
|
|
{
|
|
|
|
char *endp;
|
|
|
|
int l;
|
|
|
|
int rv = 0;
|
|
|
|
|
|
|
|
if (!val)
|
|
|
|
return -EINVAL;
|
|
|
|
l = simple_strtoul(val, &endp, 0);
|
|
|
|
if (endp == val)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
*((int *)kp->arg) = l;
|
|
|
|
if (watchdog_user)
|
|
|
|
rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-05-27 09:39:38 +08:00
|
|
|
static const struct kernel_param_ops param_ops_timeout = {
|
2010-08-12 13:04:37 +08:00
|
|
|
.set = set_param_timeout,
|
|
|
|
.get = param_get_int,
|
|
|
|
};
|
|
|
|
#define param_check_timeout param_check_int
|
2005-11-07 16:59:57 +08:00
|
|
|
|
|
|
|
typedef int (*action_fn)(const char *intval, char *outval);
|
|
|
|
|
|
|
|
static int action_op(const char *inval, char *outval);
|
|
|
|
static int preaction_op(const char *inval, char *outval);
|
|
|
|
static int preop_op(const char *inval, char *outval);
|
|
|
|
static void check_parms(void);
|
|
|
|
|
2010-08-12 13:04:37 +08:00
|
|
|
static int set_param_str(const char *val, const struct kernel_param *kp)
|
2005-11-07 16:59:57 +08:00
|
|
|
{
|
|
|
|
action_fn fn = (action_fn) kp->arg;
|
|
|
|
int rv = 0;
|
2006-12-30 08:46:53 +08:00
|
|
|
char valcp[16];
|
|
|
|
char *s;
|
2006-06-23 17:05:45 +08:00
|
|
|
|
2018-01-08 20:01:32 +08:00
|
|
|
strncpy(valcp, val, 15);
|
2006-12-30 08:46:53 +08:00
|
|
|
valcp[15] = '\0';
|
2006-06-23 17:05:45 +08:00
|
|
|
|
2006-12-30 08:46:53 +08:00
|
|
|
s = strstrip(valcp);
|
2005-11-07 16:59:57 +08:00
|
|
|
|
2006-06-23 17:05:45 +08:00
|
|
|
rv = fn(s, NULL);
|
2005-11-07 16:59:57 +08:00
|
|
|
if (rv)
|
2007-10-18 18:07:08 +08:00
|
|
|
goto out;
|
2005-11-07 16:59:57 +08:00
|
|
|
|
|
|
|
check_parms();
|
|
|
|
if (watchdog_user)
|
|
|
|
rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
|
|
|
|
|
2007-10-18 18:07:08 +08:00
|
|
|
out:
|
2005-11-07 16:59:57 +08:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2010-08-12 13:04:37 +08:00
|
|
|
static int get_param_str(char *buffer, const struct kernel_param *kp)
|
2005-11-07 16:59:57 +08:00
|
|
|
{
|
|
|
|
action_fn fn = (action_fn) kp->arg;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
rv = fn(NULL, buffer);
|
|
|
|
if (rv)
|
|
|
|
return rv;
|
|
|
|
return strlen(buffer);
|
|
|
|
}
|
|
|
|
|
2006-12-07 12:41:00 +08:00
|
|
|
|
2010-08-12 13:04:37 +08:00
|
|
|
static int set_param_wdog_ifnum(const char *val, const struct kernel_param *kp)
|
2006-12-07 12:41:00 +08:00
|
|
|
{
|
|
|
|
int rv = param_set_int(val, kp);
|
|
|
|
if (rv)
|
|
|
|
return rv;
|
|
|
|
if ((ifnum_to_use < 0) || (ifnum_to_use == watchdog_ifnum))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ipmi_unregister_watchdog(watchdog_ifnum);
|
|
|
|
ipmi_register_watchdog(ifnum_to_use);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-27 09:39:38 +08:00
|
|
|
static const struct kernel_param_ops param_ops_wdog_ifnum = {
|
2010-08-12 13:04:37 +08:00
|
|
|
.set = set_param_wdog_ifnum,
|
|
|
|
.get = param_get_int,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define param_check_wdog_ifnum param_check_int
|
|
|
|
|
2015-05-27 09:39:38 +08:00
|
|
|
static const struct kernel_param_ops param_ops_str = {
|
2010-08-12 13:04:37 +08:00
|
|
|
.set = set_param_str,
|
|
|
|
.get = get_param_str,
|
|
|
|
};
|
|
|
|
|
|
|
|
module_param(ifnum_to_use, wdog_ifnum, 0644);
|
2006-12-07 12:41:00 +08:00
|
|
|
MODULE_PARM_DESC(ifnum_to_use, "The interface number to use for the watchdog "
|
|
|
|
"timer. Setting to -1 defaults to the first registered "
|
|
|
|
"interface");
|
|
|
|
|
2010-08-12 13:04:37 +08:00
|
|
|
module_param(timeout, timeout, 0644);
|
2005-04-17 06:20:36 +08:00
|
|
|
MODULE_PARM_DESC(timeout, "Timeout value in seconds.");
|
2005-11-07 16:59:57 +08:00
|
|
|
|
2010-08-12 13:04:37 +08:00
|
|
|
module_param(pretimeout, timeout, 0644);
|
2005-04-17 06:20:36 +08:00
|
|
|
MODULE_PARM_DESC(pretimeout, "Pretimeout value in seconds.");
|
2005-11-07 16:59:57 +08:00
|
|
|
|
2015-09-29 17:39:19 +08:00
|
|
|
module_param(panic_wdt_timeout, timeout, 0644);
|
2017-11-03 17:52:28 +08:00
|
|
|
MODULE_PARM_DESC(panic_wdt_timeout, "Timeout value on kernel panic in seconds.");
|
2015-09-29 17:39:19 +08:00
|
|
|
|
2010-08-12 13:04:37 +08:00
|
|
|
module_param_cb(action, ¶m_ops_str, action_op, 0644);
|
2005-04-17 06:20:36 +08:00
|
|
|
MODULE_PARM_DESC(action, "Timeout action. One of: "
|
|
|
|
"reset, none, power_cycle, power_off.");
|
2005-11-07 16:59:57 +08:00
|
|
|
|
2010-08-12 13:04:37 +08:00
|
|
|
module_param_cb(preaction, ¶m_ops_str, preaction_op, 0644);
|
2005-04-17 06:20:36 +08:00
|
|
|
MODULE_PARM_DESC(preaction, "Pretimeout action. One of: "
|
|
|
|
"pre_none, pre_smi, pre_nmi, pre_int.");
|
2005-11-07 16:59:57 +08:00
|
|
|
|
2010-08-12 13:04:37 +08:00
|
|
|
module_param_cb(preop, ¶m_ops_str, preop_op, 0644);
|
2005-04-17 06:20:36 +08:00
|
|
|
MODULE_PARM_DESC(preop, "Pretimeout driver operation. One of: "
|
|
|
|
"preop_none, preop_panic, preop_give_data.");
|
2005-11-07 16:59:57 +08:00
|
|
|
|
2006-12-07 12:41:00 +08:00
|
|
|
module_param(start_now, int, 0444);
|
2005-04-17 06:20:36 +08:00
|
|
|
MODULE_PARM_DESC(start_now, "Set to 1 to start the watchdog as"
|
|
|
|
"soon as the driver is loaded.");
|
2005-11-07 16:59:57 +08:00
|
|
|
|
2012-03-05 23:51:11 +08:00
|
|
|
module_param(nowayout, bool, 0644);
|
2006-12-07 12:41:00 +08:00
|
|
|
MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
|
|
|
|
"(default=CONFIG_WATCHDOG_NOWAYOUT)");
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* Default state of the timer. */
|
|
|
|
static unsigned char ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
|
|
|
|
|
|
|
|
/* Is someone using the watchdog? Only one user is allowed. */
|
2006-12-10 18:19:06 +08:00
|
|
|
static unsigned long ipmi_wdog_open;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-04-29 16:01:12 +08:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2006-12-10 18:19:06 +08:00
|
|
|
static int ipmi_start_timer_on_heartbeat;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* IPMI version of the BMC. */
|
|
|
|
static unsigned char ipmi_version_major;
|
|
|
|
static unsigned char ipmi_version_minor;
|
|
|
|
|
2005-11-07 17:00:05 +08:00
|
|
|
/* If a pretimeout occurs, this is used to allow only one panic to happen. */
|
|
|
|
static atomic_t preop_panic_excl = ATOMIC_INIT(-1);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-10-18 18:07:10 +08:00
|
|
|
#ifdef HAVE_DIE_NMI
|
|
|
|
static int testing_nmi;
|
|
|
|
static int nmi_handler_registered;
|
|
|
|
#endif
|
|
|
|
|
2018-03-29 00:35:47 +08:00
|
|
|
static int __ipmi_heartbeat(void);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-04-29 16:01:12 +08:00
|
|
|
/*
|
2018-03-29 00:35:47 +08:00
|
|
|
* 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.
|
2008-04-29 16:01:12 +08:00
|
|
|
*/
|
2018-03-29 00:35:47 +08:00
|
|
|
static atomic_t msg_tofree = ATOMIC_INIT(0);
|
|
|
|
static DECLARE_COMPLETION(msg_wait);
|
|
|
|
static void msg_free_smi(struct ipmi_smi_msg *msg)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2024-06-12 13:13:20 +08:00
|
|
|
if (atomic_dec_and_test(&msg_tofree)) {
|
|
|
|
if (!oops_in_progress)
|
|
|
|
complete(&msg_wait);
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2018-03-29 00:35:47 +08:00
|
|
|
static void msg_free_recv(struct ipmi_recv_msg *msg)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2024-06-12 13:13:20 +08:00
|
|
|
if (atomic_dec_and_test(&msg_tofree)) {
|
|
|
|
if (!oops_in_progress)
|
|
|
|
complete(&msg_wait);
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2018-03-29 00:35:47 +08:00
|
|
|
static struct ipmi_smi_msg smi_msg = {
|
|
|
|
.done = msg_free_smi
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
2018-03-29 00:35:47 +08:00
|
|
|
static struct ipmi_recv_msg recv_msg = {
|
|
|
|
.done = msg_free_recv
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
2008-04-29 16:01:12 +08:00
|
|
|
|
2018-03-29 00:35:47 +08:00
|
|
|
static int __ipmi_set_timeout(struct ipmi_smi_msg *smi_msg,
|
2005-04-17 06:20:36 +08:00
|
|
|
struct ipmi_recv_msg *recv_msg,
|
|
|
|
int *send_heartbeat_now)
|
|
|
|
{
|
|
|
|
struct kernel_ipmi_msg msg;
|
|
|
|
unsigned char data[6];
|
|
|
|
int rv;
|
|
|
|
struct ipmi_system_interface_addr addr;
|
|
|
|
int hbnow = 0;
|
|
|
|
|
|
|
|
|
|
|
|
data[0] = 0;
|
|
|
|
WDOG_SET_TIMER_USE(data[0], WDOG_TIMER_USE_SMS_OS);
|
|
|
|
|
2024-06-12 13:13:20 +08:00
|
|
|
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;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
data[1] = 0;
|
|
|
|
WDOG_SET_TIMEOUT_ACT(data[1], ipmi_watchdog_state);
|
2005-09-07 06:18:39 +08:00
|
|
|
if ((pretimeout > 0) && (ipmi_watchdog_state != WDOG_TIMEOUT_NONE)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
WDOG_SET_PRETIMEOUT_ACT(data[1], preaction_val);
|
|
|
|
data[2] = pretimeout;
|
|
|
|
} else {
|
|
|
|
WDOG_SET_PRETIMEOUT_ACT(data[1], WDOG_PRETIMEOUT_NONE);
|
|
|
|
data[2] = 0; /* No pretimeout. */
|
|
|
|
}
|
|
|
|
data[3] = 0;
|
|
|
|
WDOG_SET_TIMEOUT(data[4], data[5], timeout);
|
|
|
|
|
|
|
|
addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
|
|
|
|
addr.channel = IPMI_BMC_CHANNEL;
|
|
|
|
addr.lun = 0;
|
|
|
|
|
|
|
|
msg.netfn = 0x06;
|
|
|
|
msg.cmd = IPMI_WDOG_SET_TIMER;
|
|
|
|
msg.data = data;
|
|
|
|
msg.data_len = sizeof(data);
|
|
|
|
rv = ipmi_request_supply_msgs(watchdog_user,
|
|
|
|
(struct ipmi_addr *) &addr,
|
|
|
|
0,
|
|
|
|
&msg,
|
|
|
|
NULL,
|
|
|
|
smi_msg,
|
|
|
|
recv_msg,
|
|
|
|
1);
|
2018-03-29 00:35:47 +08:00
|
|
|
if (rv)
|
2018-05-09 23:15:48 +08:00
|
|
|
pr_warn("set timeout error: %d\n", rv);
|
2018-03-29 00:35:47 +08:00
|
|
|
else if (send_heartbeat_now)
|
|
|
|
*send_heartbeat_now = hbnow;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2018-03-29 00:35:47 +08:00
|
|
|
static int _ipmi_set_timeout(int do_heartbeat)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int send_heartbeat_now;
|
|
|
|
int rv;
|
|
|
|
|
2018-03-29 00:35:47 +08:00
|
|
|
if (!watchdog_user)
|
|
|
|
return -ENODEV;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2018-03-29 00:35:47 +08:00
|
|
|
atomic_set(&msg_tofree, 2);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2018-03-29 00:35:47 +08:00
|
|
|
rv = __ipmi_set_timeout(&smi_msg,
|
|
|
|
&recv_msg,
|
2005-04-17 06:20:36 +08:00
|
|
|
&send_heartbeat_now);
|
2024-06-12 13:13:20 +08:00
|
|
|
if (rv) {
|
|
|
|
atomic_set(&msg_tofree, 0);
|
2018-03-29 00:35:47 +08:00
|
|
|
return rv;
|
2024-06-12 13:13:20 +08:00
|
|
|
}
|
2006-03-31 18:30:41 +08:00
|
|
|
|
2018-03-29 00:35:47 +08:00
|
|
|
wait_for_completion(&msg_wait);
|
2007-10-18 18:07:10 +08:00
|
|
|
|
2006-03-31 18:30:41 +08:00
|
|
|
if ((do_heartbeat == IPMI_SET_TIMEOUT_FORCE_HB)
|
2018-03-29 00:35:47 +08:00
|
|
|
|| ((send_heartbeat_now)
|
|
|
|
&& (do_heartbeat == IPMI_SET_TIMEOUT_HB_IF_NECESSARY)))
|
|
|
|
rv = __ipmi_heartbeat();
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ipmi_set_timeout(int do_heartbeat)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
mutex_lock(&ipmi_watchdog_mutex);
|
|
|
|
rv = _ipmi_set_timeout(do_heartbeat);
|
|
|
|
mutex_unlock(&ipmi_watchdog_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2007-10-18 18:07:09 +08:00
|
|
|
static atomic_t panic_done_count = ATOMIC_INIT(0);
|
|
|
|
|
|
|
|
static void panic_smi_free(struct ipmi_smi_msg *msg)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2007-10-18 18:07:09 +08:00
|
|
|
atomic_dec(&panic_done_count);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-10-18 18:07:09 +08:00
|
|
|
static void panic_recv_free(struct ipmi_recv_msg *msg)
|
|
|
|
{
|
|
|
|
atomic_dec(&panic_done_count);
|
|
|
|
}
|
|
|
|
|
2008-04-29 16:01:12 +08:00
|
|
|
static struct ipmi_smi_msg panic_halt_heartbeat_smi_msg = {
|
2007-10-18 18:07:09 +08:00
|
|
|
.done = panic_smi_free
|
|
|
|
};
|
2008-04-29 16:01:12 +08:00
|
|
|
static struct ipmi_recv_msg panic_halt_heartbeat_recv_msg = {
|
2007-10-18 18:07:09 +08:00
|
|
|
.done = panic_recv_free
|
|
|
|
};
|
|
|
|
|
|
|
|
static void panic_halt_ipmi_heartbeat(void)
|
|
|
|
{
|
|
|
|
struct kernel_ipmi_msg msg;
|
|
|
|
struct ipmi_system_interface_addr addr;
|
|
|
|
int rv;
|
|
|
|
|
2008-04-29 16:01:12 +08:00
|
|
|
/*
|
|
|
|
* Don't reset the timer if we have the timer turned off, that
|
|
|
|
* re-enables the watchdog.
|
|
|
|
*/
|
2007-10-18 18:07:09 +08:00
|
|
|
if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
|
|
|
|
addr.channel = IPMI_BMC_CHANNEL;
|
|
|
|
addr.lun = 0;
|
|
|
|
|
|
|
|
msg.netfn = 0x06;
|
|
|
|
msg.cmd = IPMI_WDOG_RESET_TIMER;
|
|
|
|
msg.data = NULL;
|
|
|
|
msg.data_len = 0;
|
2024-06-12 13:13:20 +08:00
|
|
|
atomic_add(2, &panic_done_count);
|
2007-10-18 18:07:09 +08:00
|
|
|
rv = ipmi_request_supply_msgs(watchdog_user,
|
|
|
|
(struct ipmi_addr *) &addr,
|
|
|
|
0,
|
|
|
|
&msg,
|
|
|
|
NULL,
|
|
|
|
&panic_halt_heartbeat_smi_msg,
|
|
|
|
&panic_halt_heartbeat_recv_msg,
|
|
|
|
1);
|
2012-03-29 05:42:49 +08:00
|
|
|
if (rv)
|
2024-06-12 13:13:20 +08:00
|
|
|
atomic_sub(2, &panic_done_count);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-10-18 18:07:09 +08:00
|
|
|
|
2008-04-29 16:01:12 +08:00
|
|
|
static struct ipmi_smi_msg panic_halt_smi_msg = {
|
2007-10-18 18:07:09 +08:00
|
|
|
.done = panic_smi_free
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
2008-04-29 16:01:12 +08:00
|
|
|
static struct ipmi_recv_msg panic_halt_recv_msg = {
|
2007-10-18 18:07:09 +08:00
|
|
|
.done = panic_recv_free
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2008-04-29 16:01:12 +08:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2005-04-17 06:20:36 +08:00
|
|
|
static void panic_halt_ipmi_set_timeout(void)
|
|
|
|
{
|
|
|
|
int send_heartbeat_now;
|
|
|
|
int rv;
|
|
|
|
|
2007-10-18 18:07:09 +08:00
|
|
|
/* Wait for the messages to be free. */
|
|
|
|
while (atomic_read(&panic_done_count) != 0)
|
|
|
|
ipmi_poll_interface(watchdog_user);
|
2024-06-12 13:13:20 +08:00
|
|
|
atomic_add(2, &panic_done_count);
|
2018-03-29 00:35:47 +08:00
|
|
|
rv = __ipmi_set_timeout(&panic_halt_smi_msg,
|
2005-04-17 06:20:36 +08:00
|
|
|
&panic_halt_recv_msg,
|
|
|
|
&send_heartbeat_now);
|
2012-03-29 05:42:49 +08:00
|
|
|
if (rv) {
|
2024-06-12 13:13:20 +08:00
|
|
|
atomic_sub(2, &panic_done_count);
|
2018-05-09 23:15:48 +08:00
|
|
|
pr_warn("Unable to extend the watchdog timeout\n");
|
2012-03-29 05:42:49 +08:00
|
|
|
} else {
|
|
|
|
if (send_heartbeat_now)
|
|
|
|
panic_halt_ipmi_heartbeat();
|
|
|
|
}
|
2007-10-18 18:07:09 +08:00
|
|
|
while (atomic_read(&panic_done_count) != 0)
|
|
|
|
ipmi_poll_interface(watchdog_user);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2018-03-29 00:35:47 +08:00
|
|
|
static int __ipmi_heartbeat(void)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2018-03-29 00:35:47 +08:00
|
|
|
struct kernel_ipmi_msg msg;
|
|
|
|
int rv;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct ipmi_system_interface_addr addr;
|
2018-03-29 00:35:47 +08:00
|
|
|
int timeout_retries = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-12-20 09:12:02 +08:00
|
|
|
restart:
|
2008-04-29 16:01:12 +08:00
|
|
|
/*
|
|
|
|
* Don't reset the timer if we have the timer turned off, that
|
|
|
|
* re-enables the watchdog.
|
|
|
|
*/
|
2018-03-29 00:35:47 +08:00
|
|
|
if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
2018-03-29 00:35:47 +08:00
|
|
|
|
|
|
|
atomic_set(&msg_tofree, 2);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
|
|
|
|
addr.channel = IPMI_BMC_CHANNEL;
|
|
|
|
addr.lun = 0;
|
|
|
|
|
|
|
|
msg.netfn = 0x06;
|
|
|
|
msg.cmd = IPMI_WDOG_RESET_TIMER;
|
|
|
|
msg.data = NULL;
|
|
|
|
msg.data_len = 0;
|
|
|
|
rv = ipmi_request_supply_msgs(watchdog_user,
|
|
|
|
(struct ipmi_addr *) &addr,
|
|
|
|
0,
|
|
|
|
&msg,
|
|
|
|
NULL,
|
2018-03-29 00:35:47 +08:00
|
|
|
&smi_msg,
|
|
|
|
&recv_msg,
|
2005-04-17 06:20:36 +08:00
|
|
|
1);
|
|
|
|
if (rv) {
|
2024-06-12 13:13:20 +08:00
|
|
|
atomic_set(&msg_tofree, 0);
|
2018-05-09 23:15:48 +08:00
|
|
|
pr_warn("heartbeat send failure: %d\n", rv);
|
2005-04-17 06:20:36 +08:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for the heartbeat to be sent. */
|
2018-03-29 00:35:47 +08:00
|
|
|
wait_for_completion(&msg_wait);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2018-03-29 00:35:47 +08:00
|
|
|
if (recv_msg.msg.data[0] == IPMI_WDOG_TIMER_NOT_INIT_RESP) {
|
2011-12-20 09:12:02 +08:00
|
|
|
timeout_retries++;
|
|
|
|
if (timeout_retries > 3) {
|
2018-05-09 23:15:48 +08:00
|
|
|
pr_err("Unable to restore the IPMI watchdog's settings, giving up\n");
|
2011-12-20 09:12:02 +08:00
|
|
|
rv = -EIO;
|
2018-03-29 00:35:47 +08:00
|
|
|
goto out;
|
2011-12-20 09:12:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2018-03-29 00:35:47 +08:00
|
|
|
* deadlock on this mutex
|
2011-12-20 09:12:02 +08:00
|
|
|
*/
|
2018-03-29 00:35:47 +08:00
|
|
|
rv = _ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
|
2011-12-20 09:12:02 +08:00
|
|
|
if (rv) {
|
2018-05-09 23:15:48 +08:00
|
|
|
pr_err("Unable to send the command to set the watchdog's settings, giving up\n");
|
2018-03-29 00:35:47 +08:00
|
|
|
goto out;
|
2011-12-20 09:12:02 +08:00
|
|
|
}
|
|
|
|
|
2018-03-29 00:35:47 +08:00
|
|
|
/* Might need a heartbeat send, go ahead and do it. */
|
2011-12-20 09:12:02 +08:00
|
|
|
goto restart;
|
2018-03-29 00:35:47 +08:00
|
|
|
} else if (recv_msg.msg.data[0] != 0) {
|
2008-04-29 16:01:12 +08:00
|
|
|
/*
|
|
|
|
* 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;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2018-03-29 00:35:47 +08:00
|
|
|
out:
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _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);
|
|
|
|
} else if (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();
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ipmi_heartbeat(void)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
mutex_lock(&ipmi_watchdog_mutex);
|
|
|
|
rv = _ipmi_heartbeat();
|
|
|
|
mutex_unlock(&ipmi_watchdog_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2008-04-29 16:01:12 +08:00
|
|
|
static struct watchdog_info ident = {
|
2005-04-17 06:20:36 +08:00
|
|
|
.options = 0, /* WDIOF_SETTIMEOUT, */
|
|
|
|
.firmware_version = 1,
|
|
|
|
.identity = "IPMI"
|
|
|
|
};
|
|
|
|
|
2010-04-27 06:24:05 +08:00
|
|
|
static int ipmi_ioctl(struct file *file,
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
void __user *argp = (void __user *)arg;
|
|
|
|
int i;
|
|
|
|
int val;
|
|
|
|
|
2008-04-29 16:01:12 +08:00
|
|
|
switch (cmd) {
|
2005-04-17 06:20:36 +08:00
|
|
|
case WDIOC_GETSUPPORT:
|
|
|
|
i = copy_to_user(argp, &ident, sizeof(ident));
|
|
|
|
return i ? -EFAULT : 0;
|
|
|
|
|
|
|
|
case WDIOC_SETTIMEOUT:
|
|
|
|
i = copy_from_user(&val, argp, sizeof(int));
|
|
|
|
if (i)
|
|
|
|
return -EFAULT;
|
|
|
|
timeout = val;
|
2018-03-29 00:35:47 +08:00
|
|
|
return _ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
case WDIOC_GETTIMEOUT:
|
|
|
|
i = copy_to_user(argp, &timeout, sizeof(timeout));
|
|
|
|
if (i)
|
|
|
|
return -EFAULT;
|
|
|
|
return 0;
|
|
|
|
|
2007-11-21 04:14:46 +08:00
|
|
|
case WDIOC_SETPRETIMEOUT:
|
2005-04-17 06:20:36 +08:00
|
|
|
i = copy_from_user(&val, argp, sizeof(int));
|
|
|
|
if (i)
|
|
|
|
return -EFAULT;
|
|
|
|
pretimeout = val;
|
2018-03-29 00:35:47 +08:00
|
|
|
return _ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-11-21 04:14:46 +08:00
|
|
|
case WDIOC_GETPRETIMEOUT:
|
2005-04-17 06:20:36 +08:00
|
|
|
i = copy_to_user(argp, &pretimeout, sizeof(pretimeout));
|
|
|
|
if (i)
|
|
|
|
return -EFAULT;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case WDIOC_KEEPALIVE:
|
2018-03-29 00:35:47 +08:00
|
|
|
return _ipmi_heartbeat();
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
case WDIOC_SETOPTIONS:
|
|
|
|
i = copy_from_user(&val, argp, sizeof(int));
|
|
|
|
if (i)
|
|
|
|
return -EFAULT;
|
2008-04-29 16:01:12 +08:00
|
|
|
if (val & WDIOS_DISABLECARD) {
|
2005-04-17 06:20:36 +08:00
|
|
|
ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
|
2018-03-29 00:35:47 +08:00
|
|
|
_ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
|
2005-04-17 06:20:36 +08:00
|
|
|
ipmi_start_timer_on_heartbeat = 0;
|
|
|
|
}
|
|
|
|
|
2008-04-29 16:01:12 +08:00
|
|
|
if (val & WDIOS_ENABLECARD) {
|
2005-04-17 06:20:36 +08:00
|
|
|
ipmi_watchdog_state = action_val;
|
2018-03-29 00:35:47 +08:00
|
|
|
_ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case WDIOC_GETSTATUS:
|
|
|
|
val = 0;
|
|
|
|
i = copy_to_user(argp, &val, sizeof(val));
|
|
|
|
if (i)
|
|
|
|
return -EFAULT;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -ENOIOCTLCMD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-27 06:24:05 +08:00
|
|
|
static long ipmi_unlocked_ioctl(struct file *file,
|
|
|
|
unsigned int cmd,
|
|
|
|
unsigned long arg)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2010-06-02 20:28:52 +08:00
|
|
|
mutex_lock(&ipmi_watchdog_mutex);
|
2010-04-27 06:24:05 +08:00
|
|
|
ret = ipmi_ioctl(file, cmd, arg);
|
2010-06-02 20:28:52 +08:00
|
|
|
mutex_unlock(&ipmi_watchdog_mutex);
|
2010-04-27 06:24:05 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static ssize_t ipmi_write(struct file *file,
|
|
|
|
const char __user *buf,
|
|
|
|
size_t len,
|
|
|
|
loff_t *ppos)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
if (len) {
|
2008-04-29 16:01:12 +08:00
|
|
|
if (!nowayout) {
|
|
|
|
size_t i;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* In case it was set long ago */
|
|
|
|
expect_close = 0;
|
|
|
|
|
2008-04-29 16:01:12 +08:00
|
|
|
for (i = 0; i != len; i++) {
|
2005-04-17 06:20:36 +08:00
|
|
|
char c;
|
|
|
|
|
|
|
|
if (get_user(c, buf + i))
|
|
|
|
return -EFAULT;
|
|
|
|
if (c == 'V')
|
|
|
|
expect_close = 42;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rv = ipmi_heartbeat();
|
|
|
|
if (rv)
|
|
|
|
return rv;
|
|
|
|
}
|
2008-07-11 03:27:11 +08:00
|
|
|
return len;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t ipmi_read(struct file *file,
|
|
|
|
char __user *buf,
|
|
|
|
size_t count,
|
|
|
|
loff_t *ppos)
|
|
|
|
{
|
|
|
|
int rv = 0;
|
2017-06-20 18:06:13 +08:00
|
|
|
wait_queue_entry_t wait;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (count <= 0)
|
|
|
|
return 0;
|
|
|
|
|
2008-04-29 16:01:12 +08:00
|
|
|
/*
|
|
|
|
* Reading returns if the pretimeout has gone off, and it only does
|
|
|
|
* it once per pretimeout.
|
|
|
|
*/
|
2018-03-29 00:35:47 +08:00
|
|
|
spin_lock_irq(&ipmi_read_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!data_to_read) {
|
|
|
|
if (file->f_flags & O_NONBLOCK) {
|
|
|
|
rv = -EAGAIN;
|
|
|
|
goto out;
|
|
|
|
}
|
2008-04-29 16:01:12 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
init_waitqueue_entry(&wait, current);
|
|
|
|
add_wait_queue(&read_q, &wait);
|
|
|
|
while (!data_to_read) {
|
|
|
|
set_current_state(TASK_INTERRUPTIBLE);
|
2018-03-29 00:35:47 +08:00
|
|
|
spin_unlock_irq(&ipmi_read_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
schedule();
|
2018-03-29 00:35:47 +08:00
|
|
|
spin_lock_irq(&ipmi_read_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
remove_wait_queue(&read_q, &wait);
|
2008-04-29 16:01:12 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (signal_pending(current)) {
|
|
|
|
rv = -ERESTARTSYS;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data_to_read = 0;
|
|
|
|
|
|
|
|
out:
|
2018-03-29 00:35:47 +08:00
|
|
|
spin_unlock_irq(&ipmi_read_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (rv == 0) {
|
|
|
|
if (copy_to_user(buf, &data_to_read, 1))
|
|
|
|
rv = -EFAULT;
|
|
|
|
else
|
|
|
|
rv = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ipmi_open(struct inode *ino, struct file *filep)
|
|
|
|
{
|
2008-04-29 16:01:12 +08:00
|
|
|
switch (iminor(ino)) {
|
|
|
|
case WATCHDOG_MINOR:
|
2005-09-07 06:18:45 +08:00
|
|
|
if (test_and_set_bit(0, &ipmi_wdog_open))
|
2008-04-29 16:01:12 +08:00
|
|
|
return -EBUSY;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-05-21 01:16:04 +08:00
|
|
|
|
2008-04-29 16:01:12 +08:00
|
|
|
/*
|
|
|
|
* Don't start the timer now, let it start on the
|
|
|
|
* first heartbeat.
|
|
|
|
*/
|
2005-09-07 06:18:45 +08:00
|
|
|
ipmi_start_timer_on_heartbeat = 1;
|
2019-03-27 04:51:19 +08:00
|
|
|
return stream_open(ino, filep);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-09-07 06:18:45 +08:00
|
|
|
default:
|
|
|
|
return (-ENODEV);
|
2008-04-29 16:01:12 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2017-07-03 18:39:46 +08:00
|
|
|
static __poll_t ipmi_poll(struct file *file, poll_table *wait)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2017-07-03 18:39:46 +08:00
|
|
|
__poll_t mask = 0;
|
2008-04-29 16:01:12 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
poll_wait(file, &read_q, wait);
|
|
|
|
|
2018-03-29 00:35:47 +08:00
|
|
|
spin_lock_irq(&ipmi_read_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (data_to_read)
|
2018-02-12 06:34:03 +08:00
|
|
|
mask |= (EPOLLIN | EPOLLRDNORM);
|
2018-03-29 00:35:47 +08:00
|
|
|
spin_unlock_irq(&ipmi_read_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ipmi_fasync(int fd, struct file *file, int on)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
|
|
|
result = fasync_helper(fd, file, on, &fasync_q);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ipmi_close(struct inode *ino, struct file *filep)
|
|
|
|
{
|
2006-03-31 18:30:40 +08:00
|
|
|
if (iminor(ino) == WATCHDOG_MINOR) {
|
2005-04-17 06:20:36 +08:00
|
|
|
if (expect_close == 42) {
|
2018-03-29 00:35:47 +08:00
|
|
|
mutex_lock(&ipmi_watchdog_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
|
2018-03-29 00:35:47 +08:00
|
|
|
_ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
|
|
|
|
mutex_unlock(&ipmi_watchdog_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
} else {
|
2018-05-09 23:15:48 +08:00
|
|
|
pr_crit("Unexpected close, not stopping watchdog!\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
ipmi_heartbeat();
|
|
|
|
}
|
2005-05-01 23:59:11 +08:00
|
|
|
clear_bit(0, &ipmi_wdog_open);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
expect_close = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-03 15:24:21 +08:00
|
|
|
static const struct file_operations ipmi_wdog_fops = {
|
2005-04-17 06:20:36 +08:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.read = ipmi_read,
|
|
|
|
.poll = ipmi_poll,
|
|
|
|
.write = ipmi_write,
|
2010-04-27 06:24:05 +08:00
|
|
|
.unlocked_ioctl = ipmi_unlocked_ioctl,
|
2005-04-17 06:20:36 +08:00
|
|
|
.open = ipmi_open,
|
|
|
|
.release = ipmi_close,
|
|
|
|
.fasync = ipmi_fasync,
|
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-16 00:52:59 +08:00
|
|
|
.llseek = no_llseek,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct miscdevice ipmi_wdog_miscdev = {
|
|
|
|
.minor = WATCHDOG_MINOR,
|
|
|
|
.name = "watchdog",
|
|
|
|
.fops = &ipmi_wdog_fops
|
|
|
|
};
|
|
|
|
|
|
|
|
static void ipmi_wdog_msg_handler(struct ipmi_recv_msg *msg,
|
|
|
|
void *handler_data)
|
|
|
|
{
|
2011-12-20 09:12:02 +08:00
|
|
|
if (msg->msg.cmd == IPMI_WDOG_RESET_TIMER &&
|
|
|
|
msg->msg.data[0] == IPMI_WDOG_TIMER_NOT_INIT_RESP)
|
2018-05-09 23:15:48 +08:00
|
|
|
pr_info("response: The IPMI controller appears to have been reset, will attempt to reinitialize the watchdog timer\n");
|
2011-12-20 09:12:02 +08:00
|
|
|
else if (msg->msg.data[0] != 0)
|
2018-05-09 23:15:48 +08:00
|
|
|
pr_err("response: Error %x on cmd %x\n",
|
2005-04-17 06:20:36 +08:00
|
|
|
msg->msg.data[0],
|
|
|
|
msg->msg.cmd);
|
2008-04-29 16:01:12 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
ipmi_free_recv_msg(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ipmi_wdog_pretimeout_handler(void *handler_data)
|
|
|
|
{
|
|
|
|
if (preaction_val != WDOG_PRETIMEOUT_NONE) {
|
2005-11-07 17:00:05 +08:00
|
|
|
if (preop_val == WDOG_PREOP_PANIC) {
|
|
|
|
if (atomic_inc_and_test(&preop_panic_excl))
|
|
|
|
panic("Watchdog pre-timeout");
|
|
|
|
} else if (preop_val == WDOG_PREOP_GIVE_DATA) {
|
2018-03-29 00:35:47 +08:00
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&ipmi_read_lock, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
data_to_read = 1;
|
|
|
|
wake_up_interruptible(&read_q);
|
|
|
|
kill_fasync(&fasync_q, SIGIO, POLL_IN);
|
2018-03-29 00:35:47 +08:00
|
|
|
spin_unlock_irqrestore(&ipmi_read_lock, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-29 16:01:12 +08:00
|
|
|
/*
|
|
|
|
* On some machines, the heartbeat will give an error and not
|
|
|
|
* work unless we re-enable the timer. So do so.
|
|
|
|
*/
|
2018-03-29 00:35:47 +08:00
|
|
|
atomic_set(&pretimeout_since_last_heartbeat, 1);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2018-03-29 02:30:38 +08:00
|
|
|
static void ipmi_wdog_panic_handler(void *user_data)
|
|
|
|
{
|
|
|
|
static int panic_event_handled;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
timeout = panic_wdt_timeout;
|
|
|
|
pretimeout = 0;
|
|
|
|
panic_halt_ipmi_set_timeout();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-06 00:52:10 +08:00
|
|
|
static const struct ipmi_user_hndl ipmi_hndlrs = {
|
2005-04-17 06:20:36 +08:00
|
|
|
.ipmi_recv_hndl = ipmi_wdog_msg_handler,
|
2018-03-29 02:30:38 +08:00
|
|
|
.ipmi_watchdog_pretimeout = ipmi_wdog_pretimeout_handler,
|
|
|
|
.ipmi_panic_handler = ipmi_wdog_panic_handler
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static void ipmi_register_watchdog(int ipmi_intf)
|
|
|
|
{
|
|
|
|
int rv = -EBUSY;
|
|
|
|
|
|
|
|
if (watchdog_user)
|
|
|
|
goto out;
|
|
|
|
|
2006-12-07 12:41:00 +08:00
|
|
|
if ((ifnum_to_use >= 0) && (ifnum_to_use != ipmi_intf))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
watchdog_ifnum = ipmi_intf;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
rv = ipmi_create_user(ipmi_intf, &ipmi_hndlrs, NULL, &watchdog_user);
|
|
|
|
if (rv < 0) {
|
2018-05-09 23:15:48 +08:00
|
|
|
pr_crit("Unable to register with ipmi\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2017-08-30 21:04:24 +08:00
|
|
|
rv = ipmi_get_version(watchdog_user,
|
|
|
|
&ipmi_version_major,
|
|
|
|
&ipmi_version_minor);
|
|
|
|
if (rv) {
|
2018-05-09 23:15:48 +08:00
|
|
|
pr_warn("Unable to get IPMI version, assuming 1.0\n");
|
2017-08-30 21:04:24 +08:00
|
|
|
ipmi_version_major = 1;
|
|
|
|
ipmi_version_minor = 0;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
rv = misc_register(&ipmi_wdog_miscdev);
|
|
|
|
if (rv < 0) {
|
|
|
|
ipmi_destroy_user(watchdog_user);
|
|
|
|
watchdog_user = NULL;
|
2018-05-09 23:15:48 +08:00
|
|
|
pr_crit("Unable to register misc device\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2007-10-18 18:07:10 +08:00
|
|
|
#ifdef HAVE_DIE_NMI
|
|
|
|
if (nmi_handler_registered) {
|
|
|
|
int old_pretimeout = pretimeout;
|
|
|
|
int old_timeout = timeout;
|
|
|
|
int old_preop_val = preop_val;
|
|
|
|
|
2008-04-29 16:01:12 +08:00
|
|
|
/*
|
|
|
|
* Set the pretimeout to go off in a second and give
|
|
|
|
* ourselves plenty of time to stop the timer.
|
|
|
|
*/
|
2007-10-18 18:07:10 +08:00
|
|
|
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) {
|
2018-05-09 23:15:48 +08:00
|
|
|
pr_warn("Error starting timer to test NMI: 0x%x. The NMI pretimeout will likely not work\n",
|
2018-03-29 02:04:06 +08:00
|
|
|
rv);
|
2007-10-18 18:07:10 +08:00
|
|
|
rv = 0;
|
|
|
|
goto out_restore;
|
|
|
|
}
|
|
|
|
|
|
|
|
msleep(1500);
|
|
|
|
|
|
|
|
if (testing_nmi != 2) {
|
2018-05-09 23:15:48 +08:00
|
|
|
pr_warn("IPMI NMI didn't seem to occur. The NMI pretimeout will likely not work\n");
|
2007-10-18 18:07:10 +08:00
|
|
|
}
|
2008-04-29 16:01:12 +08:00
|
|
|
out_restore:
|
2007-10-18 18:07:10 +08:00
|
|
|
testing_nmi = 0;
|
|
|
|
preop_val = old_preop_val;
|
|
|
|
pretimeout = old_pretimeout;
|
|
|
|
timeout = old_timeout;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
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);
|
2018-05-09 23:15:48 +08:00
|
|
|
pr_info("Starting now!\n");
|
2007-10-18 18:07:10 +08:00
|
|
|
} else {
|
|
|
|
/* Stop the timer now. */
|
|
|
|
ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
|
|
|
|
ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-07 12:41:00 +08:00
|
|
|
static void ipmi_unregister_watchdog(int ipmi_intf)
|
|
|
|
{
|
|
|
|
int rv;
|
2018-04-06 09:50:48 +08:00
|
|
|
struct ipmi_user *loc_user = watchdog_user;
|
2006-12-07 12:41:00 +08:00
|
|
|
|
2018-03-29 00:35:47 +08:00
|
|
|
if (!loc_user)
|
|
|
|
return;
|
2006-12-07 12:41:00 +08:00
|
|
|
|
|
|
|
if (watchdog_ifnum != ipmi_intf)
|
2018-03-29 00:35:47 +08:00
|
|
|
return;
|
2006-12-07 12:41:00 +08:00
|
|
|
|
|
|
|
/* Make sure no one can call us any more. */
|
|
|
|
misc_deregister(&ipmi_wdog_miscdev);
|
|
|
|
|
2018-03-29 00:35:47 +08:00
|
|
|
watchdog_user = NULL;
|
|
|
|
|
2008-04-29 16:01:12 +08:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-03-29 00:35:47 +08:00
|
|
|
while (atomic_read(&msg_tofree))
|
|
|
|
msg_free_smi(NULL);
|
|
|
|
|
|
|
|
mutex_lock(&ipmi_watchdog_mutex);
|
2006-12-07 12:41:00 +08:00
|
|
|
|
|
|
|
/* Disconnect from IPMI. */
|
2018-03-29 00:35:47 +08:00
|
|
|
rv = ipmi_destroy_user(loc_user);
|
|
|
|
if (rv)
|
2018-05-09 23:15:48 +08:00
|
|
|
pr_warn("error unlinking from IPMI: %d\n", rv);
|
2006-12-07 12:41:00 +08:00
|
|
|
|
2018-03-29 00:35:47 +08:00
|
|
|
/* If it comes back, restart it properly. */
|
|
|
|
ipmi_start_timer_on_heartbeat = 1;
|
|
|
|
|
|
|
|
mutex_unlock(&ipmi_watchdog_mutex);
|
2006-12-07 12:41:00 +08:00
|
|
|
}
|
|
|
|
|
2007-10-18 18:07:10 +08:00
|
|
|
#ifdef HAVE_DIE_NMI
|
2005-04-17 06:20:36 +08:00
|
|
|
static int
|
2011-10-01 03:06:21 +08:00
|
|
|
ipmi_nmi(unsigned int val, struct pt_regs *regs)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2007-10-18 18:07:10 +08:00
|
|
|
/*
|
|
|
|
* 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;
|
2011-10-01 03:06:21 +08:00
|
|
|
return NMI_HANDLED;
|
2007-10-18 18:07:10 +08:00
|
|
|
}
|
|
|
|
|
2008-04-29 16:01:12 +08:00
|
|
|
/* If we are not expecting a timeout, ignore it. */
|
2005-09-07 06:18:39 +08:00
|
|
|
if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)
|
2011-10-01 03:06:21 +08:00
|
|
|
return NMI_DONE;
|
2007-10-18 18:07:10 +08:00
|
|
|
|
|
|
|
if (preaction_val != WDOG_PRETIMEOUT_NMI)
|
2011-10-01 03:06:21 +08:00
|
|
|
return NMI_DONE;
|
2005-09-07 06:18:39 +08:00
|
|
|
|
2008-04-29 16:01:12 +08:00
|
|
|
/*
|
|
|
|
* If no one else handled the NMI, we assume it was the IPMI
|
|
|
|
* watchdog.
|
|
|
|
*/
|
2007-10-18 18:07:10 +08:00
|
|
|
if (preop_val == WDOG_PREOP_PANIC) {
|
2005-09-07 06:18:39 +08:00
|
|
|
/* On some machines, the heartbeat will give
|
|
|
|
an error and not work unless we re-enable
|
|
|
|
the timer. So do so. */
|
2018-03-29 00:35:47 +08:00
|
|
|
atomic_set(&pretimeout_since_last_heartbeat, 1);
|
2005-11-07 17:00:05 +08:00
|
|
|
if (atomic_inc_and_test(&preop_panic_excl))
|
2018-05-09 23:15:48 +08:00
|
|
|
nmi_panic(regs, "pre-timeout");
|
2005-09-07 06:18:39 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-10-01 03:06:21 +08:00
|
|
|
return NMI_HANDLED;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int wdog_reboot_handler(struct notifier_block *this,
|
|
|
|
unsigned long code,
|
|
|
|
void *unused)
|
|
|
|
{
|
2008-04-29 16:01:12 +08:00
|
|
|
static int reboot_event_handled;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if ((watchdog_user) && (!reboot_event_handled)) {
|
|
|
|
/* Make sure we only do this once. */
|
|
|
|
reboot_event_handled = 1;
|
|
|
|
|
2007-10-18 18:07:09 +08:00
|
|
|
if (code == SYS_POWER_OFF || code == SYS_HALT) {
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Disable the WDT if we are shutting down. */
|
|
|
|
ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
|
2012-03-29 05:42:50 +08:00
|
|
|
ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
|
2006-06-28 19:26:55 +08:00
|
|
|
} else if (ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
|
2017-05-06 03:07:33 +08:00
|
|
|
/* Set a long timer to let the reboot happen or
|
|
|
|
reset if it hangs, but only if the watchdog
|
2006-06-28 19:26:55 +08:00
|
|
|
timer was already running. */
|
2017-05-06 03:07:33 +08:00
|
|
|
if (timeout < 120)
|
|
|
|
timeout = 120;
|
2005-04-17 06:20:36 +08:00
|
|
|
pretimeout = 0;
|
|
|
|
ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
|
2012-03-29 05:42:50 +08:00
|
|
|
ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NOTIFY_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct notifier_block wdog_reboot_notifier = {
|
|
|
|
.notifier_call = wdog_reboot_handler,
|
|
|
|
.next = NULL,
|
|
|
|
.priority = 0
|
|
|
|
};
|
|
|
|
|
2006-03-26 17:37:21 +08:00
|
|
|
static void ipmi_new_smi(int if_num, struct device *device)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
ipmi_register_watchdog(if_num);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ipmi_smi_gone(int if_num)
|
|
|
|
{
|
2006-12-07 12:41:00 +08:00
|
|
|
ipmi_unregister_watchdog(if_num);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2008-04-29 16:01:12 +08:00
|
|
|
static struct ipmi_smi_watcher smi_watcher = {
|
2005-04-17 06:20:36 +08:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.new_smi = ipmi_new_smi,
|
|
|
|
.smi_gone = ipmi_smi_gone
|
|
|
|
};
|
|
|
|
|
2005-11-07 16:59:57 +08:00
|
|
|
static int action_op(const char *inval, char *outval)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-11-07 16:59:57 +08:00
|
|
|
if (outval)
|
|
|
|
strcpy(outval, action);
|
|
|
|
|
|
|
|
if (!inval)
|
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-11-07 16:59:57 +08:00
|
|
|
if (strcmp(inval, "reset") == 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
action_val = WDOG_TIMEOUT_RESET;
|
2005-11-07 16:59:57 +08:00
|
|
|
else if (strcmp(inval, "none") == 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
action_val = WDOG_TIMEOUT_NONE;
|
2005-11-07 16:59:57 +08:00
|
|
|
else if (strcmp(inval, "power_cycle") == 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
action_val = WDOG_TIMEOUT_POWER_CYCLE;
|
2005-11-07 16:59:57 +08:00
|
|
|
else if (strcmp(inval, "power_off") == 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
action_val = WDOG_TIMEOUT_POWER_DOWN;
|
2005-11-07 16:59:57 +08:00
|
|
|
else
|
|
|
|
return -EINVAL;
|
|
|
|
strcpy(action, inval);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int preaction_op(const char *inval, char *outval)
|
|
|
|
{
|
|
|
|
if (outval)
|
|
|
|
strcpy(outval, preaction);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-11-07 16:59:57 +08:00
|
|
|
if (!inval)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (strcmp(inval, "pre_none") == 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
preaction_val = WDOG_PRETIMEOUT_NONE;
|
2005-11-07 16:59:57 +08:00
|
|
|
else if (strcmp(inval, "pre_smi") == 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
preaction_val = WDOG_PRETIMEOUT_SMI;
|
2007-10-18 18:07:10 +08:00
|
|
|
#ifdef HAVE_DIE_NMI
|
2005-11-07 16:59:57 +08:00
|
|
|
else if (strcmp(inval, "pre_nmi") == 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
preaction_val = WDOG_PRETIMEOUT_NMI;
|
|
|
|
#endif
|
2005-11-07 16:59:57 +08:00
|
|
|
else if (strcmp(inval, "pre_int") == 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
preaction_val = WDOG_PRETIMEOUT_MSG_INT;
|
2005-11-07 16:59:57 +08:00
|
|
|
else
|
|
|
|
return -EINVAL;
|
|
|
|
strcpy(preaction, inval);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int preop_op(const char *inval, char *outval)
|
|
|
|
{
|
|
|
|
if (outval)
|
|
|
|
strcpy(outval, preop);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-11-07 16:59:57 +08:00
|
|
|
if (!inval)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (strcmp(inval, "preop_none") == 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
preop_val = WDOG_PREOP_NONE;
|
2005-11-07 16:59:57 +08:00
|
|
|
else if (strcmp(inval, "preop_panic") == 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
preop_val = WDOG_PREOP_PANIC;
|
2005-11-07 16:59:57 +08:00
|
|
|
else if (strcmp(inval, "preop_give_data") == 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
preop_val = WDOG_PREOP_GIVE_DATA;
|
2005-11-07 16:59:57 +08:00
|
|
|
else
|
|
|
|
return -EINVAL;
|
|
|
|
strcpy(preop, inval);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-11-07 16:59:57 +08:00
|
|
|
static void check_parms(void)
|
|
|
|
{
|
2007-10-18 18:07:10 +08:00
|
|
|
#ifdef HAVE_DIE_NMI
|
2005-11-07 16:59:57 +08:00
|
|
|
int do_nmi = 0;
|
|
|
|
int rv;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (preaction_val == WDOG_PRETIMEOUT_NMI) {
|
2005-11-07 16:59:57 +08:00
|
|
|
do_nmi = 1;
|
2005-04-17 06:20:36 +08:00
|
|
|
if (preop_val == WDOG_PREOP_GIVE_DATA) {
|
2018-05-09 23:15:48 +08:00
|
|
|
pr_warn("Pretimeout op is to give data but NMI pretimeout is enabled, setting pretimeout op to none\n");
|
2005-11-07 16:59:57 +08:00
|
|
|
preop_op("preop_none", NULL);
|
|
|
|
do_nmi = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2005-11-07 16:59:57 +08:00
|
|
|
}
|
|
|
|
if (do_nmi && !nmi_handler_registered) {
|
2011-10-01 03:06:21 +08:00
|
|
|
rv = register_nmi_handler(NMI_UNKNOWN, ipmi_nmi, 0,
|
|
|
|
"ipmi");
|
2005-04-17 06:20:36 +08:00
|
|
|
if (rv) {
|
2018-05-09 23:15:48 +08:00
|
|
|
pr_warn("Can't register nmi handler\n");
|
2005-11-07 16:59:57 +08:00
|
|
|
return;
|
|
|
|
} else
|
|
|
|
nmi_handler_registered = 1;
|
|
|
|
} else if (!do_nmi && nmi_handler_registered) {
|
2011-10-01 03:06:21 +08:00
|
|
|
unregister_nmi_handler(NMI_UNKNOWN, "ipmi");
|
2005-11-07 16:59:57 +08:00
|
|
|
nmi_handler_registered = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
#endif
|
2005-11-07 16:59:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int __init ipmi_wdog_init(void)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
if (action_op(action, NULL)) {
|
|
|
|
action_op("reset", NULL);
|
2018-05-09 23:15:48 +08:00
|
|
|
pr_info("Unknown action '%s', defaulting to reset\n", action);
|
2005-11-07 16:59:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (preaction_op(preaction, NULL)) {
|
|
|
|
preaction_op("pre_none", NULL);
|
2018-05-09 23:15:48 +08:00
|
|
|
pr_info("Unknown preaction '%s', defaulting to none\n",
|
2018-03-29 02:04:06 +08:00
|
|
|
preaction);
|
2005-11-07 16:59:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (preop_op(preop, NULL)) {
|
|
|
|
preop_op("preop_none", NULL);
|
2018-05-09 23:15:48 +08:00
|
|
|
pr_info("Unknown preop '%s', defaulting to none\n", preop);
|
2005-11-07 16:59:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
check_parms();
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-12-07 12:41:00 +08:00
|
|
|
register_reboot_notifier(&wdog_reboot_notifier);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
rv = ipmi_smi_watcher_register(&smi_watcher);
|
|
|
|
if (rv) {
|
2007-10-18 18:07:10 +08:00
|
|
|
#ifdef HAVE_DIE_NMI
|
|
|
|
if (nmi_handler_registered)
|
2011-10-01 03:06:21 +08:00
|
|
|
unregister_nmi_handler(NMI_UNKNOWN, "ipmi");
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif
|
2006-12-07 12:41:00 +08:00
|
|
|
unregister_reboot_notifier(&wdog_reboot_notifier);
|
2018-05-09 23:15:48 +08:00
|
|
|
pr_warn("can't register smi watcher\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2018-05-09 23:15:48 +08:00
|
|
|
pr_info("driver initialized\n");
|
2005-09-07 06:18:42 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-12-07 12:41:00 +08:00
|
|
|
static void __exit ipmi_wdog_exit(void)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-12-07 12:41:00 +08:00
|
|
|
ipmi_smi_watcher_unregister(&smi_watcher);
|
|
|
|
ipmi_unregister_watchdog(watchdog_ifnum);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-10-18 18:07:10 +08:00
|
|
|
#ifdef HAVE_DIE_NMI
|
2005-11-07 16:59:57 +08:00
|
|
|
if (nmi_handler_registered)
|
2011-10-01 03:06:21 +08:00
|
|
|
unregister_nmi_handler(NMI_UNKNOWN, "ipmi");
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
unregister_reboot_notifier(&wdog_reboot_notifier);
|
|
|
|
}
|
|
|
|
module_exit(ipmi_wdog_exit);
|
|
|
|
module_init(ipmi_wdog_init);
|
|
|
|
MODULE_LICENSE("GPL");
|
2005-09-07 06:18:42 +08:00
|
|
|
MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
|
|
|
|
MODULE_DESCRIPTION("watchdog timer based upon the IPMI interface.");
|