📄 ipmi_watchdog.c
字号:
/* * 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. * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 675 Mass Ave, Cambridge, MA 02139, USA. */#include <linux/config.h>#include <linux/module.h>#include <linux/moduleparam.h>#include <linux/ipmi.h>#include <linux/ipmi_smi.h>#include <linux/watchdog.h>#include <linux/miscdevice.h>#include <linux/init.h>#include <linux/rwsem.h>#include <linux/errno.h>#include <asm/uaccess.h>#include <linux/notifier.h>#include <linux/nmi.h>#include <linux/reboot.h>#include <linux/wait.h>#include <linux/poll.h>#include <linux/string.h>#include <linux/ctype.h>#include <asm/atomic.h>#ifdef CONFIG_X86_LOCAL_APIC#include <asm/apic.h>#endif#define PFX "IPMI Watchdog: "/* * 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#define WDOG_PREOP_GIVE_DATA 2 /* Cause data to be available to read. Doesn't work in NMI mode. *//* 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)#define IPMI_WDOG_RESET_TIMER 0x22#define IPMI_WDOG_SET_TIMER 0x24#define IPMI_WDOG_GET_TIMER 0x25/* These are here until the real ones get into the watchdog.h interface. */#ifndef WDIOC_GETTIMEOUT#define WDIOC_GETTIMEOUT _IOW(WATCHDOG_IOCTL_BASE, 20, int)#endif#ifndef WDIOC_SET_PRETIMEOUT#define WDIOC_SET_PRETIMEOUT _IOW(WATCHDOG_IOCTL_BASE, 21, int)#endif#ifndef WDIOC_GET_PRETIMEOUT#define WDIOC_GET_PRETIMEOUT _IOW(WATCHDOG_IOCTL_BASE, 22, int)#endifstatic int nowayout = WATCHDOG_NOWAYOUT;static ipmi_user_t watchdog_user = NULL;/* Default the timeout to 10 seconds. */static int timeout = 10;/* The pre-timeout is disabled by default. */static int pretimeout = 0;/* 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);static char data_to_read = 0;static DECLARE_WAIT_QUEUE_HEAD(read_q);static struct fasync_struct *fasync_q = NULL;static char pretimeout_since_last_heartbeat = 0;static char expect_close;static DECLARE_RWSEM(register_sem);/* 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 2static int ipmi_set_timeout(int do_heartbeat);/* If true, the driver will start running as soon as it is configured and ready. */static int start_now = 0;static int set_param_int(const char *val, struct kernel_param *kp){ char *endp; int l; int rv = 0; if (!val) return -EINVAL; l = simple_strtoul(val, &endp, 0); if (endp == val) return -EINVAL; down_read(®ister_sem); *((int *)kp->arg) = l; if (watchdog_user) rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY); up_read(®ister_sem); return rv;}static int get_param_int(char *buffer, struct kernel_param *kp){ return sprintf(buffer, "%i", *((int *)kp->arg));}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);static int set_param_str(const char *val, struct kernel_param *kp){ action_fn fn = (action_fn) kp->arg; int rv = 0; const char *end; char valcp[16]; int len; /* Truncate leading and trailing spaces. */ while (isspace(*val)) val++; end = val + strlen(val) - 1; while ((end >= val) && isspace(*end)) end--; len = end - val + 1; if (len > sizeof(valcp) - 1) return -EINVAL; memcpy(valcp, val, len); valcp[len] = '\0'; down_read(®ister_sem); rv = fn(valcp, NULL); if (rv) goto out_unlock; check_parms(); if (watchdog_user) rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY); out_unlock: up_read(®ister_sem); return rv;}static int get_param_str(char *buffer, struct kernel_param *kp){ action_fn fn = (action_fn) kp->arg; int rv; rv = fn(NULL, buffer); if (rv) return rv; return strlen(buffer);}module_param_call(timeout, set_param_int, get_param_int, &timeout, 0644);MODULE_PARM_DESC(timeout, "Timeout value in seconds.");module_param_call(pretimeout, set_param_int, get_param_int, &pretimeout, 0644);MODULE_PARM_DESC(pretimeout, "Pretimeout value in seconds.");module_param_call(action, set_param_str, get_param_str, action_op, 0644);MODULE_PARM_DESC(action, "Timeout action. One of: " "reset, none, power_cycle, power_off.");module_param_call(preaction, set_param_str, get_param_str, preaction_op, 0644);MODULE_PARM_DESC(preaction, "Pretimeout action. One of: " "pre_none, pre_smi, pre_nmi, pre_int.");module_param_call(preop, set_param_str, get_param_str, preop_op, 0644);MODULE_PARM_DESC(preop, "Pretimeout driver operation. One of: " "preop_none, preop_panic, preop_give_data.");module_param(start_now, int, 0);MODULE_PARM_DESC(start_now, "Set to 1 to start the watchdog as" "soon as the driver is loaded.");module_param(nowayout, int, 0644);MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");/* Default state of the timer. */static unsigned char ipmi_watchdog_state = WDOG_TIMEOUT_NONE;/* If shutting down via IPMI, we ignore the heartbeat. */static int ipmi_ignore_heartbeat = 0;/* Is someone using the watchdog? Only one user is allowed. */static unsigned long ipmi_wdog_open = 0;/* 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. */static int ipmi_start_timer_on_heartbeat = 0;/* IPMI version of the BMC. */static unsigned char ipmi_version_major;static unsigned char 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);static int ipmi_heartbeat(void);static void panic_halt_ipmi_heartbeat(void);/* We use a semaphore to make sure that only one thing can send a set timeout at one time, because we only have one copy of the data. The semaphore is claimed when the set_timeout is sent and freed when both messages are free. */static atomic_t set_timeout_tofree = ATOMIC_INIT(0);static DECLARE_MUTEX(set_timeout_lock);static void set_timeout_free_smi(struct ipmi_smi_msg *msg){ if (atomic_dec_and_test(&set_timeout_tofree)) up(&set_timeout_lock);}static void set_timeout_free_recv(struct ipmi_recv_msg *msg){ if (atomic_dec_and_test(&set_timeout_tofree)) up(&set_timeout_lock);}static struct ipmi_smi_msg set_timeout_smi_msg ={ .done = set_timeout_free_smi};static struct ipmi_recv_msg set_timeout_recv_msg ={ .done = set_timeout_free_recv}; static int i_ipmi_set_timeout(struct ipmi_smi_msg *smi_msg, 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); 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 if (ipmi_watchdog_state != WDOG_TIMEOUT_NONE) { /* In ipmi 1.0, setting the timer stops the watchdog, we need to start it back up again. */ hbnow = 1; } data[1] = 0; WDOG_SET_TIMEOUT_ACT(data[1], ipmi_watchdog_state); if ((pretimeout > 0) && (ipmi_watchdog_state != WDOG_TIMEOUT_NONE)) { 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); if (rv) { printk(KERN_WARNING PFX "set timeout error: %d\n", rv); } if (send_heartbeat_now) *send_heartbeat_now = hbnow; return rv;}static int ipmi_set_timeout(int do_heartbeat){ int send_heartbeat_now; int rv; /* We can only send one of these at a time. */ down(&set_timeout_lock); atomic_set(&set_timeout_tofree, 2); rv = i_ipmi_set_timeout(&set_timeout_smi_msg,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -