Merge branch 'kvm-updates/3.4' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[cascardo/linux.git] / drivers / watchdog / wdt.c
1 /*
2  *      Industrial Computer Source WDT501 driver
3  *
4  *      (c) Copyright 1996-1997 Alan Cox <alan@lxorguk.ukuu.org.uk>,
5  *                                              All Rights Reserved.
6  *
7  *      This program is free software; you can redistribute it and/or
8  *      modify it under the terms of the GNU General Public License
9  *      as published by the Free Software Foundation; either version
10  *      2 of the License, or (at your option) any later version.
11  *
12  *      Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
13  *      warranty for any of this software. This material is provided
14  *      "AS-IS" and at no charge.
15  *
16  *      (c) Copyright 1995    Alan Cox <alan@lxorguk.ukuu.org.uk>
17  *
18  *      Release 0.10.
19  *
20  *      Fixes
21  *              Dave Gregorich  :       Modularisation and minor bugs
22  *              Alan Cox        :       Added the watchdog ioctl() stuff
23  *              Alan Cox        :       Fixed the reboot problem (as noted by
24  *                                      Matt Crocker).
25  *              Alan Cox        :       Added wdt= boot option
26  *              Alan Cox        :       Cleaned up copy/user stuff
27  *              Tim Hockin      :       Added insmod parameters, comment
28  *                                      cleanup, parameterized timeout
29  *              Tigran Aivazian :       Restructured wdt_init() to handle
30  *                                      failures
31  *              Joel Becker     :       Added WDIOC_GET/SETTIMEOUT
32  *              Matt Domsch     :       Added nowayout module option
33  */
34
35 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36
37 #include <linux/interrupt.h>
38 #include <linux/module.h>
39 #include <linux/moduleparam.h>
40 #include <linux/types.h>
41 #include <linux/miscdevice.h>
42 #include <linux/watchdog.h>
43 #include <linux/fs.h>
44 #include <linux/ioport.h>
45 #include <linux/notifier.h>
46 #include <linux/reboot.h>
47 #include <linux/init.h>
48 #include <linux/io.h>
49 #include <linux/uaccess.h>
50
51 #include <asm/system.h>
52 #include "wd501p.h"
53
54 static unsigned long wdt_is_open;
55 static char expect_close;
56
57 /*
58  *      Module parameters
59  */
60
61 #define WD_TIMO 60                      /* Default heartbeat = 60 seconds */
62
63 static int heartbeat = WD_TIMO;
64 static int wd_heartbeat;
65 module_param(heartbeat, int, 0);
66 MODULE_PARM_DESC(heartbeat,
67         "Watchdog heartbeat in seconds. (0 < heartbeat < 65536, default="
68                                 __MODULE_STRING(WD_TIMO) ")");
69
70 static bool nowayout = WATCHDOG_NOWAYOUT;
71 module_param(nowayout, bool, 0);
72 MODULE_PARM_DESC(nowayout,
73         "Watchdog cannot be stopped once started (default="
74                                 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
75
76 /* You must set these - there is no sane way to probe for this board. */
77 static int io = 0x240;
78 static int irq = 11;
79
80 static DEFINE_SPINLOCK(wdt_lock);
81
82 module_param(io, int, 0);
83 MODULE_PARM_DESC(io, "WDT io port (default=0x240)");
84 module_param(irq, int, 0);
85 MODULE_PARM_DESC(irq, "WDT irq (default=11)");
86
87 /* Support for the Fan Tachometer on the WDT501-P */
88 static int tachometer;
89 module_param(tachometer, int, 0);
90 MODULE_PARM_DESC(tachometer,
91                 "WDT501-P Fan Tachometer support (0=disable, default=0)");
92
93 static int type = 500;
94 module_param(type, int, 0);
95 MODULE_PARM_DESC(type,
96                 "WDT501-P Card type (500 or 501, default=500)");
97
98 /*
99  *      Programming support
100  */
101
102 static void wdt_ctr_mode(int ctr, int mode)
103 {
104         ctr <<= 6;
105         ctr |= 0x30;
106         ctr |= (mode << 1);
107         outb_p(ctr, WDT_CR);
108 }
109
110 static void wdt_ctr_load(int ctr, int val)
111 {
112         outb_p(val&0xFF, WDT_COUNT0+ctr);
113         outb_p(val>>8, WDT_COUNT0+ctr);
114 }
115
116 /**
117  *      wdt_start:
118  *
119  *      Start the watchdog driver.
120  */
121
122 static int wdt_start(void)
123 {
124         unsigned long flags;
125         spin_lock_irqsave(&wdt_lock, flags);
126         inb_p(WDT_DC);                  /* Disable watchdog */
127         wdt_ctr_mode(0, 3);             /* Program CTR0 for Mode 3:
128                                                 Square Wave Generator */
129         wdt_ctr_mode(1, 2);             /* Program CTR1 for Mode 2:
130                                                 Rate Generator */
131         wdt_ctr_mode(2, 0);             /* Program CTR2 for Mode 0:
132                                                 Pulse on Terminal Count */
133         wdt_ctr_load(0, 8948);          /* Count at 100Hz */
134         wdt_ctr_load(1, wd_heartbeat);  /* Heartbeat */
135         wdt_ctr_load(2, 65535);         /* Length of reset pulse */
136         outb_p(0, WDT_DC);              /* Enable watchdog */
137         spin_unlock_irqrestore(&wdt_lock, flags);
138         return 0;
139 }
140
141 /**
142  *      wdt_stop:
143  *
144  *      Stop the watchdog driver.
145  */
146
147 static int wdt_stop(void)
148 {
149         unsigned long flags;
150         spin_lock_irqsave(&wdt_lock, flags);
151         /* Turn the card off */
152         inb_p(WDT_DC);                  /* Disable watchdog */
153         wdt_ctr_load(2, 0);             /* 0 length reset pulses now */
154         spin_unlock_irqrestore(&wdt_lock, flags);
155         return 0;
156 }
157
158 /**
159  *      wdt_ping:
160  *
161  *      Reload counter one with the watchdog heartbeat. We don't bother
162  *      reloading the cascade counter.
163  */
164
165 static void wdt_ping(void)
166 {
167         unsigned long flags;
168         spin_lock_irqsave(&wdt_lock, flags);
169         /* Write a watchdog value */
170         inb_p(WDT_DC);                  /* Disable watchdog */
171         wdt_ctr_mode(1, 2);             /* Re-Program CTR1 for Mode 2:
172                                                         Rate Generator */
173         wdt_ctr_load(1, wd_heartbeat);  /* Heartbeat */
174         outb_p(0, WDT_DC);              /* Enable watchdog */
175         spin_unlock_irqrestore(&wdt_lock, flags);
176 }
177
178 /**
179  *      wdt_set_heartbeat:
180  *      @t:             the new heartbeat value that needs to be set.
181  *
182  *      Set a new heartbeat value for the watchdog device. If the heartbeat
183  *      value is incorrect we keep the old value and return -EINVAL. If
184  *      successful we return 0.
185  */
186
187 static int wdt_set_heartbeat(int t)
188 {
189         if (t < 1 || t > 65535)
190                 return -EINVAL;
191
192         heartbeat = t;
193         wd_heartbeat = t * 100;
194         return 0;
195 }
196
197 /**
198  *      wdt_get_status:
199  *
200  *      Extract the status information from a WDT watchdog device. There are
201  *      several board variants so we have to know which bits are valid. Some
202  *      bits default to one and some to zero in order to be maximally painful.
203  *
204  *      we then map the bits onto the status ioctl flags.
205  */
206
207 static int wdt_get_status(void)
208 {
209         unsigned char new_status;
210         int status = 0;
211         unsigned long flags;
212
213         spin_lock_irqsave(&wdt_lock, flags);
214         new_status = inb_p(WDT_SR);
215         spin_unlock_irqrestore(&wdt_lock, flags);
216
217         if (new_status & WDC_SR_ISOI0)
218                 status |= WDIOF_EXTERN1;
219         if (new_status & WDC_SR_ISII1)
220                 status |= WDIOF_EXTERN2;
221         if (type == 501) {
222                 if (!(new_status & WDC_SR_TGOOD))
223                         status |= WDIOF_OVERHEAT;
224                 if (!(new_status & WDC_SR_PSUOVER))
225                         status |= WDIOF_POWEROVER;
226                 if (!(new_status & WDC_SR_PSUUNDR))
227                         status |= WDIOF_POWERUNDER;
228                 if (tachometer) {
229                         if (!(new_status & WDC_SR_FANGOOD))
230                                 status |= WDIOF_FANFAULT;
231                 }
232         }
233         return status;
234 }
235
236 /**
237  *      wdt_get_temperature:
238  *
239  *      Reports the temperature in degrees Fahrenheit. The API is in
240  *      farenheit. It was designed by an imperial measurement luddite.
241  */
242
243 static int wdt_get_temperature(void)
244 {
245         unsigned short c;
246         unsigned long flags;
247
248         spin_lock_irqsave(&wdt_lock, flags);
249         c = inb_p(WDT_RT);
250         spin_unlock_irqrestore(&wdt_lock, flags);
251         return (c * 11 / 15) + 7;
252 }
253
254 static void wdt_decode_501(int status)
255 {
256         if (!(status & WDC_SR_TGOOD))
257                 pr_crit("Overheat alarm (%d)\n", inb_p(WDT_RT));
258         if (!(status & WDC_SR_PSUOVER))
259                 pr_crit("PSU over voltage\n");
260         if (!(status & WDC_SR_PSUUNDR))
261                 pr_crit("PSU under voltage\n");
262 }
263
264 /**
265  *      wdt_interrupt:
266  *      @irq:           Interrupt number
267  *      @dev_id:        Unused as we don't allow multiple devices.
268  *
269  *      Handle an interrupt from the board. These are raised when the status
270  *      map changes in what the board considers an interesting way. That means
271  *      a failure condition occurring.
272  */
273
274 static irqreturn_t wdt_interrupt(int irq, void *dev_id)
275 {
276         /*
277          *      Read the status register see what is up and
278          *      then printk it.
279          */
280         unsigned char status;
281
282         spin_lock(&wdt_lock);
283         status = inb_p(WDT_SR);
284
285         pr_crit("WDT status %d\n", status);
286
287         if (type == 501) {
288                 wdt_decode_501(status);
289                 if (tachometer) {
290                         if (!(status & WDC_SR_FANGOOD))
291                                 pr_crit("Possible fan fault\n");
292                 }
293         }
294         if (!(status & WDC_SR_WCCR)) {
295 #ifdef SOFTWARE_REBOOT
296 #ifdef ONLY_TESTING
297                 pr_crit("Would Reboot\n");
298 #else
299                 pr_crit("Initiating system reboot\n");
300                 emergency_restart();
301 #endif
302 #else
303                 pr_crit("Reset in 5ms\n");
304 #endif
305         }
306         spin_unlock(&wdt_lock);
307         return IRQ_HANDLED;
308 }
309
310
311 /**
312  *      wdt_write:
313  *      @file: file handle to the watchdog
314  *      @buf: buffer to write (unused as data does not matter here
315  *      @count: count of bytes
316  *      @ppos: pointer to the position to write. No seeks allowed
317  *
318  *      A write to a watchdog device is defined as a keepalive signal. Any
319  *      write of data will do, as we we don't define content meaning.
320  */
321
322 static ssize_t wdt_write(struct file *file, const char __user *buf,
323                                                 size_t count, loff_t *ppos)
324 {
325         if (count) {
326                 if (!nowayout) {
327                         size_t i;
328
329                         /* In case it was set long ago */
330                         expect_close = 0;
331
332                         for (i = 0; i != count; i++) {
333                                 char c;
334                                 if (get_user(c, buf + i))
335                                         return -EFAULT;
336                                 if (c == 'V')
337                                         expect_close = 42;
338                         }
339                 }
340                 wdt_ping();
341         }
342         return count;
343 }
344
345 /**
346  *      wdt_ioctl:
347  *      @file: file handle to the device
348  *      @cmd: watchdog command
349  *      @arg: argument pointer
350  *
351  *      The watchdog API defines a common set of functions for all watchdogs
352  *      according to their available features. We only actually usefully support
353  *      querying capabilities and current status.
354  */
355
356 static long wdt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
357 {
358         void __user *argp = (void __user *)arg;
359         int __user *p = argp;
360         int new_heartbeat;
361         int status;
362
363         struct watchdog_info ident = {
364                 .options =              WDIOF_SETTIMEOUT|
365                                         WDIOF_MAGICCLOSE|
366                                         WDIOF_KEEPALIVEPING,
367                 .firmware_version =     1,
368                 .identity =             "WDT500/501",
369         };
370
371         /* Add options according to the card we have */
372         ident.options |= (WDIOF_EXTERN1|WDIOF_EXTERN2);
373         if (type == 501) {
374                 ident.options |= (WDIOF_OVERHEAT|WDIOF_POWERUNDER|
375                                                         WDIOF_POWEROVER);
376                 if (tachometer)
377                         ident.options |= WDIOF_FANFAULT;
378         }
379
380         switch (cmd) {
381         case WDIOC_GETSUPPORT:
382                 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
383         case WDIOC_GETSTATUS:
384                 status = wdt_get_status();
385                 return put_user(status, p);
386         case WDIOC_GETBOOTSTATUS:
387                 return put_user(0, p);
388         case WDIOC_KEEPALIVE:
389                 wdt_ping();
390                 return 0;
391         case WDIOC_SETTIMEOUT:
392                 if (get_user(new_heartbeat, p))
393                         return -EFAULT;
394                 if (wdt_set_heartbeat(new_heartbeat))
395                         return -EINVAL;
396                 wdt_ping();
397                 /* Fall */
398         case WDIOC_GETTIMEOUT:
399                 return put_user(heartbeat, p);
400         default:
401                 return -ENOTTY;
402         }
403 }
404
405 /**
406  *      wdt_open:
407  *      @inode: inode of device
408  *      @file: file handle to device
409  *
410  *      The watchdog device has been opened. The watchdog device is single
411  *      open and on opening we load the counters. Counter zero is a 100Hz
412  *      cascade, into counter 1 which downcounts to reboot. When the counter
413  *      triggers counter 2 downcounts the length of the reset pulse which
414  *      set set to be as long as possible.
415  */
416
417 static int wdt_open(struct inode *inode, struct file *file)
418 {
419         if (test_and_set_bit(0, &wdt_is_open))
420                 return -EBUSY;
421         /*
422          *      Activate
423          */
424         wdt_start();
425         return nonseekable_open(inode, file);
426 }
427
428 /**
429  *      wdt_release:
430  *      @inode: inode to board
431  *      @file: file handle to board
432  *
433  *      The watchdog has a configurable API. There is a religious dispute
434  *      between people who want their watchdog to be able to shut down and
435  *      those who want to be sure if the watchdog manager dies the machine
436  *      reboots. In the former case we disable the counters, in the latter
437  *      case you have to open it again very soon.
438  */
439
440 static int wdt_release(struct inode *inode, struct file *file)
441 {
442         if (expect_close == 42) {
443                 wdt_stop();
444                 clear_bit(0, &wdt_is_open);
445         } else {
446                 pr_crit("WDT device closed unexpectedly.  WDT will not stop!\n");
447                 wdt_ping();
448         }
449         expect_close = 0;
450         return 0;
451 }
452
453 /**
454  *      wdt_temp_read:
455  *      @file: file handle to the watchdog board
456  *      @buf: buffer to write 1 byte into
457  *      @count: length of buffer
458  *      @ptr: offset (no seek allowed)
459  *
460  *      Temp_read reports the temperature in degrees Fahrenheit. The API is in
461  *      farenheit. It was designed by an imperial measurement luddite.
462  */
463
464 static ssize_t wdt_temp_read(struct file *file, char __user *buf,
465                                                 size_t count, loff_t *ptr)
466 {
467         int temperature = wdt_get_temperature();
468
469         if (copy_to_user(buf, &temperature, 1))
470                 return -EFAULT;
471
472         return 1;
473 }
474
475 /**
476  *      wdt_temp_open:
477  *      @inode: inode of device
478  *      @file: file handle to device
479  *
480  *      The temperature device has been opened.
481  */
482
483 static int wdt_temp_open(struct inode *inode, struct file *file)
484 {
485         return nonseekable_open(inode, file);
486 }
487
488 /**
489  *      wdt_temp_release:
490  *      @inode: inode to board
491  *      @file: file handle to board
492  *
493  *      The temperature device has been closed.
494  */
495
496 static int wdt_temp_release(struct inode *inode, struct file *file)
497 {
498         return 0;
499 }
500
501 /**
502  *      notify_sys:
503  *      @this: our notifier block
504  *      @code: the event being reported
505  *      @unused: unused
506  *
507  *      Our notifier is called on system shutdowns. We want to turn the card
508  *      off at reboot otherwise the machine will reboot again during memory
509  *      test or worse yet during the following fsck. This would suck, in fact
510  *      trust me - if it happens it does suck.
511  */
512
513 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
514         void *unused)
515 {
516         if (code == SYS_DOWN || code == SYS_HALT)
517                 wdt_stop();
518         return NOTIFY_DONE;
519 }
520
521 /*
522  *      Kernel Interfaces
523  */
524
525
526 static const struct file_operations wdt_fops = {
527         .owner          = THIS_MODULE,
528         .llseek         = no_llseek,
529         .write          = wdt_write,
530         .unlocked_ioctl = wdt_ioctl,
531         .open           = wdt_open,
532         .release        = wdt_release,
533 };
534
535 static struct miscdevice wdt_miscdev = {
536         .minor  = WATCHDOG_MINOR,
537         .name   = "watchdog",
538         .fops   = &wdt_fops,
539 };
540
541 static const struct file_operations wdt_temp_fops = {
542         .owner          = THIS_MODULE,
543         .llseek         = no_llseek,
544         .read           = wdt_temp_read,
545         .open           = wdt_temp_open,
546         .release        = wdt_temp_release,
547 };
548
549 static struct miscdevice temp_miscdev = {
550         .minor  = TEMP_MINOR,
551         .name   = "temperature",
552         .fops   = &wdt_temp_fops,
553 };
554
555 /*
556  *      The WDT card needs to learn about soft shutdowns in order to
557  *      turn the timebomb registers off.
558  */
559
560 static struct notifier_block wdt_notifier = {
561         .notifier_call = wdt_notify_sys,
562 };
563
564 /**
565  *      cleanup_module:
566  *
567  *      Unload the watchdog. You cannot do this with any file handles open.
568  *      If your watchdog is set to continue ticking on close and you unload
569  *      it, well it keeps ticking. We won't get the interrupt but the board
570  *      will not touch PC memory so all is fine. You just have to load a new
571  *      module in 60 seconds or reboot.
572  */
573
574 static void __exit wdt_exit(void)
575 {
576         misc_deregister(&wdt_miscdev);
577         if (type == 501)
578                 misc_deregister(&temp_miscdev);
579         unregister_reboot_notifier(&wdt_notifier);
580         free_irq(irq, NULL);
581         release_region(io, 8);
582 }
583
584 /**
585  *      wdt_init:
586  *
587  *      Set up the WDT watchdog board. All we have to do is grab the
588  *      resources we require and bitch if anyone beat us to them.
589  *      The open() function will actually kick the board off.
590  */
591
592 static int __init wdt_init(void)
593 {
594         int ret;
595
596         if (type != 500 && type != 501) {
597                 pr_err("unknown card type '%d'\n", type);
598                 return -ENODEV;
599         }
600
601         /* Check that the heartbeat value is within it's range;
602            if not reset to the default */
603         if (wdt_set_heartbeat(heartbeat)) {
604                 wdt_set_heartbeat(WD_TIMO);
605                 pr_info("heartbeat value must be 0 < heartbeat < 65536, using %d\n",
606                         WD_TIMO);
607         }
608
609         if (!request_region(io, 8, "wdt501p")) {
610                 pr_err("I/O address 0x%04x already in use\n", io);
611                 ret = -EBUSY;
612                 goto out;
613         }
614
615         ret = request_irq(irq, wdt_interrupt, 0, "wdt501p", NULL);
616         if (ret) {
617                 pr_err("IRQ %d is not free\n", irq);
618                 goto outreg;
619         }
620
621         ret = register_reboot_notifier(&wdt_notifier);
622         if (ret) {
623                 pr_err("cannot register reboot notifier (err=%d)\n", ret);
624                 goto outirq;
625         }
626
627         if (type == 501) {
628                 ret = misc_register(&temp_miscdev);
629                 if (ret) {
630                         pr_err("cannot register miscdev on minor=%d (err=%d)\n",
631                                TEMP_MINOR, ret);
632                         goto outrbt;
633                 }
634         }
635
636         ret = misc_register(&wdt_miscdev);
637         if (ret) {
638                 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
639                        WATCHDOG_MINOR, ret);
640                 goto outmisc;
641         }
642
643         pr_info("WDT500/501-P driver 0.10 at 0x%04x (Interrupt %d). heartbeat=%d sec (nowayout=%d)\n",
644                 io, irq, heartbeat, nowayout);
645         if (type == 501)
646                 pr_info("Fan Tachometer is %s\n",
647                         tachometer ? "Enabled" : "Disabled");
648         return 0;
649
650 outmisc:
651         if (type == 501)
652                 misc_deregister(&temp_miscdev);
653 outrbt:
654         unregister_reboot_notifier(&wdt_notifier);
655 outirq:
656         free_irq(irq, NULL);
657 outreg:
658         release_region(io, 8);
659 out:
660         return ret;
661 }
662
663 module_init(wdt_init);
664 module_exit(wdt_exit);
665
666 MODULE_AUTHOR("Alan Cox");
667 MODULE_DESCRIPTION("Driver for ISA ICS watchdog cards (WDT500/501)");
668 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
669 MODULE_ALIAS_MISCDEV(TEMP_MINOR);
670 MODULE_LICENSE("GPL");