1/*
2 * ipmi_watchdog.c
3 *
4 * A watchdog timer based upon the IPMI interface.
5 *
6 * Author: MontaVista Software, Inc.
7 *         Corey Minyard <minyard@mvista.com>
8 *         source@mvista.com
9 *
10 * Copyright 2002 MontaVista Software Inc.
11 *
12 *  This program is free software; you can redistribute it and/or modify it
13 *  under the terms of the GNU General Public License as published by the
14 *  Free Software Foundation; either version 2 of the License, or (at your
15 *  option) any later version.
16 *
17 *
18 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
19 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24 *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
26 *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
27 *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 *  You should have received a copy of the GNU General Public License along
30 *  with this program; if not, write to the Free Software Foundation, Inc.,
31 *  675 Mass Ave, Cambridge, MA 02139, USA.
32 */
33
34#include <linux/module.h>
35#include <linux/moduleparam.h>
36#include <linux/ipmi.h>
37#include <linux/ipmi_smi.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 <asm/atomic.h>
54
55#ifdef CONFIG_X86_LOCAL_APIC
56#include <asm/apic.h>
57#endif
58
59#define	PFX "IPMI Watchdog: "
60
61/*
62 * The IPMI command/response information for the watchdog timer.
63 */
64
65/* values for byte 1 of the set command, byte 2 of the get response. */
66#define WDOG_DONT_LOG		(1 << 7)
67#define WDOG_DONT_STOP_ON_SET	(1 << 6)
68#define WDOG_SET_TIMER_USE(byte, use) \
69	byte = ((byte) & 0xf8) | ((use) & 0x7)
70#define WDOG_GET_TIMER_USE(byte) ((byte) & 0x7)
71#define WDOG_TIMER_USE_BIOS_FRB2	1
72#define WDOG_TIMER_USE_BIOS_POST	2
73#define WDOG_TIMER_USE_OS_LOAD		3
74#define WDOG_TIMER_USE_SMS_OS		4
75#define WDOG_TIMER_USE_OEM		5
76
77/* values for byte 2 of the set command, byte 3 of the get response. */
78#define WDOG_SET_PRETIMEOUT_ACT(byte, use) \
79	byte = ((byte) & 0x8f) | (((use) & 0x7) << 4)
80#define WDOG_GET_PRETIMEOUT_ACT(byte) (((byte) >> 4) & 0x7)
81#define WDOG_PRETIMEOUT_NONE		0
82#define WDOG_PRETIMEOUT_SMI		1
83#define WDOG_PRETIMEOUT_NMI		2
84#define WDOG_PRETIMEOUT_MSG_INT		3
85
86/* Operations that can be performed on a pretimout. */
87#define WDOG_PREOP_NONE		0
88#define WDOG_PREOP_PANIC	1
89#define WDOG_PREOP_GIVE_DATA	2 /* Cause data to be available to
90                                     read.  Doesn't work in NMI
91                                     mode. */
92
93/* Actions to perform on a full timeout. */
94#define WDOG_SET_TIMEOUT_ACT(byte, use) \
95	byte = ((byte) & 0xf8) | ((use) & 0x7)
96#define WDOG_GET_TIMEOUT_ACT(byte) ((byte) & 0x7)
97#define WDOG_TIMEOUT_NONE		0
98#define WDOG_TIMEOUT_RESET		1
99#define WDOG_TIMEOUT_POWER_DOWN		2
100#define WDOG_TIMEOUT_POWER_CYCLE	3
101
102/* Byte 3 of the get command, byte 4 of the get response is the
103   pre-timeout in seconds. */
104
105/* Bits for setting byte 4 of the set command, byte 5 of the get response. */
106#define WDOG_EXPIRE_CLEAR_BIOS_FRB2	(1 << 1)
107#define WDOG_EXPIRE_CLEAR_BIOS_POST	(1 << 2)
108#define WDOG_EXPIRE_CLEAR_OS_LOAD	(1 << 3)
109#define WDOG_EXPIRE_CLEAR_SMS_OS	(1 << 4)
110#define WDOG_EXPIRE_CLEAR_OEM		(1 << 5)
111
112/* Setting/getting the watchdog timer value.  This is for bytes 5 and
113   6 (the timeout time) of the set command, and bytes 6 and 7 (the
114   timeout time) and 8 and 9 (the current countdown value) of the
115   response.  The timeout value is given in seconds (in the command it
116   is 100ms intervals). */
117#define WDOG_SET_TIMEOUT(byte1, byte2, val) \
118	(byte1) = (((val) * 10) & 0xff), (byte2) = (((val) * 10) >> 8)
119#define WDOG_GET_TIMEOUT(byte1, byte2) \
120	(((byte1) | ((byte2) << 8)) / 10)
121
122#define IPMI_WDOG_RESET_TIMER		0x22
123#define IPMI_WDOG_SET_TIMER		0x24
124#define IPMI_WDOG_GET_TIMER		0x25
125
126/* These are here until the real ones get into the watchdog.h interface. */
127#ifndef WDIOC_GETTIMEOUT
128#define	WDIOC_GETTIMEOUT        _IOW(WATCHDOG_IOCTL_BASE, 20, int)
129#endif
130#ifndef WDIOC_SET_PRETIMEOUT
131#define	WDIOC_SET_PRETIMEOUT     _IOW(WATCHDOG_IOCTL_BASE, 21, int)
132#endif
133#ifndef WDIOC_GET_PRETIMEOUT
134#define	WDIOC_GET_PRETIMEOUT     _IOW(WATCHDOG_IOCTL_BASE, 22, int)
135#endif
136
137static int nowayout = WATCHDOG_NOWAYOUT;
138
139static ipmi_user_t watchdog_user;
140static int watchdog_ifnum;
141
142/* Default the timeout to 10 seconds. */
143static int timeout = 10;
144
145/* The pre-timeout is disabled by default. */
146static int pretimeout;
147
148/* Default action is to reset the board on a timeout. */
149static unsigned char action_val = WDOG_TIMEOUT_RESET;
150
151static char action[16] = "reset";
152
153static unsigned char preaction_val = WDOG_PRETIMEOUT_NONE;
154
155static char preaction[16] = "pre_none";
156
157static unsigned char preop_val = WDOG_PREOP_NONE;
158
159static char preop[16] = "preop_none";
160static DEFINE_SPINLOCK(ipmi_read_lock);
161static char data_to_read;
162static DECLARE_WAIT_QUEUE_HEAD(read_q);
163static struct fasync_struct *fasync_q;
164static char pretimeout_since_last_heartbeat;
165static char expect_close;
166
167static int ifnum_to_use = -1;
168
169static DECLARE_RWSEM(register_sem);
170
171/* Parameters to ipmi_set_timeout */
172#define IPMI_SET_TIMEOUT_NO_HB			0
173#define IPMI_SET_TIMEOUT_HB_IF_NECESSARY	1
174#define IPMI_SET_TIMEOUT_FORCE_HB		2
175
176static int ipmi_set_timeout(int do_heartbeat);
177static void ipmi_register_watchdog(int ipmi_intf);
178static void ipmi_unregister_watchdog(int ipmi_intf);
179
180/* If true, the driver will start running as soon as it is configured
181   and ready. */
182static int start_now;
183
184static int set_param_int(const char *val, struct kernel_param *kp)
185{
186	char *endp;
187	int  l;
188	int  rv = 0;
189
190	if (!val)
191		return -EINVAL;
192	l = simple_strtoul(val, &endp, 0);
193	if (endp == val)
194		return -EINVAL;
195
196	down_read(&register_sem);
197	*((int *)kp->arg) = l;
198	if (watchdog_user)
199		rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
200	up_read(&register_sem);
201
202	return rv;
203}
204
205static int get_param_int(char *buffer, struct kernel_param *kp)
206{
207	return sprintf(buffer, "%i", *((int *)kp->arg));
208}
209
210typedef int (*action_fn)(const char *intval, char *outval);
211
212static int action_op(const char *inval, char *outval);
213static int preaction_op(const char *inval, char *outval);
214static int preop_op(const char *inval, char *outval);
215static void check_parms(void);
216
217static int set_param_str(const char *val, struct kernel_param *kp)
218{
219	action_fn  fn = (action_fn) kp->arg;
220	int        rv = 0;
221	char       valcp[16];
222	char       *s;
223
224	strncpy(valcp, val, 16);
225	valcp[15] = '\0';
226
227	s = strstrip(valcp);
228
229	down_read(&register_sem);
230	rv = fn(s, NULL);
231	if (rv)
232		goto out_unlock;
233
234	check_parms();
235	if (watchdog_user)
236		rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
237
238 out_unlock:
239	up_read(&register_sem);
240	return rv;
241}
242
243static int get_param_str(char *buffer, struct kernel_param *kp)
244{
245	action_fn fn = (action_fn) kp->arg;
246	int       rv;
247
248	rv = fn(NULL, buffer);
249	if (rv)
250		return rv;
251	return strlen(buffer);
252}
253
254
255static int set_param_wdog_ifnum(const char *val, struct kernel_param *kp)
256{
257	int rv = param_set_int(val, kp);
258	if (rv)
259		return rv;
260	if ((ifnum_to_use < 0) || (ifnum_to_use == watchdog_ifnum))
261		return 0;
262
263	ipmi_unregister_watchdog(watchdog_ifnum);
264	ipmi_register_watchdog(ifnum_to_use);
265	return 0;
266}
267
268module_param_call(ifnum_to_use, set_param_wdog_ifnum, get_param_int,
269		  &ifnum_to_use, 0644);
270MODULE_PARM_DESC(ifnum_to_use, "The interface number to use for the watchdog "
271		 "timer.  Setting to -1 defaults to the first registered "
272		 "interface");
273
274module_param_call(timeout, set_param_int, get_param_int, &timeout, 0644);
275MODULE_PARM_DESC(timeout, "Timeout value in seconds.");
276
277module_param_call(pretimeout, set_param_int, get_param_int, &pretimeout, 0644);
278MODULE_PARM_DESC(pretimeout, "Pretimeout value in seconds.");
279
280module_param_call(action, set_param_str, get_param_str, action_op, 0644);
281MODULE_PARM_DESC(action, "Timeout action. One of: "
282		 "reset, none, power_cycle, power_off.");
283
284module_param_call(preaction, set_param_str, get_param_str, preaction_op, 0644);
285MODULE_PARM_DESC(preaction, "Pretimeout action.  One of: "
286		 "pre_none, pre_smi, pre_nmi, pre_int.");
287
288module_param_call(preop, set_param_str, get_param_str, preop_op, 0644);
289MODULE_PARM_DESC(preop, "Pretimeout driver operation.  One of: "
290		 "preop_none, preop_panic, preop_give_data.");
291
292module_param(start_now, int, 0444);
293MODULE_PARM_DESC(start_now, "Set to 1 to start the watchdog as"
294		 "soon as the driver is loaded.");
295
296module_param(nowayout, int, 0644);
297MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
298		 "(default=CONFIG_WATCHDOG_NOWAYOUT)");
299
300/* Default state of the timer. */
301static unsigned char ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
302
303/* If shutting down via IPMI, we ignore the heartbeat. */
304static int ipmi_ignore_heartbeat;
305
306/* Is someone using the watchdog?  Only one user is allowed. */
307static unsigned long ipmi_wdog_open;
308
309/* If set to 1, the heartbeat command will set the state to reset and
310   start the timer.  The timer doesn't normally run when the driver is
311   first opened until the heartbeat is set the first time, this
312   variable is used to accomplish this. */
313static int ipmi_start_timer_on_heartbeat;
314
315/* IPMI version of the BMC. */
316static unsigned char ipmi_version_major;
317static unsigned char ipmi_version_minor;
318
319/* If a pretimeout occurs, this is used to allow only one panic to happen. */
320static atomic_t preop_panic_excl = ATOMIC_INIT(-1);
321
322static int ipmi_heartbeat(void);
323static void panic_halt_ipmi_heartbeat(void);
324
325
326/* We use a mutex to make sure that only one thing can send a set
327   timeout at one time, because we only have one copy of the data.
328   The mutex is claimed when the set_timeout is sent and freed
329   when both messages are free. */
330static atomic_t set_timeout_tofree = ATOMIC_INIT(0);
331static DEFINE_MUTEX(set_timeout_lock);
332static DECLARE_COMPLETION(set_timeout_wait);
333static void set_timeout_free_smi(struct ipmi_smi_msg *msg)
334{
335    if (atomic_dec_and_test(&set_timeout_tofree))
336	    complete(&set_timeout_wait);
337}
338static void set_timeout_free_recv(struct ipmi_recv_msg *msg)
339{
340    if (atomic_dec_and_test(&set_timeout_tofree))
341	    complete(&set_timeout_wait);
342}
343static struct ipmi_smi_msg set_timeout_smi_msg =
344{
345	.done = set_timeout_free_smi
346};
347static struct ipmi_recv_msg set_timeout_recv_msg =
348{
349	.done = set_timeout_free_recv
350};
351
352static int i_ipmi_set_timeout(struct ipmi_smi_msg  *smi_msg,
353			      struct ipmi_recv_msg *recv_msg,
354			      int                  *send_heartbeat_now)
355{
356	struct kernel_ipmi_msg            msg;
357	unsigned char                     data[6];
358	int                               rv;
359	struct ipmi_system_interface_addr addr;
360	int                               hbnow = 0;
361
362
363	data[0] = 0;
364	WDOG_SET_TIMER_USE(data[0], WDOG_TIMER_USE_SMS_OS);
365
366	if ((ipmi_version_major > 1)
367	    || ((ipmi_version_major == 1) && (ipmi_version_minor >= 5)))
368	{
369		/* This is an IPMI 1.5-only feature. */
370		data[0] |= WDOG_DONT_STOP_ON_SET;
371	} else if (ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
372		/* In ipmi 1.0, setting the timer stops the watchdog, we
373		   need to start it back up again. */
374		hbnow = 1;
375	}
376
377	data[1] = 0;
378	WDOG_SET_TIMEOUT_ACT(data[1], ipmi_watchdog_state);
379	if ((pretimeout > 0) && (ipmi_watchdog_state != WDOG_TIMEOUT_NONE)) {
380	    WDOG_SET_PRETIMEOUT_ACT(data[1], preaction_val);
381	    data[2] = pretimeout;
382	} else {
383	    WDOG_SET_PRETIMEOUT_ACT(data[1], WDOG_PRETIMEOUT_NONE);
384	    data[2] = 0; /* No pretimeout. */
385	}
386	data[3] = 0;
387	WDOG_SET_TIMEOUT(data[4], data[5], timeout);
388
389	addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
390	addr.channel = IPMI_BMC_CHANNEL;
391	addr.lun = 0;
392
393	msg.netfn = 0x06;
394	msg.cmd = IPMI_WDOG_SET_TIMER;
395	msg.data = data;
396	msg.data_len = sizeof(data);
397	rv = ipmi_request_supply_msgs(watchdog_user,
398				      (struct ipmi_addr *) &addr,
399				      0,
400				      &msg,
401				      NULL,
402				      smi_msg,
403				      recv_msg,
404				      1);
405	if (rv) {
406		printk(KERN_WARNING PFX "set timeout error: %d\n",
407		       rv);
408	}
409
410	if (send_heartbeat_now)
411	    *send_heartbeat_now = hbnow;
412
413	return rv;
414}
415
416static int ipmi_set_timeout(int do_heartbeat)
417{
418	int send_heartbeat_now;
419	int rv;
420
421
422	/* We can only send one of these at a time. */
423	mutex_lock(&set_timeout_lock);
424
425	atomic_set(&set_timeout_tofree, 2);
426
427	rv = i_ipmi_set_timeout(&set_timeout_smi_msg,
428				&set_timeout_recv_msg,
429				&send_heartbeat_now);
430	if (rv) {
431		mutex_unlock(&set_timeout_lock);
432		goto out;
433	}
434
435	wait_for_completion(&set_timeout_wait);
436
437	if ((do_heartbeat == IPMI_SET_TIMEOUT_FORCE_HB)
438	    || ((send_heartbeat_now)
439		&& (do_heartbeat == IPMI_SET_TIMEOUT_HB_IF_NECESSARY)))
440	{
441		rv = ipmi_heartbeat();
442	}
443	mutex_unlock(&set_timeout_lock);
444
445out:
446	return rv;
447}
448
449static void dummy_smi_free(struct ipmi_smi_msg *msg)
450{
451}
452static void dummy_recv_free(struct ipmi_recv_msg *msg)
453{
454}
455static struct ipmi_smi_msg panic_halt_smi_msg =
456{
457	.done = dummy_smi_free
458};
459static struct ipmi_recv_msg panic_halt_recv_msg =
460{
461	.done = dummy_recv_free
462};
463
464/* Special call, doesn't claim any locks.  This is only to be called
465   at panic or halt time, in run-to-completion mode, when the caller
466   is the only CPU and the only thing that will be going is these IPMI
467   calls. */
468static void panic_halt_ipmi_set_timeout(void)
469{
470	int send_heartbeat_now;
471	int rv;
472
473	rv = i_ipmi_set_timeout(&panic_halt_smi_msg,
474				&panic_halt_recv_msg,
475				&send_heartbeat_now);
476	if (!rv) {
477		if (send_heartbeat_now)
478			panic_halt_ipmi_heartbeat();
479	}
480}
481
482/* We use a semaphore to make sure that only one thing can send a
483   heartbeat at one time, because we only have one copy of the data.
484   The semaphore is claimed when the set_timeout is sent and freed
485   when both messages are free. */
486static atomic_t heartbeat_tofree = ATOMIC_INIT(0);
487static DEFINE_MUTEX(heartbeat_lock);
488static DECLARE_COMPLETION(heartbeat_wait);
489static void heartbeat_free_smi(struct ipmi_smi_msg *msg)
490{
491    if (atomic_dec_and_test(&heartbeat_tofree))
492	    complete(&heartbeat_wait);
493}
494static void heartbeat_free_recv(struct ipmi_recv_msg *msg)
495{
496    if (atomic_dec_and_test(&heartbeat_tofree))
497	    complete(&heartbeat_wait);
498}
499static struct ipmi_smi_msg heartbeat_smi_msg =
500{
501	.done = heartbeat_free_smi
502};
503static struct ipmi_recv_msg heartbeat_recv_msg =
504{
505	.done = heartbeat_free_recv
506};
507
508static struct ipmi_smi_msg panic_halt_heartbeat_smi_msg =
509{
510	.done = dummy_smi_free
511};
512static struct ipmi_recv_msg panic_halt_heartbeat_recv_msg =
513{
514	.done = dummy_recv_free
515};
516
517static int ipmi_heartbeat(void)
518{
519	struct kernel_ipmi_msg            msg;
520	int                               rv;
521	struct ipmi_system_interface_addr addr;
522
523	if (ipmi_ignore_heartbeat) {
524		return 0;
525	}
526
527	if (ipmi_start_timer_on_heartbeat) {
528		ipmi_start_timer_on_heartbeat = 0;
529		ipmi_watchdog_state = action_val;
530		return ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
531	} else if (pretimeout_since_last_heartbeat) {
532		/* A pretimeout occurred, make sure we set the timeout.
533		   We don't want to set the action, though, we want to
534		   leave that alone (thus it can't be combined with the
535		   above operation. */
536		pretimeout_since_last_heartbeat = 0;
537		return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
538	}
539
540	mutex_lock(&heartbeat_lock);
541
542	atomic_set(&heartbeat_tofree, 2);
543
544	/* Don't reset the timer if we have the timer turned off, that
545           re-enables the watchdog. */
546	if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE) {
547		mutex_unlock(&heartbeat_lock);
548		return 0;
549	}
550
551	addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
552	addr.channel = IPMI_BMC_CHANNEL;
553	addr.lun = 0;
554
555	msg.netfn = 0x06;
556	msg.cmd = IPMI_WDOG_RESET_TIMER;
557	msg.data = NULL;
558	msg.data_len = 0;
559	rv = ipmi_request_supply_msgs(watchdog_user,
560				      (struct ipmi_addr *) &addr,
561				      0,
562				      &msg,
563				      NULL,
564				      &heartbeat_smi_msg,
565				      &heartbeat_recv_msg,
566				      1);
567	if (rv) {
568		mutex_unlock(&heartbeat_lock);
569		printk(KERN_WARNING PFX "heartbeat failure: %d\n",
570		       rv);
571		return rv;
572	}
573
574	/* Wait for the heartbeat to be sent. */
575	wait_for_completion(&heartbeat_wait);
576
577	if (heartbeat_recv_msg.msg.data[0] != 0) {
578	    /* Got an error in the heartbeat response.  It was already
579	       reported in ipmi_wdog_msg_handler, but we should return
580	       an error here. */
581	    rv = -EINVAL;
582	}
583
584	mutex_unlock(&heartbeat_lock);
585
586	return rv;
587}
588
589static void panic_halt_ipmi_heartbeat(void)
590{
591	struct kernel_ipmi_msg             msg;
592	struct ipmi_system_interface_addr addr;
593
594
595	/* Don't reset the timer if we have the timer turned off, that
596           re-enables the watchdog. */
597	if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)
598		return;
599
600	addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
601	addr.channel = IPMI_BMC_CHANNEL;
602	addr.lun = 0;
603
604	msg.netfn = 0x06;
605	msg.cmd = IPMI_WDOG_RESET_TIMER;
606	msg.data = NULL;
607	msg.data_len = 0;
608	ipmi_request_supply_msgs(watchdog_user,
609				 (struct ipmi_addr *) &addr,
610				 0,
611				 &msg,
612				 NULL,
613				 &panic_halt_heartbeat_smi_msg,
614				 &panic_halt_heartbeat_recv_msg,
615				 1);
616}
617
618static struct watchdog_info ident =
619{
620	.options	= 0,	/* WDIOF_SETTIMEOUT, */
621	.firmware_version = 1,
622	.identity	= "IPMI"
623};
624
625static int ipmi_ioctl(struct inode *inode, struct file *file,
626		      unsigned int cmd, unsigned long arg)
627{
628	void __user *argp = (void __user *)arg;
629	int i;
630	int val;
631
632	switch(cmd) {
633	case WDIOC_GETSUPPORT:
634		i = copy_to_user(argp, &ident, sizeof(ident));
635		return i ? -EFAULT : 0;
636
637	case WDIOC_SETTIMEOUT:
638		i = copy_from_user(&val, argp, sizeof(int));
639		if (i)
640			return -EFAULT;
641		timeout = val;
642		return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
643
644	case WDIOC_GETTIMEOUT:
645		i = copy_to_user(argp, &timeout, sizeof(timeout));
646		if (i)
647			return -EFAULT;
648		return 0;
649
650	case WDIOC_SET_PRETIMEOUT:
651		i = copy_from_user(&val, argp, sizeof(int));
652		if (i)
653			return -EFAULT;
654		pretimeout = val;
655		return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
656
657	case WDIOC_GET_PRETIMEOUT:
658		i = copy_to_user(argp, &pretimeout, sizeof(pretimeout));
659		if (i)
660			return -EFAULT;
661		return 0;
662
663	case WDIOC_KEEPALIVE:
664		return ipmi_heartbeat();
665
666	case WDIOC_SETOPTIONS:
667		i = copy_from_user(&val, argp, sizeof(int));
668		if (i)
669			return -EFAULT;
670		if (val & WDIOS_DISABLECARD)
671		{
672			ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
673			ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
674			ipmi_start_timer_on_heartbeat = 0;
675		}
676
677		if (val & WDIOS_ENABLECARD)
678		{
679			ipmi_watchdog_state = action_val;
680			ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
681		}
682		return 0;
683
684	case WDIOC_GETSTATUS:
685		val = 0;
686		i = copy_to_user(argp, &val, sizeof(val));
687		if (i)
688			return -EFAULT;
689		return 0;
690
691	default:
692		return -ENOIOCTLCMD;
693	}
694}
695
696static ssize_t ipmi_write(struct file *file,
697			  const char  __user *buf,
698			  size_t      len,
699			  loff_t      *ppos)
700{
701	int rv;
702
703	if (len) {
704	    	if (!nowayout) {
705		    	size_t i;
706
707			/* In case it was set long ago */
708			expect_close = 0;
709
710    			for (i = 0; i != len; i++) {
711				char c;
712
713				if (get_user(c, buf + i))
714					return -EFAULT;
715				if (c == 'V')
716					expect_close = 42;
717			}
718		}
719		rv = ipmi_heartbeat();
720		if (rv)
721			return rv;
722		return 1;
723	}
724	return 0;
725}
726
727static ssize_t ipmi_read(struct file *file,
728			 char        __user *buf,
729			 size_t      count,
730			 loff_t      *ppos)
731{
732	int          rv = 0;
733	wait_queue_t wait;
734
735	if (count <= 0)
736		return 0;
737
738	/* Reading returns if the pretimeout has gone off, and it only does
739	   it once per pretimeout. */
740	spin_lock(&ipmi_read_lock);
741	if (!data_to_read) {
742		if (file->f_flags & O_NONBLOCK) {
743			rv = -EAGAIN;
744			goto out;
745		}
746
747		init_waitqueue_entry(&wait, current);
748		add_wait_queue(&read_q, &wait);
749		while (!data_to_read) {
750			set_current_state(TASK_INTERRUPTIBLE);
751			spin_unlock(&ipmi_read_lock);
752			schedule();
753			spin_lock(&ipmi_read_lock);
754		}
755		remove_wait_queue(&read_q, &wait);
756
757		if (signal_pending(current)) {
758			rv = -ERESTARTSYS;
759			goto out;
760		}
761	}
762	data_to_read = 0;
763
764 out:
765	spin_unlock(&ipmi_read_lock);
766
767	if (rv == 0) {
768		if (copy_to_user(buf, &data_to_read, 1))
769			rv = -EFAULT;
770		else
771			rv = 1;
772	}
773
774	return rv;
775}
776
777static int ipmi_open(struct inode *ino, struct file *filep)
778{
779        switch (iminor(ino)) {
780        case WATCHDOG_MINOR:
781		if (test_and_set_bit(0, &ipmi_wdog_open))
782                        return -EBUSY;
783
784		/* Don't start the timer now, let it start on the
785		   first heartbeat. */
786		ipmi_start_timer_on_heartbeat = 1;
787		return nonseekable_open(ino, filep);
788
789	default:
790		return (-ENODEV);
791        }
792}
793
794static unsigned int ipmi_poll(struct file *file, poll_table *wait)
795{
796	unsigned int mask = 0;
797
798	poll_wait(file, &read_q, wait);
799
800	spin_lock(&ipmi_read_lock);
801	if (data_to_read)
802		mask |= (POLLIN | POLLRDNORM);
803	spin_unlock(&ipmi_read_lock);
804
805	return mask;
806}
807
808static int ipmi_fasync(int fd, struct file *file, int on)
809{
810	int result;
811
812	result = fasync_helper(fd, file, on, &fasync_q);
813
814	return (result);
815}
816
817static int ipmi_close(struct inode *ino, struct file *filep)
818{
819	if (iminor(ino) == WATCHDOG_MINOR) {
820		if (expect_close == 42) {
821			ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
822			ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
823		} else {
824			printk(KERN_CRIT PFX
825			       "Unexpected close, not stopping watchdog!\n");
826			ipmi_heartbeat();
827		}
828		clear_bit(0, &ipmi_wdog_open);
829	}
830
831	ipmi_fasync (-1, filep, 0);
832	expect_close = 0;
833
834	return 0;
835}
836
837static const struct file_operations ipmi_wdog_fops = {
838	.owner   = THIS_MODULE,
839	.read    = ipmi_read,
840	.poll    = ipmi_poll,
841	.write   = ipmi_write,
842	.ioctl   = ipmi_ioctl,
843	.open    = ipmi_open,
844	.release = ipmi_close,
845	.fasync  = ipmi_fasync,
846};
847
848static struct miscdevice ipmi_wdog_miscdev = {
849	.minor		= WATCHDOG_MINOR,
850	.name		= "watchdog",
851	.fops		= &ipmi_wdog_fops
852};
853
854static void ipmi_wdog_msg_handler(struct ipmi_recv_msg *msg,
855				  void                 *handler_data)
856{
857	if (msg->msg.data[0] != 0) {
858		printk(KERN_ERR PFX "response: Error %x on cmd %x\n",
859		       msg->msg.data[0],
860		       msg->msg.cmd);
861	}
862
863	ipmi_free_recv_msg(msg);
864}
865
866static void ipmi_wdog_pretimeout_handler(void *handler_data)
867{
868	if (preaction_val != WDOG_PRETIMEOUT_NONE) {
869		if (preop_val == WDOG_PREOP_PANIC) {
870			if (atomic_inc_and_test(&preop_panic_excl))
871				panic("Watchdog pre-timeout");
872		} else if (preop_val == WDOG_PREOP_GIVE_DATA) {
873			spin_lock(&ipmi_read_lock);
874			data_to_read = 1;
875			wake_up_interruptible(&read_q);
876			kill_fasync(&fasync_q, SIGIO, POLL_IN);
877
878			spin_unlock(&ipmi_read_lock);
879		}
880	}
881
882	/* On some machines, the heartbeat will give
883	   an error and not work unless we re-enable
884	   the timer.   So do so. */
885	pretimeout_since_last_heartbeat = 1;
886}
887
888static struct ipmi_user_hndl ipmi_hndlrs =
889{
890	.ipmi_recv_hndl           = ipmi_wdog_msg_handler,
891	.ipmi_watchdog_pretimeout = ipmi_wdog_pretimeout_handler
892};
893
894static void ipmi_register_watchdog(int ipmi_intf)
895{
896	int rv = -EBUSY;
897
898	down_write(&register_sem);
899	if (watchdog_user)
900		goto out;
901
902	if ((ifnum_to_use >= 0) && (ifnum_to_use != ipmi_intf))
903		goto out;
904
905	watchdog_ifnum = ipmi_intf;
906
907	rv = ipmi_create_user(ipmi_intf, &ipmi_hndlrs, NULL, &watchdog_user);
908	if (rv < 0) {
909		printk(KERN_CRIT PFX "Unable to register with ipmi\n");
910		goto out;
911	}
912
913	ipmi_get_version(watchdog_user,
914			 &ipmi_version_major,
915			 &ipmi_version_minor);
916
917	rv = misc_register(&ipmi_wdog_miscdev);
918	if (rv < 0) {
919		ipmi_destroy_user(watchdog_user);
920		watchdog_user = NULL;
921		printk(KERN_CRIT PFX "Unable to register misc device\n");
922	}
923
924 out:
925	up_write(&register_sem);
926
927	if ((start_now) && (rv == 0)) {
928		/* Run from startup, so start the timer now. */
929		start_now = 0; /* Disable this function after first startup. */
930		ipmi_watchdog_state = action_val;
931		ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
932		printk(KERN_INFO PFX "Starting now!\n");
933	}
934}
935
936static void ipmi_unregister_watchdog(int ipmi_intf)
937{
938	int rv;
939
940	down_write(&register_sem);
941
942	if (!watchdog_user)
943		goto out;
944
945	if (watchdog_ifnum != ipmi_intf)
946		goto out;
947
948	/* Make sure no one can call us any more. */
949	misc_deregister(&ipmi_wdog_miscdev);
950
951	/* Wait to make sure the message makes it out.  The lower layer has
952	   pointers to our buffers, we want to make sure they are done before
953	   we release our memory. */
954	while (atomic_read(&set_timeout_tofree))
955		schedule_timeout_uninterruptible(1);
956
957	/* Disconnect from IPMI. */
958	rv = ipmi_destroy_user(watchdog_user);
959	if (rv) {
960		printk(KERN_WARNING PFX "error unlinking from IPMI: %d\n",
961		       rv);
962	}
963	watchdog_user = NULL;
964
965 out:
966	up_write(&register_sem);
967}
968
969#ifdef HAVE_NMI_HANDLER
970static int
971ipmi_nmi(void *dev_id, int cpu, int handled)
972{
973        /* If we are not expecting a timeout, ignore it. */
974	if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)
975		return NOTIFY_DONE;
976
977	/* If no one else handled the NMI, we assume it was the IPMI
978           watchdog. */
979	if ((!handled) && (preop_val == WDOG_PREOP_PANIC)) {
980		/* On some machines, the heartbeat will give
981		   an error and not work unless we re-enable
982		   the timer.   So do so. */
983		pretimeout_since_last_heartbeat = 1;
984		if (atomic_inc_and_test(&preop_panic_excl))
985			panic(PFX "pre-timeout");
986	}
987
988	return NOTIFY_DONE;
989}
990
991static struct nmi_handler ipmi_nmi_handler =
992{
993	.link     = LIST_HEAD_INIT(ipmi_nmi_handler.link),
994	.dev_name = "ipmi_watchdog",
995	.dev_id   = NULL,
996	.handler  = ipmi_nmi,
997	.priority = 0, /* Call us last. */
998};
999int nmi_handler_registered;
1000#endif
1001
1002static int wdog_reboot_handler(struct notifier_block *this,
1003			       unsigned long         code,
1004			       void                  *unused)
1005{
1006	static int reboot_event_handled = 0;
1007
1008	if ((watchdog_user) && (!reboot_event_handled)) {
1009		/* Make sure we only do this once. */
1010		reboot_event_handled = 1;
1011
1012		if (code == SYS_DOWN || code == SYS_HALT) {
1013			/* Disable the WDT if we are shutting down. */
1014			ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
1015			panic_halt_ipmi_set_timeout();
1016		} else if (ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
1017			/* Set a long timer to let the reboot happens, but
1018			   reboot if it hangs, but only if the watchdog
1019			   timer was already running. */
1020			timeout = 120;
1021			pretimeout = 0;
1022			ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
1023			panic_halt_ipmi_set_timeout();
1024		}
1025	}
1026	return NOTIFY_OK;
1027}
1028
1029static struct notifier_block wdog_reboot_notifier = {
1030	.notifier_call	= wdog_reboot_handler,
1031	.next		= NULL,
1032	.priority	= 0
1033};
1034
1035static int wdog_panic_handler(struct notifier_block *this,
1036			      unsigned long         event,
1037			      void                  *unused)
1038{
1039	static int panic_event_handled = 0;
1040
1041	/* On a panic, if we have a panic timeout, make sure to extend
1042	   the watchdog timer to a reasonable value to complete the
1043	   panic, if the watchdog timer is running.  Plus the
1044	   pretimeout is meaningless at panic time. */
1045	if (watchdog_user && !panic_event_handled &&
1046	    ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
1047		/* Make sure we do this only once. */
1048		panic_event_handled = 1;
1049
1050		timeout = 255;
1051		pretimeout = 0;
1052		panic_halt_ipmi_set_timeout();
1053	}
1054
1055	return NOTIFY_OK;
1056}
1057
1058static struct notifier_block wdog_panic_notifier = {
1059	.notifier_call	= wdog_panic_handler,
1060	.next		= NULL,
1061	.priority	= 150	/* priority: INT_MAX >= x >= 0 */
1062};
1063
1064
1065static void ipmi_new_smi(int if_num, struct device *device)
1066{
1067	ipmi_register_watchdog(if_num);
1068}
1069
1070static void ipmi_smi_gone(int if_num)
1071{
1072	ipmi_unregister_watchdog(if_num);
1073}
1074
1075static struct ipmi_smi_watcher smi_watcher =
1076{
1077	.owner    = THIS_MODULE,
1078	.new_smi  = ipmi_new_smi,
1079	.smi_gone = ipmi_smi_gone
1080};
1081
1082static int action_op(const char *inval, char *outval)
1083{
1084	if (outval)
1085		strcpy(outval, action);
1086
1087	if (!inval)
1088		return 0;
1089
1090	if (strcmp(inval, "reset") == 0)
1091		action_val = WDOG_TIMEOUT_RESET;
1092	else if (strcmp(inval, "none") == 0)
1093		action_val = WDOG_TIMEOUT_NONE;
1094	else if (strcmp(inval, "power_cycle") == 0)
1095		action_val = WDOG_TIMEOUT_POWER_CYCLE;
1096	else if (strcmp(inval, "power_off") == 0)
1097		action_val = WDOG_TIMEOUT_POWER_DOWN;
1098	else
1099		return -EINVAL;
1100	strcpy(action, inval);
1101	return 0;
1102}
1103
1104static int preaction_op(const char *inval, char *outval)
1105{
1106	if (outval)
1107		strcpy(outval, preaction);
1108
1109	if (!inval)
1110		return 0;
1111
1112	if (strcmp(inval, "pre_none") == 0)
1113		preaction_val = WDOG_PRETIMEOUT_NONE;
1114	else if (strcmp(inval, "pre_smi") == 0)
1115		preaction_val = WDOG_PRETIMEOUT_SMI;
1116#ifdef HAVE_NMI_HANDLER
1117	else if (strcmp(inval, "pre_nmi") == 0)
1118		preaction_val = WDOG_PRETIMEOUT_NMI;
1119#endif
1120	else if (strcmp(inval, "pre_int") == 0)
1121		preaction_val = WDOG_PRETIMEOUT_MSG_INT;
1122	else
1123		return -EINVAL;
1124	strcpy(preaction, inval);
1125	return 0;
1126}
1127
1128static int preop_op(const char *inval, char *outval)
1129{
1130	if (outval)
1131		strcpy(outval, preop);
1132
1133	if (!inval)
1134		return 0;
1135
1136	if (strcmp(inval, "preop_none") == 0)
1137		preop_val = WDOG_PREOP_NONE;
1138	else if (strcmp(inval, "preop_panic") == 0)
1139		preop_val = WDOG_PREOP_PANIC;
1140	else if (strcmp(inval, "preop_give_data") == 0)
1141		preop_val = WDOG_PREOP_GIVE_DATA;
1142	else
1143		return -EINVAL;
1144	strcpy(preop, inval);
1145	return 0;
1146}
1147
1148static void check_parms(void)
1149{
1150#ifdef HAVE_NMI_HANDLER
1151	int do_nmi = 0;
1152	int rv;
1153
1154	if (preaction_val == WDOG_PRETIMEOUT_NMI) {
1155		do_nmi = 1;
1156		if (preop_val == WDOG_PREOP_GIVE_DATA) {
1157			printk(KERN_WARNING PFX "Pretimeout op is to give data"
1158			       " but NMI pretimeout is enabled, setting"
1159			       " pretimeout op to none\n");
1160			preop_op("preop_none", NULL);
1161			do_nmi = 0;
1162		}
1163#ifdef CONFIG_X86_LOCAL_APIC
1164		if (nmi_watchdog == NMI_IO_APIC) {
1165			printk(KERN_WARNING PFX "nmi_watchdog is set to IO APIC"
1166			       " mode (value is %d), that is incompatible"
1167			       " with using NMI in the IPMI watchdog."
1168			       " Disabling IPMI nmi pretimeout.\n",
1169			       nmi_watchdog);
1170			preaction_val = WDOG_PRETIMEOUT_NONE;
1171			do_nmi = 0;
1172		}
1173#endif
1174	}
1175	if (do_nmi && !nmi_handler_registered) {
1176		rv = request_nmi(&ipmi_nmi_handler);
1177		if (rv) {
1178			printk(KERN_WARNING PFX
1179			       "Can't register nmi handler\n");
1180			return;
1181		} else
1182			nmi_handler_registered = 1;
1183	} else if (!do_nmi && nmi_handler_registered) {
1184		release_nmi(&ipmi_nmi_handler);
1185		nmi_handler_registered = 0;
1186	}
1187#endif
1188}
1189
1190static int __init ipmi_wdog_init(void)
1191{
1192	int rv;
1193
1194	if (action_op(action, NULL)) {
1195		action_op("reset", NULL);
1196		printk(KERN_INFO PFX "Unknown action '%s', defaulting to"
1197		       " reset\n", action);
1198	}
1199
1200	if (preaction_op(preaction, NULL)) {
1201		preaction_op("pre_none", NULL);
1202		printk(KERN_INFO PFX "Unknown preaction '%s', defaulting to"
1203		       " none\n", preaction);
1204	}
1205
1206	if (preop_op(preop, NULL)) {
1207		preop_op("preop_none", NULL);
1208		printk(KERN_INFO PFX "Unknown preop '%s', defaulting to"
1209		       " none\n", preop);
1210	}
1211
1212	check_parms();
1213
1214	register_reboot_notifier(&wdog_reboot_notifier);
1215	atomic_notifier_chain_register(&panic_notifier_list,
1216			&wdog_panic_notifier);
1217
1218	rv = ipmi_smi_watcher_register(&smi_watcher);
1219	if (rv) {
1220#ifdef HAVE_NMI_HANDLER
1221		if (preaction_val == WDOG_PRETIMEOUT_NMI)
1222			release_nmi(&ipmi_nmi_handler);
1223#endif
1224		atomic_notifier_chain_unregister(&panic_notifier_list,
1225						 &wdog_panic_notifier);
1226		unregister_reboot_notifier(&wdog_reboot_notifier);
1227		printk(KERN_WARNING PFX "can't register smi watcher\n");
1228		return rv;
1229	}
1230
1231	printk(KERN_INFO PFX "driver initialized\n");
1232
1233	return 0;
1234}
1235
1236static void __exit ipmi_wdog_exit(void)
1237{
1238	ipmi_smi_watcher_unregister(&smi_watcher);
1239	ipmi_unregister_watchdog(watchdog_ifnum);
1240
1241#ifdef HAVE_NMI_HANDLER
1242	if (nmi_handler_registered)
1243		release_nmi(&ipmi_nmi_handler);
1244#endif
1245
1246	atomic_notifier_chain_unregister(&panic_notifier_list,
1247					 &wdog_panic_notifier);
1248	unregister_reboot_notifier(&wdog_reboot_notifier);
1249}
1250module_exit(ipmi_wdog_exit);
1251module_init(ipmi_wdog_init);
1252MODULE_LICENSE("GPL");
1253MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
1254MODULE_DESCRIPTION("watchdog timer based upon the IPMI interface.");
1255