Path: blob/master/drivers/char/ipmi/ipmi_watchdog.c
15112 views
/*1* ipmi_watchdog.c2*3* A watchdog timer based upon the IPMI interface.4*5* Author: MontaVista Software, Inc.6* Corey Minyard <[email protected]>7* [email protected]8*9* Copyright 2002 MontaVista Software Inc.10*11* This program is free software; you can redistribute it and/or modify it12* under the terms of the GNU General Public License as published by the13* Free Software Foundation; either version 2 of the License, or (at your14* option) any later version.15*16*17* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED18* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF19* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.20* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,21* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,22* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS23* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND24* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR25* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE26* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.27*28* You should have received a copy of the GNU General Public License along29* with this program; if not, write to the Free Software Foundation, Inc.,30* 675 Mass Ave, Cambridge, MA 02139, USA.31*/3233#include <linux/module.h>34#include <linux/moduleparam.h>35#include <linux/ipmi.h>36#include <linux/ipmi_smi.h>37#include <linux/mutex.h>38#include <linux/watchdog.h>39#include <linux/miscdevice.h>40#include <linux/init.h>41#include <linux/completion.h>42#include <linux/kdebug.h>43#include <linux/rwsem.h>44#include <linux/errno.h>45#include <asm/uaccess.h>46#include <linux/notifier.h>47#include <linux/nmi.h>48#include <linux/reboot.h>49#include <linux/wait.h>50#include <linux/poll.h>51#include <linux/string.h>52#include <linux/ctype.h>53#include <linux/delay.h>54#include <asm/atomic.h>5556#ifdef CONFIG_X8657/*58* This is ugly, but I've determined that x86 is the only architecture59* that can reasonably support the IPMI NMI watchdog timeout at this60* time. If another architecture adds this capability somehow, it61* will have to be a somewhat different mechanism and I have no idea62* how it will work. So in the unlikely event that another63* architecture supports this, we can figure out a good generic64* mechanism for it at that time.65*/66#include <asm/kdebug.h>67#define HAVE_DIE_NMI68#endif6970#define PFX "IPMI Watchdog: "7172/*73* The IPMI command/response information for the watchdog timer.74*/7576/* values for byte 1 of the set command, byte 2 of the get response. */77#define WDOG_DONT_LOG (1 << 7)78#define WDOG_DONT_STOP_ON_SET (1 << 6)79#define WDOG_SET_TIMER_USE(byte, use) \80byte = ((byte) & 0xf8) | ((use) & 0x7)81#define WDOG_GET_TIMER_USE(byte) ((byte) & 0x7)82#define WDOG_TIMER_USE_BIOS_FRB2 183#define WDOG_TIMER_USE_BIOS_POST 284#define WDOG_TIMER_USE_OS_LOAD 385#define WDOG_TIMER_USE_SMS_OS 486#define WDOG_TIMER_USE_OEM 58788/* values for byte 2 of the set command, byte 3 of the get response. */89#define WDOG_SET_PRETIMEOUT_ACT(byte, use) \90byte = ((byte) & 0x8f) | (((use) & 0x7) << 4)91#define WDOG_GET_PRETIMEOUT_ACT(byte) (((byte) >> 4) & 0x7)92#define WDOG_PRETIMEOUT_NONE 093#define WDOG_PRETIMEOUT_SMI 194#define WDOG_PRETIMEOUT_NMI 295#define WDOG_PRETIMEOUT_MSG_INT 39697/* Operations that can be performed on a pretimout. */98#define WDOG_PREOP_NONE 099#define WDOG_PREOP_PANIC 1100/* Cause data to be available to read. Doesn't work in NMI mode. */101#define WDOG_PREOP_GIVE_DATA 2102103/* Actions to perform on a full timeout. */104#define WDOG_SET_TIMEOUT_ACT(byte, use) \105byte = ((byte) & 0xf8) | ((use) & 0x7)106#define WDOG_GET_TIMEOUT_ACT(byte) ((byte) & 0x7)107#define WDOG_TIMEOUT_NONE 0108#define WDOG_TIMEOUT_RESET 1109#define WDOG_TIMEOUT_POWER_DOWN 2110#define WDOG_TIMEOUT_POWER_CYCLE 3111112/*113* Byte 3 of the get command, byte 4 of the get response is the114* pre-timeout in seconds.115*/116117/* Bits for setting byte 4 of the set command, byte 5 of the get response. */118#define WDOG_EXPIRE_CLEAR_BIOS_FRB2 (1 << 1)119#define WDOG_EXPIRE_CLEAR_BIOS_POST (1 << 2)120#define WDOG_EXPIRE_CLEAR_OS_LOAD (1 << 3)121#define WDOG_EXPIRE_CLEAR_SMS_OS (1 << 4)122#define WDOG_EXPIRE_CLEAR_OEM (1 << 5)123124/*125* Setting/getting the watchdog timer value. This is for bytes 5 and126* 6 (the timeout time) of the set command, and bytes 6 and 7 (the127* timeout time) and 8 and 9 (the current countdown value) of the128* response. The timeout value is given in seconds (in the command it129* is 100ms intervals).130*/131#define WDOG_SET_TIMEOUT(byte1, byte2, val) \132(byte1) = (((val) * 10) & 0xff), (byte2) = (((val) * 10) >> 8)133#define WDOG_GET_TIMEOUT(byte1, byte2) \134(((byte1) | ((byte2) << 8)) / 10)135136#define IPMI_WDOG_RESET_TIMER 0x22137#define IPMI_WDOG_SET_TIMER 0x24138#define IPMI_WDOG_GET_TIMER 0x25139140/* These are here until the real ones get into the watchdog.h interface. */141#ifndef WDIOC_GETTIMEOUT142#define WDIOC_GETTIMEOUT _IOW(WATCHDOG_IOCTL_BASE, 20, int)143#endif144#ifndef WDIOC_SET_PRETIMEOUT145#define WDIOC_SET_PRETIMEOUT _IOW(WATCHDOG_IOCTL_BASE, 21, int)146#endif147#ifndef WDIOC_GET_PRETIMEOUT148#define WDIOC_GET_PRETIMEOUT _IOW(WATCHDOG_IOCTL_BASE, 22, int)149#endif150151static DEFINE_MUTEX(ipmi_watchdog_mutex);152static int nowayout = WATCHDOG_NOWAYOUT;153154static ipmi_user_t watchdog_user;155static int watchdog_ifnum;156157/* Default the timeout to 10 seconds. */158static int timeout = 10;159160/* The pre-timeout is disabled by default. */161static int pretimeout;162163/* Default action is to reset the board on a timeout. */164static unsigned char action_val = WDOG_TIMEOUT_RESET;165166static char action[16] = "reset";167168static unsigned char preaction_val = WDOG_PRETIMEOUT_NONE;169170static char preaction[16] = "pre_none";171172static unsigned char preop_val = WDOG_PREOP_NONE;173174static char preop[16] = "preop_none";175static DEFINE_SPINLOCK(ipmi_read_lock);176static char data_to_read;177static DECLARE_WAIT_QUEUE_HEAD(read_q);178static struct fasync_struct *fasync_q;179static char pretimeout_since_last_heartbeat;180static char expect_close;181182static int ifnum_to_use = -1;183184/* Parameters to ipmi_set_timeout */185#define IPMI_SET_TIMEOUT_NO_HB 0186#define IPMI_SET_TIMEOUT_HB_IF_NECESSARY 1187#define IPMI_SET_TIMEOUT_FORCE_HB 2188189static int ipmi_set_timeout(int do_heartbeat);190static void ipmi_register_watchdog(int ipmi_intf);191static void ipmi_unregister_watchdog(int ipmi_intf);192193/*194* If true, the driver will start running as soon as it is configured195* and ready.196*/197static int start_now;198199static int set_param_timeout(const char *val, const struct kernel_param *kp)200{201char *endp;202int l;203int rv = 0;204205if (!val)206return -EINVAL;207l = simple_strtoul(val, &endp, 0);208if (endp == val)209return -EINVAL;210211*((int *)kp->arg) = l;212if (watchdog_user)213rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);214215return rv;216}217218static struct kernel_param_ops param_ops_timeout = {219.set = set_param_timeout,220.get = param_get_int,221};222#define param_check_timeout param_check_int223224typedef int (*action_fn)(const char *intval, char *outval);225226static int action_op(const char *inval, char *outval);227static int preaction_op(const char *inval, char *outval);228static int preop_op(const char *inval, char *outval);229static void check_parms(void);230231static int set_param_str(const char *val, const struct kernel_param *kp)232{233action_fn fn = (action_fn) kp->arg;234int rv = 0;235char valcp[16];236char *s;237238strncpy(valcp, val, 16);239valcp[15] = '\0';240241s = strstrip(valcp);242243rv = fn(s, NULL);244if (rv)245goto out;246247check_parms();248if (watchdog_user)249rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);250251out:252return rv;253}254255static int get_param_str(char *buffer, const struct kernel_param *kp)256{257action_fn fn = (action_fn) kp->arg;258int rv;259260rv = fn(NULL, buffer);261if (rv)262return rv;263return strlen(buffer);264}265266267static int set_param_wdog_ifnum(const char *val, const struct kernel_param *kp)268{269int rv = param_set_int(val, kp);270if (rv)271return rv;272if ((ifnum_to_use < 0) || (ifnum_to_use == watchdog_ifnum))273return 0;274275ipmi_unregister_watchdog(watchdog_ifnum);276ipmi_register_watchdog(ifnum_to_use);277return 0;278}279280static struct kernel_param_ops param_ops_wdog_ifnum = {281.set = set_param_wdog_ifnum,282.get = param_get_int,283};284285#define param_check_wdog_ifnum param_check_int286287static struct kernel_param_ops param_ops_str = {288.set = set_param_str,289.get = get_param_str,290};291292module_param(ifnum_to_use, wdog_ifnum, 0644);293MODULE_PARM_DESC(ifnum_to_use, "The interface number to use for the watchdog "294"timer. Setting to -1 defaults to the first registered "295"interface");296297module_param(timeout, timeout, 0644);298MODULE_PARM_DESC(timeout, "Timeout value in seconds.");299300module_param(pretimeout, timeout, 0644);301MODULE_PARM_DESC(pretimeout, "Pretimeout value in seconds.");302303module_param_cb(action, ¶m_ops_str, action_op, 0644);304MODULE_PARM_DESC(action, "Timeout action. One of: "305"reset, none, power_cycle, power_off.");306307module_param_cb(preaction, ¶m_ops_str, preaction_op, 0644);308MODULE_PARM_DESC(preaction, "Pretimeout action. One of: "309"pre_none, pre_smi, pre_nmi, pre_int.");310311module_param_cb(preop, ¶m_ops_str, preop_op, 0644);312MODULE_PARM_DESC(preop, "Pretimeout driver operation. One of: "313"preop_none, preop_panic, preop_give_data.");314315module_param(start_now, int, 0444);316MODULE_PARM_DESC(start_now, "Set to 1 to start the watchdog as"317"soon as the driver is loaded.");318319module_param(nowayout, int, 0644);320MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "321"(default=CONFIG_WATCHDOG_NOWAYOUT)");322323/* Default state of the timer. */324static unsigned char ipmi_watchdog_state = WDOG_TIMEOUT_NONE;325326/* If shutting down via IPMI, we ignore the heartbeat. */327static int ipmi_ignore_heartbeat;328329/* Is someone using the watchdog? Only one user is allowed. */330static unsigned long ipmi_wdog_open;331332/*333* If set to 1, the heartbeat command will set the state to reset and334* start the timer. The timer doesn't normally run when the driver is335* first opened until the heartbeat is set the first time, this336* variable is used to accomplish this.337*/338static int ipmi_start_timer_on_heartbeat;339340/* IPMI version of the BMC. */341static unsigned char ipmi_version_major;342static unsigned char ipmi_version_minor;343344/* If a pretimeout occurs, this is used to allow only one panic to happen. */345static atomic_t preop_panic_excl = ATOMIC_INIT(-1);346347#ifdef HAVE_DIE_NMI348static int testing_nmi;349static int nmi_handler_registered;350#endif351352static int ipmi_heartbeat(void);353354/*355* We use a mutex to make sure that only one thing can send a set356* timeout at one time, because we only have one copy of the data.357* The mutex is claimed when the set_timeout is sent and freed358* when both messages are free.359*/360static atomic_t set_timeout_tofree = ATOMIC_INIT(0);361static DEFINE_MUTEX(set_timeout_lock);362static DECLARE_COMPLETION(set_timeout_wait);363static void set_timeout_free_smi(struct ipmi_smi_msg *msg)364{365if (atomic_dec_and_test(&set_timeout_tofree))366complete(&set_timeout_wait);367}368static void set_timeout_free_recv(struct ipmi_recv_msg *msg)369{370if (atomic_dec_and_test(&set_timeout_tofree))371complete(&set_timeout_wait);372}373static struct ipmi_smi_msg set_timeout_smi_msg = {374.done = set_timeout_free_smi375};376static struct ipmi_recv_msg set_timeout_recv_msg = {377.done = set_timeout_free_recv378};379380static int i_ipmi_set_timeout(struct ipmi_smi_msg *smi_msg,381struct ipmi_recv_msg *recv_msg,382int *send_heartbeat_now)383{384struct kernel_ipmi_msg msg;385unsigned char data[6];386int rv;387struct ipmi_system_interface_addr addr;388int hbnow = 0;389390391/* These can be cleared as we are setting the timeout. */392pretimeout_since_last_heartbeat = 0;393394data[0] = 0;395WDOG_SET_TIMER_USE(data[0], WDOG_TIMER_USE_SMS_OS);396397if ((ipmi_version_major > 1)398|| ((ipmi_version_major == 1) && (ipmi_version_minor >= 5))) {399/* This is an IPMI 1.5-only feature. */400data[0] |= WDOG_DONT_STOP_ON_SET;401} else if (ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {402/*403* In ipmi 1.0, setting the timer stops the watchdog, we404* need to start it back up again.405*/406hbnow = 1;407}408409data[1] = 0;410WDOG_SET_TIMEOUT_ACT(data[1], ipmi_watchdog_state);411if ((pretimeout > 0) && (ipmi_watchdog_state != WDOG_TIMEOUT_NONE)) {412WDOG_SET_PRETIMEOUT_ACT(data[1], preaction_val);413data[2] = pretimeout;414} else {415WDOG_SET_PRETIMEOUT_ACT(data[1], WDOG_PRETIMEOUT_NONE);416data[2] = 0; /* No pretimeout. */417}418data[3] = 0;419WDOG_SET_TIMEOUT(data[4], data[5], timeout);420421addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;422addr.channel = IPMI_BMC_CHANNEL;423addr.lun = 0;424425msg.netfn = 0x06;426msg.cmd = IPMI_WDOG_SET_TIMER;427msg.data = data;428msg.data_len = sizeof(data);429rv = ipmi_request_supply_msgs(watchdog_user,430(struct ipmi_addr *) &addr,4310,432&msg,433NULL,434smi_msg,435recv_msg,4361);437if (rv) {438printk(KERN_WARNING PFX "set timeout error: %d\n",439rv);440}441442if (send_heartbeat_now)443*send_heartbeat_now = hbnow;444445return rv;446}447448static int ipmi_set_timeout(int do_heartbeat)449{450int send_heartbeat_now;451int rv;452453454/* We can only send one of these at a time. */455mutex_lock(&set_timeout_lock);456457atomic_set(&set_timeout_tofree, 2);458459rv = i_ipmi_set_timeout(&set_timeout_smi_msg,460&set_timeout_recv_msg,461&send_heartbeat_now);462if (rv) {463mutex_unlock(&set_timeout_lock);464goto out;465}466467wait_for_completion(&set_timeout_wait);468469mutex_unlock(&set_timeout_lock);470471if ((do_heartbeat == IPMI_SET_TIMEOUT_FORCE_HB)472|| ((send_heartbeat_now)473&& (do_heartbeat == IPMI_SET_TIMEOUT_HB_IF_NECESSARY)))474rv = ipmi_heartbeat();475476out:477return rv;478}479480static atomic_t panic_done_count = ATOMIC_INIT(0);481482static void panic_smi_free(struct ipmi_smi_msg *msg)483{484atomic_dec(&panic_done_count);485}486static void panic_recv_free(struct ipmi_recv_msg *msg)487{488atomic_dec(&panic_done_count);489}490491static struct ipmi_smi_msg panic_halt_heartbeat_smi_msg = {492.done = panic_smi_free493};494static struct ipmi_recv_msg panic_halt_heartbeat_recv_msg = {495.done = panic_recv_free496};497498static void panic_halt_ipmi_heartbeat(void)499{500struct kernel_ipmi_msg msg;501struct ipmi_system_interface_addr addr;502int rv;503504/*505* Don't reset the timer if we have the timer turned off, that506* re-enables the watchdog.507*/508if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)509return;510511addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;512addr.channel = IPMI_BMC_CHANNEL;513addr.lun = 0;514515msg.netfn = 0x06;516msg.cmd = IPMI_WDOG_RESET_TIMER;517msg.data = NULL;518msg.data_len = 0;519rv = ipmi_request_supply_msgs(watchdog_user,520(struct ipmi_addr *) &addr,5210,522&msg,523NULL,524&panic_halt_heartbeat_smi_msg,525&panic_halt_heartbeat_recv_msg,5261);527if (!rv)528atomic_add(2, &panic_done_count);529}530531static struct ipmi_smi_msg panic_halt_smi_msg = {532.done = panic_smi_free533};534static struct ipmi_recv_msg panic_halt_recv_msg = {535.done = panic_recv_free536};537538/*539* Special call, doesn't claim any locks. This is only to be called540* at panic or halt time, in run-to-completion mode, when the caller541* is the only CPU and the only thing that will be going is these IPMI542* calls.543*/544static void panic_halt_ipmi_set_timeout(void)545{546int send_heartbeat_now;547int rv;548549/* Wait for the messages to be free. */550while (atomic_read(&panic_done_count) != 0)551ipmi_poll_interface(watchdog_user);552rv = i_ipmi_set_timeout(&panic_halt_smi_msg,553&panic_halt_recv_msg,554&send_heartbeat_now);555if (!rv) {556atomic_add(2, &panic_done_count);557if (send_heartbeat_now)558panic_halt_ipmi_heartbeat();559} else560printk(KERN_WARNING PFX561"Unable to extend the watchdog timeout.");562while (atomic_read(&panic_done_count) != 0)563ipmi_poll_interface(watchdog_user);564}565566/*567* We use a mutex to make sure that only one thing can send a568* heartbeat at one time, because we only have one copy of the data.569* The semaphore is claimed when the set_timeout is sent and freed570* when both messages are free.571*/572static atomic_t heartbeat_tofree = ATOMIC_INIT(0);573static DEFINE_MUTEX(heartbeat_lock);574static DECLARE_COMPLETION(heartbeat_wait);575static void heartbeat_free_smi(struct ipmi_smi_msg *msg)576{577if (atomic_dec_and_test(&heartbeat_tofree))578complete(&heartbeat_wait);579}580static void heartbeat_free_recv(struct ipmi_recv_msg *msg)581{582if (atomic_dec_and_test(&heartbeat_tofree))583complete(&heartbeat_wait);584}585static struct ipmi_smi_msg heartbeat_smi_msg = {586.done = heartbeat_free_smi587};588static struct ipmi_recv_msg heartbeat_recv_msg = {589.done = heartbeat_free_recv590};591592static int ipmi_heartbeat(void)593{594struct kernel_ipmi_msg msg;595int rv;596struct ipmi_system_interface_addr addr;597598if (ipmi_ignore_heartbeat)599return 0;600601if (ipmi_start_timer_on_heartbeat) {602ipmi_start_timer_on_heartbeat = 0;603ipmi_watchdog_state = action_val;604return ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);605} else if (pretimeout_since_last_heartbeat) {606/*607* A pretimeout occurred, make sure we set the timeout.608* We don't want to set the action, though, we want to609* leave that alone (thus it can't be combined with the610* above operation.611*/612return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);613}614615mutex_lock(&heartbeat_lock);616617atomic_set(&heartbeat_tofree, 2);618619/*620* Don't reset the timer if we have the timer turned off, that621* re-enables the watchdog.622*/623if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE) {624mutex_unlock(&heartbeat_lock);625return 0;626}627628addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;629addr.channel = IPMI_BMC_CHANNEL;630addr.lun = 0;631632msg.netfn = 0x06;633msg.cmd = IPMI_WDOG_RESET_TIMER;634msg.data = NULL;635msg.data_len = 0;636rv = ipmi_request_supply_msgs(watchdog_user,637(struct ipmi_addr *) &addr,6380,639&msg,640NULL,641&heartbeat_smi_msg,642&heartbeat_recv_msg,6431);644if (rv) {645mutex_unlock(&heartbeat_lock);646printk(KERN_WARNING PFX "heartbeat failure: %d\n",647rv);648return rv;649}650651/* Wait for the heartbeat to be sent. */652wait_for_completion(&heartbeat_wait);653654if (heartbeat_recv_msg.msg.data[0] != 0) {655/*656* Got an error in the heartbeat response. It was already657* reported in ipmi_wdog_msg_handler, but we should return658* an error here.659*/660rv = -EINVAL;661}662663mutex_unlock(&heartbeat_lock);664665return rv;666}667668static struct watchdog_info ident = {669.options = 0, /* WDIOF_SETTIMEOUT, */670.firmware_version = 1,671.identity = "IPMI"672};673674static int ipmi_ioctl(struct file *file,675unsigned int cmd, unsigned long arg)676{677void __user *argp = (void __user *)arg;678int i;679int val;680681switch (cmd) {682case WDIOC_GETSUPPORT:683i = copy_to_user(argp, &ident, sizeof(ident));684return i ? -EFAULT : 0;685686case WDIOC_SETTIMEOUT:687i = copy_from_user(&val, argp, sizeof(int));688if (i)689return -EFAULT;690timeout = val;691return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);692693case WDIOC_GETTIMEOUT:694i = copy_to_user(argp, &timeout, sizeof(timeout));695if (i)696return -EFAULT;697return 0;698699case WDIOC_SET_PRETIMEOUT:700case WDIOC_SETPRETIMEOUT:701i = copy_from_user(&val, argp, sizeof(int));702if (i)703return -EFAULT;704pretimeout = val;705return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);706707case WDIOC_GET_PRETIMEOUT:708case WDIOC_GETPRETIMEOUT:709i = copy_to_user(argp, &pretimeout, sizeof(pretimeout));710if (i)711return -EFAULT;712return 0;713714case WDIOC_KEEPALIVE:715return ipmi_heartbeat();716717case WDIOC_SETOPTIONS:718i = copy_from_user(&val, argp, sizeof(int));719if (i)720return -EFAULT;721if (val & WDIOS_DISABLECARD) {722ipmi_watchdog_state = WDOG_TIMEOUT_NONE;723ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);724ipmi_start_timer_on_heartbeat = 0;725}726727if (val & WDIOS_ENABLECARD) {728ipmi_watchdog_state = action_val;729ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);730}731return 0;732733case WDIOC_GETSTATUS:734val = 0;735i = copy_to_user(argp, &val, sizeof(val));736if (i)737return -EFAULT;738return 0;739740default:741return -ENOIOCTLCMD;742}743}744745static long ipmi_unlocked_ioctl(struct file *file,746unsigned int cmd,747unsigned long arg)748{749int ret;750751mutex_lock(&ipmi_watchdog_mutex);752ret = ipmi_ioctl(file, cmd, arg);753mutex_unlock(&ipmi_watchdog_mutex);754755return ret;756}757758static ssize_t ipmi_write(struct file *file,759const char __user *buf,760size_t len,761loff_t *ppos)762{763int rv;764765if (len) {766if (!nowayout) {767size_t i;768769/* In case it was set long ago */770expect_close = 0;771772for (i = 0; i != len; i++) {773char c;774775if (get_user(c, buf + i))776return -EFAULT;777if (c == 'V')778expect_close = 42;779}780}781rv = ipmi_heartbeat();782if (rv)783return rv;784}785return len;786}787788static ssize_t ipmi_read(struct file *file,789char __user *buf,790size_t count,791loff_t *ppos)792{793int rv = 0;794wait_queue_t wait;795796if (count <= 0)797return 0;798799/*800* Reading returns if the pretimeout has gone off, and it only does801* it once per pretimeout.802*/803spin_lock(&ipmi_read_lock);804if (!data_to_read) {805if (file->f_flags & O_NONBLOCK) {806rv = -EAGAIN;807goto out;808}809810init_waitqueue_entry(&wait, current);811add_wait_queue(&read_q, &wait);812while (!data_to_read) {813set_current_state(TASK_INTERRUPTIBLE);814spin_unlock(&ipmi_read_lock);815schedule();816spin_lock(&ipmi_read_lock);817}818remove_wait_queue(&read_q, &wait);819820if (signal_pending(current)) {821rv = -ERESTARTSYS;822goto out;823}824}825data_to_read = 0;826827out:828spin_unlock(&ipmi_read_lock);829830if (rv == 0) {831if (copy_to_user(buf, &data_to_read, 1))832rv = -EFAULT;833else834rv = 1;835}836837return rv;838}839840static int ipmi_open(struct inode *ino, struct file *filep)841{842switch (iminor(ino)) {843case WATCHDOG_MINOR:844if (test_and_set_bit(0, &ipmi_wdog_open))845return -EBUSY;846847848/*849* Don't start the timer now, let it start on the850* first heartbeat.851*/852ipmi_start_timer_on_heartbeat = 1;853return nonseekable_open(ino, filep);854855default:856return (-ENODEV);857}858}859860static unsigned int ipmi_poll(struct file *file, poll_table *wait)861{862unsigned int mask = 0;863864poll_wait(file, &read_q, wait);865866spin_lock(&ipmi_read_lock);867if (data_to_read)868mask |= (POLLIN | POLLRDNORM);869spin_unlock(&ipmi_read_lock);870871return mask;872}873874static int ipmi_fasync(int fd, struct file *file, int on)875{876int result;877878result = fasync_helper(fd, file, on, &fasync_q);879880return (result);881}882883static int ipmi_close(struct inode *ino, struct file *filep)884{885if (iminor(ino) == WATCHDOG_MINOR) {886if (expect_close == 42) {887ipmi_watchdog_state = WDOG_TIMEOUT_NONE;888ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);889} else {890printk(KERN_CRIT PFX891"Unexpected close, not stopping watchdog!\n");892ipmi_heartbeat();893}894clear_bit(0, &ipmi_wdog_open);895}896897expect_close = 0;898899return 0;900}901902static const struct file_operations ipmi_wdog_fops = {903.owner = THIS_MODULE,904.read = ipmi_read,905.poll = ipmi_poll,906.write = ipmi_write,907.unlocked_ioctl = ipmi_unlocked_ioctl,908.open = ipmi_open,909.release = ipmi_close,910.fasync = ipmi_fasync,911.llseek = no_llseek,912};913914static struct miscdevice ipmi_wdog_miscdev = {915.minor = WATCHDOG_MINOR,916.name = "watchdog",917.fops = &ipmi_wdog_fops918};919920static void ipmi_wdog_msg_handler(struct ipmi_recv_msg *msg,921void *handler_data)922{923if (msg->msg.data[0] != 0) {924printk(KERN_ERR PFX "response: Error %x on cmd %x\n",925msg->msg.data[0],926msg->msg.cmd);927}928929ipmi_free_recv_msg(msg);930}931932static void ipmi_wdog_pretimeout_handler(void *handler_data)933{934if (preaction_val != WDOG_PRETIMEOUT_NONE) {935if (preop_val == WDOG_PREOP_PANIC) {936if (atomic_inc_and_test(&preop_panic_excl))937panic("Watchdog pre-timeout");938} else if (preop_val == WDOG_PREOP_GIVE_DATA) {939spin_lock(&ipmi_read_lock);940data_to_read = 1;941wake_up_interruptible(&read_q);942kill_fasync(&fasync_q, SIGIO, POLL_IN);943944spin_unlock(&ipmi_read_lock);945}946}947948/*949* On some machines, the heartbeat will give an error and not950* work unless we re-enable the timer. So do so.951*/952pretimeout_since_last_heartbeat = 1;953}954955static struct ipmi_user_hndl ipmi_hndlrs = {956.ipmi_recv_hndl = ipmi_wdog_msg_handler,957.ipmi_watchdog_pretimeout = ipmi_wdog_pretimeout_handler958};959960static void ipmi_register_watchdog(int ipmi_intf)961{962int rv = -EBUSY;963964if (watchdog_user)965goto out;966967if ((ifnum_to_use >= 0) && (ifnum_to_use != ipmi_intf))968goto out;969970watchdog_ifnum = ipmi_intf;971972rv = ipmi_create_user(ipmi_intf, &ipmi_hndlrs, NULL, &watchdog_user);973if (rv < 0) {974printk(KERN_CRIT PFX "Unable to register with ipmi\n");975goto out;976}977978ipmi_get_version(watchdog_user,979&ipmi_version_major,980&ipmi_version_minor);981982rv = misc_register(&ipmi_wdog_miscdev);983if (rv < 0) {984ipmi_destroy_user(watchdog_user);985watchdog_user = NULL;986printk(KERN_CRIT PFX "Unable to register misc device\n");987}988989#ifdef HAVE_DIE_NMI990if (nmi_handler_registered) {991int old_pretimeout = pretimeout;992int old_timeout = timeout;993int old_preop_val = preop_val;994995/*996* Set the pretimeout to go off in a second and give997* ourselves plenty of time to stop the timer.998*/999ipmi_watchdog_state = WDOG_TIMEOUT_RESET;1000preop_val = WDOG_PREOP_NONE; /* Make sure nothing happens */1001pretimeout = 99;1002timeout = 100;10031004testing_nmi = 1;10051006rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);1007if (rv) {1008printk(KERN_WARNING PFX "Error starting timer to"1009" test NMI: 0x%x. The NMI pretimeout will"1010" likely not work\n", rv);1011rv = 0;1012goto out_restore;1013}10141015msleep(1500);10161017if (testing_nmi != 2) {1018printk(KERN_WARNING PFX "IPMI NMI didn't seem to"1019" occur. The NMI pretimeout will"1020" likely not work\n");1021}1022out_restore:1023testing_nmi = 0;1024preop_val = old_preop_val;1025pretimeout = old_pretimeout;1026timeout = old_timeout;1027}1028#endif10291030out:1031if ((start_now) && (rv == 0)) {1032/* Run from startup, so start the timer now. */1033start_now = 0; /* Disable this function after first startup. */1034ipmi_watchdog_state = action_val;1035ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);1036printk(KERN_INFO PFX "Starting now!\n");1037} else {1038/* Stop the timer now. */1039ipmi_watchdog_state = WDOG_TIMEOUT_NONE;1040ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);1041}1042}10431044static void ipmi_unregister_watchdog(int ipmi_intf)1045{1046int rv;10471048if (!watchdog_user)1049goto out;10501051if (watchdog_ifnum != ipmi_intf)1052goto out;10531054/* Make sure no one can call us any more. */1055misc_deregister(&ipmi_wdog_miscdev);10561057/*1058* Wait to make sure the message makes it out. The lower layer has1059* pointers to our buffers, we want to make sure they are done before1060* we release our memory.1061*/1062while (atomic_read(&set_timeout_tofree))1063schedule_timeout_uninterruptible(1);10641065/* Disconnect from IPMI. */1066rv = ipmi_destroy_user(watchdog_user);1067if (rv) {1068printk(KERN_WARNING PFX "error unlinking from IPMI: %d\n",1069rv);1070}1071watchdog_user = NULL;10721073out:1074return;1075}10761077#ifdef HAVE_DIE_NMI1078static int1079ipmi_nmi(struct notifier_block *self, unsigned long val, void *data)1080{1081struct die_args *args = data;10821083if (val != DIE_NMIUNKNOWN)1084return NOTIFY_OK;10851086/* Hack, if it's a memory or I/O error, ignore it. */1087if (args->err & 0xc0)1088return NOTIFY_OK;10891090/*1091* If we get here, it's an NMI that's not a memory or I/O1092* error. We can't truly tell if it's from IPMI or not1093* without sending a message, and sending a message is almost1094* impossible because of locking.1095*/10961097if (testing_nmi) {1098testing_nmi = 2;1099return NOTIFY_STOP;1100}11011102/* If we are not expecting a timeout, ignore it. */1103if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)1104return NOTIFY_OK;11051106if (preaction_val != WDOG_PRETIMEOUT_NMI)1107return NOTIFY_OK;11081109/*1110* If no one else handled the NMI, we assume it was the IPMI1111* watchdog.1112*/1113if (preop_val == WDOG_PREOP_PANIC) {1114/* On some machines, the heartbeat will give1115an error and not work unless we re-enable1116the timer. So do so. */1117pretimeout_since_last_heartbeat = 1;1118if (atomic_inc_and_test(&preop_panic_excl))1119panic(PFX "pre-timeout");1120}11211122return NOTIFY_STOP;1123}11241125static struct notifier_block ipmi_nmi_handler = {1126.notifier_call = ipmi_nmi1127};1128#endif11291130static int wdog_reboot_handler(struct notifier_block *this,1131unsigned long code,1132void *unused)1133{1134static int reboot_event_handled;11351136if ((watchdog_user) && (!reboot_event_handled)) {1137/* Make sure we only do this once. */1138reboot_event_handled = 1;11391140if (code == SYS_POWER_OFF || code == SYS_HALT) {1141/* Disable the WDT if we are shutting down. */1142ipmi_watchdog_state = WDOG_TIMEOUT_NONE;1143panic_halt_ipmi_set_timeout();1144} else if (ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {1145/* Set a long timer to let the reboot happens, but1146reboot if it hangs, but only if the watchdog1147timer was already running. */1148timeout = 120;1149pretimeout = 0;1150ipmi_watchdog_state = WDOG_TIMEOUT_RESET;1151panic_halt_ipmi_set_timeout();1152}1153}1154return NOTIFY_OK;1155}11561157static struct notifier_block wdog_reboot_notifier = {1158.notifier_call = wdog_reboot_handler,1159.next = NULL,1160.priority = 01161};11621163static int wdog_panic_handler(struct notifier_block *this,1164unsigned long event,1165void *unused)1166{1167static int panic_event_handled;11681169/* On a panic, if we have a panic timeout, make sure to extend1170the watchdog timer to a reasonable value to complete the1171panic, if the watchdog timer is running. Plus the1172pretimeout is meaningless at panic time. */1173if (watchdog_user && !panic_event_handled &&1174ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {1175/* Make sure we do this only once. */1176panic_event_handled = 1;11771178timeout = 255;1179pretimeout = 0;1180panic_halt_ipmi_set_timeout();1181}11821183return NOTIFY_OK;1184}11851186static struct notifier_block wdog_panic_notifier = {1187.notifier_call = wdog_panic_handler,1188.next = NULL,1189.priority = 150 /* priority: INT_MAX >= x >= 0 */1190};119111921193static void ipmi_new_smi(int if_num, struct device *device)1194{1195ipmi_register_watchdog(if_num);1196}11971198static void ipmi_smi_gone(int if_num)1199{1200ipmi_unregister_watchdog(if_num);1201}12021203static struct ipmi_smi_watcher smi_watcher = {1204.owner = THIS_MODULE,1205.new_smi = ipmi_new_smi,1206.smi_gone = ipmi_smi_gone1207};12081209static int action_op(const char *inval, char *outval)1210{1211if (outval)1212strcpy(outval, action);12131214if (!inval)1215return 0;12161217if (strcmp(inval, "reset") == 0)1218action_val = WDOG_TIMEOUT_RESET;1219else if (strcmp(inval, "none") == 0)1220action_val = WDOG_TIMEOUT_NONE;1221else if (strcmp(inval, "power_cycle") == 0)1222action_val = WDOG_TIMEOUT_POWER_CYCLE;1223else if (strcmp(inval, "power_off") == 0)1224action_val = WDOG_TIMEOUT_POWER_DOWN;1225else1226return -EINVAL;1227strcpy(action, inval);1228return 0;1229}12301231static int preaction_op(const char *inval, char *outval)1232{1233if (outval)1234strcpy(outval, preaction);12351236if (!inval)1237return 0;12381239if (strcmp(inval, "pre_none") == 0)1240preaction_val = WDOG_PRETIMEOUT_NONE;1241else if (strcmp(inval, "pre_smi") == 0)1242preaction_val = WDOG_PRETIMEOUT_SMI;1243#ifdef HAVE_DIE_NMI1244else if (strcmp(inval, "pre_nmi") == 0)1245preaction_val = WDOG_PRETIMEOUT_NMI;1246#endif1247else if (strcmp(inval, "pre_int") == 0)1248preaction_val = WDOG_PRETIMEOUT_MSG_INT;1249else1250return -EINVAL;1251strcpy(preaction, inval);1252return 0;1253}12541255static int preop_op(const char *inval, char *outval)1256{1257if (outval)1258strcpy(outval, preop);12591260if (!inval)1261return 0;12621263if (strcmp(inval, "preop_none") == 0)1264preop_val = WDOG_PREOP_NONE;1265else if (strcmp(inval, "preop_panic") == 0)1266preop_val = WDOG_PREOP_PANIC;1267else if (strcmp(inval, "preop_give_data") == 0)1268preop_val = WDOG_PREOP_GIVE_DATA;1269else1270return -EINVAL;1271strcpy(preop, inval);1272return 0;1273}12741275static void check_parms(void)1276{1277#ifdef HAVE_DIE_NMI1278int do_nmi = 0;1279int rv;12801281if (preaction_val == WDOG_PRETIMEOUT_NMI) {1282do_nmi = 1;1283if (preop_val == WDOG_PREOP_GIVE_DATA) {1284printk(KERN_WARNING PFX "Pretimeout op is to give data"1285" but NMI pretimeout is enabled, setting"1286" pretimeout op to none\n");1287preop_op("preop_none", NULL);1288do_nmi = 0;1289}1290}1291if (do_nmi && !nmi_handler_registered) {1292rv = register_die_notifier(&ipmi_nmi_handler);1293if (rv) {1294printk(KERN_WARNING PFX1295"Can't register nmi handler\n");1296return;1297} else1298nmi_handler_registered = 1;1299} else if (!do_nmi && nmi_handler_registered) {1300unregister_die_notifier(&ipmi_nmi_handler);1301nmi_handler_registered = 0;1302}1303#endif1304}13051306static int __init ipmi_wdog_init(void)1307{1308int rv;13091310if (action_op(action, NULL)) {1311action_op("reset", NULL);1312printk(KERN_INFO PFX "Unknown action '%s', defaulting to"1313" reset\n", action);1314}13151316if (preaction_op(preaction, NULL)) {1317preaction_op("pre_none", NULL);1318printk(KERN_INFO PFX "Unknown preaction '%s', defaulting to"1319" none\n", preaction);1320}13211322if (preop_op(preop, NULL)) {1323preop_op("preop_none", NULL);1324printk(KERN_INFO PFX "Unknown preop '%s', defaulting to"1325" none\n", preop);1326}13271328check_parms();13291330register_reboot_notifier(&wdog_reboot_notifier);1331atomic_notifier_chain_register(&panic_notifier_list,1332&wdog_panic_notifier);13331334rv = ipmi_smi_watcher_register(&smi_watcher);1335if (rv) {1336#ifdef HAVE_DIE_NMI1337if (nmi_handler_registered)1338unregister_die_notifier(&ipmi_nmi_handler);1339#endif1340atomic_notifier_chain_unregister(&panic_notifier_list,1341&wdog_panic_notifier);1342unregister_reboot_notifier(&wdog_reboot_notifier);1343printk(KERN_WARNING PFX "can't register smi watcher\n");1344return rv;1345}13461347printk(KERN_INFO PFX "driver initialized\n");13481349return 0;1350}13511352static void __exit ipmi_wdog_exit(void)1353{1354ipmi_smi_watcher_unregister(&smi_watcher);1355ipmi_unregister_watchdog(watchdog_ifnum);13561357#ifdef HAVE_DIE_NMI1358if (nmi_handler_registered)1359unregister_die_notifier(&ipmi_nmi_handler);1360#endif13611362atomic_notifier_chain_unregister(&panic_notifier_list,1363&wdog_panic_notifier);1364unregister_reboot_notifier(&wdog_reboot_notifier);1365}1366module_exit(ipmi_wdog_exit);1367module_init(ipmi_wdog_init);1368MODULE_LICENSE("GPL");1369MODULE_AUTHOR("Corey Minyard <[email protected]>");1370MODULE_DESCRIPTION("watchdog timer based upon the IPMI interface.");137113721373