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