Merge tag 'vfio-ccw-20190717-2' of https://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / drivers / watchdog / softdog.c
CommitLineData
d0173278 1// SPDX-License-Identifier: GPL-2.0+
1da177e4 2/*
a5132caf 3 * SoftDog: A Software Watchdog Device
1da177e4 4 *
143a2e54
WVS
5 * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
6 * All Rights Reserved.
1da177e4 7 *
1da177e4
LT
8 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
9 * warranty for any of this software. This material is provided
10 * "AS-IS" and at no charge.
11 *
12 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
13 *
14 * Software only watchdog driver. Unlike its big brother the WDT501P
15 * driver this won't always recover a failed machine.
1da177e4
LT
16 */
17
27c766aa
JP
18#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
8d5755b3 20#include <linux/hrtimer.h>
e65c5825 21#include <linux/init.h>
e65c5825 22#include <linux/kernel.h>
1da177e4
LT
23#include <linux/module.h>
24#include <linux/moduleparam.h>
e65c5825 25#include <linux/reboot.h>
e65c5825 26#include <linux/types.h>
1da177e4 27#include <linux/watchdog.h>
1da177e4 28
1da177e4 29#define TIMER_MARGIN 60 /* Default is 60 seconds */
a5132caf
AC
30static unsigned int soft_margin = TIMER_MARGIN; /* in seconds */
31module_param(soft_margin, uint, 0);
f92d3749
AC
32MODULE_PARM_DESC(soft_margin,
33 "Watchdog soft_margin in seconds. (0 < soft_margin < 65536, default="
34 __MODULE_STRING(TIMER_MARGIN) ")");
1da177e4 35
86a1e189
WVS
36static bool nowayout = WATCHDOG_NOWAYOUT;
37module_param(nowayout, bool, 0);
f92d3749
AC
38MODULE_PARM_DESC(nowayout,
39 "Watchdog cannot be stopped once started (default="
40 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
1da177e4 41
5f5e1909 42static int soft_noboot;
1da177e4 43module_param(soft_noboot, int, 0);
a77dba7e 44MODULE_PARM_DESC(soft_noboot,
a5132caf 45 "Softdog action, set to 1 to ignore reboots, 0 to reboot (default=0)");
1da177e4 46
7fff4beb
AJ
47static int soft_panic;
48module_param(soft_panic, int, 0);
49MODULE_PARM_DESC(soft_panic,
50 "Softdog action, set to 1 to panic, 0 to reboot (default=0)");
51
8d5755b3
NC
52static struct hrtimer softdog_ticktock;
53static struct hrtimer softdog_preticktock;
54
55static enum hrtimer_restart softdog_fire(struct hrtimer *timer)
1da177e4 56{
5889f06b 57 module_put(THIS_MODULE);
4a23e2bf 58 if (soft_noboot) {
27c766aa 59 pr_crit("Triggered - Reboot ignored\n");
4a23e2bf 60 } else if (soft_panic) {
27c766aa
JP
61 pr_crit("Initiating panic\n");
62 panic("Software Watchdog Timer expired");
7fff4beb 63 } else {
27c766aa 64 pr_crit("Initiating system reboot\n");
479d0f41 65 emergency_restart();
27c766aa 66 pr_crit("Reboot didn't ?????\n");
1da177e4 67 }
1da177e4 68
8d5755b3
NC
69 return HRTIMER_NORESTART;
70}
44ba0f04 71
2accf320
WS
72static struct watchdog_device softdog_dev;
73
8d5755b3 74static enum hrtimer_restart softdog_pretimeout(struct hrtimer *timer)
2accf320
WS
75{
76 watchdog_notify_pretimeout(&softdog_dev);
2accf320 77
8d5755b3
NC
78 return HRTIMER_NORESTART;
79}
2accf320 80
a5132caf 81static int softdog_ping(struct watchdog_device *w)
1da177e4 82{
8d5755b3 83 if (!hrtimer_active(&softdog_ticktock))
5889f06b 84 __module_get(THIS_MODULE);
8d5755b3
NC
85 hrtimer_start(&softdog_ticktock, ktime_set(w->timeout, 0),
86 HRTIMER_MODE_REL);
2accf320 87
4cbc6902
WS
88 if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT)) {
89 if (w->pretimeout)
8d5755b3
NC
90 hrtimer_start(&softdog_preticktock,
91 ktime_set(w->timeout - w->pretimeout, 0),
92 HRTIMER_MODE_REL);
4cbc6902 93 else
8d5755b3 94 hrtimer_cancel(&softdog_preticktock);
4cbc6902 95 }
2accf320 96
1da177e4
LT
97 return 0;
98}
99
a5132caf 100static int softdog_stop(struct watchdog_device *w)
1da177e4 101{
8d5755b3 102 if (hrtimer_cancel(&softdog_ticktock))
5889f06b
LR
103 module_put(THIS_MODULE);
104
4cbc6902 105 if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT))
8d5755b3 106 hrtimer_cancel(&softdog_preticktock);
2accf320 107
1da177e4
LT
108 return 0;
109}
110
a5132caf
AC
111static struct watchdog_info softdog_info = {
112 .identity = "Software Watchdog",
4cbc6902 113 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
1da177e4
LT
114};
115
85f15cfc 116static const struct watchdog_ops softdog_ops = {
a5132caf
AC
117 .owner = THIS_MODULE,
118 .start = softdog_ping,
119 .stop = softdog_stop,
a5132caf
AC
120};
121
122static struct watchdog_device softdog_dev = {
123 .info = &softdog_info,
124 .ops = &softdog_ops,
125 .min_timeout = 1,
e8cf96ab
WS
126 .max_timeout = 65535,
127 .timeout = TIMER_MARGIN,
1da177e4
LT
128};
129
0efc70b8 130static int __init softdog_init(void)
1da177e4
LT
131{
132 int ret;
133
e8cf96ab 134 watchdog_init_timeout(&softdog_dev, soft_margin, NULL);
a5132caf 135 watchdog_set_nowayout(&softdog_dev, nowayout);
84ebcc17 136 watchdog_stop_on_reboot(&softdog_dev);
1da177e4 137
8d5755b3
NC
138 hrtimer_init(&softdog_ticktock, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
139 softdog_ticktock.function = softdog_fire;
140
141 if (IS_ENABLED(CONFIG_SOFT_WATCHDOG_PRETIMEOUT)) {
4cbc6902 142 softdog_info.options |= WDIOF_PRETIMEOUT;
8d5755b3
NC
143 hrtimer_init(&softdog_preticktock, CLOCK_MONOTONIC,
144 HRTIMER_MODE_REL);
145 softdog_preticktock.function = softdog_pretimeout;
146 }
4cbc6902 147
a5132caf 148 ret = watchdog_register_device(&softdog_dev);
84ebcc17 149 if (ret)
1da177e4 150 return ret;
1da177e4 151
e8cf96ab
WS
152 pr_info("initialized. soft_noboot=%d soft_margin=%d sec soft_panic=%d (nowayout=%d)\n",
153 soft_noboot, softdog_dev.timeout, soft_panic, nowayout);
1da177e4
LT
154
155 return 0;
156}
0efc70b8 157module_init(softdog_init);
1da177e4 158
0efc70b8 159static void __exit softdog_exit(void)
1da177e4 160{
a5132caf 161 watchdog_unregister_device(&softdog_dev);
1da177e4 162}
0efc70b8 163module_exit(softdog_exit);
1da177e4
LT
164
165MODULE_AUTHOR("Alan Cox");
166MODULE_DESCRIPTION("Software Watchdog Device Driver");
167MODULE_LICENSE("GPL");