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