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