^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * watchdog_dev.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * (c) Copyright 2008-2011 Alan Cox <alan@lxorguk.ukuu.org.uk>,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * (c) Copyright 2008-2011 Wim Van Sebroeck <wim@iguana.be>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * This source code is part of the generic code that can be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * by all the watchdog timer drivers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * This part of the generic code takes care of the following
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * misc device: /dev/watchdog.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * Based on source code of the following authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * Matt Domsch <Matt_Domsch@dell.com>,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * Rob Radez <rob@osinvestor.com>,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * Rusty Lynch <rusty@linux.co.intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * Satyam Sharma <satyam@infradead.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * Randy Dunlap <randy.dunlap@oracle.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * Neither Alan Cox, CymruNet Ltd., Wim Van Sebroeck nor Iguana vzw.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * admit liability nor provide warranty for any of this software.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * This material is provided "AS-IS" and at no charge.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/cdev.h> /* For character device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/errno.h> /* For the -ENODEV/... values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/fs.h> /* For file operations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/init.h> /* For __init/__exit/... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <linux/hrtimer.h> /* For hrtimers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <linux/kernel.h> /* For printk/panic/... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <linux/kthread.h> /* For kthread_work */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <linux/miscdevice.h> /* For handling misc devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <linux/module.h> /* For module stuff/... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <linux/mutex.h> /* For mutexes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <linux/slab.h> /* For memory functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include <linux/types.h> /* For standard types (like size_t) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <linux/watchdog.h> /* For watchdog specific items */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include "watchdog_core.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #include "watchdog_pretimeout.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * struct watchdog_core_data - watchdog core internal data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * @dev: The watchdog's internal device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * @cdev: The watchdog's Character device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * @wdd: Pointer to watchdog device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * @lock: Lock for watchdog core.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * @status: Watchdog core internal status bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct watchdog_core_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct device dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct cdev cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct watchdog_device *wdd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) ktime_t last_keepalive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) ktime_t last_hw_keepalive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) ktime_t open_deadline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct hrtimer timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct kthread_work work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) unsigned long status; /* Internal status bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #define _WDOG_DEV_OPEN 0 /* Opened ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) #define _WDOG_ALLOW_RELEASE 1 /* Did we receive the magic char ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #define _WDOG_KEEPALIVE 2 /* Did we receive a keepalive ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /* the dev_t structure to store the dynamically allocated watchdog devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static dev_t watchdog_devt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) /* Reference to watchdog device behind /dev/watchdog */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static struct watchdog_core_data *old_wd_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static struct kthread_worker *watchdog_kworker;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static bool handle_boot_enabled =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) IS_ENABLED(CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static unsigned open_timeout = CONFIG_WATCHDOG_OPEN_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static bool watchdog_past_open_deadline(struct watchdog_core_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return ktime_after(ktime_get(), data->open_deadline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) static void watchdog_set_open_deadline(struct watchdog_core_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) data->open_deadline = open_timeout ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) ktime_get() + ktime_set(open_timeout, 0) : KTIME_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static inline bool watchdog_need_worker(struct watchdog_device *wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /* All variables in milli-seconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) unsigned int hm = wdd->max_hw_heartbeat_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) unsigned int t = wdd->timeout * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * A worker to generate heartbeat requests is needed if all of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * following conditions are true.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * - Userspace activated the watchdog.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * - The driver provided a value for the maximum hardware timeout, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * thus is aware that the framework supports generating heartbeat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * requests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * - Userspace requests a longer timeout than the hardware can handle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * Alternatively, if userspace has not opened the watchdog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * device, we take care of feeding the watchdog if it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * running.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return (hm && watchdog_active(wdd) && t > hm) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) (t && !watchdog_active(wdd) && watchdog_hw_running(wdd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static ktime_t watchdog_next_keepalive(struct watchdog_device *wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct watchdog_core_data *wd_data = wdd->wd_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) unsigned int timeout_ms = wdd->timeout * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) ktime_t keepalive_interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) ktime_t last_heartbeat, latest_heartbeat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) ktime_t virt_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) unsigned int hw_heartbeat_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (watchdog_active(wdd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) virt_timeout = ktime_add(wd_data->last_keepalive,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) ms_to_ktime(timeout_ms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) virt_timeout = wd_data->open_deadline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) hw_heartbeat_ms = min_not_zero(timeout_ms, wdd->max_hw_heartbeat_ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) keepalive_interval = ms_to_ktime(hw_heartbeat_ms / 2);
^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) * To ensure that the watchdog times out wdd->timeout seconds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * after the most recent ping from userspace, the last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * worker ping has to come in hw_heartbeat_ms before this timeout.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) last_heartbeat = ktime_sub(virt_timeout, ms_to_ktime(hw_heartbeat_ms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) latest_heartbeat = ktime_sub(last_heartbeat, ktime_get());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (ktime_before(latest_heartbeat, keepalive_interval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return latest_heartbeat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return keepalive_interval;
^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) static inline void watchdog_update_worker(struct watchdog_device *wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct watchdog_core_data *wd_data = wdd->wd_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (watchdog_need_worker(wdd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) ktime_t t = watchdog_next_keepalive(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (t > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) hrtimer_start(&wd_data->timer, t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) HRTIMER_MODE_REL_HARD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) hrtimer_cancel(&wd_data->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static int __watchdog_ping(struct watchdog_device *wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct watchdog_core_data *wd_data = wdd->wd_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) ktime_t earliest_keepalive, now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) earliest_keepalive = ktime_add(wd_data->last_hw_keepalive,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) ms_to_ktime(wdd->min_hw_heartbeat_ms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) now = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (ktime_after(earliest_keepalive, now)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) hrtimer_start(&wd_data->timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) ktime_sub(earliest_keepalive, now),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) HRTIMER_MODE_REL_HARD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) wd_data->last_hw_keepalive = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (wdd->ops->ping)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) err = wdd->ops->ping(wdd); /* ping the watchdog */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) err = wdd->ops->start(wdd); /* restart watchdog */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) watchdog_update_worker(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * watchdog_ping: ping the watchdog.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * @wdd: the watchdog device to ping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * The caller must hold wd_data->lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * If the watchdog has no own ping operation then it needs to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * restarted via the start operation. This wrapper function does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * exactly that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * We only ping when the watchdog device is running.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static int watchdog_ping(struct watchdog_device *wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) struct watchdog_core_data *wd_data = wdd->wd_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (!watchdog_active(wdd) && !watchdog_hw_running(wdd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) set_bit(_WDOG_KEEPALIVE, &wd_data->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) wd_data->last_keepalive = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return __watchdog_ping(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static bool watchdog_worker_should_ping(struct watchdog_core_data *wd_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) struct watchdog_device *wdd = wd_data->wdd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (!wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (watchdog_active(wdd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return watchdog_hw_running(wdd) && !watchdog_past_open_deadline(wd_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) static void watchdog_ping_work(struct kthread_work *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) struct watchdog_core_data *wd_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) wd_data = container_of(work, struct watchdog_core_data, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) mutex_lock(&wd_data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (watchdog_worker_should_ping(wd_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) __watchdog_ping(wd_data->wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) mutex_unlock(&wd_data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static enum hrtimer_restart watchdog_timer_expired(struct hrtimer *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) struct watchdog_core_data *wd_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) wd_data = container_of(timer, struct watchdog_core_data, timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) kthread_queue_work(watchdog_kworker, &wd_data->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) return HRTIMER_NORESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^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) * watchdog_start: wrapper to start the watchdog.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * @wdd: the watchdog device to start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * The caller must hold wd_data->lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * Start the watchdog if it is not active and mark it active.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * This function returns zero on success or a negative errno code for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) static int watchdog_start(struct watchdog_device *wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct watchdog_core_data *wd_data = wdd->wd_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) ktime_t started_at;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (watchdog_active(wdd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) set_bit(_WDOG_KEEPALIVE, &wd_data->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) started_at = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if (watchdog_hw_running(wdd) && wdd->ops->ping) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) err = __watchdog_ping(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (err == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) set_bit(WDOG_ACTIVE, &wdd->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) err = wdd->ops->start(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (err == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) set_bit(WDOG_ACTIVE, &wdd->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) wd_data->last_keepalive = started_at;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) wd_data->last_hw_keepalive = started_at;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) watchdog_update_worker(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * watchdog_stop: wrapper to stop the watchdog.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * @wdd: the watchdog device to stop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * The caller must hold wd_data->lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * Stop the watchdog if it is still active and unmark it active.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * This function returns zero on success or a negative errno code for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * If the 'nowayout' feature was set, the watchdog cannot be stopped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) static int watchdog_stop(struct watchdog_device *wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (!watchdog_active(wdd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (test_bit(WDOG_NO_WAY_OUT, &wdd->status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) pr_info("watchdog%d: nowayout prevents watchdog being stopped!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) wdd->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (wdd->ops->stop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) clear_bit(WDOG_HW_RUNNING, &wdd->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) err = wdd->ops->stop(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) set_bit(WDOG_HW_RUNNING, &wdd->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (err == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) clear_bit(WDOG_ACTIVE, &wdd->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) watchdog_update_worker(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) * watchdog_get_status: wrapper to get the watchdog status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) * @wdd: the watchdog device to get the status from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) * The caller must hold wd_data->lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * Get the watchdog's status flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) static unsigned int watchdog_get_status(struct watchdog_device *wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) struct watchdog_core_data *wd_data = wdd->wd_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) unsigned int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (wdd->ops->status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) status = wdd->ops->status(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) status = wdd->bootstatus & (WDIOF_CARDRESET |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) WDIOF_OVERHEAT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) WDIOF_FANFAULT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) WDIOF_EXTERN1 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) WDIOF_EXTERN2 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) WDIOF_POWERUNDER |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) WDIOF_POWEROVER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (test_bit(_WDOG_ALLOW_RELEASE, &wd_data->status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) status |= WDIOF_MAGICCLOSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if (test_and_clear_bit(_WDOG_KEEPALIVE, &wd_data->status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) status |= WDIOF_KEEPALIVEPING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) * watchdog_set_timeout: set the watchdog timer timeout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) * @wdd: the watchdog device to set the timeout for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) * @timeout: timeout to set in seconds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) * The caller must hold wd_data->lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) static int watchdog_set_timeout(struct watchdog_device *wdd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) unsigned int timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (!(wdd->info->options & WDIOF_SETTIMEOUT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (watchdog_timeout_invalid(wdd, timeout))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) if (wdd->ops->set_timeout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) err = wdd->ops->set_timeout(wdd, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) wdd->timeout = timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) /* Disable pretimeout if it doesn't fit the new timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (wdd->pretimeout >= wdd->timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) wdd->pretimeout = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) watchdog_update_worker(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) * watchdog_set_pretimeout: set the watchdog timer pretimeout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * @wdd: the watchdog device to set the timeout for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * @timeout: pretimeout to set in seconds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) static int watchdog_set_pretimeout(struct watchdog_device *wdd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) unsigned int timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) if (!(wdd->info->options & WDIOF_PRETIMEOUT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) if (watchdog_pretimeout_invalid(wdd, timeout))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) if (wdd->ops->set_pretimeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) err = wdd->ops->set_pretimeout(wdd, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) wdd->pretimeout = timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * watchdog_get_timeleft: wrapper to get the time left before a reboot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) * @wdd: the watchdog device to get the remaining time from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) * @timeleft: the time that's left
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) * The caller must hold wd_data->lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) * Get the time before a watchdog will reboot (if not pinged).
^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) static int watchdog_get_timeleft(struct watchdog_device *wdd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) unsigned int *timeleft)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) *timeleft = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) if (!wdd->ops->get_timeleft)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) *timeleft = wdd->ops->get_timeleft(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) #ifdef CONFIG_WATCHDOG_SYSFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) static ssize_t nowayout_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) struct watchdog_device *wdd = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) return sprintf(buf, "%d\n", !!test_bit(WDOG_NO_WAY_OUT, &wdd->status));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) static ssize_t nowayout_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) struct watchdog_device *wdd = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) unsigned int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) ret = kstrtouint(buf, 0, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) if (value > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) /* nowayout cannot be disabled once set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) if (test_bit(WDOG_NO_WAY_OUT, &wdd->status) && !value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) watchdog_set_nowayout(wdd, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) static DEVICE_ATTR_RW(nowayout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) static ssize_t status_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) struct watchdog_device *wdd = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) struct watchdog_core_data *wd_data = wdd->wd_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) unsigned int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) mutex_lock(&wd_data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) status = watchdog_get_status(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) mutex_unlock(&wd_data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) return sprintf(buf, "0x%x\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) static DEVICE_ATTR_RO(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) static ssize_t bootstatus_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) struct watchdog_device *wdd = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) return sprintf(buf, "%u\n", wdd->bootstatus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) static DEVICE_ATTR_RO(bootstatus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) static ssize_t timeleft_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) struct watchdog_device *wdd = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) struct watchdog_core_data *wd_data = wdd->wd_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) ssize_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) unsigned int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) mutex_lock(&wd_data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) status = watchdog_get_timeleft(wdd, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) mutex_unlock(&wd_data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) if (!status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) status = sprintf(buf, "%u\n", val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) static DEVICE_ATTR_RO(timeleft);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) static ssize_t timeout_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) struct watchdog_device *wdd = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) return sprintf(buf, "%u\n", wdd->timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) static DEVICE_ATTR_RO(timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) static ssize_t pretimeout_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) struct watchdog_device *wdd = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) return sprintf(buf, "%u\n", wdd->pretimeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) static DEVICE_ATTR_RO(pretimeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) static ssize_t identity_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) struct watchdog_device *wdd = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) return sprintf(buf, "%s\n", wdd->info->identity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) static DEVICE_ATTR_RO(identity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) static ssize_t state_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) struct watchdog_device *wdd = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) if (watchdog_active(wdd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) return sprintf(buf, "active\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) return sprintf(buf, "inactive\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) static DEVICE_ATTR_RO(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) static ssize_t pretimeout_available_governors_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) return watchdog_pretimeout_available_governors_get(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) static DEVICE_ATTR_RO(pretimeout_available_governors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) static ssize_t pretimeout_governor_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) struct watchdog_device *wdd = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) return watchdog_pretimeout_governor_get(wdd, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) static ssize_t pretimeout_governor_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) struct watchdog_device *wdd = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) int ret = watchdog_pretimeout_governor_set(wdd, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) static DEVICE_ATTR_RW(pretimeout_governor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) static umode_t wdt_is_visible(struct kobject *kobj, struct attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) struct device *dev = kobj_to_dev(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) struct watchdog_device *wdd = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) umode_t mode = attr->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) if (attr == &dev_attr_timeleft.attr && !wdd->ops->get_timeleft)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) mode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) else if (attr == &dev_attr_pretimeout.attr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) !(wdd->info->options & WDIOF_PRETIMEOUT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) mode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) else if ((attr == &dev_attr_pretimeout_governor.attr ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) attr == &dev_attr_pretimeout_available_governors.attr) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) (!(wdd->info->options & WDIOF_PRETIMEOUT) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) !IS_ENABLED(CONFIG_WATCHDOG_PRETIMEOUT_GOV)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) mode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) return mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) static struct attribute *wdt_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) &dev_attr_state.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) &dev_attr_identity.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) &dev_attr_timeout.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) &dev_attr_pretimeout.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) &dev_attr_timeleft.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) &dev_attr_bootstatus.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) &dev_attr_status.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) &dev_attr_nowayout.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) &dev_attr_pretimeout_governor.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) &dev_attr_pretimeout_available_governors.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) static const struct attribute_group wdt_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) .attrs = wdt_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) .is_visible = wdt_is_visible,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) __ATTRIBUTE_GROUPS(wdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) #define wdt_groups NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) * watchdog_ioctl_op: call the watchdog drivers ioctl op if defined
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) * @wdd: the watchdog device to do the ioctl on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) * @cmd: watchdog command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) * @arg: argument pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) * The caller must hold wd_data->lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) static int watchdog_ioctl_op(struct watchdog_device *wdd, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) if (!wdd->ops->ioctl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) return -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) return wdd->ops->ioctl(wdd, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) * watchdog_write: writes to the watchdog.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) * @file: file from VFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) * @data: user address of data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) * @len: length of data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) * @ppos: pointer to the file offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) * A write to a watchdog device is defined as a keepalive ping.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) * Writing the magic 'V' sequence allows the next close to turn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) * off the watchdog (if 'nowayout' is not set).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) static ssize_t watchdog_write(struct file *file, const char __user *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) size_t len, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) struct watchdog_core_data *wd_data = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) struct watchdog_device *wdd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) char c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) if (len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) * Note: just in case someone wrote the magic character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) * five months ago...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) clear_bit(_WDOG_ALLOW_RELEASE, &wd_data->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) /* scan to see whether or not we got the magic character */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) for (i = 0; i != len; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) if (get_user(c, data + i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) if (c == 'V')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) set_bit(_WDOG_ALLOW_RELEASE, &wd_data->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) /* someone wrote to us, so we send the watchdog a keepalive ping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) mutex_lock(&wd_data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) wdd = wd_data->wdd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) if (wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) err = watchdog_ping(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) mutex_unlock(&wd_data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) * watchdog_ioctl: handle the different ioctl's for the watchdog device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) * @file: file handle to the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) * @cmd: watchdog command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) * @arg: argument pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) * The watchdog API defines a common set of functions for all watchdogs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) * according to their available features.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) static long watchdog_ioctl(struct file *file, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) struct watchdog_core_data *wd_data = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) void __user *argp = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) struct watchdog_device *wdd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) int __user *p = argp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) unsigned int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) mutex_lock(&wd_data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) wdd = wd_data->wdd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) if (!wdd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) goto out_ioctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) err = watchdog_ioctl_op(wdd, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) if (err != -ENOIOCTLCMD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) goto out_ioctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) case WDIOC_GETSUPPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) err = copy_to_user(argp, wdd->info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) sizeof(struct watchdog_info)) ? -EFAULT : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) case WDIOC_GETSTATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) val = watchdog_get_status(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) err = put_user(val, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) case WDIOC_GETBOOTSTATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) err = put_user(wdd->bootstatus, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) case WDIOC_SETOPTIONS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) if (get_user(val, p)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) if (val & WDIOS_DISABLECARD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) err = watchdog_stop(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) if (val & WDIOS_ENABLECARD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) err = watchdog_start(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) case WDIOC_KEEPALIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) if (!(wdd->info->options & WDIOF_KEEPALIVEPING)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) err = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) err = watchdog_ping(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) case WDIOC_SETTIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) if (get_user(val, p)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) err = watchdog_set_timeout(wdd, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) /* If the watchdog is active then we send a keepalive ping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) * to make sure that the watchdog keep's running (and if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) * possible that it takes the new timeout) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) err = watchdog_ping(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) case WDIOC_GETTIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) /* timeout == 0 means that we don't know the timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) if (wdd->timeout == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) err = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) err = put_user(wdd->timeout, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) case WDIOC_GETTIMELEFT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) err = watchdog_get_timeleft(wdd, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) err = put_user(val, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) case WDIOC_SETPRETIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) if (get_user(val, p)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) err = watchdog_set_pretimeout(wdd, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) case WDIOC_GETPRETIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) err = put_user(wdd->pretimeout, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) err = -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) out_ioctl:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) mutex_unlock(&wd_data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) * watchdog_open: open the /dev/watchdog* devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) * @inode: inode of device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) * @file: file handle to device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) * When the /dev/watchdog* device gets opened, we start the watchdog.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) * Watch out: the /dev/watchdog device is single open, so we make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) * it can only be opened once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) static int watchdog_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) struct watchdog_core_data *wd_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) struct watchdog_device *wdd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) bool hw_running;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) /* Get the corresponding watchdog device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) if (imajor(inode) == MISC_MAJOR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) wd_data = old_wd_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) wd_data = container_of(inode->i_cdev, struct watchdog_core_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) /* the watchdog is single open! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) if (test_and_set_bit(_WDOG_DEV_OPEN, &wd_data->status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) wdd = wd_data->wdd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) * If the /dev/watchdog device is open, we don't want the module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) * to be unloaded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) hw_running = watchdog_hw_running(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) if (!hw_running && !try_module_get(wdd->ops->owner)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) goto out_clear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) err = watchdog_start(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) goto out_mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) file->private_data = wd_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) if (!hw_running)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) get_device(&wd_data->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) * open_timeout only applies for the first open from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) * userspace. Set open_deadline to infinity so that the kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) * will take care of an always-running hardware watchdog in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) * case the device gets magic-closed or WDIOS_DISABLECARD is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) * applied.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) wd_data->open_deadline = KTIME_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) return stream_open(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) out_mod:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) module_put(wd_data->wdd->ops->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) out_clear:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) static void watchdog_core_data_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) struct watchdog_core_data *wd_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) wd_data = container_of(dev, struct watchdog_core_data, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) kfree(wd_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) * watchdog_release: release the watchdog device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) * @inode: inode of device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) * @file: file handle to device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) * This is the code for when /dev/watchdog gets closed. We will only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) * stop the watchdog when we have received the magic char (and nowayout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) * was not set), else the watchdog will keep running.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) static int watchdog_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) struct watchdog_core_data *wd_data = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) struct watchdog_device *wdd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) int err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) bool running;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) mutex_lock(&wd_data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) wdd = wd_data->wdd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) if (!wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) * We only stop the watchdog if we received the magic character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) * watchdog_stop will fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) if (!watchdog_active(wdd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) else if (test_and_clear_bit(_WDOG_ALLOW_RELEASE, &wd_data->status) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) !(wdd->info->options & WDIOF_MAGICCLOSE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) err = watchdog_stop(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) /* If the watchdog was not stopped, send a keepalive ping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) pr_crit("watchdog%d: watchdog did not stop!\n", wdd->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) watchdog_ping(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) watchdog_update_worker(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) /* make sure that /dev/watchdog can be re-opened */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) running = wdd && watchdog_hw_running(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) mutex_unlock(&wd_data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) * Allow the owner module to be unloaded again unless the watchdog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) * is still running. If the watchdog is still running, it can not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) * be stopped, and its driver must not be unloaded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) if (!running) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) module_put(wd_data->cdev.owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) put_device(&wd_data->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) static const struct file_operations watchdog_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) .write = watchdog_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) .unlocked_ioctl = watchdog_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) .compat_ioctl = compat_ptr_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) .open = watchdog_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) .release = watchdog_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) static struct miscdevice watchdog_miscdev = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) .minor = WATCHDOG_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) .name = "watchdog",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) .fops = &watchdog_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) static struct class watchdog_class = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) .name = "watchdog",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) .dev_groups = wdt_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) * watchdog_cdev_register: register watchdog character device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) * @wdd: watchdog device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) * Register a watchdog character device including handling the legacy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) * thus we set it up like that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) static int watchdog_cdev_register(struct watchdog_device *wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) struct watchdog_core_data *wd_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) wd_data = kzalloc(sizeof(struct watchdog_core_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) if (!wd_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) mutex_init(&wd_data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) wd_data->wdd = wdd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) wdd->wd_data = wd_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) if (IS_ERR_OR_NULL(watchdog_kworker)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) kfree(wd_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) device_initialize(&wd_data->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) wd_data->dev.devt = MKDEV(MAJOR(watchdog_devt), wdd->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) wd_data->dev.class = &watchdog_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) wd_data->dev.parent = wdd->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) wd_data->dev.groups = wdd->groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) wd_data->dev.release = watchdog_core_data_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) dev_set_drvdata(&wd_data->dev, wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) dev_set_name(&wd_data->dev, "watchdog%d", wdd->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) kthread_init_work(&wd_data->work, watchdog_ping_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) hrtimer_init(&wd_data->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) wd_data->timer.function = watchdog_timer_expired;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) if (wdd->id == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) old_wd_data = wd_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) watchdog_miscdev.parent = wdd->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) err = misc_register(&watchdog_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) if (err != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) wdd->info->identity, WATCHDOG_MINOR, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) if (err == -EBUSY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) pr_err("%s: a legacy watchdog module is probably present.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) wdd->info->identity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) old_wd_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) put_device(&wd_data->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) /* Fill in the data structures */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) cdev_init(&wd_data->cdev, &watchdog_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) /* Add the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) err = cdev_device_add(&wd_data->cdev, &wd_data->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) pr_err("watchdog%d unable to add device %d:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) wdd->id, MAJOR(watchdog_devt), wdd->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) if (wdd->id == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) misc_deregister(&watchdog_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) old_wd_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) put_device(&wd_data->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) wd_data->cdev.owner = wdd->ops->owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) /* Record time of most recent heartbeat as 'just before now'. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) wd_data->last_hw_keepalive = ktime_sub(ktime_get(), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) watchdog_set_open_deadline(wd_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) * If the watchdog is running, prevent its driver from being unloaded,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) * and schedule an immediate ping.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) if (watchdog_hw_running(wdd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) __module_get(wdd->ops->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) get_device(&wd_data->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) if (handle_boot_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) hrtimer_start(&wd_data->timer, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) HRTIMER_MODE_REL_HARD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) pr_info("watchdog%d running and kernel based pre-userspace handler disabled\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) wdd->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) * watchdog_cdev_unregister: unregister watchdog character device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) * @watchdog: watchdog device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) * Unregister watchdog character device and if needed the legacy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) * /dev/watchdog device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) static void watchdog_cdev_unregister(struct watchdog_device *wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) struct watchdog_core_data *wd_data = wdd->wd_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) cdev_device_del(&wd_data->cdev, &wd_data->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) if (wdd->id == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) misc_deregister(&watchdog_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) old_wd_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) if (watchdog_active(wdd) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) test_bit(WDOG_STOP_ON_UNREGISTER, &wdd->status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) watchdog_stop(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) mutex_lock(&wd_data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) wd_data->wdd = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) wdd->wd_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) mutex_unlock(&wd_data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) hrtimer_cancel(&wd_data->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) kthread_cancel_work_sync(&wd_data->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) put_device(&wd_data->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) * watchdog_dev_register: register a watchdog device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) * @wdd: watchdog device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) * Register a watchdog device including handling the legacy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) * thus we set it up like that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) int watchdog_dev_register(struct watchdog_device *wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) ret = watchdog_cdev_register(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) ret = watchdog_register_pretimeout(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) watchdog_cdev_unregister(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) * watchdog_dev_unregister: unregister a watchdog device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) * @watchdog: watchdog device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) * Unregister watchdog device and if needed the legacy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) * /dev/watchdog device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) void watchdog_dev_unregister(struct watchdog_device *wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) watchdog_unregister_pretimeout(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) watchdog_cdev_unregister(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) * watchdog_set_last_hw_keepalive: set last HW keepalive time for watchdog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) * @wdd: watchdog device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) * @last_ping_ms: time since last HW heartbeat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) * Adjusts the last known HW keepalive time for a watchdog timer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) * This is needed if the watchdog is already running when the probe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) * function is called, and it can't be pinged immediately. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) * function must be called immediately after watchdog registration,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) * and min_hw_heartbeat_ms must be set for this to be useful.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) int watchdog_set_last_hw_keepalive(struct watchdog_device *wdd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) unsigned int last_ping_ms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) struct watchdog_core_data *wd_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) ktime_t now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) if (!wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) wd_data = wdd->wd_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) now = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) wd_data->last_hw_keepalive = ktime_sub(now, ms_to_ktime(last_ping_ms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) if (watchdog_hw_running(wdd) && handle_boot_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) return __watchdog_ping(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) EXPORT_SYMBOL_GPL(watchdog_set_last_hw_keepalive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) * watchdog_dev_init: init dev part of watchdog core
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) * Allocate a range of chardev nodes to use for watchdog devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) int __init watchdog_dev_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) watchdog_kworker = kthread_create_worker(0, "watchdogd");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) if (IS_ERR(watchdog_kworker)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) pr_err("Failed to create watchdog kworker\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) return PTR_ERR(watchdog_kworker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) sched_set_fifo(watchdog_kworker->task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) err = class_register(&watchdog_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) pr_err("couldn't register class\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) goto err_register;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) pr_err("watchdog: unable to allocate char dev region\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) goto err_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) err_alloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) class_unregister(&watchdog_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) err_register:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) kthread_destroy_worker(watchdog_kworker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) * watchdog_dev_exit: exit dev part of watchdog core
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) * Release the range of chardev nodes used for watchdog devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) void __exit watchdog_dev_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) unregister_chrdev_region(watchdog_devt, MAX_DOGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) class_unregister(&watchdog_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) kthread_destroy_worker(watchdog_kworker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) module_param(handle_boot_enabled, bool, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) MODULE_PARM_DESC(handle_boot_enabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) "Watchdog core auto-updates boot enabled watchdogs before userspace takes over (default="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) __MODULE_STRING(IS_ENABLED(CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED)) ")");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) module_param(open_timeout, uint, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) MODULE_PARM_DESC(open_timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) "Maximum time (in seconds, 0 means infinity) for userspace to take over a running watchdog (default="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) __MODULE_STRING(CONFIG_WATCHDOG_OPEN_TIMEOUT) ")");