drm/i915: Catch up to obj_priv->page_list rename in disabled debug code.
[safe/jmp/linux-2.6] / drivers / watchdog / wdrtas.c
1 /*
2  * FIXME: add wdrtas_get_status and wdrtas_get_boot_status as soon as
3  * RTAS calls are available
4  */
5
6 /*
7  * RTAS watchdog driver
8  *
9  * (C) Copyright IBM Corp. 2005
10  * device driver to exploit watchdog RTAS functions
11  *
12  * Authors : Utz Bacher <utz.bacher@de.ibm.com>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2, or (at your option)
17  * any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  */
28
29 #include <linux/fs.h>
30 #include <linux/init.h>
31 #include <linux/kernel.h>
32 #include <linux/miscdevice.h>
33 #include <linux/module.h>
34 #include <linux/notifier.h>
35 #include <linux/reboot.h>
36 #include <linux/types.h>
37 #include <linux/watchdog.h>
38 #include <linux/uaccess.h>
39
40 #include <asm/rtas.h>
41
42 #define WDRTAS_MAGIC_CHAR               42
43 #define WDRTAS_SUPPORTED_MASK           (WDIOF_SETTIMEOUT | \
44                                          WDIOF_MAGICCLOSE)
45
46 MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>");
47 MODULE_DESCRIPTION("RTAS watchdog driver");
48 MODULE_LICENSE("GPL");
49 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
50 MODULE_ALIAS_MISCDEV(TEMP_MINOR);
51
52 static int wdrtas_nowayout = WATCHDOG_NOWAYOUT;
53 static atomic_t wdrtas_miscdev_open = ATOMIC_INIT(0);
54 static char wdrtas_expect_close;
55
56 static int wdrtas_interval;
57
58 #define WDRTAS_THERMAL_SENSOR           3
59 static int wdrtas_token_get_sensor_state;
60 #define WDRTAS_SURVEILLANCE_IND         9000
61 static int wdrtas_token_set_indicator;
62 #define WDRTAS_SP_SPI                   28
63 static int wdrtas_token_get_sp;
64 static int wdrtas_token_event_scan;
65
66 #define WDRTAS_DEFAULT_INTERVAL         300
67
68 #define WDRTAS_LOGBUFFER_LEN            128
69 static char wdrtas_logbuffer[WDRTAS_LOGBUFFER_LEN];
70
71
72 /*** watchdog access functions */
73
74 /**
75  * wdrtas_set_interval - sets the watchdog interval
76  * @interval: new interval
77  *
78  * returns 0 on success, <0 on failures
79  *
80  * wdrtas_set_interval sets the watchdog keepalive interval by calling the
81  * RTAS function set-indicator (surveillance). The unit of interval is
82  * seconds.
83  */
84
85 static int wdrtas_set_interval(int interval)
86 {
87         long result;
88         static int print_msg = 10;
89
90         /* rtas uses minutes */
91         interval = (interval + 59) / 60;
92
93         result = rtas_call(wdrtas_token_set_indicator, 3, 1, NULL,
94                            WDRTAS_SURVEILLANCE_IND, 0, interval);
95         if (result < 0 && print_msg) {
96                 printk(KERN_ERR "wdrtas: setting the watchdog to %i "
97                        "timeout failed: %li\n", interval, result);
98                 print_msg--;
99         }
100
101         return result;
102 }
103
104 #define WDRTAS_SP_SPI_LEN 4
105
106 /**
107  * wdrtas_get_interval - returns the current watchdog interval
108  * @fallback_value: value (in seconds) to use, if the RTAS call fails
109  *
110  * returns the interval
111  *
112  * wdrtas_get_interval returns the current watchdog keepalive interval
113  * as reported by the RTAS function ibm,get-system-parameter. The unit
114  * of the return value is seconds.
115  */
116 static int wdrtas_get_interval(int fallback_value)
117 {
118         long result;
119         char value[WDRTAS_SP_SPI_LEN];
120
121         spin_lock(&rtas_data_buf_lock);
122         memset(rtas_data_buf, 0, WDRTAS_SP_SPI_LEN);
123         result = rtas_call(wdrtas_token_get_sp, 3, 1, NULL,
124                            WDRTAS_SP_SPI, __pa(rtas_data_buf),
125                            WDRTAS_SP_SPI_LEN);
126
127         memcpy(value, rtas_data_buf, WDRTAS_SP_SPI_LEN);
128         spin_unlock(&rtas_data_buf_lock);
129
130         if (value[0] != 0 || value[1] != 2 || value[3] != 0 || result < 0) {
131                 printk(KERN_WARNING "wdrtas: could not get sp_spi watchdog "
132                        "timeout (%li). Continuing\n", result);
133                 return fallback_value;
134         }
135
136         /* rtas uses minutes */
137         return ((int)value[2]) * 60;
138 }
139
140 /**
141  * wdrtas_timer_start - starts watchdog
142  *
143  * wdrtas_timer_start starts the watchdog by calling the RTAS function
144  * set-interval (surveillance)
145  */
146 static void wdrtas_timer_start(void)
147 {
148         wdrtas_set_interval(wdrtas_interval);
149 }
150
151 /**
152  * wdrtas_timer_stop - stops watchdog
153  *
154  * wdrtas_timer_stop stops the watchdog timer by calling the RTAS function
155  * set-interval (surveillance)
156  */
157 static void wdrtas_timer_stop(void)
158 {
159         wdrtas_set_interval(0);
160 }
161
162 /**
163  * wdrtas_log_scanned_event - logs an event we received during keepalive
164  *
165  * wdrtas_log_scanned_event prints a message to the log buffer dumping
166  * the results of the last event-scan call
167  */
168 static void wdrtas_log_scanned_event(void)
169 {
170         int i;
171
172         for (i = 0; i < WDRTAS_LOGBUFFER_LEN; i += 16)
173                 printk(KERN_INFO "wdrtas: dumping event (line %i/%i), data = "
174                        "%02x %02x %02x %02x  %02x %02x %02x %02x   "
175                        "%02x %02x %02x %02x  %02x %02x %02x %02x\n",
176                        (i / 16) + 1, (WDRTAS_LOGBUFFER_LEN / 16),
177                        wdrtas_logbuffer[i + 0], wdrtas_logbuffer[i + 1],
178                        wdrtas_logbuffer[i + 2], wdrtas_logbuffer[i + 3],
179                        wdrtas_logbuffer[i + 4], wdrtas_logbuffer[i + 5],
180                        wdrtas_logbuffer[i + 6], wdrtas_logbuffer[i + 7],
181                        wdrtas_logbuffer[i + 8], wdrtas_logbuffer[i + 9],
182                        wdrtas_logbuffer[i + 10], wdrtas_logbuffer[i + 11],
183                        wdrtas_logbuffer[i + 12], wdrtas_logbuffer[i + 13],
184                        wdrtas_logbuffer[i + 14], wdrtas_logbuffer[i + 15]);
185 }
186
187 /**
188  * wdrtas_timer_keepalive - resets watchdog timer to keep system alive
189  *
190  * wdrtas_timer_keepalive restarts the watchdog timer by calling the
191  * RTAS function event-scan and repeats these calls as long as there are
192  * events available. All events will be dumped.
193  */
194 static void wdrtas_timer_keepalive(void)
195 {
196         long result;
197
198         do {
199                 result = rtas_call(wdrtas_token_event_scan, 4, 1, NULL,
200                                    RTAS_EVENT_SCAN_ALL_EVENTS, 0,
201                                    (void *)__pa(wdrtas_logbuffer),
202                                    WDRTAS_LOGBUFFER_LEN);
203                 if (result < 0)
204                         printk(KERN_ERR "wdrtas: event-scan failed: %li\n",
205                                result);
206                 if (result == 0)
207                         wdrtas_log_scanned_event();
208         } while (result == 0);
209 }
210
211 /**
212  * wdrtas_get_temperature - returns current temperature
213  *
214  * returns temperature or <0 on failures
215  *
216  * wdrtas_get_temperature returns the current temperature in Fahrenheit. It
217  * uses the RTAS call get-sensor-state, token 3 to do so
218  */
219 static int wdrtas_get_temperature(void)
220 {
221         long result;
222         int temperature = 0;
223
224         result = rtas_call(wdrtas_token_get_sensor_state, 2, 2,
225                            (void *)__pa(&temperature),
226                            WDRTAS_THERMAL_SENSOR, 0);
227
228         if (result < 0)
229                 printk(KERN_WARNING "wdrtas: reading the thermal sensor "
230                        "faild: %li\n", result);
231         else
232                 temperature = ((temperature * 9) / 5) + 32; /* fahrenheit */
233
234         return temperature;
235 }
236
237 /**
238  * wdrtas_get_status - returns the status of the watchdog
239  *
240  * returns a bitmask of defines WDIOF_... as defined in
241  * include/linux/watchdog.h
242  */
243 static int wdrtas_get_status(void)
244 {
245         return 0; /* TODO */
246 }
247
248 /**
249  * wdrtas_get_boot_status - returns the reason for the last boot
250  *
251  * returns a bitmask of defines WDIOF_... as defined in
252  * include/linux/watchdog.h, indicating why the watchdog rebooted the system
253  */
254 static int wdrtas_get_boot_status(void)
255 {
256         return 0; /* TODO */
257 }
258
259 /*** watchdog API and operations stuff */
260
261 /* wdrtas_write - called when watchdog device is written to
262  * @file: file structure
263  * @buf: user buffer with data
264  * @len: amount to data written
265  * @ppos: position in file
266  *
267  * returns the number of successfully processed characters, which is always
268  * the number of bytes passed to this function
269  *
270  * wdrtas_write processes all the data given to it and looks for the magic
271  * character 'V'. This character allows the watchdog device to be closed
272  * properly.
273  */
274 static ssize_t wdrtas_write(struct file *file, const char __user *buf,
275              size_t len, loff_t *ppos)
276 {
277         int i;
278         char c;
279
280         if (!len)
281                 goto out;
282
283         if (!wdrtas_nowayout) {
284                 wdrtas_expect_close = 0;
285                 /* look for 'V' */
286                 for (i = 0; i < len; i++) {
287                         if (get_user(c, buf + i))
288                                 return -EFAULT;
289                         /* allow to close device */
290                         if (c == 'V')
291                                 wdrtas_expect_close = WDRTAS_MAGIC_CHAR;
292                 }
293         }
294
295         wdrtas_timer_keepalive();
296
297 out:
298         return len;
299 }
300
301 /**
302  * wdrtas_ioctl - ioctl function for the watchdog device
303  * @file: file structure
304  * @cmd: command for ioctl
305  * @arg: argument pointer
306  *
307  * returns 0 on success, <0 on failure
308  *
309  * wdrtas_ioctl implements the watchdog API ioctls
310  */
311
312 static long wdrtas_ioctl(struct file *file, unsigned int cmd,
313                                                         unsigned long arg)
314 {
315         int __user *argp = (void __user *)arg;
316         int i;
317         static struct watchdog_info wdinfo = {
318                 .options = WDRTAS_SUPPORTED_MASK,
319                 .firmware_version = 0,
320                 .identity = "wdrtas",
321         };
322
323         switch (cmd) {
324         case WDIOC_GETSUPPORT:
325                 if (copy_to_user(argp, &wdinfo, sizeof(wdinfo)))
326                         return -EFAULT;
327                 return 0;
328
329         case WDIOC_GETSTATUS:
330                 i = wdrtas_get_status();
331                 return put_user(i, argp);
332
333         case WDIOC_GETBOOTSTATUS:
334                 i = wdrtas_get_boot_status();
335                 return put_user(i, argp);
336
337         case WDIOC_GETTEMP:
338                 if (wdrtas_token_get_sensor_state == RTAS_UNKNOWN_SERVICE)
339                         return -EOPNOTSUPP;
340
341                 i = wdrtas_get_temperature();
342                 return put_user(i, argp);
343
344         case WDIOC_SETOPTIONS:
345                 if (get_user(i, argp))
346                         return -EFAULT;
347                 if (i & WDIOS_DISABLECARD)
348                         wdrtas_timer_stop();
349                 if (i & WDIOS_ENABLECARD) {
350                         wdrtas_timer_keepalive();
351                         wdrtas_timer_start();
352                 }
353                 /* not implemented. Done by H8
354                 if (i & WDIOS_TEMPPANIC) {
355                 } */
356                 return 0;
357
358         case WDIOC_KEEPALIVE:
359                 wdrtas_timer_keepalive();
360                 return 0;
361
362         case WDIOC_SETTIMEOUT:
363                 if (get_user(i, argp))
364                         return -EFAULT;
365
366                 if (wdrtas_set_interval(i))
367                         return -EINVAL;
368
369                 wdrtas_timer_keepalive();
370
371                 if (wdrtas_token_get_sp == RTAS_UNKNOWN_SERVICE)
372                         wdrtas_interval = i;
373                 else
374                         wdrtas_interval = wdrtas_get_interval(i);
375                 /* fallthrough */
376
377         case WDIOC_GETTIMEOUT:
378                 return put_user(wdrtas_interval, argp);
379
380         default:
381                 return -ENOTTY;
382         }
383 }
384
385 /**
386  * wdrtas_open - open function of watchdog device
387  * @inode: inode structure
388  * @file: file structure
389  *
390  * returns 0 on success, -EBUSY if the file has been opened already, <0 on
391  * other failures
392  *
393  * function called when watchdog device is opened
394  */
395 static int wdrtas_open(struct inode *inode, struct file *file)
396 {
397         /* only open once */
398         if (atomic_inc_return(&wdrtas_miscdev_open) > 1) {
399                 atomic_dec(&wdrtas_miscdev_open);
400                 return -EBUSY;
401         }
402
403         wdrtas_timer_start();
404         wdrtas_timer_keepalive();
405
406         return nonseekable_open(inode, file);
407 }
408
409 /**
410  * wdrtas_close - close function of watchdog device
411  * @inode: inode structure
412  * @file: file structure
413  *
414  * returns 0 on success
415  *
416  * close function. Always succeeds
417  */
418 static int wdrtas_close(struct inode *inode, struct file *file)
419 {
420         /* only stop watchdog, if this was announced using 'V' before */
421         if (wdrtas_expect_close == WDRTAS_MAGIC_CHAR)
422                 wdrtas_timer_stop();
423         else {
424                 printk(KERN_WARNING "wdrtas: got unexpected close. Watchdog "
425                        "not stopped.\n");
426                 wdrtas_timer_keepalive();
427         }
428
429         wdrtas_expect_close = 0;
430         atomic_dec(&wdrtas_miscdev_open);
431         return 0;
432 }
433
434 /**
435  * wdrtas_temp_read - gives back the temperature in fahrenheit
436  * @file: file structure
437  * @buf: user buffer
438  * @count: number of bytes to be read
439  * @ppos: position in file
440  *
441  * returns always 1 or -EFAULT in case of user space copy failures, <0 on
442  * other failures
443  *
444  * wdrtas_temp_read gives the temperature to the users by copying this
445  * value as one byte into the user space buffer. The unit is Fahrenheit...
446  */
447 static ssize_t wdrtas_temp_read(struct file *file, char __user *buf,
448                  size_t count, loff_t *ppos)
449 {
450         int temperature = 0;
451
452         temperature = wdrtas_get_temperature();
453         if (temperature < 0)
454                 return temperature;
455
456         if (copy_to_user(buf, &temperature, 1))
457                 return -EFAULT;
458
459         return 1;
460 }
461
462 /**
463  * wdrtas_temp_open - open function of temperature device
464  * @inode: inode structure
465  * @file: file structure
466  *
467  * returns 0 on success, <0 on failure
468  *
469  * function called when temperature device is opened
470  */
471 static int wdrtas_temp_open(struct inode *inode, struct file *file)
472 {
473         return nonseekable_open(inode, file);
474 }
475
476 /**
477  * wdrtas_temp_close - close function of temperature device
478  * @inode: inode structure
479  * @file: file structure
480  *
481  * returns 0 on success
482  *
483  * close function. Always succeeds
484  */
485 static int wdrtas_temp_close(struct inode *inode, struct file *file)
486 {
487         return 0;
488 }
489
490 /**
491  * wdrtas_reboot - reboot notifier function
492  * @nb: notifier block structure
493  * @code: reboot code
494  * @ptr: unused
495  *
496  * returns NOTIFY_DONE
497  *
498  * wdrtas_reboot stops the watchdog in case of a reboot
499  */
500 static int wdrtas_reboot(struct notifier_block *this,
501                                         unsigned long code, void *ptr)
502 {
503         if (code == SYS_DOWN || code == SYS_HALT)
504                 wdrtas_timer_stop();
505
506         return NOTIFY_DONE;
507 }
508
509 /*** initialization stuff */
510
511 static const struct file_operations wdrtas_fops = {
512         .owner          = THIS_MODULE,
513         .llseek         = no_llseek,
514         .write          = wdrtas_write,
515         .unlocked_ioctl = wdrtas_ioctl,
516         .open           = wdrtas_open,
517         .release        = wdrtas_close,
518 };
519
520 static struct miscdevice wdrtas_miscdev = {
521         .minor =        WATCHDOG_MINOR,
522         .name =         "watchdog",
523         .fops =         &wdrtas_fops,
524 };
525
526 static const struct file_operations wdrtas_temp_fops = {
527         .owner          = THIS_MODULE,
528         .llseek         = no_llseek,
529         .read           = wdrtas_temp_read,
530         .open           = wdrtas_temp_open,
531         .release        = wdrtas_temp_close,
532 };
533
534 static struct miscdevice wdrtas_tempdev = {
535         .minor =        TEMP_MINOR,
536         .name =         "temperature",
537         .fops =         &wdrtas_temp_fops,
538 };
539
540 static struct notifier_block wdrtas_notifier = {
541         .notifier_call =        wdrtas_reboot,
542 };
543
544 /**
545  * wdrtas_get_tokens - reads in RTAS tokens
546  *
547  * returns 0 on succes, <0 on failure
548  *
549  * wdrtas_get_tokens reads in the tokens for the RTAS calls used in
550  * this watchdog driver. It tolerates, if "get-sensor-state" and
551  * "ibm,get-system-parameter" are not available.
552  */
553 static int wdrtas_get_tokens(void)
554 {
555         wdrtas_token_get_sensor_state = rtas_token("get-sensor-state");
556         if (wdrtas_token_get_sensor_state == RTAS_UNKNOWN_SERVICE) {
557                 printk(KERN_WARNING "wdrtas: couldn't get token for "
558                        "get-sensor-state. Trying to continue without "
559                        "temperature support.\n");
560         }
561
562         wdrtas_token_get_sp = rtas_token("ibm,get-system-parameter");
563         if (wdrtas_token_get_sp == RTAS_UNKNOWN_SERVICE) {
564                 printk(KERN_WARNING "wdrtas: couldn't get token for "
565                        "ibm,get-system-parameter. Trying to continue with "
566                        "a default timeout value of %i seconds.\n",
567                        WDRTAS_DEFAULT_INTERVAL);
568         }
569
570         wdrtas_token_set_indicator = rtas_token("set-indicator");
571         if (wdrtas_token_set_indicator == RTAS_UNKNOWN_SERVICE) {
572                 printk(KERN_ERR "wdrtas: couldn't get token for "
573                        "set-indicator. Terminating watchdog code.\n");
574                 return -EIO;
575         }
576
577         wdrtas_token_event_scan = rtas_token("event-scan");
578         if (wdrtas_token_event_scan == RTAS_UNKNOWN_SERVICE) {
579                 printk(KERN_ERR "wdrtas: couldn't get token for event-scan. "
580                        "Terminating watchdog code.\n");
581                 return -EIO;
582         }
583
584         return 0;
585 }
586
587 /**
588  * wdrtas_unregister_devs - unregisters the misc dev handlers
589  *
590  * wdrtas_register_devs unregisters the watchdog and temperature watchdog
591  * misc devs
592  */
593 static void wdrtas_unregister_devs(void)
594 {
595         misc_deregister(&wdrtas_miscdev);
596         if (wdrtas_token_get_sensor_state != RTAS_UNKNOWN_SERVICE)
597                 misc_deregister(&wdrtas_tempdev);
598 }
599
600 /**
601  * wdrtas_register_devs - registers the misc dev handlers
602  *
603  * returns 0 on succes, <0 on failure
604  *
605  * wdrtas_register_devs registers the watchdog and temperature watchdog
606  * misc devs
607  */
608 static int wdrtas_register_devs(void)
609 {
610         int result;
611
612         result = misc_register(&wdrtas_miscdev);
613         if (result) {
614                 printk(KERN_ERR "wdrtas: couldn't register watchdog misc "
615                        "device. Terminating watchdog code.\n");
616                 return result;
617         }
618
619         if (wdrtas_token_get_sensor_state != RTAS_UNKNOWN_SERVICE) {
620                 result = misc_register(&wdrtas_tempdev);
621                 if (result) {
622                         printk(KERN_WARNING "wdrtas: couldn't register "
623                                "watchdog temperature misc device. Continuing "
624                                "without temperature support.\n");
625                         wdrtas_token_get_sensor_state = RTAS_UNKNOWN_SERVICE;
626                 }
627         }
628
629         return 0;
630 }
631
632 /**
633  * wdrtas_init - init function of the watchdog driver
634  *
635  * returns 0 on succes, <0 on failure
636  *
637  * registers the file handlers and the reboot notifier
638  */
639 static int __init wdrtas_init(void)
640 {
641         if (wdrtas_get_tokens())
642                 return -ENODEV;
643
644         if (wdrtas_register_devs())
645                 return -ENODEV;
646
647         if (register_reboot_notifier(&wdrtas_notifier)) {
648                 printk(KERN_ERR "wdrtas: could not register reboot notifier. "
649                        "Terminating watchdog code.\n");
650                 wdrtas_unregister_devs();
651                 return -ENODEV;
652         }
653
654         if (wdrtas_token_get_sp == RTAS_UNKNOWN_SERVICE)
655                 wdrtas_interval = WDRTAS_DEFAULT_INTERVAL;
656         else
657                 wdrtas_interval = wdrtas_get_interval(WDRTAS_DEFAULT_INTERVAL);
658
659         return 0;
660 }
661
662 /**
663  * wdrtas_exit - exit function of the watchdog driver
664  *
665  * unregisters the file handlers and the reboot notifier
666  */
667 static void __exit wdrtas_exit(void)
668 {
669         if (!wdrtas_nowayout)
670                 wdrtas_timer_stop();
671
672         wdrtas_unregister_devs();
673
674         unregister_reboot_notifier(&wdrtas_notifier);
675 }
676
677 module_init(wdrtas_init);
678 module_exit(wdrtas_exit);