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