34 #include <linux/module.h>
36 #include <linux/ipmi.h>
39 #include <linux/watchdog.h>
45 #include <linux/errno.h>
46 #include <asm/uaccess.h>
49 #include <linux/reboot.h>
50 #include <linux/wait.h>
51 #include <linux/poll.h>
52 #include <linux/string.h>
53 #include <linux/ctype.h>
67 #include <asm/kdebug.h>
72 #define PFX "IPMI Watchdog: "
79 #define WDOG_DONT_LOG (1 << 7)
80 #define WDOG_DONT_STOP_ON_SET (1 << 6)
81 #define WDOG_SET_TIMER_USE(byte, use) \
82 byte = ((byte) & 0xf8) | ((use) & 0x7)
83 #define WDOG_GET_TIMER_USE(byte) ((byte) & 0x7)
84 #define WDOG_TIMER_USE_BIOS_FRB2 1
85 #define WDOG_TIMER_USE_BIOS_POST 2
86 #define WDOG_TIMER_USE_OS_LOAD 3
87 #define WDOG_TIMER_USE_SMS_OS 4
88 #define WDOG_TIMER_USE_OEM 5
91 #define WDOG_SET_PRETIMEOUT_ACT(byte, use) \
92 byte = ((byte) & 0x8f) | (((use) & 0x7) << 4)
93 #define WDOG_GET_PRETIMEOUT_ACT(byte) (((byte) >> 4) & 0x7)
94 #define WDOG_PRETIMEOUT_NONE 0
95 #define WDOG_PRETIMEOUT_SMI 1
96 #define WDOG_PRETIMEOUT_NMI 2
97 #define WDOG_PRETIMEOUT_MSG_INT 3
100 #define WDOG_PREOP_NONE 0
101 #define WDOG_PREOP_PANIC 1
103 #define WDOG_PREOP_GIVE_DATA 2
106 #define WDOG_SET_TIMEOUT_ACT(byte, use) \
107 byte = ((byte) & 0xf8) | ((use) & 0x7)
108 #define WDOG_GET_TIMEOUT_ACT(byte) ((byte) & 0x7)
109 #define WDOG_TIMEOUT_NONE 0
110 #define WDOG_TIMEOUT_RESET 1
111 #define WDOG_TIMEOUT_POWER_DOWN 2
112 #define WDOG_TIMEOUT_POWER_CYCLE 3
120 #define WDOG_EXPIRE_CLEAR_BIOS_FRB2 (1 << 1)
121 #define WDOG_EXPIRE_CLEAR_BIOS_POST (1 << 2)
122 #define WDOG_EXPIRE_CLEAR_OS_LOAD (1 << 3)
123 #define WDOG_EXPIRE_CLEAR_SMS_OS (1 << 4)
124 #define WDOG_EXPIRE_CLEAR_OEM (1 << 5)
133 #define WDOG_SET_TIMEOUT(byte1, byte2, val) \
134 (byte1) = (((val) * 10) & 0xff), (byte2) = (((val) * 10) >> 8)
135 #define WDOG_GET_TIMEOUT(byte1, byte2) \
136 (((byte1) | ((byte2) << 8)) / 10)
138 #define IPMI_WDOG_RESET_TIMER 0x22
139 #define IPMI_WDOG_SET_TIMER 0x24
140 #define IPMI_WDOG_GET_TIMER 0x25
142 #define IPMI_WDOG_TIMER_NOT_INIT_RESP 0x80
148 static int watchdog_ifnum;
154 static int pretimeout;
159 static char action[16] =
"reset";
163 static char preaction[16] =
"pre_none";
167 static char preop[16] =
"preop_none";
169 static char data_to_read;
172 static char pretimeout_since_last_heartbeat;
173 static char expect_close;
175 static int ifnum_to_use = -1;
178 #define IPMI_SET_TIMEOUT_NO_HB 0
179 #define IPMI_SET_TIMEOUT_HB_IF_NECESSARY 1
180 #define IPMI_SET_TIMEOUT_FORCE_HB 2
182 static int ipmi_set_timeout(
int do_heartbeat);
183 static void ipmi_register_watchdog(
int ipmi_intf);
184 static void ipmi_unregister_watchdog(
int ipmi_intf);
190 static int start_now;
192 static int set_param_timeout(
const char *
val,
const struct kernel_param *kp)
204 *((
int *)kp->
arg) =
l;
212 .set = set_param_timeout,
215 #define param_check_timeout param_check_int
219 static int action_op(
const char *inval,
char *outval);
220 static int preaction_op(
const char *inval,
char *outval);
221 static int preop_op(
const char *inval,
char *outval);
222 static void check_parms(
void);
224 static int set_param_str(
const char *val,
const struct kernel_param *kp)
253 rv = fn(
NULL, buffer);
260 static int set_param_wdog_ifnum(
const char *val,
const struct kernel_param *kp)
265 if ((ifnum_to_use < 0) || (ifnum_to_use == watchdog_ifnum))
268 ipmi_unregister_watchdog(watchdog_ifnum);
269 ipmi_register_watchdog(ifnum_to_use);
274 .set = set_param_wdog_ifnum,
278 #define param_check_wdog_ifnum param_check_int
281 .set = set_param_str,
282 .get = get_param_str,
286 MODULE_PARM_DESC(ifnum_to_use,
"The interface number to use for the watchdog "
287 "timer. Setting to -1 defaults to the first registered "
298 "reset, none, power_cycle, power_off.");
302 "pre_none, pre_smi, pre_nmi, pre_int.");
306 "preop_none, preop_panic, preop_give_data.");
310 "soon as the driver is loaded.");
314 "(default=CONFIG_WATCHDOG_NOWAYOUT)");
320 static int ipmi_ignore_heartbeat;
323 static unsigned long ipmi_wdog_open;
331 static int ipmi_start_timer_on_heartbeat;
341 static int testing_nmi;
342 static int nmi_handler_registered;
345 static int ipmi_heartbeat(
void);
367 .done = set_timeout_free_smi
370 .done = set_timeout_free_recv
373 static int i_ipmi_set_timeout(
struct ipmi_smi_msg *smi_msg,
375 int *send_heartbeat_now)
378 unsigned char data[6];
385 pretimeout_since_last_heartbeat = 0;
390 if ((ipmi_version_major > 1)
391 || ((ipmi_version_major == 1) && (ipmi_version_minor >= 5))) {
406 data[2] = pretimeout;
421 msg.data_len =
sizeof(
data);
435 if (send_heartbeat_now)
436 *send_heartbeat_now = hbnow;
441 static int ipmi_set_timeout(
int do_heartbeat)
443 int send_heartbeat_now;
452 rv = i_ipmi_set_timeout(&set_timeout_smi_msg,
453 &set_timeout_recv_msg,
454 &send_heartbeat_now);
465 || ((send_heartbeat_now)
467 rv = ipmi_heartbeat();
484 static struct ipmi_smi_msg panic_halt_heartbeat_smi_msg = {
485 .done = panic_smi_free
487 static struct ipmi_recv_msg panic_halt_heartbeat_recv_msg = {
488 .done = panic_recv_free
491 static void panic_halt_ipmi_heartbeat(
void)
518 &panic_halt_heartbeat_smi_msg,
519 &panic_halt_heartbeat_recv_msg,
526 .done = panic_smi_free
529 .done = panic_recv_free
538 static void panic_halt_ipmi_set_timeout(
void)
540 int send_heartbeat_now;
547 rv = i_ipmi_set_timeout(&panic_halt_smi_msg,
548 &panic_halt_recv_msg,
549 &send_heartbeat_now);
553 "Unable to extend the watchdog timeout.");
555 if (send_heartbeat_now)
556 panic_halt_ipmi_heartbeat();
571 static void heartbeat_free_smi(
struct ipmi_smi_msg *msg)
582 .done = heartbeat_free_smi
585 .done = heartbeat_free_recv
588 static int ipmi_heartbeat(
void)
593 int timeout_retries = 0;
595 if (ipmi_ignore_heartbeat)
598 if (ipmi_start_timer_on_heartbeat) {
599 ipmi_start_timer_on_heartbeat = 0;
600 ipmi_watchdog_state = action_val;
602 }
else if (pretimeout_since_last_heartbeat) {
654 if (timeout_retries > 3) {
656 " watchdog's settings, giving up.\n");
672 " set the watchdog's settings, giving up.\n");
678 }
else if (heartbeat_recv_msg.
msg.data[0] != 0) {
695 .firmware_version = 1,
699 static int ipmi_ioctl(
struct file *
file,
700 unsigned int cmd,
unsigned long arg)
732 i =
copy_to_user(argp, &pretimeout,
sizeof(pretimeout));
738 return ipmi_heartbeat();
747 ipmi_start_timer_on_heartbeat = 0;
751 ipmi_watchdog_state = action_val;
768 static long ipmi_unlocked_ioctl(
struct file *file,
775 ret = ipmi_ioctl(file, cmd, arg);
781 static ssize_t ipmi_write(
struct file *file,
782 const char __user *
buf,
795 for (i = 0; i != len; i++) {
804 rv = ipmi_heartbeat();
811 static ssize_t ipmi_read(
struct file *file,
826 spin_lock(&ipmi_read_lock);
833 init_waitqueue_entry(&wait,
current);
835 while (!data_to_read) {
837 spin_unlock(&ipmi_read_lock);
839 spin_lock(&ipmi_read_lock);
851 spin_unlock(&ipmi_read_lock);
863 static int ipmi_open(
struct inode *
ino,
struct file *filep)
865 switch (iminor(ino)) {
875 ipmi_start_timer_on_heartbeat = 1;
883 static unsigned int ipmi_poll(
struct file *file,
poll_table *wait)
885 unsigned int mask = 0;
887 poll_wait(file, &read_q, wait);
889 spin_lock(&ipmi_read_lock);
892 spin_unlock(&ipmi_read_lock);
897 static int ipmi_fasync(
int fd,
struct file *file,
int on)
906 static int ipmi_close(
struct inode *ino,
struct file *filep)
909 if (expect_close == 42) {
914 "Unexpected close, not stopping watchdog!\n");
930 .unlocked_ioctl = ipmi_unlocked_ioctl,
932 .release = ipmi_close,
933 .fasync = ipmi_fasync,
937 static struct miscdevice ipmi_wdog_miscdev = {
940 .fops = &ipmi_wdog_fops
949 " to have been reset, will attempt to reinitialize"
950 " the watchdog timer\n");
951 else if (msg->
msg.data[0] != 0)
959 static void ipmi_wdog_pretimeout_handler(
void *handler_data)
964 panic(
"Watchdog pre-timeout");
966 spin_lock(&ipmi_read_lock);
971 spin_unlock(&ipmi_read_lock);
979 pretimeout_since_last_heartbeat = 1;
983 .ipmi_recv_hndl = ipmi_wdog_msg_handler,
984 .ipmi_watchdog_pretimeout = ipmi_wdog_pretimeout_handler
987 static void ipmi_register_watchdog(
int ipmi_intf)
994 if ((ifnum_to_use >= 0) && (ifnum_to_use != ipmi_intf))
997 watchdog_ifnum = ipmi_intf;
1006 &ipmi_version_major,
1007 &ipmi_version_minor);
1012 watchdog_user =
NULL;
1017 if (nmi_handler_registered) {
1018 int old_pretimeout = pretimeout;
1020 int old_preop_val = preop_val;
1036 " test NMI: 0x%x. The NMI pretimeout will"
1037 " likely not work\n", rv);
1044 if (testing_nmi != 2) {
1046 " occur. The NMI pretimeout will"
1047 " likely not work\n");
1051 preop_val = old_preop_val;
1052 pretimeout = old_pretimeout;
1058 if ((start_now) && (rv == 0)) {
1061 ipmi_watchdog_state = action_val;
1071 static void ipmi_unregister_watchdog(
int ipmi_intf)
1078 if (watchdog_ifnum != ipmi_intf)
1098 watchdog_user =
NULL;
1135 pretimeout_since_last_heartbeat = 1;
1148 static int reboot_event_handled;
1150 if ((watchdog_user) && (!reboot_event_handled)) {
1152 reboot_event_handled = 1;
1172 .notifier_call = wdog_reboot_handler,
1178 unsigned long event,
1181 static int panic_event_handled;
1187 if (watchdog_user && !panic_event_handled &&
1190 panic_event_handled = 1;
1194 panic_halt_ipmi_set_timeout();
1201 .notifier_call = wdog_panic_handler,
1207 static void ipmi_new_smi(
int if_num,
struct device *
device)
1209 ipmi_register_watchdog(if_num);
1212 static void ipmi_smi_gone(
int if_num)
1214 ipmi_unregister_watchdog(if_num);
1219 .new_smi = ipmi_new_smi,
1220 .smi_gone = ipmi_smi_gone
1223 static int action_op(
const char *inval,
char *outval)
1231 if (
strcmp(inval,
"reset") == 0)
1233 else if (
strcmp(inval,
"none") == 0)
1235 else if (
strcmp(inval,
"power_cycle") == 0)
1237 else if (
strcmp(inval,
"power_off") == 0)
1245 static int preaction_op(
const char *inval,
char *outval)
1248 strcpy(outval, preaction);
1253 if (
strcmp(inval,
"pre_none") == 0)
1255 else if (
strcmp(inval,
"pre_smi") == 0)
1258 else if (
strcmp(inval,
"pre_nmi") == 0)
1261 else if (
strcmp(inval,
"pre_int") == 0)
1265 strcpy(preaction, inval);
1269 static int preop_op(
const char *inval,
char *outval)
1277 if (
strcmp(inval,
"preop_none") == 0)
1279 else if (
strcmp(inval,
"preop_panic") == 0)
1281 else if (
strcmp(inval,
"preop_give_data") == 0)
1289 static void check_parms(
void)
1299 " but NMI pretimeout is enabled, setting"
1300 " pretimeout op to none\n");
1301 preop_op(
"preop_none",
NULL);
1305 if (do_nmi && !nmi_handler_registered) {
1310 "Can't register nmi handler\n");
1313 nmi_handler_registered = 1;
1314 }
else if (!do_nmi && nmi_handler_registered) {
1316 nmi_handler_registered = 0;
1321 static int __init ipmi_wdog_init(
void)
1326 action_op(
"reset",
NULL);
1331 if (preaction_op(preaction,
NULL)) {
1332 preaction_op(
"pre_none",
NULL);
1334 " none\n", preaction);
1337 if (preop_op(preop,
NULL)) {
1338 preop_op(
"preop_none",
NULL);
1347 &wdog_panic_notifier);
1352 if (nmi_handler_registered)
1356 &wdog_panic_notifier);
1367 static void __exit ipmi_wdog_exit(
void)
1370 ipmi_unregister_watchdog(watchdog_ifnum);
1373 if (nmi_handler_registered)
1378 &wdog_panic_notifier);