Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / drivers / char / ipmi / ipmi_watchdog.c
index fb84924..2d11ddd 100644 (file)
@@ -39,6 +39,7 @@
 #include <linux/watchdog.h>
 #include <linux/miscdevice.h>
 #include <linux/init.h>
+#include <linux/completion.h>
 #include <linux/rwsem.h>
 #include <linux/errno.h>
 #include <asm/uaccess.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 IPMI_WATCHDOG_VERSION "v32"
+#define        PFX "IPMI Watchdog: "
 
 /*
  * The IPMI command/response information for the watchdog timer.
 #define        WDIOC_GET_PRETIMEOUT     _IOW(WATCHDOG_IOCTL_BASE, 22, int)
 #endif
 
-#ifdef CONFIG_WATCHDOG_NOWAYOUT
-static int nowayout = 1;
-#else
-static int nowayout;
-#endif
+static int nowayout = WATCHDOG_NOWAYOUT;
 
 static ipmi_user_t watchdog_user = NULL;
 
@@ -155,33 +155,127 @@ static char preaction[16] = "pre_none";
 static unsigned char preop_val = WDOG_PREOP_NONE;
 
 static char preop[16] = "preop_none";
-static spinlock_t ipmi_read_lock = SPIN_LOCK_UNLOCKED;
+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              2
+
+static 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;
 
-module_param(timeout, int, 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(&register_sem);
+       *((int *)kp->arg) = l;
+       if (watchdog_user)
+               rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
+       up_read(&register_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(&register_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(&register_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(pretimeout, int, 0);
+
+module_param_call(pretimeout, set_param_int, get_param_int, &pretimeout, 0644);
 MODULE_PARM_DESC(pretimeout, "Pretimeout value in seconds.");
-module_param_string(action, action, sizeof(action), 0);
+
+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_string(preaction, preaction, sizeof(preaction), 0);
+
+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_string(preop, preop, sizeof(preop), 0);
+
+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, 0);
+
+module_param(nowayout, int, 0644);
 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
 
 /* Default state of the timer. */
@@ -191,7 +285,7 @@ static unsigned char ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
 static int ipmi_ignore_heartbeat = 0;
 
 /* Is someone using the watchdog?  Only one user is allowed. */
-static int ipmi_wdog_open = 0;
+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
@@ -203,26 +297,29 @@ static int ipmi_start_timer_on_heartbeat = 0;
 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
+/* We use a mutex 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
+   The mutex 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 DEFINE_MUTEX(set_timeout_lock);
+static DECLARE_COMPLETION(set_timeout_wait);
 static void set_timeout_free_smi(struct ipmi_smi_msg *msg)
 {
     if (atomic_dec_and_test(&set_timeout_tofree))
-           up(&set_timeout_lock);
+           complete(&set_timeout_wait);
 }
 static void set_timeout_free_recv(struct ipmi_recv_msg *msg)
 {
     if (atomic_dec_and_test(&set_timeout_tofree))
-           up(&set_timeout_lock);
+           complete(&set_timeout_wait);
 }
 static struct ipmi_smi_msg set_timeout_smi_msg =
 {
@@ -260,7 +357,7 @@ static int i_ipmi_set_timeout(struct ipmi_smi_msg  *smi_msg,
 
        data[1] = 0;
        WDOG_SET_TIMEOUT_ACT(data[1], ipmi_watchdog_state);
-       if (pretimeout > 0) {
+       if ((pretimeout > 0) && (ipmi_watchdog_state != WDOG_TIMEOUT_NONE)) {
            WDOG_SET_PRETIMEOUT_ACT(data[1], preaction_val);
            data[2] = pretimeout;
        } else {
@@ -287,7 +384,7 @@ static int i_ipmi_set_timeout(struct ipmi_smi_msg  *smi_msg,
                                      recv_msg,
                                      1);
        if (rv) {
-               printk(KERN_WARNING "IPMI Watchdog, set timeout error: %d\n",
+               printk(KERN_WARNING PFX "set timeout error: %d\n",
                       rv);
        }
 
@@ -297,11 +394,6 @@ static int i_ipmi_set_timeout(struct ipmi_smi_msg  *smi_msg,
        return rv;
 }
 
-/* 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)
 {
        int send_heartbeat_now;
@@ -309,7 +401,7 @@ static int ipmi_set_timeout(int do_heartbeat)
 
 
        /* We can only send one of these at a time. */
-       down(&set_timeout_lock);
+       mutex_lock(&set_timeout_lock);
 
        atomic_set(&set_timeout_tofree, 2);
 
@@ -317,16 +409,21 @@ static int ipmi_set_timeout(int do_heartbeat)
                                &set_timeout_recv_msg,
                                &send_heartbeat_now);
        if (rv) {
-               up(&set_timeout_lock);
-       } else {
-               if ((do_heartbeat == IPMI_SET_TIMEOUT_FORCE_HB)
-                   || ((send_heartbeat_now)
-                       && (do_heartbeat == IPMI_SET_TIMEOUT_HB_IF_NECESSARY)))
-               {
-                       rv = ipmi_heartbeat();
-               }
+               mutex_unlock(&set_timeout_lock);
+               goto out;
        }
 
+       wait_for_completion(&set_timeout_wait);
+
+       if ((do_heartbeat == IPMI_SET_TIMEOUT_FORCE_HB)
+           || ((send_heartbeat_now)
+               && (do_heartbeat == IPMI_SET_TIMEOUT_HB_IF_NECESSARY)))
+       {
+               rv = ipmi_heartbeat();
+       }
+       mutex_unlock(&set_timeout_lock);
+
+out:
        return rv;
 }
 
@@ -363,36 +460,22 @@ static void panic_halt_ipmi_set_timeout(void)
        }
 }
 
-/* Do a delayed shutdown, with the delay in milliseconds.  If power_off is
-   false, do a reset.  If power_off is true, do a power down.  This is
-   primarily for the IMB code's shutdown. */
-void ipmi_delayed_shutdown(long delay, int power_off)
-{
-       ipmi_ignore_heartbeat = 1;
-       if (power_off) 
-               ipmi_watchdog_state = WDOG_TIMEOUT_POWER_DOWN;
-       else
-               ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
-       timeout = delay;
-       ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
-}
-
 /* We use a semaphore to make sure that only one thing can send a
    heartbeat 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 heartbeat_tofree = ATOMIC_INIT(0);
-static DECLARE_MUTEX(heartbeat_lock);
-static DECLARE_MUTEX_LOCKED(heartbeat_wait_lock);
+static DEFINE_MUTEX(heartbeat_lock);
+static DECLARE_COMPLETION(heartbeat_wait);
 static void heartbeat_free_smi(struct ipmi_smi_msg *msg)
 {
     if (atomic_dec_and_test(&heartbeat_tofree))
-           up(&heartbeat_wait_lock);
+           complete(&heartbeat_wait);
 }
 static void heartbeat_free_recv(struct ipmi_recv_msg *msg)
 {
     if (atomic_dec_and_test(&heartbeat_tofree))
-           up(&heartbeat_wait_lock);
+           complete(&heartbeat_wait);
 }
 static struct ipmi_smi_msg heartbeat_smi_msg =
 {
@@ -435,14 +518,14 @@ static int ipmi_heartbeat(void)
                return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
        }
 
-       down(&heartbeat_lock);
+       mutex_lock(&heartbeat_lock);
 
        atomic_set(&heartbeat_tofree, 2);
 
        /* Don't reset the timer if we have the timer turned off, that
            re-enables the watchdog. */
        if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE) {
-               up(&heartbeat_lock);
+               mutex_unlock(&heartbeat_lock);
                return 0;
        }
 
@@ -463,14 +546,14 @@ static int ipmi_heartbeat(void)
                                      &heartbeat_recv_msg,
                                      1);
        if (rv) {
-               up(&heartbeat_lock);
-               printk(KERN_WARNING "IPMI Watchdog, heartbeat failure: %d\n",
+               mutex_unlock(&heartbeat_lock);
+               printk(KERN_WARNING PFX "heartbeat failure: %d\n",
                       rv);
                return rv;
        }
 
        /* Wait for the heartbeat to be sent. */
-       down(&heartbeat_wait_lock);
+       wait_for_completion(&heartbeat_wait);
 
        if (heartbeat_recv_msg.msg.data[0] != 0) {
            /* Got an error in the heartbeat response.  It was already
@@ -479,7 +562,7 @@ static int ipmi_heartbeat(void)
            rv = -EINVAL;
        }
 
-       up(&heartbeat_lock);
+       mutex_unlock(&heartbeat_lock);
 
        return rv;
 }
@@ -513,11 +596,11 @@ static void panic_halt_ipmi_heartbeat(void)
                                 1);
 }
 
-static struct watchdog_info ident=
+static struct watchdog_info ident =
 {
-       0, /* WDIOF_SETTIMEOUT, */
-       1,
-       "IPMI"
+       .options        = 0,    /* WDIOF_SETTIMEOUT, */
+       .firmware_version = 1,
+       .identity       = "IPMI"
 };
 
 static int ipmi_ioctl(struct inode *inode, struct file *file,
@@ -599,6 +682,21 @@ static ssize_t ipmi_write(struct file *file,
        int rv;
 
        if (len) {
+               if (!nowayout) {
+                       size_t i;
+
+                       /* In case it was set long ago */
+                       expect_close = 0;
+
+                       for (i = 0; i != len; i++) {
+                               char c;
+
+                               if (get_user(c, buf + i))
+                                       return -EFAULT;
+                               if (c == 'V')
+                                       expect_close = 42;
+                       }
+               }
                rv = ipmi_heartbeat();
                if (rv)
                        return rv;
@@ -659,21 +757,18 @@ static ssize_t ipmi_read(struct file *file,
 
 static int ipmi_open(struct inode *ino, struct file *filep)
 {
-        switch (iminor(ino))
-        {
-                case WATCHDOG_MINOR:
-                    if (ipmi_wdog_open)
+        switch (iminor(ino)) {
+        case WATCHDOG_MINOR:
+               if (test_and_set_bit(0, &ipmi_wdog_open))
                         return -EBUSY;
 
-                    ipmi_wdog_open = 1;
-
-                   /* Don't start the timer now, let it start on the
-                      first heartbeat. */
-                   ipmi_start_timer_on_heartbeat = 1;
-                    return nonseekable_open(ino, filep);
+               /* Don't start the timer now, let it start on the
+                  first heartbeat. */
+               ipmi_start_timer_on_heartbeat = 1;
+               return nonseekable_open(ino, filep);
 
-                default:
-                    return (-ENODEV);
+       default:
+               return (-ENODEV);
         }
 }
 
@@ -702,16 +797,20 @@ static int ipmi_fasync(int fd, struct file *file, int on)
 
 static int ipmi_close(struct inode *ino, struct file *filep)
 {
-       if (iminor(ino)==WATCHDOG_MINOR)
-       {
-               if (!nowayout) {
+       if (iminor(ino) == WATCHDOG_MINOR) {
+               if (expect_close == 42) {
                        ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
                        ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
+               } else {
+                       printk(KERN_CRIT PFX
+                              "Unexpected close, not stopping watchdog!\n");
+                       ipmi_heartbeat();
                }
-               ipmi_wdog_open = 0;
+               clear_bit(0, &ipmi_wdog_open);
        }
 
        ipmi_fasync (-1, filep, 0);
+       expect_close = 0;
 
        return 0;
 }
@@ -728,18 +827,16 @@ static struct file_operations ipmi_wdog_fops = {
 };
 
 static struct miscdevice ipmi_wdog_miscdev = {
-       WATCHDOG_MINOR,
-       "watchdog",
-       &ipmi_wdog_fops
+       .minor          = WATCHDOG_MINOR,
+       .name           = "watchdog",
+       .fops           = &ipmi_wdog_fops
 };
 
-static DECLARE_RWSEM(register_sem);
-
 static void ipmi_wdog_msg_handler(struct ipmi_recv_msg *msg,
                                  void                 *handler_data)
 {
        if (msg->msg.data[0] != 0) {
-               printk(KERN_ERR "IPMI Watchdog response: Error %x on cmd %x\n",
+               printk(KERN_ERR PFX "response: Error %x on cmd %x\n",
                       msg->msg.data[0],
                       msg->msg.cmd);
        }
@@ -750,9 +847,10 @@ static void ipmi_wdog_msg_handler(struct ipmi_recv_msg *msg,
 static void ipmi_wdog_pretimeout_handler(void *handler_data)
 {
        if (preaction_val != WDOG_PRETIMEOUT_NONE) {
-               if (preop_val == WDOG_PREOP_PANIC)
-                       panic("Watchdog pre-timeout");
-               else if (preop_val == WDOG_PREOP_GIVE_DATA) {
+               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) {
                        spin_lock(&ipmi_read_lock);
                        data_to_read = 1;
                        wake_up_interruptible(&read_q);
@@ -784,7 +882,7 @@ static void ipmi_register_watchdog(int ipmi_intf)
 
        rv = ipmi_create_user(ipmi_intf, &ipmi_hndlrs, NULL, &watchdog_user);
        if (rv < 0) {
-               printk("IPMI watchdog: Unable to register with ipmi\n");
+               printk(KERN_CRIT PFX "Unable to register with ipmi\n");
                goto out;
        }
 
@@ -796,7 +894,7 @@ static void ipmi_register_watchdog(int ipmi_intf)
        if (rv < 0) {
                ipmi_destroy_user(watchdog_user);
                watchdog_user = NULL;
-               printk("IPMI watchdog: Unable to register misc device\n");
+               printk(KERN_CRIT PFX "Unable to register misc device\n");
        }
 
  out:
@@ -807,7 +905,7 @@ static void ipmi_register_watchdog(int ipmi_intf)
                start_now = 0; /* Disable this function after first startup. */
                ipmi_watchdog_state = action_val;
                ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
-               printk("Starting IPMI Watchdog now!\n");
+               printk(KERN_INFO PFX "Starting now!\n");
        }
 }
 
@@ -815,15 +913,20 @@ static void ipmi_register_watchdog(int ipmi_intf)
 static int
 ipmi_nmi(void *dev_id, struct pt_regs *regs, int cpu, int handled)
 {
+        /* If we are not expecting a timeout, ignore it. */
+       if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)
+               return NOTIFY_DONE;
+
        /* If no one else handled the NMI, we assume it was the IPMI
            watchdog. */
-       if ((!handled) && (preop_val == WDOG_PREOP_PANIC))
-               panic("IPMI watchdog pre-timeout");
-
-       /* On some machines, the heartbeat will give
-          an error and not work unless we re-enable
-          the timer.   So do so. */
-       pretimeout_since_last_heartbeat = 1;
+       if ((!handled) && (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. */
+               pretimeout_since_last_heartbeat = 1;
+               if (atomic_inc_and_test(&preop_panic_excl))
+                       panic(PFX "pre-timeout");
+       }
 
        return NOTIFY_DONE;
 }
@@ -836,6 +939,7 @@ static struct nmi_handler ipmi_nmi_handler =
        .handler  = ipmi_nmi,
        .priority = 0, /* Call us last. */
 };
+int nmi_handler_registered;
 #endif
 
 static int wdog_reboot_handler(struct notifier_block *this,
@@ -865,13 +969,11 @@ static int wdog_reboot_handler(struct notifier_block *this,
 }
 
 static struct notifier_block wdog_reboot_notifier = {
-       wdog_reboot_handler,
-       NULL,
-       0
+       .notifier_call  = wdog_reboot_handler,
+       .next           = NULL,
+       .priority       = 0
 };
 
-extern int panic_timeout; /* Why isn't this defined anywhere? */
-
 static int wdog_panic_handler(struct notifier_block *this,
                              unsigned long         event,
                              void                  *unused)
@@ -895,13 +997,13 @@ static int wdog_panic_handler(struct notifier_block *this,
 }
 
 static struct notifier_block wdog_panic_notifier = {
-       wdog_panic_handler,
-       NULL,
-       150   /* priority: INT_MAX >= x >= 0 */
+       .notifier_call  = wdog_panic_handler,
+       .next           = NULL,
+       .priority       = 150   /* priority: INT_MAX >= x >= 0 */
 };
 
 
-static void ipmi_new_smi(int if_num)
+static void ipmi_new_smi(int if_num, struct device *device)
 {
        ipmi_register_watchdog(if_num);
 }
@@ -920,86 +1022,137 @@ static struct ipmi_smi_watcher smi_watcher =
        .smi_gone = ipmi_smi_gone
 };
 
-static int __init ipmi_wdog_init(void)
+static int action_op(const char *inval, char *outval)
 {
-       int rv;
+       if (outval)
+               strcpy(outval, action);
 
-       printk(KERN_INFO "IPMI watchdog driver version "
-              IPMI_WATCHDOG_VERSION "\n");
+       if (!inval)
+               return 0;
 
-       if (strcmp(action, "reset") == 0) {
+       if (strcmp(inval, "reset") == 0)
                action_val = WDOG_TIMEOUT_RESET;
-       } else if (strcmp(action, "none") == 0) {
+       else if (strcmp(inval, "none") == 0)
                action_val = WDOG_TIMEOUT_NONE;
-       } else if (strcmp(action, "power_cycle") == 0) {
+       else if (strcmp(inval, "power_cycle") == 0)
                action_val = WDOG_TIMEOUT_POWER_CYCLE;
-       } else if (strcmp(action, "power_off") == 0) {
+       else if (strcmp(inval, "power_off") == 0)
                action_val = WDOG_TIMEOUT_POWER_DOWN;
-       } else {
-               action_val = WDOG_TIMEOUT_RESET;
-               printk("ipmi_watchdog: Unknown action '%s', defaulting to"
-                      " reset\n", action);
-       }
+       else
+               return -EINVAL;
+       strcpy(action, inval);
+       return 0;
+}
+
+static int preaction_op(const char *inval, char *outval)
+{
+       if (outval)
+               strcpy(outval, preaction);
 
-       if (strcmp(preaction, "pre_none") == 0) {
+       if (!inval)
+               return 0;
+
+       if (strcmp(inval, "pre_none") == 0)
                preaction_val = WDOG_PRETIMEOUT_NONE;
-       } else if (strcmp(preaction, "pre_smi") == 0) {
+       else if (strcmp(inval, "pre_smi") == 0)
                preaction_val = WDOG_PRETIMEOUT_SMI;
 #ifdef HAVE_NMI_HANDLER
-       } else if (strcmp(preaction, "pre_nmi") == 0) {
+       else if (strcmp(inval, "pre_nmi") == 0)
                preaction_val = WDOG_PRETIMEOUT_NMI;
 #endif
-       } else if (strcmp(preaction, "pre_int") == 0) {
+       else if (strcmp(inval, "pre_int") == 0)
                preaction_val = WDOG_PRETIMEOUT_MSG_INT;
-       } else {
-               preaction_val = WDOG_PRETIMEOUT_NONE;
-               printk("ipmi_watchdog: Unknown preaction '%s', defaulting to"
-                      " none\n", preaction);
-       }
+       else
+               return -EINVAL;
+       strcpy(preaction, inval);
+       return 0;
+}
 
-       if (strcmp(preop, "preop_none") == 0) {
+static int preop_op(const char *inval, char *outval)
+{
+       if (outval)
+               strcpy(outval, preop);
+
+       if (!inval)
+               return 0;
+
+       if (strcmp(inval, "preop_none") == 0)
                preop_val = WDOG_PREOP_NONE;
-       } else if (strcmp(preop, "preop_panic") == 0) {
+       else if (strcmp(inval, "preop_panic") == 0)
                preop_val = WDOG_PREOP_PANIC;
-       } else if (strcmp(preop, "preop_give_data") == 0) {
+       else if (strcmp(inval, "preop_give_data") == 0)
                preop_val = WDOG_PREOP_GIVE_DATA;
-       } else {
-               preop_val = WDOG_PREOP_NONE;
-               printk("ipmi_watchdog: Unknown preop '%s', defaulting to"
-                      " none\n", preop);
-       }
+       else
+               return -EINVAL;
+       strcpy(preop, inval);
+       return 0;
+}
 
+static void check_parms(void)
+{
 #ifdef HAVE_NMI_HANDLER
+       int do_nmi = 0;
+       int rv;
+
        if (preaction_val == WDOG_PRETIMEOUT_NMI) {
+               do_nmi = 1;
                if (preop_val == WDOG_PREOP_GIVE_DATA) {
-                       printk(KERN_WARNING
-                              "ipmi_watchdog: Pretimeout op is to give data"
+                       printk(KERN_WARNING PFX "Pretimeout op is to give data"
                               " but NMI pretimeout is enabled, setting"
                               " pretimeout op to none\n");
-                       preop_val = WDOG_PREOP_NONE;
+                       preop_op("preop_none", NULL);
+                       do_nmi = 0;
                }
 #ifdef CONFIG_X86_LOCAL_APIC
                if (nmi_watchdog == NMI_IO_APIC) {
-                       printk(KERN_WARNING
-                              "ipmi_watchdog: nmi_watchdog is set to IO APIC"
+                       printk(KERN_WARNING PFX "nmi_watchdog is set to IO APIC"
                               " mode (value is %d), that is incompatible"
                               " with using NMI in the IPMI watchdog."
                               " Disabling IPMI nmi pretimeout.\n",
                               nmi_watchdog);
                        preaction_val = WDOG_PRETIMEOUT_NONE;
-               } else {
+                       do_nmi = 0;
+               }
 #endif
+       }
+       if (do_nmi && !nmi_handler_registered) {
                rv = request_nmi(&ipmi_nmi_handler);
                if (rv) {
-                       printk(KERN_WARNING
-                              "ipmi_watchdog: Can't register nmi handler\n");
-                       return rv;
-               }
-#ifdef CONFIG_X86_LOCAL_APIC
-               }
-#endif
+                       printk(KERN_WARNING PFX
+                              "Can't register nmi handler\n");
+                       return;
+               } else
+                       nmi_handler_registered = 1;
+       } else if (!do_nmi && nmi_handler_registered) {
+               release_nmi(&ipmi_nmi_handler);
+               nmi_handler_registered = 0;
        }
 #endif
+}
+
+static int __init ipmi_wdog_init(void)
+{
+       int rv;
+
+       if (action_op(action, NULL)) {
+               action_op("reset", NULL);
+               printk(KERN_INFO PFX "Unknown action '%s', defaulting to"
+                      " reset\n", action);
+       }
+
+       if (preaction_op(preaction, NULL)) {
+               preaction_op("pre_none", NULL);
+               printk(KERN_INFO PFX "Unknown preaction '%s', defaulting to"
+                      " none\n", preaction);
+       }
+
+       if (preop_op(preop, NULL)) {
+               preop_op("preop_none", NULL);
+               printk(KERN_INFO PFX "Unknown preop '%s', defaulting to"
+                      " none\n", preop);
+       }
+
+       check_parms();
 
        rv = ipmi_smi_watcher_register(&smi_watcher);
        if (rv) {
@@ -1007,13 +1160,15 @@ static int __init ipmi_wdog_init(void)
                if (preaction_val == WDOG_PRETIMEOUT_NMI)
                        release_nmi(&ipmi_nmi_handler);
 #endif
-               printk(KERN_WARNING
-                      "ipmi_watchdog: can't register smi watcher\n");
+               printk(KERN_WARNING PFX "can't register smi watcher\n");
                return rv;
        }
 
        register_reboot_notifier(&wdog_reboot_notifier);
-       notifier_chain_register(&panic_notifier_list, &wdog_panic_notifier);
+       atomic_notifier_chain_register(&panic_notifier_list,
+                       &wdog_panic_notifier);
+
+       printk(KERN_INFO PFX "driver initialized\n");
 
        return 0;
 }
@@ -1025,11 +1180,12 @@ static __exit void ipmi_unregister_watchdog(void)
        down_write(&register_sem);
 
 #ifdef HAVE_NMI_HANDLER
-       if (preaction_val == WDOG_PRETIMEOUT_NMI)
+       if (nmi_handler_registered)
                release_nmi(&ipmi_nmi_handler);
 #endif
 
-       notifier_chain_unregister(&panic_notifier_list, &wdog_panic_notifier);
+       atomic_notifier_chain_unregister(&panic_notifier_list,
+                       &wdog_panic_notifier);
        unregister_reboot_notifier(&wdog_reboot_notifier);
 
        if (! watchdog_user)
@@ -1038,23 +1194,16 @@ static __exit void ipmi_unregister_watchdog(void)
        /* Make sure no one can call us any more. */
        misc_deregister(&ipmi_wdog_miscdev);
 
-       /*  Disable the timer. */
-       ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
-       ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
-
        /* 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(&set_timeout_tofree)) {
-               set_current_state(TASK_UNINTERRUPTIBLE);
-               schedule_timeout(1);
-       }
+       while (atomic_read(&set_timeout_tofree))
+               schedule_timeout_uninterruptible(1);
 
        /* Disconnect from IPMI. */
        rv = ipmi_destroy_user(watchdog_user);
        if (rv) {
-               printk(KERN_WARNING
-                      "IPMI Watchdog, error unlinking from IPMI: %d\n",
+               printk(KERN_WARNING PFX "error unlinking from IPMI: %d\n",
                       rv);
        }
        watchdog_user = NULL;
@@ -1069,8 +1218,7 @@ static void __exit ipmi_wdog_exit(void)
        ipmi_unregister_watchdog();
 }
 module_exit(ipmi_wdog_exit);
-
-EXPORT_SYMBOL(ipmi_delayed_shutdown);
-
 module_init(ipmi_wdog_init);
 MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
+MODULE_DESCRIPTION("watchdog timer based upon the IPMI interface.");