Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * watchdog_core.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 | * This source code is part of the generic code that can be used |
| 10 | * by all the watchdog timer drivers. |
| 11 | * |
| 12 | * Based on source code of the following authors: |
| 13 | * Matt Domsch <Matt_Domsch@dell.com>, |
| 14 | * Rob Radez <rob@osinvestor.com>, |
| 15 | * Rusty Lynch <rusty@linux.co.intel.com> |
| 16 | * Satyam Sharma <satyam@infradead.org> |
| 17 | * Randy Dunlap <randy.dunlap@oracle.com> |
| 18 | * |
| 19 | * This program is free software; you can redistribute it and/or |
| 20 | * modify it under the terms of the GNU General Public License |
| 21 | * as published by the Free Software Foundation; either version |
| 22 | * 2 of the License, or (at your option) any later version. |
| 23 | * |
| 24 | * Neither Alan Cox, CymruNet Ltd., Wim Van Sebroeck nor Iguana vzw. |
| 25 | * admit liability nor provide warranty for any of this software. |
| 26 | * This material is provided "AS-IS" and at no charge. |
| 27 | */ |
| 28 | |
| 29 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 30 | |
| 31 | #include <linux/module.h> /* For EXPORT_SYMBOL/module stuff/... */ |
| 32 | #include <linux/types.h> /* For standard types */ |
| 33 | #include <linux/errno.h> /* For the -ENODEV/... values */ |
| 34 | #include <linux/kernel.h> /* For printk/panic/... */ |
Damien Riegel | 2165bf5 | 2015-11-16 12:27:59 -0500 | [diff] [blame] | 35 | #include <linux/reboot.h> /* For restart handler */ |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 36 | #include <linux/watchdog.h> /* For watchdog specific items */ |
| 37 | #include <linux/init.h> /* For __init/__exit/... */ |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 38 | #include <linux/idr.h> /* For ida_* macros */ |
Alan Cox | d6b469d | 2012-05-11 12:00:20 +0200 | [diff] [blame] | 39 | #include <linux/err.h> /* For IS_ERR macros */ |
Fabio Porcedda | 3048253 | 2013-01-08 11:04:10 +0100 | [diff] [blame] | 40 | #include <linux/of.h> /* For of_get_timeout_sec */ |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 41 | |
Wim Van Sebroeck | 6cfb5aa | 2012-05-21 15:31:06 +0200 | [diff] [blame] | 42 | #include "watchdog_core.h" /* For watchdog_dev_register/... */ |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 43 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 44 | static DEFINE_IDA(watchdog_ida); |
Alan Cox | d6b469d | 2012-05-11 12:00:20 +0200 | [diff] [blame] | 45 | static struct class *watchdog_class; |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 46 | |
Jean-Baptiste Theou | ef90174 | 2015-06-09 09:55:02 -0700 | [diff] [blame] | 47 | /* |
| 48 | * Deferred Registration infrastructure. |
| 49 | * |
| 50 | * Sometimes watchdog drivers needs to be loaded as soon as possible, |
| 51 | * for example when it's impossible to disable it. To do so, |
| 52 | * raising the initcall level of the watchdog driver is a solution. |
| 53 | * But in such case, the miscdev is maybe not ready (subsys_initcall), and |
| 54 | * watchdog_core need miscdev to register the watchdog as a char device. |
| 55 | * |
| 56 | * The deferred registration infrastructure offer a way for the watchdog |
| 57 | * subsystem to register a watchdog properly, even before miscdev is ready. |
| 58 | */ |
| 59 | |
| 60 | static DEFINE_MUTEX(wtd_deferred_reg_mutex); |
| 61 | static LIST_HEAD(wtd_deferred_reg_list); |
| 62 | static bool wtd_deferred_reg_done; |
| 63 | |
| 64 | static int watchdog_deferred_registration_add(struct watchdog_device *wdd) |
| 65 | { |
| 66 | list_add_tail(&wdd->deferred, |
| 67 | &wtd_deferred_reg_list); |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static void watchdog_deferred_registration_del(struct watchdog_device *wdd) |
| 72 | { |
| 73 | struct list_head *p, *n; |
| 74 | struct watchdog_device *wdd_tmp; |
| 75 | |
| 76 | list_for_each_safe(p, n, &wtd_deferred_reg_list) { |
| 77 | wdd_tmp = list_entry(p, struct watchdog_device, |
| 78 | deferred); |
| 79 | if (wdd_tmp == wdd) { |
| 80 | list_del(&wdd_tmp->deferred); |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
Fabio Porcedda | 3048253 | 2013-01-08 11:04:10 +0100 | [diff] [blame] | 86 | static void watchdog_check_min_max_timeout(struct watchdog_device *wdd) |
| 87 | { |
| 88 | /* |
| 89 | * Check that we have valid min and max timeout values, if |
| 90 | * not reset them both to 0 (=not used or unknown) |
| 91 | */ |
| 92 | if (wdd->min_timeout > wdd->max_timeout) { |
| 93 | pr_info("Invalid min and max timeout values, resetting to 0!\n"); |
| 94 | wdd->min_timeout = 0; |
| 95 | wdd->max_timeout = 0; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * watchdog_init_timeout() - initialize the timeout field |
| 101 | * @timeout_parm: timeout module parameter |
| 102 | * @dev: Device that stores the timeout-sec property |
| 103 | * |
| 104 | * Initialize the timeout field of the watchdog_device struct with either the |
| 105 | * timeout module parameter (if it is valid value) or the timeout-sec property |
| 106 | * (only if it is a valid value and the timeout_parm is out of bounds). |
| 107 | * If none of them are valid then we keep the old value (which should normally |
| 108 | * be the default timeout value. |
| 109 | * |
| 110 | * A zero is returned on success and -EINVAL for failure. |
| 111 | */ |
| 112 | int watchdog_init_timeout(struct watchdog_device *wdd, |
| 113 | unsigned int timeout_parm, struct device *dev) |
| 114 | { |
| 115 | unsigned int t = 0; |
| 116 | int ret = 0; |
| 117 | |
| 118 | watchdog_check_min_max_timeout(wdd); |
| 119 | |
Sachin Kamat | 6ffcff9 | 2013-10-28 14:17:17 +0530 | [diff] [blame] | 120 | /* try to get the timeout module parameter first */ |
Doug Anderson | 2c34d59 | 2013-11-26 10:22:52 -0800 | [diff] [blame] | 121 | if (!watchdog_timeout_invalid(wdd, timeout_parm) && timeout_parm) { |
Fabio Porcedda | 3048253 | 2013-01-08 11:04:10 +0100 | [diff] [blame] | 122 | wdd->timeout = timeout_parm; |
| 123 | return ret; |
| 124 | } |
| 125 | if (timeout_parm) |
| 126 | ret = -EINVAL; |
| 127 | |
| 128 | /* try to get the timeout_sec property */ |
| 129 | if (dev == NULL || dev->of_node == NULL) |
| 130 | return ret; |
| 131 | of_property_read_u32(dev->of_node, "timeout-sec", &t); |
Doug Anderson | 2c34d59 | 2013-11-26 10:22:52 -0800 | [diff] [blame] | 132 | if (!watchdog_timeout_invalid(wdd, t) && t) |
Fabio Porcedda | 3048253 | 2013-01-08 11:04:10 +0100 | [diff] [blame] | 133 | wdd->timeout = t; |
| 134 | else |
| 135 | ret = -EINVAL; |
| 136 | |
| 137 | return ret; |
| 138 | } |
| 139 | EXPORT_SYMBOL_GPL(watchdog_init_timeout); |
| 140 | |
Damien Riegel | 2165bf5 | 2015-11-16 12:27:59 -0500 | [diff] [blame] | 141 | static int watchdog_restart_notifier(struct notifier_block *nb, |
| 142 | unsigned long action, void *data) |
| 143 | { |
| 144 | struct watchdog_device *wdd = container_of(nb, struct watchdog_device, |
| 145 | restart_nb); |
| 146 | |
| 147 | int ret; |
| 148 | |
| 149 | ret = wdd->ops->restart(wdd); |
| 150 | if (ret) |
| 151 | return NOTIFY_BAD; |
| 152 | |
| 153 | return NOTIFY_DONE; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * watchdog_set_restart_priority - Change priority of restart handler |
| 158 | * @wdd: watchdog device |
| 159 | * @priority: priority of the restart handler, should follow these guidelines: |
| 160 | * 0: use watchdog's restart function as last resort, has limited restart |
| 161 | * capabilies |
| 162 | * 128: default restart handler, use if no other handler is expected to be |
| 163 | * available and/or if restart is sufficient to restart the entire system |
| 164 | * 255: preempt all other handlers |
| 165 | * |
| 166 | * If a wdd->ops->restart function is provided when watchdog_register_device is |
| 167 | * called, it will be registered as a restart handler with the priority given |
| 168 | * here. |
| 169 | */ |
| 170 | void watchdog_set_restart_priority(struct watchdog_device *wdd, int priority) |
| 171 | { |
| 172 | wdd->restart_nb.priority = priority; |
| 173 | } |
| 174 | EXPORT_SYMBOL_GPL(watchdog_set_restart_priority); |
| 175 | |
Jean-Baptiste Theou | ef90174 | 2015-06-09 09:55:02 -0700 | [diff] [blame] | 176 | static int __watchdog_register_device(struct watchdog_device *wdd) |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 177 | { |
Justin Chen | 9dd4e17 | 2015-09-02 11:00:17 -0700 | [diff] [blame] | 178 | int ret, id = -1, devno; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 179 | |
| 180 | if (wdd == NULL || wdd->info == NULL || wdd->ops == NULL) |
| 181 | return -EINVAL; |
| 182 | |
| 183 | /* Mandatory operations need to be supported */ |
| 184 | if (wdd->ops->start == NULL || wdd->ops->stop == NULL) |
| 185 | return -EINVAL; |
| 186 | |
Fabio Porcedda | 3048253 | 2013-01-08 11:04:10 +0100 | [diff] [blame] | 187 | watchdog_check_min_max_timeout(wdd); |
Wim Van Sebroeck | 3f43f68 | 2011-07-22 19:00:16 +0000 | [diff] [blame] | 188 | |
| 189 | /* |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 190 | * Note: now that all watchdog_device data has been verified, we |
| 191 | * will not check this anymore in other functions. If data gets |
| 192 | * corrupted in a later stage then we expect a kernel panic! |
| 193 | */ |
| 194 | |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 195 | mutex_init(&wdd->lock); |
Justin Chen | 9dd4e17 | 2015-09-02 11:00:17 -0700 | [diff] [blame] | 196 | |
| 197 | /* Use alias for watchdog id if possible */ |
| 198 | if (wdd->parent) { |
| 199 | ret = of_alias_get_id(wdd->parent->of_node, "watchdog"); |
| 200 | if (ret >= 0) |
| 201 | id = ida_simple_get(&watchdog_ida, ret, |
| 202 | ret + 1, GFP_KERNEL); |
| 203 | } |
| 204 | |
| 205 | if (id < 0) |
| 206 | id = ida_simple_get(&watchdog_ida, 0, MAX_DOGS, GFP_KERNEL); |
| 207 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 208 | if (id < 0) |
| 209 | return id; |
| 210 | wdd->id = id; |
| 211 | |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 212 | ret = watchdog_dev_register(wdd); |
| 213 | if (ret) { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 214 | ida_simple_remove(&watchdog_ida, id); |
| 215 | if (!(id == 0 && ret == -EBUSY)) |
| 216 | return ret; |
| 217 | |
| 218 | /* Retry in case a legacy watchdog module exists */ |
| 219 | id = ida_simple_get(&watchdog_ida, 1, MAX_DOGS, GFP_KERNEL); |
| 220 | if (id < 0) |
| 221 | return id; |
| 222 | wdd->id = id; |
| 223 | |
| 224 | ret = watchdog_dev_register(wdd); |
| 225 | if (ret) { |
| 226 | ida_simple_remove(&watchdog_ida, id); |
| 227 | return ret; |
| 228 | } |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Alan Cox | d6b469d | 2012-05-11 12:00:20 +0200 | [diff] [blame] | 231 | devno = wdd->cdev.dev; |
| 232 | wdd->dev = device_create(watchdog_class, wdd->parent, devno, |
| 233 | NULL, "watchdog%d", wdd->id); |
| 234 | if (IS_ERR(wdd->dev)) { |
| 235 | watchdog_dev_unregister(wdd); |
| 236 | ida_simple_remove(&watchdog_ida, id); |
| 237 | ret = PTR_ERR(wdd->dev); |
| 238 | return ret; |
| 239 | } |
| 240 | |
Damien Riegel | 2165bf5 | 2015-11-16 12:27:59 -0500 | [diff] [blame] | 241 | if (wdd->ops->restart) { |
| 242 | wdd->restart_nb.notifier_call = watchdog_restart_notifier; |
| 243 | |
| 244 | ret = register_restart_handler(&wdd->restart_nb); |
| 245 | if (ret) |
| 246 | dev_warn(wdd->dev, "Cannot register restart handler (%d)\n", |
| 247 | ret); |
| 248 | } |
| 249 | |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 250 | return 0; |
| 251 | } |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 252 | |
| 253 | /** |
Jean-Baptiste Theou | ef90174 | 2015-06-09 09:55:02 -0700 | [diff] [blame] | 254 | * watchdog_register_device() - register a watchdog device |
| 255 | * @wdd: watchdog device |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 256 | * |
Jean-Baptiste Theou | ef90174 | 2015-06-09 09:55:02 -0700 | [diff] [blame] | 257 | * Register a watchdog device with the kernel so that the |
| 258 | * watchdog timer can be accessed from userspace. |
| 259 | * |
| 260 | * A zero is returned on success and a negative errno code for |
| 261 | * failure. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 262 | */ |
Jean-Baptiste Theou | ef90174 | 2015-06-09 09:55:02 -0700 | [diff] [blame] | 263 | |
| 264 | int watchdog_register_device(struct watchdog_device *wdd) |
| 265 | { |
| 266 | int ret; |
| 267 | |
| 268 | mutex_lock(&wtd_deferred_reg_mutex); |
| 269 | if (wtd_deferred_reg_done) |
| 270 | ret = __watchdog_register_device(wdd); |
| 271 | else |
| 272 | ret = watchdog_deferred_registration_add(wdd); |
| 273 | mutex_unlock(&wtd_deferred_reg_mutex); |
| 274 | return ret; |
| 275 | } |
| 276 | EXPORT_SYMBOL_GPL(watchdog_register_device); |
| 277 | |
| 278 | static void __watchdog_unregister_device(struct watchdog_device *wdd) |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 279 | { |
| 280 | int ret; |
Wei Yongjun | b232a70 | 2012-09-10 12:41:15 +0800 | [diff] [blame] | 281 | int devno; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 282 | |
| 283 | if (wdd == NULL) |
| 284 | return; |
| 285 | |
Damien Riegel | 2165bf5 | 2015-11-16 12:27:59 -0500 | [diff] [blame] | 286 | if (wdd->ops->restart) |
| 287 | unregister_restart_handler(&wdd->restart_nb); |
| 288 | |
Wei Yongjun | b232a70 | 2012-09-10 12:41:15 +0800 | [diff] [blame] | 289 | devno = wdd->cdev.dev; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 290 | ret = watchdog_dev_unregister(wdd); |
| 291 | if (ret) |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 292 | pr_err("error unregistering /dev/watchdog (err=%d)\n", ret); |
Alan Cox | d6b469d | 2012-05-11 12:00:20 +0200 | [diff] [blame] | 293 | device_destroy(watchdog_class, devno); |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 294 | ida_simple_remove(&watchdog_ida, wdd->id); |
Alan Cox | d6b469d | 2012-05-11 12:00:20 +0200 | [diff] [blame] | 295 | wdd->dev = NULL; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 296 | } |
Jean-Baptiste Theou | ef90174 | 2015-06-09 09:55:02 -0700 | [diff] [blame] | 297 | |
| 298 | /** |
| 299 | * watchdog_unregister_device() - unregister a watchdog device |
| 300 | * @wdd: watchdog device to unregister |
| 301 | * |
| 302 | * Unregister a watchdog device that was previously successfully |
| 303 | * registered with watchdog_register_device(). |
| 304 | */ |
| 305 | |
| 306 | void watchdog_unregister_device(struct watchdog_device *wdd) |
| 307 | { |
| 308 | mutex_lock(&wtd_deferred_reg_mutex); |
| 309 | if (wtd_deferred_reg_done) |
| 310 | __watchdog_unregister_device(wdd); |
| 311 | else |
| 312 | watchdog_deferred_registration_del(wdd); |
| 313 | mutex_unlock(&wtd_deferred_reg_mutex); |
| 314 | } |
| 315 | |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 316 | EXPORT_SYMBOL_GPL(watchdog_unregister_device); |
| 317 | |
Jean-Baptiste Theou | ef90174 | 2015-06-09 09:55:02 -0700 | [diff] [blame] | 318 | static int __init watchdog_deferred_registration(void) |
| 319 | { |
| 320 | mutex_lock(&wtd_deferred_reg_mutex); |
| 321 | wtd_deferred_reg_done = true; |
| 322 | while (!list_empty(&wtd_deferred_reg_list)) { |
| 323 | struct watchdog_device *wdd; |
| 324 | |
| 325 | wdd = list_first_entry(&wtd_deferred_reg_list, |
| 326 | struct watchdog_device, deferred); |
| 327 | list_del(&wdd->deferred); |
| 328 | __watchdog_register_device(wdd); |
| 329 | } |
| 330 | mutex_unlock(&wtd_deferred_reg_mutex); |
| 331 | return 0; |
| 332 | } |
| 333 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 334 | static int __init watchdog_init(void) |
| 335 | { |
Alan Cox | d6b469d | 2012-05-11 12:00:20 +0200 | [diff] [blame] | 336 | int err; |
| 337 | |
| 338 | watchdog_class = class_create(THIS_MODULE, "watchdog"); |
| 339 | if (IS_ERR(watchdog_class)) { |
| 340 | pr_err("couldn't create class\n"); |
| 341 | return PTR_ERR(watchdog_class); |
| 342 | } |
| 343 | |
| 344 | err = watchdog_dev_init(); |
| 345 | if (err < 0) { |
| 346 | class_destroy(watchdog_class); |
| 347 | return err; |
| 348 | } |
| 349 | |
Jean-Baptiste Theou | ef90174 | 2015-06-09 09:55:02 -0700 | [diff] [blame] | 350 | watchdog_deferred_registration(); |
Alan Cox | d6b469d | 2012-05-11 12:00:20 +0200 | [diff] [blame] | 351 | return 0; |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | static void __exit watchdog_exit(void) |
| 355 | { |
| 356 | watchdog_dev_exit(); |
Alan Cox | d6b469d | 2012-05-11 12:00:20 +0200 | [diff] [blame] | 357 | class_destroy(watchdog_class); |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 358 | ida_destroy(&watchdog_ida); |
| 359 | } |
| 360 | |
Jean-Baptiste Theou | ef90174 | 2015-06-09 09:55:02 -0700 | [diff] [blame] | 361 | subsys_initcall_sync(watchdog_init); |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 362 | module_exit(watchdog_exit); |
| 363 | |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 364 | MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>"); |
| 365 | MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>"); |
| 366 | MODULE_DESCRIPTION("WatchDog Timer Driver Core"); |
| 367 | MODULE_LICENSE("GPL"); |