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