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