Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * ipmi_watchdog.c
0004  *
0005  * A watchdog timer based upon the IPMI interface.
0006  *
0007  * Author: MontaVista Software, Inc.
0008  *         Corey Minyard <minyard@mvista.com>
0009  *         source@mvista.com
0010  *
0011  * Copyright 2002 MontaVista Software Inc.
0012  */
0013 
0014 #define pr_fmt(fmt) "IPMI Watchdog: " fmt
0015 
0016 #include <linux/module.h>
0017 #include <linux/moduleparam.h>
0018 #include <linux/ipmi.h>
0019 #include <linux/ipmi_smi.h>
0020 #include <linux/mutex.h>
0021 #include <linux/watchdog.h>
0022 #include <linux/miscdevice.h>
0023 #include <linux/init.h>
0024 #include <linux/completion.h>
0025 #include <linux/kdebug.h>
0026 #include <linux/rwsem.h>
0027 #include <linux/errno.h>
0028 #include <linux/uaccess.h>
0029 #include <linux/notifier.h>
0030 #include <linux/nmi.h>
0031 #include <linux/reboot.h>
0032 #include <linux/wait.h>
0033 #include <linux/poll.h>
0034 #include <linux/string.h>
0035 #include <linux/ctype.h>
0036 #include <linux/delay.h>
0037 #include <linux/atomic.h>
0038 #include <linux/sched/signal.h>
0039 
0040 #ifdef CONFIG_X86
0041 /*
0042  * This is ugly, but I've determined that x86 is the only architecture
0043  * that can reasonably support the IPMI NMI watchdog timeout at this
0044  * time.  If another architecture adds this capability somehow, it
0045  * will have to be a somewhat different mechanism and I have no idea
0046  * how it will work.  So in the unlikely event that another
0047  * architecture supports this, we can figure out a good generic
0048  * mechanism for it at that time.
0049  */
0050 #include <asm/kdebug.h>
0051 #include <asm/nmi.h>
0052 #define HAVE_DIE_NMI
0053 #endif
0054 
0055 /*
0056  * The IPMI command/response information for the watchdog timer.
0057  */
0058 
0059 /* values for byte 1 of the set command, byte 2 of the get response. */
0060 #define WDOG_DONT_LOG       (1 << 7)
0061 #define WDOG_DONT_STOP_ON_SET   (1 << 6)
0062 #define WDOG_SET_TIMER_USE(byte, use) \
0063     byte = ((byte) & 0xf8) | ((use) & 0x7)
0064 #define WDOG_GET_TIMER_USE(byte) ((byte) & 0x7)
0065 #define WDOG_TIMER_USE_BIOS_FRB2    1
0066 #define WDOG_TIMER_USE_BIOS_POST    2
0067 #define WDOG_TIMER_USE_OS_LOAD      3
0068 #define WDOG_TIMER_USE_SMS_OS       4
0069 #define WDOG_TIMER_USE_OEM      5
0070 
0071 /* values for byte 2 of the set command, byte 3 of the get response. */
0072 #define WDOG_SET_PRETIMEOUT_ACT(byte, use) \
0073     byte = ((byte) & 0x8f) | (((use) & 0x7) << 4)
0074 #define WDOG_GET_PRETIMEOUT_ACT(byte) (((byte) >> 4) & 0x7)
0075 #define WDOG_PRETIMEOUT_NONE        0
0076 #define WDOG_PRETIMEOUT_SMI     1
0077 #define WDOG_PRETIMEOUT_NMI     2
0078 #define WDOG_PRETIMEOUT_MSG_INT     3
0079 
0080 /* Operations that can be performed on a pretimout. */
0081 #define WDOG_PREOP_NONE     0
0082 #define WDOG_PREOP_PANIC    1
0083 /* Cause data to be available to read.  Doesn't work in NMI mode. */
0084 #define WDOG_PREOP_GIVE_DATA    2
0085 
0086 /* Actions to perform on a full timeout. */
0087 #define WDOG_SET_TIMEOUT_ACT(byte, use) \
0088     byte = ((byte) & 0xf8) | ((use) & 0x7)
0089 #define WDOG_GET_TIMEOUT_ACT(byte) ((byte) & 0x7)
0090 #define WDOG_TIMEOUT_NONE       0
0091 #define WDOG_TIMEOUT_RESET      1
0092 #define WDOG_TIMEOUT_POWER_DOWN     2
0093 #define WDOG_TIMEOUT_POWER_CYCLE    3
0094 
0095 /*
0096  * Byte 3 of the get command, byte 4 of the get response is the
0097  * pre-timeout in seconds.
0098  */
0099 
0100 /* Bits for setting byte 4 of the set command, byte 5 of the get response. */
0101 #define WDOG_EXPIRE_CLEAR_BIOS_FRB2 (1 << 1)
0102 #define WDOG_EXPIRE_CLEAR_BIOS_POST (1 << 2)
0103 #define WDOG_EXPIRE_CLEAR_OS_LOAD   (1 << 3)
0104 #define WDOG_EXPIRE_CLEAR_SMS_OS    (1 << 4)
0105 #define WDOG_EXPIRE_CLEAR_OEM       (1 << 5)
0106 
0107 /*
0108  * Setting/getting the watchdog timer value.  This is for bytes 5 and
0109  * 6 (the timeout time) of the set command, and bytes 6 and 7 (the
0110  * timeout time) and 8 and 9 (the current countdown value) of the
0111  * response.  The timeout value is given in seconds (in the command it
0112  * is 100ms intervals).
0113  */
0114 #define WDOG_SET_TIMEOUT(byte1, byte2, val) \
0115     (byte1) = (((val) * 10) & 0xff), (byte2) = (((val) * 10) >> 8)
0116 #define WDOG_GET_TIMEOUT(byte1, byte2) \
0117     (((byte1) | ((byte2) << 8)) / 10)
0118 
0119 #define IPMI_WDOG_RESET_TIMER       0x22
0120 #define IPMI_WDOG_SET_TIMER     0x24
0121 #define IPMI_WDOG_GET_TIMER     0x25
0122 
0123 #define IPMI_WDOG_TIMER_NOT_INIT_RESP   0x80
0124 
0125 static DEFINE_MUTEX(ipmi_watchdog_mutex);
0126 static bool nowayout = WATCHDOG_NOWAYOUT;
0127 
0128 static struct ipmi_user *watchdog_user;
0129 static int watchdog_ifnum;
0130 
0131 /* Default the timeout to 10 seconds. */
0132 static int timeout = 10;
0133 
0134 /* The pre-timeout is disabled by default. */
0135 static int pretimeout;
0136 
0137 /* Default timeout to set on panic */
0138 static int panic_wdt_timeout = 255;
0139 
0140 /* Default action is to reset the board on a timeout. */
0141 static unsigned char action_val = WDOG_TIMEOUT_RESET;
0142 
0143 static char action[16] = "reset";
0144 
0145 static unsigned char preaction_val = WDOG_PRETIMEOUT_NONE;
0146 
0147 static char preaction[16] = "pre_none";
0148 
0149 static unsigned char preop_val = WDOG_PREOP_NONE;
0150 
0151 static char preop[16] = "preop_none";
0152 static DEFINE_SPINLOCK(ipmi_read_lock);
0153 static char data_to_read;
0154 static DECLARE_WAIT_QUEUE_HEAD(read_q);
0155 static struct fasync_struct *fasync_q;
0156 static atomic_t pretimeout_since_last_heartbeat;
0157 static char expect_close;
0158 
0159 static int ifnum_to_use = -1;
0160 
0161 /* Parameters to ipmi_set_timeout */
0162 #define IPMI_SET_TIMEOUT_NO_HB          0
0163 #define IPMI_SET_TIMEOUT_HB_IF_NECESSARY    1
0164 #define IPMI_SET_TIMEOUT_FORCE_HB       2
0165 
0166 static int ipmi_set_timeout(int do_heartbeat);
0167 static void ipmi_register_watchdog(int ipmi_intf);
0168 static void ipmi_unregister_watchdog(int ipmi_intf);
0169 
0170 /*
0171  * If true, the driver will start running as soon as it is configured
0172  * and ready.
0173  */
0174 static int start_now;
0175 
0176 static int set_param_timeout(const char *val, const struct kernel_param *kp)
0177 {
0178     char *endp;
0179     int  l;
0180     int  rv = 0;
0181 
0182     if (!val)
0183         return -EINVAL;
0184     l = simple_strtoul(val, &endp, 0);
0185     if (endp == val)
0186         return -EINVAL;
0187 
0188     *((int *)kp->arg) = l;
0189     if (watchdog_user)
0190         rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
0191 
0192     return rv;
0193 }
0194 
0195 static const struct kernel_param_ops param_ops_timeout = {
0196     .set = set_param_timeout,
0197     .get = param_get_int,
0198 };
0199 #define param_check_timeout param_check_int
0200 
0201 typedef int (*action_fn)(const char *intval, char *outval);
0202 
0203 static int action_op(const char *inval, char *outval);
0204 static int preaction_op(const char *inval, char *outval);
0205 static int preop_op(const char *inval, char *outval);
0206 static void check_parms(void);
0207 
0208 static int set_param_str(const char *val, const struct kernel_param *kp)
0209 {
0210     action_fn  fn = (action_fn) kp->arg;
0211     int        rv = 0;
0212     char       valcp[16];
0213     char       *s;
0214 
0215     strncpy(valcp, val, 15);
0216     valcp[15] = '\0';
0217 
0218     s = strstrip(valcp);
0219 
0220     rv = fn(s, NULL);
0221     if (rv)
0222         goto out;
0223 
0224     check_parms();
0225     if (watchdog_user)
0226         rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
0227 
0228  out:
0229     return rv;
0230 }
0231 
0232 static int get_param_str(char *buffer, const struct kernel_param *kp)
0233 {
0234     action_fn fn = (action_fn) kp->arg;
0235     int rv, len;
0236 
0237     rv = fn(NULL, buffer);
0238     if (rv)
0239         return rv;
0240 
0241     len = strlen(buffer);
0242     buffer[len++] = '\n';
0243     buffer[len] = 0;
0244 
0245     return len;
0246 }
0247 
0248 
0249 static int set_param_wdog_ifnum(const char *val, const struct kernel_param *kp)
0250 {
0251     int rv = param_set_int(val, kp);
0252     if (rv)
0253         return rv;
0254     if ((ifnum_to_use < 0) || (ifnum_to_use == watchdog_ifnum))
0255         return 0;
0256 
0257     ipmi_unregister_watchdog(watchdog_ifnum);
0258     ipmi_register_watchdog(ifnum_to_use);
0259     return 0;
0260 }
0261 
0262 static const struct kernel_param_ops param_ops_wdog_ifnum = {
0263     .set = set_param_wdog_ifnum,
0264     .get = param_get_int,
0265 };
0266 
0267 #define param_check_wdog_ifnum param_check_int
0268 
0269 static const struct kernel_param_ops param_ops_str = {
0270     .set = set_param_str,
0271     .get = get_param_str,
0272 };
0273 
0274 module_param(ifnum_to_use, wdog_ifnum, 0644);
0275 MODULE_PARM_DESC(ifnum_to_use, "The interface number to use for the watchdog "
0276          "timer.  Setting to -1 defaults to the first registered "
0277          "interface");
0278 
0279 module_param(timeout, timeout, 0644);
0280 MODULE_PARM_DESC(timeout, "Timeout value in seconds.");
0281 
0282 module_param(pretimeout, timeout, 0644);
0283 MODULE_PARM_DESC(pretimeout, "Pretimeout value in seconds.");
0284 
0285 module_param(panic_wdt_timeout, timeout, 0644);
0286 MODULE_PARM_DESC(panic_wdt_timeout, "Timeout value on kernel panic in seconds.");
0287 
0288 module_param_cb(action, &param_ops_str, action_op, 0644);
0289 MODULE_PARM_DESC(action, "Timeout action. One of: "
0290          "reset, none, power_cycle, power_off.");
0291 
0292 module_param_cb(preaction, &param_ops_str, preaction_op, 0644);
0293 MODULE_PARM_DESC(preaction, "Pretimeout action.  One of: "
0294          "pre_none, pre_smi, pre_nmi, pre_int.");
0295 
0296 module_param_cb(preop, &param_ops_str, preop_op, 0644);
0297 MODULE_PARM_DESC(preop, "Pretimeout driver operation.  One of: "
0298          "preop_none, preop_panic, preop_give_data.");
0299 
0300 module_param(start_now, int, 0444);
0301 MODULE_PARM_DESC(start_now, "Set to 1 to start the watchdog as"
0302          "soon as the driver is loaded.");
0303 
0304 module_param(nowayout, bool, 0644);
0305 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
0306          "(default=CONFIG_WATCHDOG_NOWAYOUT)");
0307 
0308 /* Default state of the timer. */
0309 static unsigned char ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
0310 
0311 /* Is someone using the watchdog?  Only one user is allowed. */
0312 static unsigned long ipmi_wdog_open;
0313 
0314 /*
0315  * If set to 1, the heartbeat command will set the state to reset and
0316  * start the timer.  The timer doesn't normally run when the driver is
0317  * first opened until the heartbeat is set the first time, this
0318  * variable is used to accomplish this.
0319  */
0320 static int ipmi_start_timer_on_heartbeat;
0321 
0322 /* IPMI version of the BMC. */
0323 static unsigned char ipmi_version_major;
0324 static unsigned char ipmi_version_minor;
0325 
0326 /* If a pretimeout occurs, this is used to allow only one panic to happen. */
0327 static atomic_t preop_panic_excl = ATOMIC_INIT(-1);
0328 
0329 #ifdef HAVE_DIE_NMI
0330 static int testing_nmi;
0331 static int nmi_handler_registered;
0332 #endif
0333 
0334 static int __ipmi_heartbeat(void);
0335 
0336 /*
0337  * We use a mutex to make sure that only one thing can send a set a
0338  * message at one time.  The mutex is claimed when a message is sent
0339  * and freed when both the send and receive messages are free.
0340  */
0341 static atomic_t msg_tofree = ATOMIC_INIT(0);
0342 static DECLARE_COMPLETION(msg_wait);
0343 static void msg_free_smi(struct ipmi_smi_msg *msg)
0344 {
0345     if (atomic_dec_and_test(&msg_tofree)) {
0346         if (!oops_in_progress)
0347             complete(&msg_wait);
0348     }
0349 }
0350 static void msg_free_recv(struct ipmi_recv_msg *msg)
0351 {
0352     if (atomic_dec_and_test(&msg_tofree)) {
0353         if (!oops_in_progress)
0354             complete(&msg_wait);
0355     }
0356 }
0357 static struct ipmi_smi_msg smi_msg = INIT_IPMI_SMI_MSG(msg_free_smi);
0358 static struct ipmi_recv_msg recv_msg = INIT_IPMI_RECV_MSG(msg_free_recv);
0359 
0360 static int __ipmi_set_timeout(struct ipmi_smi_msg  *smi_msg,
0361                   struct ipmi_recv_msg *recv_msg,
0362                   int                  *send_heartbeat_now)
0363 {
0364     struct kernel_ipmi_msg            msg;
0365     unsigned char                     data[6];
0366     int                               rv;
0367     struct ipmi_system_interface_addr addr;
0368     int                               hbnow = 0;
0369 
0370 
0371     data[0] = 0;
0372     WDOG_SET_TIMER_USE(data[0], WDOG_TIMER_USE_SMS_OS);
0373 
0374     if (ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
0375         if ((ipmi_version_major > 1) ||
0376             ((ipmi_version_major == 1) && (ipmi_version_minor >= 5))) {
0377             /* This is an IPMI 1.5-only feature. */
0378             data[0] |= WDOG_DONT_STOP_ON_SET;
0379         } else {
0380             /*
0381              * In ipmi 1.0, setting the timer stops the watchdog, we
0382              * need to start it back up again.
0383              */
0384             hbnow = 1;
0385         }
0386     }
0387 
0388     data[1] = 0;
0389     WDOG_SET_TIMEOUT_ACT(data[1], ipmi_watchdog_state);
0390     if ((pretimeout > 0) && (ipmi_watchdog_state != WDOG_TIMEOUT_NONE)) {
0391         WDOG_SET_PRETIMEOUT_ACT(data[1], preaction_val);
0392         data[2] = pretimeout;
0393     } else {
0394         WDOG_SET_PRETIMEOUT_ACT(data[1], WDOG_PRETIMEOUT_NONE);
0395         data[2] = 0; /* No pretimeout. */
0396     }
0397     data[3] = 0;
0398     WDOG_SET_TIMEOUT(data[4], data[5], timeout);
0399 
0400     addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
0401     addr.channel = IPMI_BMC_CHANNEL;
0402     addr.lun = 0;
0403 
0404     msg.netfn = 0x06;
0405     msg.cmd = IPMI_WDOG_SET_TIMER;
0406     msg.data = data;
0407     msg.data_len = sizeof(data);
0408     rv = ipmi_request_supply_msgs(watchdog_user,
0409                       (struct ipmi_addr *) &addr,
0410                       0,
0411                       &msg,
0412                       NULL,
0413                       smi_msg,
0414                       recv_msg,
0415                       1);
0416     if (rv)
0417         pr_warn("set timeout error: %d\n", rv);
0418     else if (send_heartbeat_now)
0419         *send_heartbeat_now = hbnow;
0420 
0421     return rv;
0422 }
0423 
0424 static int _ipmi_set_timeout(int do_heartbeat)
0425 {
0426     int send_heartbeat_now;
0427     int rv;
0428 
0429     if (!watchdog_user)
0430         return -ENODEV;
0431 
0432     atomic_set(&msg_tofree, 2);
0433 
0434     rv = __ipmi_set_timeout(&smi_msg,
0435                 &recv_msg,
0436                 &send_heartbeat_now);
0437     if (rv) {
0438         atomic_set(&msg_tofree, 0);
0439         return rv;
0440     }
0441 
0442     wait_for_completion(&msg_wait);
0443 
0444     if ((do_heartbeat == IPMI_SET_TIMEOUT_FORCE_HB)
0445         || ((send_heartbeat_now)
0446             && (do_heartbeat == IPMI_SET_TIMEOUT_HB_IF_NECESSARY)))
0447         rv = __ipmi_heartbeat();
0448 
0449     return rv;
0450 }
0451 
0452 static int ipmi_set_timeout(int do_heartbeat)
0453 {
0454     int rv;
0455 
0456     mutex_lock(&ipmi_watchdog_mutex);
0457     rv = _ipmi_set_timeout(do_heartbeat);
0458     mutex_unlock(&ipmi_watchdog_mutex);
0459 
0460     return rv;
0461 }
0462 
0463 static atomic_t panic_done_count = ATOMIC_INIT(0);
0464 
0465 static void panic_smi_free(struct ipmi_smi_msg *msg)
0466 {
0467     atomic_dec(&panic_done_count);
0468 }
0469 static void panic_recv_free(struct ipmi_recv_msg *msg)
0470 {
0471     atomic_dec(&panic_done_count);
0472 }
0473 
0474 static struct ipmi_smi_msg panic_halt_heartbeat_smi_msg =
0475     INIT_IPMI_SMI_MSG(panic_smi_free);
0476 static struct ipmi_recv_msg panic_halt_heartbeat_recv_msg =
0477     INIT_IPMI_RECV_MSG(panic_recv_free);
0478 
0479 static void panic_halt_ipmi_heartbeat(void)
0480 {
0481     struct kernel_ipmi_msg             msg;
0482     struct ipmi_system_interface_addr addr;
0483     int rv;
0484 
0485     /*
0486      * Don't reset the timer if we have the timer turned off, that
0487      * re-enables the watchdog.
0488      */
0489     if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)
0490         return;
0491 
0492     addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
0493     addr.channel = IPMI_BMC_CHANNEL;
0494     addr.lun = 0;
0495 
0496     msg.netfn = 0x06;
0497     msg.cmd = IPMI_WDOG_RESET_TIMER;
0498     msg.data = NULL;
0499     msg.data_len = 0;
0500     atomic_add(2, &panic_done_count);
0501     rv = ipmi_request_supply_msgs(watchdog_user,
0502                       (struct ipmi_addr *) &addr,
0503                       0,
0504                       &msg,
0505                       NULL,
0506                       &panic_halt_heartbeat_smi_msg,
0507                       &panic_halt_heartbeat_recv_msg,
0508                       1);
0509     if (rv)
0510         atomic_sub(2, &panic_done_count);
0511 }
0512 
0513 static struct ipmi_smi_msg panic_halt_smi_msg =
0514     INIT_IPMI_SMI_MSG(panic_smi_free);
0515 static struct ipmi_recv_msg panic_halt_recv_msg =
0516     INIT_IPMI_RECV_MSG(panic_recv_free);
0517 
0518 /*
0519  * Special call, doesn't claim any locks.  This is only to be called
0520  * at panic or halt time, in run-to-completion mode, when the caller
0521  * is the only CPU and the only thing that will be going is these IPMI
0522  * calls.
0523  */
0524 static void panic_halt_ipmi_set_timeout(void)
0525 {
0526     int send_heartbeat_now;
0527     int rv;
0528 
0529     /* Wait for the messages to be free. */
0530     while (atomic_read(&panic_done_count) != 0)
0531         ipmi_poll_interface(watchdog_user);
0532     atomic_add(2, &panic_done_count);
0533     rv = __ipmi_set_timeout(&panic_halt_smi_msg,
0534                 &panic_halt_recv_msg,
0535                 &send_heartbeat_now);
0536     if (rv) {
0537         atomic_sub(2, &panic_done_count);
0538         pr_warn("Unable to extend the watchdog timeout\n");
0539     } else {
0540         if (send_heartbeat_now)
0541             panic_halt_ipmi_heartbeat();
0542     }
0543     while (atomic_read(&panic_done_count) != 0)
0544         ipmi_poll_interface(watchdog_user);
0545 }
0546 
0547 static int __ipmi_heartbeat(void)
0548 {
0549     struct kernel_ipmi_msg msg;
0550     int rv;
0551     struct ipmi_system_interface_addr addr;
0552     int timeout_retries = 0;
0553 
0554 restart:
0555     /*
0556      * Don't reset the timer if we have the timer turned off, that
0557      * re-enables the watchdog.
0558      */
0559     if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)
0560         return 0;
0561 
0562     atomic_set(&msg_tofree, 2);
0563 
0564     addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
0565     addr.channel = IPMI_BMC_CHANNEL;
0566     addr.lun = 0;
0567 
0568     msg.netfn = 0x06;
0569     msg.cmd = IPMI_WDOG_RESET_TIMER;
0570     msg.data = NULL;
0571     msg.data_len = 0;
0572     rv = ipmi_request_supply_msgs(watchdog_user,
0573                       (struct ipmi_addr *) &addr,
0574                       0,
0575                       &msg,
0576                       NULL,
0577                       &smi_msg,
0578                       &recv_msg,
0579                       1);
0580     if (rv) {
0581         atomic_set(&msg_tofree, 0);
0582         pr_warn("heartbeat send failure: %d\n", rv);
0583         return rv;
0584     }
0585 
0586     /* Wait for the heartbeat to be sent. */
0587     wait_for_completion(&msg_wait);
0588 
0589     if (recv_msg.msg.data[0] == IPMI_WDOG_TIMER_NOT_INIT_RESP)  {
0590         timeout_retries++;
0591         if (timeout_retries > 3) {
0592             pr_err("Unable to restore the IPMI watchdog's settings, giving up\n");
0593             rv = -EIO;
0594             goto out;
0595         }
0596 
0597         /*
0598          * The timer was not initialized, that means the BMC was
0599          * probably reset and lost the watchdog information.  Attempt
0600          * to restore the timer's info.  Note that we still hold
0601          * the heartbeat lock, to keep a heartbeat from happening
0602          * in this process, so must say no heartbeat to avoid a
0603          * deadlock on this mutex
0604          */
0605         rv = _ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
0606         if (rv) {
0607             pr_err("Unable to send the command to set the watchdog's settings, giving up\n");
0608             goto out;
0609         }
0610 
0611         /* Might need a heartbeat send, go ahead and do it. */
0612         goto restart;
0613     } else if (recv_msg.msg.data[0] != 0) {
0614         /*
0615          * Got an error in the heartbeat response.  It was already
0616          * reported in ipmi_wdog_msg_handler, but we should return
0617          * an error here.
0618          */
0619         rv = -EINVAL;
0620     }
0621 
0622 out:
0623     return rv;
0624 }
0625 
0626 static int _ipmi_heartbeat(void)
0627 {
0628     int rv;
0629 
0630     if (!watchdog_user)
0631         return -ENODEV;
0632 
0633     if (ipmi_start_timer_on_heartbeat) {
0634         ipmi_start_timer_on_heartbeat = 0;
0635         ipmi_watchdog_state = action_val;
0636         rv = _ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
0637     } else if (atomic_cmpxchg(&pretimeout_since_last_heartbeat, 1, 0)) {
0638         /*
0639          * A pretimeout occurred, make sure we set the timeout.
0640          * We don't want to set the action, though, we want to
0641          * leave that alone (thus it can't be combined with the
0642          * above operation.
0643          */
0644         rv = _ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
0645     } else {
0646         rv = __ipmi_heartbeat();
0647     }
0648 
0649     return rv;
0650 }
0651 
0652 static int ipmi_heartbeat(void)
0653 {
0654     int rv;
0655 
0656     mutex_lock(&ipmi_watchdog_mutex);
0657     rv = _ipmi_heartbeat();
0658     mutex_unlock(&ipmi_watchdog_mutex);
0659 
0660     return rv;
0661 }
0662 
0663 static const struct watchdog_info ident = {
0664     .options    = 0,    /* WDIOF_SETTIMEOUT, */
0665     .firmware_version = 1,
0666     .identity   = "IPMI"
0667 };
0668 
0669 static int ipmi_ioctl(struct file *file,
0670               unsigned int cmd, unsigned long arg)
0671 {
0672     void __user *argp = (void __user *)arg;
0673     int i;
0674     int val;
0675 
0676     switch (cmd) {
0677     case WDIOC_GETSUPPORT:
0678         i = copy_to_user(argp, &ident, sizeof(ident));
0679         return i ? -EFAULT : 0;
0680 
0681     case WDIOC_SETTIMEOUT:
0682         i = copy_from_user(&val, argp, sizeof(int));
0683         if (i)
0684             return -EFAULT;
0685         timeout = val;
0686         return _ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
0687 
0688     case WDIOC_GETTIMEOUT:
0689         i = copy_to_user(argp, &timeout, sizeof(timeout));
0690         if (i)
0691             return -EFAULT;
0692         return 0;
0693 
0694     case WDIOC_SETPRETIMEOUT:
0695         i = copy_from_user(&val, argp, sizeof(int));
0696         if (i)
0697             return -EFAULT;
0698         pretimeout = val;
0699         return _ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
0700 
0701     case WDIOC_GETPRETIMEOUT:
0702         i = copy_to_user(argp, &pretimeout, sizeof(pretimeout));
0703         if (i)
0704             return -EFAULT;
0705         return 0;
0706 
0707     case WDIOC_KEEPALIVE:
0708         return _ipmi_heartbeat();
0709 
0710     case WDIOC_SETOPTIONS:
0711         i = copy_from_user(&val, argp, sizeof(int));
0712         if (i)
0713             return -EFAULT;
0714         if (val & WDIOS_DISABLECARD) {
0715             ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
0716             _ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
0717             ipmi_start_timer_on_heartbeat = 0;
0718         }
0719 
0720         if (val & WDIOS_ENABLECARD) {
0721             ipmi_watchdog_state = action_val;
0722             _ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
0723         }
0724         return 0;
0725 
0726     case WDIOC_GETSTATUS:
0727         val = 0;
0728         i = copy_to_user(argp, &val, sizeof(val));
0729         if (i)
0730             return -EFAULT;
0731         return 0;
0732 
0733     default:
0734         return -ENOIOCTLCMD;
0735     }
0736 }
0737 
0738 static long ipmi_unlocked_ioctl(struct file *file,
0739                 unsigned int cmd,
0740                 unsigned long arg)
0741 {
0742     int ret;
0743 
0744     mutex_lock(&ipmi_watchdog_mutex);
0745     ret = ipmi_ioctl(file, cmd, arg);
0746     mutex_unlock(&ipmi_watchdog_mutex);
0747 
0748     return ret;
0749 }
0750 
0751 static ssize_t ipmi_write(struct file *file,
0752               const char  __user *buf,
0753               size_t      len,
0754               loff_t      *ppos)
0755 {
0756     int rv;
0757 
0758     if (len) {
0759         if (!nowayout) {
0760             size_t i;
0761 
0762             /* In case it was set long ago */
0763             expect_close = 0;
0764 
0765             for (i = 0; i != len; i++) {
0766                 char c;
0767 
0768                 if (get_user(c, buf + i))
0769                     return -EFAULT;
0770                 if (c == 'V')
0771                     expect_close = 42;
0772             }
0773         }
0774         rv = ipmi_heartbeat();
0775         if (rv)
0776             return rv;
0777     }
0778     return len;
0779 }
0780 
0781 static ssize_t ipmi_read(struct file *file,
0782              char        __user *buf,
0783              size_t      count,
0784              loff_t      *ppos)
0785 {
0786     int          rv = 0;
0787     wait_queue_entry_t wait;
0788 
0789     if (count <= 0)
0790         return 0;
0791 
0792     /*
0793      * Reading returns if the pretimeout has gone off, and it only does
0794      * it once per pretimeout.
0795      */
0796     spin_lock_irq(&ipmi_read_lock);
0797     if (!data_to_read) {
0798         if (file->f_flags & O_NONBLOCK) {
0799             rv = -EAGAIN;
0800             goto out;
0801         }
0802 
0803         init_waitqueue_entry(&wait, current);
0804         add_wait_queue(&read_q, &wait);
0805         while (!data_to_read) {
0806             set_current_state(TASK_INTERRUPTIBLE);
0807             spin_unlock_irq(&ipmi_read_lock);
0808             schedule();
0809             spin_lock_irq(&ipmi_read_lock);
0810         }
0811         remove_wait_queue(&read_q, &wait);
0812 
0813         if (signal_pending(current)) {
0814             rv = -ERESTARTSYS;
0815             goto out;
0816         }
0817     }
0818     data_to_read = 0;
0819 
0820  out:
0821     spin_unlock_irq(&ipmi_read_lock);
0822 
0823     if (rv == 0) {
0824         if (copy_to_user(buf, &data_to_read, 1))
0825             rv = -EFAULT;
0826         else
0827             rv = 1;
0828     }
0829 
0830     return rv;
0831 }
0832 
0833 static int ipmi_open(struct inode *ino, struct file *filep)
0834 {
0835     switch (iminor(ino)) {
0836     case WATCHDOG_MINOR:
0837         if (test_and_set_bit(0, &ipmi_wdog_open))
0838             return -EBUSY;
0839 
0840 
0841         /*
0842          * Don't start the timer now, let it start on the
0843          * first heartbeat.
0844          */
0845         ipmi_start_timer_on_heartbeat = 1;
0846         return stream_open(ino, filep);
0847 
0848     default:
0849         return (-ENODEV);
0850     }
0851 }
0852 
0853 static __poll_t ipmi_poll(struct file *file, poll_table *wait)
0854 {
0855     __poll_t mask = 0;
0856 
0857     poll_wait(file, &read_q, wait);
0858 
0859     spin_lock_irq(&ipmi_read_lock);
0860     if (data_to_read)
0861         mask |= (EPOLLIN | EPOLLRDNORM);
0862     spin_unlock_irq(&ipmi_read_lock);
0863 
0864     return mask;
0865 }
0866 
0867 static int ipmi_fasync(int fd, struct file *file, int on)
0868 {
0869     int result;
0870 
0871     result = fasync_helper(fd, file, on, &fasync_q);
0872 
0873     return (result);
0874 }
0875 
0876 static int ipmi_close(struct inode *ino, struct file *filep)
0877 {
0878     if (iminor(ino) == WATCHDOG_MINOR) {
0879         if (expect_close == 42) {
0880             mutex_lock(&ipmi_watchdog_mutex);
0881             ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
0882             _ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
0883             mutex_unlock(&ipmi_watchdog_mutex);
0884         } else {
0885             pr_crit("Unexpected close, not stopping watchdog!\n");
0886             ipmi_heartbeat();
0887         }
0888         clear_bit(0, &ipmi_wdog_open);
0889     }
0890 
0891     expect_close = 0;
0892 
0893     return 0;
0894 }
0895 
0896 static const struct file_operations ipmi_wdog_fops = {
0897     .owner   = THIS_MODULE,
0898     .read    = ipmi_read,
0899     .poll    = ipmi_poll,
0900     .write   = ipmi_write,
0901     .unlocked_ioctl = ipmi_unlocked_ioctl,
0902     .compat_ioctl   = compat_ptr_ioctl,
0903     .open    = ipmi_open,
0904     .release = ipmi_close,
0905     .fasync  = ipmi_fasync,
0906     .llseek  = no_llseek,
0907 };
0908 
0909 static struct miscdevice ipmi_wdog_miscdev = {
0910     .minor      = WATCHDOG_MINOR,
0911     .name       = "watchdog",
0912     .fops       = &ipmi_wdog_fops
0913 };
0914 
0915 static void ipmi_wdog_msg_handler(struct ipmi_recv_msg *msg,
0916                   void                 *handler_data)
0917 {
0918     if (msg->msg.cmd == IPMI_WDOG_RESET_TIMER &&
0919             msg->msg.data[0] == IPMI_WDOG_TIMER_NOT_INIT_RESP)
0920         pr_info("response: The IPMI controller appears to have been reset, will attempt to reinitialize the watchdog timer\n");
0921     else if (msg->msg.data[0] != 0)
0922         pr_err("response: Error %x on cmd %x\n",
0923                msg->msg.data[0],
0924                msg->msg.cmd);
0925 
0926     ipmi_free_recv_msg(msg);
0927 }
0928 
0929 static void ipmi_wdog_pretimeout_handler(void *handler_data)
0930 {
0931     if (preaction_val != WDOG_PRETIMEOUT_NONE) {
0932         if (preop_val == WDOG_PREOP_PANIC) {
0933             if (atomic_inc_and_test(&preop_panic_excl))
0934                 panic("Watchdog pre-timeout");
0935         } else if (preop_val == WDOG_PREOP_GIVE_DATA) {
0936             unsigned long flags;
0937 
0938             spin_lock_irqsave(&ipmi_read_lock, flags);
0939             data_to_read = 1;
0940             wake_up_interruptible(&read_q);
0941             kill_fasync(&fasync_q, SIGIO, POLL_IN);
0942             spin_unlock_irqrestore(&ipmi_read_lock, flags);
0943         }
0944     }
0945 
0946     /*
0947      * On some machines, the heartbeat will give an error and not
0948      * work unless we re-enable the timer.  So do so.
0949      */
0950     atomic_set(&pretimeout_since_last_heartbeat, 1);
0951 }
0952 
0953 static void ipmi_wdog_panic_handler(void *user_data)
0954 {
0955     static int panic_event_handled;
0956 
0957     /*
0958      * On a panic, if we have a panic timeout, make sure to extend
0959      * the watchdog timer to a reasonable value to complete the
0960      * panic, if the watchdog timer is running.  Plus the
0961      * pretimeout is meaningless at panic time.
0962      */
0963     if (watchdog_user && !panic_event_handled &&
0964         ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
0965         /* Make sure we do this only once. */
0966         panic_event_handled = 1;
0967 
0968         timeout = panic_wdt_timeout;
0969         pretimeout = 0;
0970         panic_halt_ipmi_set_timeout();
0971     }
0972 }
0973 
0974 static const struct ipmi_user_hndl ipmi_hndlrs = {
0975     .ipmi_recv_hndl           = ipmi_wdog_msg_handler,
0976     .ipmi_watchdog_pretimeout = ipmi_wdog_pretimeout_handler,
0977     .ipmi_panic_handler       = ipmi_wdog_panic_handler
0978 };
0979 
0980 static void ipmi_register_watchdog(int ipmi_intf)
0981 {
0982     int rv = -EBUSY;
0983 
0984     if (watchdog_user)
0985         goto out;
0986 
0987     if ((ifnum_to_use >= 0) && (ifnum_to_use != ipmi_intf))
0988         goto out;
0989 
0990     watchdog_ifnum = ipmi_intf;
0991 
0992     rv = ipmi_create_user(ipmi_intf, &ipmi_hndlrs, NULL, &watchdog_user);
0993     if (rv < 0) {
0994         pr_crit("Unable to register with ipmi\n");
0995         goto out;
0996     }
0997 
0998     rv = ipmi_get_version(watchdog_user,
0999                   &ipmi_version_major,
1000                   &ipmi_version_minor);
1001     if (rv) {
1002         pr_warn("Unable to get IPMI version, assuming 1.0\n");
1003         ipmi_version_major = 1;
1004         ipmi_version_minor = 0;
1005     }
1006 
1007     rv = misc_register(&ipmi_wdog_miscdev);
1008     if (rv < 0) {
1009         ipmi_destroy_user(watchdog_user);
1010         watchdog_user = NULL;
1011         pr_crit("Unable to register misc device\n");
1012     }
1013 
1014 #ifdef HAVE_DIE_NMI
1015     if (nmi_handler_registered) {
1016         int old_pretimeout = pretimeout;
1017         int old_timeout = timeout;
1018         int old_preop_val = preop_val;
1019 
1020         /*
1021          * Set the pretimeout to go off in a second and give
1022          * ourselves plenty of time to stop the timer.
1023          */
1024         ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
1025         preop_val = WDOG_PREOP_NONE; /* Make sure nothing happens */
1026         pretimeout = 99;
1027         timeout = 100;
1028 
1029         testing_nmi = 1;
1030 
1031         rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
1032         if (rv) {
1033             pr_warn("Error starting timer to test NMI: 0x%x.  The NMI pretimeout will likely not work\n",
1034                 rv);
1035             rv = 0;
1036             goto out_restore;
1037         }
1038 
1039         msleep(1500);
1040 
1041         if (testing_nmi != 2) {
1042             pr_warn("IPMI NMI didn't seem to occur.  The NMI pretimeout will likely not work\n");
1043         }
1044  out_restore:
1045         testing_nmi = 0;
1046         preop_val = old_preop_val;
1047         pretimeout = old_pretimeout;
1048         timeout = old_timeout;
1049     }
1050 #endif
1051 
1052  out:
1053     if ((start_now) && (rv == 0)) {
1054         /* Run from startup, so start the timer now. */
1055         start_now = 0; /* Disable this function after first startup. */
1056         ipmi_watchdog_state = action_val;
1057         ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
1058         pr_info("Starting now!\n");
1059     } else {
1060         /* Stop the timer now. */
1061         ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
1062         ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
1063     }
1064 }
1065 
1066 static void ipmi_unregister_watchdog(int ipmi_intf)
1067 {
1068     int rv;
1069     struct ipmi_user *loc_user = watchdog_user;
1070 
1071     if (!loc_user)
1072         return;
1073 
1074     if (watchdog_ifnum != ipmi_intf)
1075         return;
1076 
1077     /* Make sure no one can call us any more. */
1078     misc_deregister(&ipmi_wdog_miscdev);
1079 
1080     watchdog_user = NULL;
1081 
1082     /*
1083      * Wait to make sure the message makes it out.  The lower layer has
1084      * pointers to our buffers, we want to make sure they are done before
1085      * we release our memory.
1086      */
1087     while (atomic_read(&msg_tofree))
1088         msg_free_smi(NULL);
1089 
1090     mutex_lock(&ipmi_watchdog_mutex);
1091 
1092     /* Disconnect from IPMI. */
1093     rv = ipmi_destroy_user(loc_user);
1094     if (rv)
1095         pr_warn("error unlinking from IPMI: %d\n",  rv);
1096 
1097     /* If it comes back, restart it properly. */
1098     ipmi_start_timer_on_heartbeat = 1;
1099 
1100     mutex_unlock(&ipmi_watchdog_mutex);
1101 }
1102 
1103 #ifdef HAVE_DIE_NMI
1104 static int
1105 ipmi_nmi(unsigned int val, struct pt_regs *regs)
1106 {
1107     /*
1108      * If we get here, it's an NMI that's not a memory or I/O
1109      * error.  We can't truly tell if it's from IPMI or not
1110      * without sending a message, and sending a message is almost
1111      * impossible because of locking.
1112      */
1113 
1114     if (testing_nmi) {
1115         testing_nmi = 2;
1116         return NMI_HANDLED;
1117     }
1118 
1119     /* If we are not expecting a timeout, ignore it. */
1120     if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)
1121         return NMI_DONE;
1122 
1123     if (preaction_val != WDOG_PRETIMEOUT_NMI)
1124         return NMI_DONE;
1125 
1126     /*
1127      * If no one else handled the NMI, we assume it was the IPMI
1128      * watchdog.
1129      */
1130     if (preop_val == WDOG_PREOP_PANIC) {
1131         /* On some machines, the heartbeat will give
1132            an error and not work unless we re-enable
1133            the timer.   So do so. */
1134         atomic_set(&pretimeout_since_last_heartbeat, 1);
1135         if (atomic_inc_and_test(&preop_panic_excl))
1136             nmi_panic(regs, "pre-timeout");
1137     }
1138 
1139     return NMI_HANDLED;
1140 }
1141 #endif
1142 
1143 static int wdog_reboot_handler(struct notifier_block *this,
1144                    unsigned long         code,
1145                    void                  *unused)
1146 {
1147     static int reboot_event_handled;
1148 
1149     if ((watchdog_user) && (!reboot_event_handled)) {
1150         /* Make sure we only do this once. */
1151         reboot_event_handled = 1;
1152 
1153         if (code == SYS_POWER_OFF || code == SYS_HALT) {
1154             /* Disable the WDT if we are shutting down. */
1155             ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
1156             ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
1157         } else if (ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
1158             /* Set a long timer to let the reboot happen or
1159                reset if it hangs, but only if the watchdog
1160                timer was already running. */
1161             if (timeout < 120)
1162                 timeout = 120;
1163             pretimeout = 0;
1164             ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
1165             ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
1166         }
1167     }
1168     return NOTIFY_OK;
1169 }
1170 
1171 static struct notifier_block wdog_reboot_notifier = {
1172     .notifier_call  = wdog_reboot_handler,
1173     .next       = NULL,
1174     .priority   = 0
1175 };
1176 
1177 static void ipmi_new_smi(int if_num, struct device *device)
1178 {
1179     ipmi_register_watchdog(if_num);
1180 }
1181 
1182 static void ipmi_smi_gone(int if_num)
1183 {
1184     ipmi_unregister_watchdog(if_num);
1185 }
1186 
1187 static struct ipmi_smi_watcher smi_watcher = {
1188     .owner    = THIS_MODULE,
1189     .new_smi  = ipmi_new_smi,
1190     .smi_gone = ipmi_smi_gone
1191 };
1192 
1193 static int action_op(const char *inval, char *outval)
1194 {
1195     if (outval)
1196         strcpy(outval, action);
1197 
1198     if (!inval)
1199         return 0;
1200 
1201     if (strcmp(inval, "reset") == 0)
1202         action_val = WDOG_TIMEOUT_RESET;
1203     else if (strcmp(inval, "none") == 0)
1204         action_val = WDOG_TIMEOUT_NONE;
1205     else if (strcmp(inval, "power_cycle") == 0)
1206         action_val = WDOG_TIMEOUT_POWER_CYCLE;
1207     else if (strcmp(inval, "power_off") == 0)
1208         action_val = WDOG_TIMEOUT_POWER_DOWN;
1209     else
1210         return -EINVAL;
1211     strcpy(action, inval);
1212     return 0;
1213 }
1214 
1215 static int preaction_op(const char *inval, char *outval)
1216 {
1217     if (outval)
1218         strcpy(outval, preaction);
1219 
1220     if (!inval)
1221         return 0;
1222 
1223     if (strcmp(inval, "pre_none") == 0)
1224         preaction_val = WDOG_PRETIMEOUT_NONE;
1225     else if (strcmp(inval, "pre_smi") == 0)
1226         preaction_val = WDOG_PRETIMEOUT_SMI;
1227 #ifdef HAVE_DIE_NMI
1228     else if (strcmp(inval, "pre_nmi") == 0)
1229         preaction_val = WDOG_PRETIMEOUT_NMI;
1230 #endif
1231     else if (strcmp(inval, "pre_int") == 0)
1232         preaction_val = WDOG_PRETIMEOUT_MSG_INT;
1233     else
1234         return -EINVAL;
1235     strcpy(preaction, inval);
1236     return 0;
1237 }
1238 
1239 static int preop_op(const char *inval, char *outval)
1240 {
1241     if (outval)
1242         strcpy(outval, preop);
1243 
1244     if (!inval)
1245         return 0;
1246 
1247     if (strcmp(inval, "preop_none") == 0)
1248         preop_val = WDOG_PREOP_NONE;
1249     else if (strcmp(inval, "preop_panic") == 0)
1250         preop_val = WDOG_PREOP_PANIC;
1251     else if (strcmp(inval, "preop_give_data") == 0)
1252         preop_val = WDOG_PREOP_GIVE_DATA;
1253     else
1254         return -EINVAL;
1255     strcpy(preop, inval);
1256     return 0;
1257 }
1258 
1259 static void check_parms(void)
1260 {
1261 #ifdef HAVE_DIE_NMI
1262     int do_nmi = 0;
1263     int rv;
1264 
1265     if (preaction_val == WDOG_PRETIMEOUT_NMI) {
1266         do_nmi = 1;
1267         if (preop_val == WDOG_PREOP_GIVE_DATA) {
1268             pr_warn("Pretimeout op is to give data but NMI pretimeout is enabled, setting pretimeout op to none\n");
1269             preop_op("preop_none", NULL);
1270             do_nmi = 0;
1271         }
1272     }
1273     if (do_nmi && !nmi_handler_registered) {
1274         rv = register_nmi_handler(NMI_UNKNOWN, ipmi_nmi, 0,
1275                         "ipmi");
1276         if (rv) {
1277             pr_warn("Can't register nmi handler\n");
1278             return;
1279         } else
1280             nmi_handler_registered = 1;
1281     } else if (!do_nmi && nmi_handler_registered) {
1282         unregister_nmi_handler(NMI_UNKNOWN, "ipmi");
1283         nmi_handler_registered = 0;
1284     }
1285 #endif
1286 }
1287 
1288 static int __init ipmi_wdog_init(void)
1289 {
1290     int rv;
1291 
1292     if (action_op(action, NULL)) {
1293         action_op("reset", NULL);
1294         pr_info("Unknown action '%s', defaulting to reset\n", action);
1295     }
1296 
1297     if (preaction_op(preaction, NULL)) {
1298         preaction_op("pre_none", NULL);
1299         pr_info("Unknown preaction '%s', defaulting to none\n",
1300             preaction);
1301     }
1302 
1303     if (preop_op(preop, NULL)) {
1304         preop_op("preop_none", NULL);
1305         pr_info("Unknown preop '%s', defaulting to none\n", preop);
1306     }
1307 
1308     check_parms();
1309 
1310     register_reboot_notifier(&wdog_reboot_notifier);
1311 
1312     rv = ipmi_smi_watcher_register(&smi_watcher);
1313     if (rv) {
1314 #ifdef HAVE_DIE_NMI
1315         if (nmi_handler_registered)
1316             unregister_nmi_handler(NMI_UNKNOWN, "ipmi");
1317 #endif
1318         unregister_reboot_notifier(&wdog_reboot_notifier);
1319         pr_warn("can't register smi watcher\n");
1320         return rv;
1321     }
1322 
1323     pr_info("driver initialized\n");
1324 
1325     return 0;
1326 }
1327 
1328 static void __exit ipmi_wdog_exit(void)
1329 {
1330     ipmi_smi_watcher_unregister(&smi_watcher);
1331     ipmi_unregister_watchdog(watchdog_ifnum);
1332 
1333 #ifdef HAVE_DIE_NMI
1334     if (nmi_handler_registered)
1335         unregister_nmi_handler(NMI_UNKNOWN, "ipmi");
1336 #endif
1337 
1338     unregister_reboot_notifier(&wdog_reboot_notifier);
1339 }
1340 module_exit(ipmi_wdog_exit);
1341 module_init(ipmi_wdog_init);
1342 MODULE_LICENSE("GPL");
1343 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
1344 MODULE_DESCRIPTION("watchdog timer based upon the IPMI interface.");