29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33 #include <linux/kernel.h>
35 #include <linux/module.h>
37 #include <linux/reboot.h>
38 #include <linux/types.h>
39 #include <linux/watchdog.h>
44 #define WDRTAS_MAGIC_CHAR 42
45 #define WDRTAS_SUPPORTED_MASK (WDIOF_SETTIMEOUT | \
56 static char wdrtas_expect_close;
58 static int wdrtas_interval;
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;
68 #define WDRTAS_DEFAULT_INTERVAL 300
70 #define WDRTAS_LOGBUFFER_LEN 128
87 static int wdrtas_set_interval(
int interval)
90 static int print_msg = 10;
93 interval = (interval + 59) / 60;
97 if (result < 0 && print_msg) {
98 pr_err(
"setting the watchdog to %i timeout failed: %li\n",
106 #define WDRTAS_SP_SPI_LEN 4
118 static int wdrtas_get_interval(
int fallback_value)
123 spin_lock(&rtas_data_buf_lock);
130 spin_unlock(&rtas_data_buf_lock);
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",
135 return fallback_value;
139 return ((
int)value[2]) * 60;
148 static void wdrtas_timer_start(
void)
150 wdrtas_set_interval(wdrtas_interval);
159 static void wdrtas_timer_stop(
void)
161 wdrtas_set_interval(0);
170 static void wdrtas_log_scanned_event(
void)
175 pr_info(
"dumping event (line %i/%i), data = "
176 "%02x %02x %02x %02x %02x %02x %02x %02x "
177 "%02x %02x %02x %02x %02x %02x %02x %02x\n",
178 (i / 16) + 1, (WDRTAS_LOGBUFFER_LEN / 16),
179 wdrtas_logbuffer[i + 0], wdrtas_logbuffer[i + 1],
180 wdrtas_logbuffer[i + 2], wdrtas_logbuffer[i + 3],
181 wdrtas_logbuffer[i + 4], wdrtas_logbuffer[i + 5],
182 wdrtas_logbuffer[i + 6], wdrtas_logbuffer[i + 7],
183 wdrtas_logbuffer[i + 8], wdrtas_logbuffer[i + 9],
184 wdrtas_logbuffer[i + 10], wdrtas_logbuffer[i + 11],
185 wdrtas_logbuffer[i + 12], wdrtas_logbuffer[i + 13],
186 wdrtas_logbuffer[i + 14], wdrtas_logbuffer[i + 15]);
196 static void wdrtas_timer_keepalive(
void)
202 RTAS_EVENT_SCAN_ALL_EVENTS, 0,
203 (
void *)
__pa(wdrtas_logbuffer),
204 WDRTAS_LOGBUFFER_LEN);
206 pr_err(
"event-scan failed: %li\n", result);
208 wdrtas_log_scanned_event();
209 }
while (result == 0);
220 static int wdrtas_get_temperature(
void)
228 pr_warn(
"reading the thermal sensor failed: %i\n", result);
230 temperature = ((temperature * 9) / 5) + 32;
241 static int wdrtas_get_status(
void)
252 static int wdrtas_get_boot_status(
void)
273 size_t len, loff_t *ppos)
281 if (!wdrtas_nowayout) {
282 wdrtas_expect_close = 0;
284 for (i = 0; i < len; i++) {
293 wdrtas_timer_keepalive();
310 static long wdrtas_ioctl(
struct file *file,
unsigned int cmd,
317 .firmware_version = 0,
318 .identity =
"wdrtas",
328 i = wdrtas_get_status();
332 i = wdrtas_get_boot_status();
336 if (wdrtas_token_get_sensor_state == RTAS_UNKNOWN_SERVICE)
339 i = wdrtas_get_temperature();
348 wdrtas_timer_keepalive();
349 wdrtas_timer_start();
357 wdrtas_timer_keepalive();
364 if (wdrtas_set_interval(i))
367 wdrtas_timer_keepalive();
369 if (wdrtas_token_get_sp == RTAS_UNKNOWN_SERVICE)
372 wdrtas_interval = wdrtas_get_interval(i);
376 return put_user(wdrtas_interval, argp);
393 static int wdrtas_open(
struct inode *
inode,
struct file *file)
401 wdrtas_timer_start();
402 wdrtas_timer_keepalive();
416 static int wdrtas_close(
struct inode *inode,
struct file *file)
422 pr_warn(
"got unexpected close. Watchdog not stopped.\n");
423 wdrtas_timer_keepalive();
426 wdrtas_expect_close = 0;
444 static ssize_t wdrtas_temp_read(
struct file *file,
char __user *buf,
445 size_t count, loff_t *ppos)
449 temperature = wdrtas_get_temperature();
468 static int wdrtas_temp_open(
struct inode *inode,
struct file *file)
482 static int wdrtas_temp_close(
struct inode *inode,
struct file *file)
511 .write = wdrtas_write,
512 .unlocked_ioctl = wdrtas_ioctl,
514 .release = wdrtas_close,
520 .fops = &wdrtas_fops,
526 .read = wdrtas_temp_read,
527 .open = wdrtas_temp_open,
528 .release = wdrtas_temp_close,
533 .name =
"temperature",
534 .fops = &wdrtas_temp_fops,
550 static int wdrtas_get_tokens(
void)
552 wdrtas_token_get_sensor_state =
rtas_token(
"get-sensor-state");
553 if (wdrtas_token_get_sensor_state == RTAS_UNKNOWN_SERVICE) {
554 pr_warn(
"couldn't get token for get-sensor-state. Trying to continue without temperature support.\n");
557 wdrtas_token_get_sp =
rtas_token(
"ibm,get-system-parameter");
558 if (wdrtas_token_get_sp == RTAS_UNKNOWN_SERVICE) {
559 pr_warn(
"couldn't get token for ibm,get-system-parameter. Trying to continue with a default timeout value of %i seconds.\n",
563 wdrtas_token_set_indicator =
rtas_token(
"set-indicator");
564 if (wdrtas_token_set_indicator == RTAS_UNKNOWN_SERVICE) {
565 pr_err(
"couldn't get token for set-indicator. Terminating watchdog code.\n");
569 wdrtas_token_event_scan =
rtas_token(
"event-scan");
570 if (wdrtas_token_event_scan == RTAS_UNKNOWN_SERVICE) {
571 pr_err(
"couldn't get token for event-scan. Terminating watchdog code.\n");
584 static void wdrtas_unregister_devs(
void)
587 if (wdrtas_token_get_sensor_state != RTAS_UNKNOWN_SERVICE)
599 static int wdrtas_register_devs(
void)
605 pr_err(
"couldn't register watchdog misc device. Terminating watchdog code.\n");
609 if (wdrtas_token_get_sensor_state != RTAS_UNKNOWN_SERVICE) {
612 pr_warn(
"couldn't register watchdog temperature misc device. Continuing without temperature support.\n");
613 wdrtas_token_get_sensor_state = RTAS_UNKNOWN_SERVICE;
627 static int __init wdrtas_init(
void)
629 if (wdrtas_get_tokens())
632 if (wdrtas_register_devs())
636 pr_err(
"could not register reboot notifier. Terminating watchdog code.\n");
637 wdrtas_unregister_devs();
641 if (wdrtas_token_get_sp == RTAS_UNKNOWN_SERVICE)
654 static void __exit wdrtas_exit(
void)
656 if (!wdrtas_nowayout)
659 wdrtas_unregister_devs();