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