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