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