VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / char / ipmi / ipmi_watchdog.c
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/config.h>
35 #include <linux/module.h>
36 #include <linux/moduleparam.h>
37 #include <linux/ipmi.h>
38 #include <linux/ipmi_smi.h>
39 #include <linux/watchdog.h>
40 #include <linux/miscdevice.h>
41 #include <linux/init.h>
42 #include <linux/rwsem.h>
43 #include <linux/errno.h>
44 #include <asm/uaccess.h>
45 #include <linux/notifier.h>
46 #include <linux/nmi.h>
47 #include <linux/reboot.h>
48 #include <linux/wait.h>
49 #include <linux/poll.h>
50 #ifdef CONFIG_X86_LOCAL_APIC
51 #include <asm/apic.h>
52 #endif
53
54 #define IPMI_WATCHDOG_VERSION "v32"
55
56 /*
57  * The IPMI command/response information for the watchdog timer.
58  */
59
60 /* values for byte 1 of the set command, byte 2 of the get response. */
61 #define WDOG_DONT_LOG           (1 << 7)
62 #define WDOG_DONT_STOP_ON_SET   (1 << 6)
63 #define WDOG_SET_TIMER_USE(byte, use) \
64         byte = ((byte) & 0xf8) | ((use) & 0x7)
65 #define WDOG_GET_TIMER_USE(byte) ((byte) & 0x7)
66 #define WDOG_TIMER_USE_BIOS_FRB2        1
67 #define WDOG_TIMER_USE_BIOS_POST        2
68 #define WDOG_TIMER_USE_OS_LOAD          3
69 #define WDOG_TIMER_USE_SMS_OS           4
70 #define WDOG_TIMER_USE_OEM              5
71
72 /* values for byte 2 of the set command, byte 3 of the get response. */
73 #define WDOG_SET_PRETIMEOUT_ACT(byte, use) \
74         byte = ((byte) & 0x8f) | (((use) & 0x7) << 4)
75 #define WDOG_GET_PRETIMEOUT_ACT(byte) (((byte) >> 4) & 0x7)
76 #define WDOG_PRETIMEOUT_NONE            0
77 #define WDOG_PRETIMEOUT_SMI             1
78 #define WDOG_PRETIMEOUT_NMI             2
79 #define WDOG_PRETIMEOUT_MSG_INT         3
80
81 /* Operations that can be performed on a pretimout. */
82 #define WDOG_PREOP_NONE         0
83 #define WDOG_PREOP_PANIC        1
84 #define WDOG_PREOP_GIVE_DATA    2 /* Cause data to be available to
85                                      read.  Doesn't work in NMI
86                                      mode. */
87
88 /* Actions to perform on a full timeout. */
89 #define WDOG_SET_TIMEOUT_ACT(byte, use) \
90         byte = ((byte) & 0xf8) | ((use) & 0x7)
91 #define WDOG_GET_TIMEOUT_ACT(byte) ((byte) & 0x7)
92 #define WDOG_TIMEOUT_NONE               0
93 #define WDOG_TIMEOUT_RESET              1
94 #define WDOG_TIMEOUT_POWER_DOWN         2
95 #define WDOG_TIMEOUT_POWER_CYCLE        3
96
97 /* Byte 3 of the get command, byte 4 of the get response is the
98    pre-timeout in seconds. */
99
100 /* Bits for setting byte 4 of the set command, byte 5 of the get response. */
101 #define WDOG_EXPIRE_CLEAR_BIOS_FRB2     (1 << 1)
102 #define WDOG_EXPIRE_CLEAR_BIOS_POST     (1 << 2)
103 #define WDOG_EXPIRE_CLEAR_OS_LOAD       (1 << 3)
104 #define WDOG_EXPIRE_CLEAR_SMS_OS        (1 << 4)
105 #define WDOG_EXPIRE_CLEAR_OEM           (1 << 5)
106
107 /* Setting/getting the watchdog timer value.  This is for bytes 5 and
108    6 (the timeout time) of the set command, and bytes 6 and 7 (the
109    timeout time) and 8 and 9 (the current countdown value) of the
110    response.  The timeout value is given in seconds (in the command it
111    is 100ms intervals). */
112 #define WDOG_SET_TIMEOUT(byte1, byte2, val) \
113         (byte1) = (((val) * 10) & 0xff), (byte2) = (((val) * 10) >> 8)
114 #define WDOG_GET_TIMEOUT(byte1, byte2) \
115         (((byte1) | ((byte2) << 8)) / 10)
116
117 #define IPMI_WDOG_RESET_TIMER           0x22
118 #define IPMI_WDOG_SET_TIMER             0x24
119 #define IPMI_WDOG_GET_TIMER             0x25
120
121 /* These are here until the real ones get into the watchdog.h interface. */
122 #ifndef WDIOC_GETTIMEOUT
123 #define WDIOC_GETTIMEOUT        _IOW(WATCHDOG_IOCTL_BASE, 20, int)
124 #endif
125 #ifndef WDIOC_SET_PRETIMEOUT
126 #define WDIOC_SET_PRETIMEOUT     _IOW(WATCHDOG_IOCTL_BASE, 21, int)
127 #endif
128 #ifndef WDIOC_GET_PRETIMEOUT
129 #define WDIOC_GET_PRETIMEOUT     _IOW(WATCHDOG_IOCTL_BASE, 22, int)
130 #endif
131
132 #ifdef CONFIG_WATCHDOG_NOWAYOUT
133 static int nowayout = 1;
134 #else
135 static int nowayout;
136 #endif
137
138 static ipmi_user_t watchdog_user = NULL;
139
140 /* Default the timeout to 10 seconds. */
141 static int timeout = 10;
142
143 /* The pre-timeout is disabled by default. */
144 static int pretimeout = 0;
145
146 /* Default action is to reset the board on a timeout. */
147 static unsigned char action_val = WDOG_TIMEOUT_RESET;
148
149 static char action[16] = "reset";
150
151 static unsigned char preaction_val = WDOG_PRETIMEOUT_NONE;
152
153 static char preaction[16] = "pre_none";
154
155 static unsigned char preop_val = WDOG_PREOP_NONE;
156
157 static char preop[16] = "preop_none";
158 static spinlock_t ipmi_read_lock = SPIN_LOCK_UNLOCKED;
159 static char data_to_read = 0;
160 static DECLARE_WAIT_QUEUE_HEAD(read_q);
161 static struct fasync_struct *fasync_q = NULL;
162 static char pretimeout_since_last_heartbeat = 0;
163
164 /* If true, the driver will start running as soon as it is configured
165    and ready. */
166 static int start_now = 0;
167
168 module_param(timeout, int, 0);
169 MODULE_PARM_DESC(timeout, "Timeout value in seconds.");
170 module_param(pretimeout, int, 0);
171 MODULE_PARM_DESC(pretimeout, "Pretimeout value in seconds.");
172 module_param_string(action, action, sizeof(action), 0);
173 MODULE_PARM_DESC(action, "Timeout action. One of: "
174                  "reset, none, power_cycle, power_off.");
175 module_param_string(preaction, preaction, sizeof(preaction), 0);
176 MODULE_PARM_DESC(preaction, "Pretimeout action.  One of: "
177                  "pre_none, pre_smi, pre_nmi, pre_int.");
178 module_param_string(preop, preop, sizeof(preop), 0);
179 MODULE_PARM_DESC(preop, "Pretimeout driver operation.  One of: "
180                  "preop_none, preop_panic, preop_give_data.");
181 module_param(start_now, int, 0);
182 MODULE_PARM_DESC(start_now, "Set to 1 to start the watchdog as"
183                  "soon as the driver is loaded.");
184 module_param(nowayout, int, 0);
185 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
186
187 /* Default state of the timer. */
188 static unsigned char ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
189
190 /* If shutting down via IPMI, we ignore the heartbeat. */
191 static int ipmi_ignore_heartbeat = 0;
192
193 /* Is someone using the watchdog?  Only one user is allowed. */
194 static int ipmi_wdog_open = 0;
195
196 /* If set to 1, the heartbeat command will set the state to reset and
197    start the timer.  The timer doesn't normally run when the driver is
198    first opened until the heartbeat is set the first time, this
199    variable is used to accomplish this. */
200 static int ipmi_start_timer_on_heartbeat = 0;
201
202 /* IPMI version of the BMC. */
203 static unsigned char ipmi_version_major;
204 static unsigned char ipmi_version_minor;
205
206
207 static int ipmi_heartbeat(void);
208 static void panic_halt_ipmi_heartbeat(void);
209
210
211 /* We use a semaphore to make sure that only one thing can send a set
212    timeout at one time, because we only have one copy of the data.
213    The semaphore is claimed when the set_timeout is sent and freed
214    when both messages are free. */
215 static atomic_t set_timeout_tofree = ATOMIC_INIT(0);
216 static DECLARE_MUTEX(set_timeout_lock);
217 static void set_timeout_free_smi(struct ipmi_smi_msg *msg)
218 {
219     if (atomic_dec_and_test(&set_timeout_tofree))
220             up(&set_timeout_lock);
221 }
222 static void set_timeout_free_recv(struct ipmi_recv_msg *msg)
223 {
224     if (atomic_dec_and_test(&set_timeout_tofree))
225             up(&set_timeout_lock);
226 }
227 static struct ipmi_smi_msg set_timeout_smi_msg =
228 {
229         .done = set_timeout_free_smi
230 };
231 static struct ipmi_recv_msg set_timeout_recv_msg =
232 {
233         .done = set_timeout_free_recv
234 };
235  
236 static int i_ipmi_set_timeout(struct ipmi_smi_msg  *smi_msg,
237                               struct ipmi_recv_msg *recv_msg,
238                               int                  *send_heartbeat_now)
239 {
240         struct kernel_ipmi_msg            msg;
241         unsigned char                     data[6];
242         int                               rv;
243         struct ipmi_system_interface_addr addr;
244         int                               hbnow = 0;
245
246
247         data[0] = 0;
248         WDOG_SET_TIMER_USE(data[0], WDOG_TIMER_USE_SMS_OS);
249
250         if ((ipmi_version_major > 1)
251             || ((ipmi_version_major == 1) && (ipmi_version_minor >= 5)))
252         {
253                 /* This is an IPMI 1.5-only feature. */
254                 data[0] |= WDOG_DONT_STOP_ON_SET;
255         } else if (ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
256                 /* In ipmi 1.0, setting the timer stops the watchdog, we
257                    need to start it back up again. */
258                 hbnow = 1;
259         }
260
261         data[1] = 0;
262         WDOG_SET_TIMEOUT_ACT(data[1], ipmi_watchdog_state);
263         if (pretimeout > 0) {
264             WDOG_SET_PRETIMEOUT_ACT(data[1], preaction_val);
265             data[2] = pretimeout;
266         } else {
267             WDOG_SET_PRETIMEOUT_ACT(data[1], WDOG_PRETIMEOUT_NONE);
268             data[2] = 0; /* No pretimeout. */
269         }
270         data[3] = 0;
271         WDOG_SET_TIMEOUT(data[4], data[5], timeout);
272
273         addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
274         addr.channel = IPMI_BMC_CHANNEL;
275         addr.lun = 0;
276
277         msg.netfn = 0x06;
278         msg.cmd = IPMI_WDOG_SET_TIMER;
279         msg.data = data;
280         msg.data_len = sizeof(data);
281         rv = ipmi_request_supply_msgs(watchdog_user,
282                                       (struct ipmi_addr *) &addr,
283                                       0,
284                                       &msg,
285                                       NULL,
286                                       smi_msg,
287                                       recv_msg,
288                                       1);
289         if (rv) {
290                 printk(KERN_WARNING "IPMI Watchdog, set timeout error: %d\n",
291                        rv);
292         }
293
294         if (send_heartbeat_now)
295             *send_heartbeat_now = hbnow;
296
297         return rv;
298 }
299
300 /* Parameters to ipmi_set_timeout */
301 #define IPMI_SET_TIMEOUT_NO_HB                  0
302 #define IPMI_SET_TIMEOUT_HB_IF_NECESSARY        1
303 #define IPMI_SET_TIMEOUT_FORCE_HB               2
304
305 static int ipmi_set_timeout(int do_heartbeat)
306 {
307         int send_heartbeat_now;
308         int rv;
309
310
311         /* We can only send one of these at a time. */
312         down(&set_timeout_lock);
313
314         atomic_set(&set_timeout_tofree, 2);
315
316         rv = i_ipmi_set_timeout(&set_timeout_smi_msg,
317                                 &set_timeout_recv_msg,
318                                 &send_heartbeat_now);
319         if (rv) {
320                 up(&set_timeout_lock);
321         } else {
322                 if ((do_heartbeat == IPMI_SET_TIMEOUT_FORCE_HB)
323                     || ((send_heartbeat_now)
324                         && (do_heartbeat == IPMI_SET_TIMEOUT_HB_IF_NECESSARY)))
325                 {
326                         rv = ipmi_heartbeat();
327                 }
328         }
329
330         return rv;
331 }
332
333 static void dummy_smi_free(struct ipmi_smi_msg *msg)
334 {
335 }
336 static void dummy_recv_free(struct ipmi_recv_msg *msg)
337 {
338 }
339 static struct ipmi_smi_msg panic_halt_smi_msg =
340 {
341         .done = dummy_smi_free
342 };
343 static struct ipmi_recv_msg panic_halt_recv_msg =
344 {
345         .done = dummy_recv_free
346 };
347
348 /* Special call, doesn't claim any locks.  This is only to be called
349    at panic or halt time, in run-to-completion mode, when the caller
350    is the only CPU and the only thing that will be going is these IPMI
351    calls. */
352 static void panic_halt_ipmi_set_timeout(void)
353 {
354         int send_heartbeat_now;
355         int rv;
356
357         rv = i_ipmi_set_timeout(&panic_halt_smi_msg,
358                                 &panic_halt_recv_msg,
359                                 &send_heartbeat_now);
360         if (!rv) {
361                 if (send_heartbeat_now)
362                         panic_halt_ipmi_heartbeat();
363         }
364 }
365
366 /* Do a delayed shutdown, with the delay in milliseconds.  If power_off is
367    false, do a reset.  If power_off is true, do a power down.  This is
368    primarily for the IMB code's shutdown. */
369 void ipmi_delayed_shutdown(long delay, int power_off)
370 {
371         ipmi_ignore_heartbeat = 1;
372         if (power_off) 
373                 ipmi_watchdog_state = WDOG_TIMEOUT_POWER_DOWN;
374         else
375                 ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
376         timeout = delay;
377         ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
378 }
379
380 /* We use a semaphore to make sure that only one thing can send a
381    heartbeat at one time, because we only have one copy of the data.
382    The semaphore is claimed when the set_timeout is sent and freed
383    when both messages are free. */
384 static atomic_t heartbeat_tofree = ATOMIC_INIT(0);
385 static DECLARE_MUTEX(heartbeat_lock);
386 static DECLARE_MUTEX_LOCKED(heartbeat_wait_lock);
387 static void heartbeat_free_smi(struct ipmi_smi_msg *msg)
388 {
389     if (atomic_dec_and_test(&heartbeat_tofree))
390             up(&heartbeat_wait_lock);
391 }
392 static void heartbeat_free_recv(struct ipmi_recv_msg *msg)
393 {
394     if (atomic_dec_and_test(&heartbeat_tofree))
395             up(&heartbeat_wait_lock);
396 }
397 static struct ipmi_smi_msg heartbeat_smi_msg =
398 {
399         .done = heartbeat_free_smi
400 };
401 static struct ipmi_recv_msg heartbeat_recv_msg =
402 {
403         .done = heartbeat_free_recv
404 };
405  
406 static struct ipmi_smi_msg panic_halt_heartbeat_smi_msg =
407 {
408         .done = dummy_smi_free
409 };
410 static struct ipmi_recv_msg panic_halt_heartbeat_recv_msg =
411 {
412         .done = dummy_recv_free
413 };
414  
415 static int ipmi_heartbeat(void)
416 {
417         struct kernel_ipmi_msg            msg;
418         int                               rv;
419         struct ipmi_system_interface_addr addr;
420
421         if (ipmi_ignore_heartbeat) {
422                 return 0;
423         }
424
425         if (ipmi_start_timer_on_heartbeat) {
426                 ipmi_start_timer_on_heartbeat = 0;
427                 ipmi_watchdog_state = action_val;
428                 return ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
429         } else if (pretimeout_since_last_heartbeat) {
430                 /* A pretimeout occurred, make sure we set the timeout.
431                    We don't want to set the action, though, we want to
432                    leave that alone (thus it can't be combined with the
433                    above operation. */
434                 pretimeout_since_last_heartbeat = 0;
435                 return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
436         }
437
438         down(&heartbeat_lock);
439
440         atomic_set(&heartbeat_tofree, 2);
441
442         /* Don't reset the timer if we have the timer turned off, that
443            re-enables the watchdog. */
444         if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE) {
445                 up(&heartbeat_lock);
446                 return 0;
447         }
448
449         addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
450         addr.channel = IPMI_BMC_CHANNEL;
451         addr.lun = 0;
452
453         msg.netfn = 0x06;
454         msg.cmd = IPMI_WDOG_RESET_TIMER;
455         msg.data = NULL;
456         msg.data_len = 0;
457         rv = ipmi_request_supply_msgs(watchdog_user,
458                                       (struct ipmi_addr *) &addr,
459                                       0,
460                                       &msg,
461                                       NULL,
462                                       &heartbeat_smi_msg,
463                                       &heartbeat_recv_msg,
464                                       1);
465         if (rv) {
466                 up(&heartbeat_lock);
467                 printk(KERN_WARNING "IPMI Watchdog, heartbeat failure: %d\n",
468                        rv);
469                 return rv;
470         }
471
472         /* Wait for the heartbeat to be sent. */
473         down(&heartbeat_wait_lock);
474
475         if (heartbeat_recv_msg.msg.data[0] != 0) {
476             /* Got an error in the heartbeat response.  It was already
477                reported in ipmi_wdog_msg_handler, but we should return
478                an error here. */
479             rv = -EINVAL;
480         }
481
482         up(&heartbeat_lock);
483
484         return rv;
485 }
486
487 static void panic_halt_ipmi_heartbeat(void)
488 {
489         struct kernel_ipmi_msg             msg;
490         struct ipmi_system_interface_addr addr;
491
492
493         /* Don't reset the timer if we have the timer turned off, that
494            re-enables the watchdog. */
495         if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)
496                 return;
497
498         addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
499         addr.channel = IPMI_BMC_CHANNEL;
500         addr.lun = 0;
501
502         msg.netfn = 0x06;
503         msg.cmd = IPMI_WDOG_RESET_TIMER;
504         msg.data = NULL;
505         msg.data_len = 0;
506         ipmi_request_supply_msgs(watchdog_user,
507                                  (struct ipmi_addr *) &addr,
508                                  0,
509                                  &msg,
510                                  NULL,
511                                  &panic_halt_heartbeat_smi_msg,
512                                  &panic_halt_heartbeat_recv_msg,
513                                  1);
514 }
515
516 static struct watchdog_info ident=
517 {
518         0, /* WDIOF_SETTIMEOUT, */
519         1,
520         "IPMI"
521 };
522
523 static int ipmi_ioctl(struct inode *inode, struct file *file,
524                       unsigned int cmd, unsigned long arg)
525 {
526         void __user *argp = (void __user *)arg;
527         int i;
528         int val;
529
530         switch(cmd) {
531         case WDIOC_GETSUPPORT:
532                 i = copy_to_user(argp, &ident, sizeof(ident));
533                 return i ? -EFAULT : 0;
534
535         case WDIOC_SETTIMEOUT:
536                 i = copy_from_user(&val, argp, sizeof(int));
537                 if (i)
538                         return -EFAULT;
539                 timeout = val;
540                 return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
541
542         case WDIOC_GETTIMEOUT:
543                 i = copy_to_user(argp, &timeout, sizeof(timeout));
544                 if (i)
545                         return -EFAULT;
546                 return 0;
547
548         case WDIOC_SET_PRETIMEOUT:
549                 i = copy_from_user(&val, argp, sizeof(int));
550                 if (i)
551                         return -EFAULT;
552                 pretimeout = val;
553                 return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
554
555         case WDIOC_GET_PRETIMEOUT:
556                 i = copy_to_user(argp, &pretimeout, sizeof(pretimeout));
557                 if (i)
558                         return -EFAULT;
559                 return 0;
560
561         case WDIOC_KEEPALIVE:
562                 return ipmi_heartbeat();
563
564         case WDIOC_SETOPTIONS:
565                 i = copy_from_user(&val, argp, sizeof(int));
566                 if (i)
567                         return -EFAULT;
568                 if (val & WDIOS_DISABLECARD)
569                 {
570                         ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
571                         ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
572                         ipmi_start_timer_on_heartbeat = 0;
573                 }
574
575                 if (val & WDIOS_ENABLECARD)
576                 {
577                         ipmi_watchdog_state = action_val;
578                         ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
579                 }
580                 return 0;
581
582         case WDIOC_GETSTATUS:
583                 val = 0;
584                 i = copy_to_user(argp, &val, sizeof(val));
585                 if (i)
586                         return -EFAULT;
587                 return 0;
588
589         default:
590                 return -ENOIOCTLCMD;
591         }
592 }
593
594 static ssize_t ipmi_write(struct file *file,
595                           const char  __user *buf,
596                           size_t      len,
597                           loff_t      *ppos)
598 {
599         int rv;
600
601         if (len) {
602                 rv = ipmi_heartbeat();
603                 if (rv)
604                         return rv;
605                 return 1;
606         }
607         return 0;
608 }
609
610 static ssize_t ipmi_read(struct file *file,
611                          char        __user *buf,
612                          size_t      count,
613                          loff_t      *ppos)
614 {
615         int          rv = 0;
616         wait_queue_t wait;
617
618         if (count <= 0)
619                 return 0;
620
621         /* Reading returns if the pretimeout has gone off, and it only does
622            it once per pretimeout. */
623         spin_lock(&ipmi_read_lock);
624         if (!data_to_read) {
625                 if (file->f_flags & O_NONBLOCK) {
626                         rv = -EAGAIN;
627                         goto out;
628                 }
629                 
630                 init_waitqueue_entry(&wait, current);
631                 add_wait_queue(&read_q, &wait);
632                 while (!data_to_read) {
633                         set_current_state(TASK_INTERRUPTIBLE);
634                         spin_unlock(&ipmi_read_lock);
635                         schedule();
636                         spin_lock(&ipmi_read_lock);
637                 }
638                 remove_wait_queue(&read_q, &wait);
639             
640                 if (signal_pending(current)) {
641                         rv = -ERESTARTSYS;
642                         goto out;
643                 }
644         }
645         data_to_read = 0;
646
647  out:
648         spin_unlock(&ipmi_read_lock);
649
650         if (rv == 0) {
651                 if (copy_to_user(buf, &data_to_read, 1))
652                         rv = -EFAULT;
653                 else
654                         rv = 1;
655         }
656
657         return rv;
658 }
659
660 static int ipmi_open(struct inode *ino, struct file *filep)
661 {
662         switch (iminor(ino))
663         {
664                 case WATCHDOG_MINOR:
665                     if (ipmi_wdog_open)
666                         return -EBUSY;
667
668                     ipmi_wdog_open = 1;
669
670                     /* Don't start the timer now, let it start on the
671                        first heartbeat. */
672                     ipmi_start_timer_on_heartbeat = 1;
673                     return nonseekable_open(ino, filep);
674
675                 default:
676                     return (-ENODEV);
677         }
678 }
679
680 static unsigned int ipmi_poll(struct file *file, poll_table *wait)
681 {
682         unsigned int mask = 0;
683         
684         poll_wait(file, &read_q, wait);
685
686         spin_lock(&ipmi_read_lock);
687         if (data_to_read)
688                 mask |= (POLLIN | POLLRDNORM);
689         spin_unlock(&ipmi_read_lock);
690
691         return mask;
692 }
693
694 static int ipmi_fasync(int fd, struct file *file, int on)
695 {
696         int result;
697
698         result = fasync_helper(fd, file, on, &fasync_q);
699
700         return (result);
701 }
702
703 static int ipmi_close(struct inode *ino, struct file *filep)
704 {
705         if (iminor(ino)==WATCHDOG_MINOR)
706         {
707                 if (!nowayout) {
708                         ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
709                         ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
710                 }
711                 ipmi_wdog_open = 0;
712         }
713
714         ipmi_fasync (-1, filep, 0);
715
716         return 0;
717 }
718
719 static struct file_operations ipmi_wdog_fops = {
720         .owner   = THIS_MODULE,
721         .read    = ipmi_read,
722         .poll    = ipmi_poll,
723         .write   = ipmi_write,
724         .ioctl   = ipmi_ioctl,
725         .open    = ipmi_open,
726         .release = ipmi_close,
727         .fasync  = ipmi_fasync,
728 };
729
730 static struct miscdevice ipmi_wdog_miscdev = {
731         WATCHDOG_MINOR,
732         "watchdog",
733         &ipmi_wdog_fops
734 };
735
736 static DECLARE_RWSEM(register_sem);
737
738 static void ipmi_wdog_msg_handler(struct ipmi_recv_msg *msg,
739                                   void                 *handler_data)
740 {
741         if (msg->msg.data[0] != 0) {
742                 printk(KERN_ERR "IPMI Watchdog response: Error %x on cmd %x\n",
743                        msg->msg.data[0],
744                        msg->msg.cmd);
745         }
746         
747         ipmi_free_recv_msg(msg);
748 }
749
750 static void ipmi_wdog_pretimeout_handler(void *handler_data)
751 {
752         if (preaction_val != WDOG_PRETIMEOUT_NONE) {
753                 if (preop_val == WDOG_PREOP_PANIC)
754                         panic("Watchdog pre-timeout");
755                 else if (preop_val == WDOG_PREOP_GIVE_DATA) {
756                         spin_lock(&ipmi_read_lock);
757                         data_to_read = 1;
758                         wake_up_interruptible(&read_q);
759                         kill_fasync(&fasync_q, SIGIO, POLL_IN);
760
761                         spin_unlock(&ipmi_read_lock);
762                 }
763         }
764
765         /* On some machines, the heartbeat will give
766            an error and not work unless we re-enable
767            the timer.   So do so. */
768         pretimeout_since_last_heartbeat = 1;
769 }
770
771 static struct ipmi_user_hndl ipmi_hndlrs =
772 {
773         .ipmi_recv_hndl           = ipmi_wdog_msg_handler,
774         .ipmi_watchdog_pretimeout = ipmi_wdog_pretimeout_handler
775 };
776
777 static void ipmi_register_watchdog(int ipmi_intf)
778 {
779         int rv = -EBUSY;
780
781         down_write(&register_sem);
782         if (watchdog_user)
783                 goto out;
784
785         rv = ipmi_create_user(ipmi_intf, &ipmi_hndlrs, NULL, &watchdog_user);
786         if (rv < 0) {
787                 printk("IPMI watchdog: Unable to register with ipmi\n");
788                 goto out;
789         }
790
791         ipmi_get_version(watchdog_user,
792                          &ipmi_version_major,
793                          &ipmi_version_minor);
794
795         rv = misc_register(&ipmi_wdog_miscdev);
796         if (rv < 0) {
797                 ipmi_destroy_user(watchdog_user);
798                 watchdog_user = NULL;
799                 printk("IPMI watchdog: Unable to register misc device\n");
800         }
801
802  out:
803         up_write(&register_sem);
804
805         if ((start_now) && (rv == 0)) {
806                 /* Run from startup, so start the timer now. */
807                 start_now = 0; /* Disable this function after first startup. */
808                 ipmi_watchdog_state = action_val;
809                 ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
810                 printk("Starting IPMI Watchdog now!\n");
811         }
812 }
813
814 #ifdef HAVE_NMI_HANDLER
815 static int
816 ipmi_nmi(void *dev_id, struct pt_regs *regs, int cpu, int handled)
817 {
818         /* If no one else handled the NMI, we assume it was the IPMI
819            watchdog. */
820         if ((!handled) && (preop_val == WDOG_PREOP_PANIC))
821                 panic("IPMI watchdog pre-timeout");
822
823         /* On some machines, the heartbeat will give
824            an error and not work unless we re-enable
825            the timer.   So do so. */
826         pretimeout_since_last_heartbeat = 1;
827
828         return NOTIFY_DONE;
829 }
830
831 static struct nmi_handler ipmi_nmi_handler =
832 {
833         .link     = LIST_HEAD_INIT(ipmi_nmi_handler.link),
834         .dev_name = "ipmi_watchdog",
835         .dev_id   = NULL,
836         .handler  = ipmi_nmi,
837         .priority = 0, /* Call us last. */
838 };
839 #endif
840
841 static int wdog_reboot_handler(struct notifier_block *this,
842                                unsigned long         code,
843                                void                  *unused)
844 {
845         static int reboot_event_handled = 0;
846
847         if ((watchdog_user) && (!reboot_event_handled)) {
848                 /* Make sure we only do this once. */
849                 reboot_event_handled = 1;
850
851                 if (code == SYS_DOWN || code == SYS_HALT) {
852                         /* Disable the WDT if we are shutting down. */
853                         ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
854                         panic_halt_ipmi_set_timeout();
855                 } else {
856                         /* Set a long timer to let the reboot happens, but
857                            reboot if it hangs. */
858                         timeout = 120;
859                         pretimeout = 0;
860                         ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
861                         panic_halt_ipmi_set_timeout();
862                 }
863         }
864         return NOTIFY_OK;
865 }
866
867 static struct notifier_block wdog_reboot_notifier = {
868         wdog_reboot_handler,
869         NULL,
870         0
871 };
872
873 extern int panic_timeout; /* Why isn't this defined anywhere? */
874
875 static int wdog_panic_handler(struct notifier_block *this,
876                               unsigned long         event,
877                               void                  *unused)
878 {
879         static int panic_event_handled = 0;
880
881         /* On a panic, if we have a panic timeout, make sure that the thing
882            reboots, even if it hangs during that panic. */
883         if (watchdog_user && !panic_event_handled) {
884                 /* Make sure the panic doesn't hang, and make sure we
885                    do this only once. */
886                 panic_event_handled = 1;
887             
888                 timeout = 255;
889                 pretimeout = 0;
890                 ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
891                 panic_halt_ipmi_set_timeout();
892         }
893
894         return NOTIFY_OK;
895 }
896
897 static struct notifier_block wdog_panic_notifier = {
898         wdog_panic_handler,
899         NULL,
900         150   /* priority: INT_MAX >= x >= 0 */
901 };
902
903
904 static void ipmi_new_smi(int if_num)
905 {
906         ipmi_register_watchdog(if_num);
907 }
908
909 static void ipmi_smi_gone(int if_num)
910 {
911         /* This can never be called, because once the watchdog is
912            registered, the interface can't go away until the watchdog
913            is unregistered. */
914 }
915
916 static struct ipmi_smi_watcher smi_watcher =
917 {
918         .owner    = THIS_MODULE,
919         .new_smi  = ipmi_new_smi,
920         .smi_gone = ipmi_smi_gone
921 };
922
923 static int __init ipmi_wdog_init(void)
924 {
925         int rv;
926
927         printk(KERN_INFO "IPMI watchdog driver version "
928                IPMI_WATCHDOG_VERSION "\n");
929
930         if (strcmp(action, "reset") == 0) {
931                 action_val = WDOG_TIMEOUT_RESET;
932         } else if (strcmp(action, "none") == 0) {
933                 action_val = WDOG_TIMEOUT_NONE;
934         } else if (strcmp(action, "power_cycle") == 0) {
935                 action_val = WDOG_TIMEOUT_POWER_CYCLE;
936         } else if (strcmp(action, "power_off") == 0) {
937                 action_val = WDOG_TIMEOUT_POWER_DOWN;
938         } else {
939                 action_val = WDOG_TIMEOUT_RESET;
940                 printk("ipmi_watchdog: Unknown action '%s', defaulting to"
941                        " reset\n", action);
942         }
943
944         if (strcmp(preaction, "pre_none") == 0) {
945                 preaction_val = WDOG_PRETIMEOUT_NONE;
946         } else if (strcmp(preaction, "pre_smi") == 0) {
947                 preaction_val = WDOG_PRETIMEOUT_SMI;
948 #ifdef HAVE_NMI_HANDLER
949         } else if (strcmp(preaction, "pre_nmi") == 0) {
950                 preaction_val = WDOG_PRETIMEOUT_NMI;
951 #endif
952         } else if (strcmp(preaction, "pre_int") == 0) {
953                 preaction_val = WDOG_PRETIMEOUT_MSG_INT;
954         } else {
955                 preaction_val = WDOG_PRETIMEOUT_NONE;
956                 printk("ipmi_watchdog: Unknown preaction '%s', defaulting to"
957                        " none\n", preaction);
958         }
959
960         if (strcmp(preop, "preop_none") == 0) {
961                 preop_val = WDOG_PREOP_NONE;
962         } else if (strcmp(preop, "preop_panic") == 0) {
963                 preop_val = WDOG_PREOP_PANIC;
964         } else if (strcmp(preop, "preop_give_data") == 0) {
965                 preop_val = WDOG_PREOP_GIVE_DATA;
966         } else {
967                 preop_val = WDOG_PREOP_NONE;
968                 printk("ipmi_watchdog: Unknown preop '%s', defaulting to"
969                        " none\n", preop);
970         }
971
972 #ifdef HAVE_NMI_HANDLER
973         if (preaction_val == WDOG_PRETIMEOUT_NMI) {
974                 if (preop_val == WDOG_PREOP_GIVE_DATA) {
975                         printk(KERN_WARNING
976                                "ipmi_watchdog: Pretimeout op is to give data"
977                                " but NMI pretimeout is enabled, setting"
978                                " pretimeout op to none\n");
979                         preop_val = WDOG_PREOP_NONE;
980                 }
981 #ifdef CONFIG_X86_LOCAL_APIC
982                 if (nmi_watchdog == NMI_IO_APIC) {
983                         printk(KERN_WARNING
984                                "ipmi_watchdog: nmi_watchdog is set to IO APIC"
985                                " mode (value is %d), that is incompatible"
986                                " with using NMI in the IPMI watchdog."
987                                " Disabling IPMI nmi pretimeout.\n",
988                                nmi_watchdog);
989                         preaction_val = WDOG_PRETIMEOUT_NONE;
990                 } else {
991 #endif
992                 rv = request_nmi(&ipmi_nmi_handler);
993                 if (rv) {
994                         printk(KERN_WARNING
995                                "ipmi_watchdog: Can't register nmi handler\n");
996                         return rv;
997                 }
998 #ifdef CONFIG_X86_LOCAL_APIC
999                 }
1000 #endif
1001         }
1002 #endif
1003
1004         rv = ipmi_smi_watcher_register(&smi_watcher);
1005         if (rv) {
1006 #ifdef HAVE_NMI_HANDLER
1007                 if (preaction_val == WDOG_PRETIMEOUT_NMI)
1008                         release_nmi(&ipmi_nmi_handler);
1009 #endif
1010                 printk(KERN_WARNING
1011                        "ipmi_watchdog: can't register smi watcher\n");
1012                 return rv;
1013         }
1014
1015         register_reboot_notifier(&wdog_reboot_notifier);
1016         notifier_chain_register(&panic_notifier_list, &wdog_panic_notifier);
1017
1018         return 0;
1019 }
1020
1021 static __exit void ipmi_unregister_watchdog(void)
1022 {
1023         int rv;
1024
1025         down_write(&register_sem);
1026
1027 #ifdef HAVE_NMI_HANDLER
1028         if (preaction_val == WDOG_PRETIMEOUT_NMI)
1029                 release_nmi(&ipmi_nmi_handler);
1030 #endif
1031
1032         notifier_chain_unregister(&panic_notifier_list, &wdog_panic_notifier);
1033         unregister_reboot_notifier(&wdog_reboot_notifier);
1034
1035         if (! watchdog_user)
1036                 goto out;
1037
1038         /* Make sure no one can call us any more. */
1039         misc_deregister(&ipmi_wdog_miscdev);
1040
1041         /*  Disable the timer. */
1042         ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
1043         ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
1044
1045         /* Wait to make sure the message makes it out.  The lower layer has
1046            pointers to our buffers, we want to make sure they are done before
1047            we release our memory. */
1048         while (atomic_read(&set_timeout_tofree)) {
1049                 set_current_state(TASK_UNINTERRUPTIBLE);
1050                 schedule_timeout(1);
1051         }
1052
1053         /* Disconnect from IPMI. */
1054         rv = ipmi_destroy_user(watchdog_user);
1055         if (rv) {
1056                 printk(KERN_WARNING
1057                        "IPMI Watchdog, error unlinking from IPMI: %d\n",
1058                        rv);
1059         }
1060         watchdog_user = NULL;
1061
1062  out:
1063         up_write(&register_sem);
1064 }
1065
1066 static void __exit ipmi_wdog_exit(void)
1067 {
1068         ipmi_smi_watcher_unregister(&smi_watcher);
1069         ipmi_unregister_watchdog();
1070 }
1071 module_exit(ipmi_wdog_exit);
1072
1073 EXPORT_SYMBOL(ipmi_delayed_shutdown);
1074
1075 module_init(ipmi_wdog_init);
1076 MODULE_LICENSE("GPL");