0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0026
0027 #include <linux/module.h> /* For EXPORT_SYMBOL/module stuff/... */
0028 #include <linux/types.h> /* For standard types */
0029 #include <linux/errno.h> /* For the -ENODEV/... values */
0030 #include <linux/kernel.h> /* For printk/panic/... */
0031 #include <linux/reboot.h> /* For restart handler */
0032 #include <linux/watchdog.h> /* For watchdog specific items */
0033 #include <linux/init.h> /* For __init/__exit/... */
0034 #include <linux/idr.h> /* For ida_* macros */
0035 #include <linux/err.h> /* For IS_ERR macros */
0036 #include <linux/of.h> /* For of_get_timeout_sec */
0037 #include <linux/suspend.h>
0038
0039 #include "watchdog_core.h" /* For watchdog_dev_register/... */
0040
0041 static DEFINE_IDA(watchdog_ida);
0042
0043 static int stop_on_reboot = -1;
0044 module_param(stop_on_reboot, int, 0444);
0045 MODULE_PARM_DESC(stop_on_reboot, "Stop watchdogs on reboot (0=keep watching, 1=stop)");
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060 static DEFINE_MUTEX(wtd_deferred_reg_mutex);
0061 static LIST_HEAD(wtd_deferred_reg_list);
0062 static bool wtd_deferred_reg_done;
0063
0064 static void watchdog_deferred_registration_add(struct watchdog_device *wdd)
0065 {
0066 list_add_tail(&wdd->deferred,
0067 &wtd_deferred_reg_list);
0068 }
0069
0070 static void watchdog_deferred_registration_del(struct watchdog_device *wdd)
0071 {
0072 struct list_head *p, *n;
0073 struct watchdog_device *wdd_tmp;
0074
0075 list_for_each_safe(p, n, &wtd_deferred_reg_list) {
0076 wdd_tmp = list_entry(p, struct watchdog_device,
0077 deferred);
0078 if (wdd_tmp == wdd) {
0079 list_del(&wdd_tmp->deferred);
0080 break;
0081 }
0082 }
0083 }
0084
0085 static void watchdog_check_min_max_timeout(struct watchdog_device *wdd)
0086 {
0087
0088
0089
0090
0091 if (!wdd->max_hw_heartbeat_ms && wdd->min_timeout > wdd->max_timeout) {
0092 pr_info("Invalid min and max timeout values, resetting to 0!\n");
0093 wdd->min_timeout = 0;
0094 wdd->max_timeout = 0;
0095 }
0096 }
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115 int watchdog_init_timeout(struct watchdog_device *wdd,
0116 unsigned int timeout_parm, struct device *dev)
0117 {
0118 const char *dev_str = wdd->parent ? dev_name(wdd->parent) :
0119 (const char *)wdd->info->identity;
0120 unsigned int t = 0;
0121 int ret = 0;
0122
0123 watchdog_check_min_max_timeout(wdd);
0124
0125
0126 if (timeout_parm) {
0127 if (!watchdog_timeout_invalid(wdd, timeout_parm)) {
0128 wdd->timeout = timeout_parm;
0129 return 0;
0130 }
0131 pr_err("%s: driver supplied timeout (%u) out of range\n",
0132 dev_str, timeout_parm);
0133 ret = -EINVAL;
0134 }
0135
0136
0137 if (dev && dev->of_node &&
0138 of_property_read_u32(dev->of_node, "timeout-sec", &t) == 0) {
0139 if (t && !watchdog_timeout_invalid(wdd, t)) {
0140 wdd->timeout = t;
0141 return 0;
0142 }
0143 pr_err("%s: DT supplied timeout (%u) out of range\n", dev_str, t);
0144 ret = -EINVAL;
0145 }
0146
0147 if (ret < 0 && wdd->timeout)
0148 pr_warn("%s: falling back to default timeout (%u)\n", dev_str,
0149 wdd->timeout);
0150
0151 return ret;
0152 }
0153 EXPORT_SYMBOL_GPL(watchdog_init_timeout);
0154
0155 static int watchdog_reboot_notifier(struct notifier_block *nb,
0156 unsigned long code, void *data)
0157 {
0158 struct watchdog_device *wdd;
0159
0160 wdd = container_of(nb, struct watchdog_device, reboot_nb);
0161 if (code == SYS_DOWN || code == SYS_HALT) {
0162 if (watchdog_active(wdd) || watchdog_hw_running(wdd)) {
0163 int ret;
0164
0165 ret = wdd->ops->stop(wdd);
0166 if (ret)
0167 return NOTIFY_BAD;
0168 }
0169 }
0170
0171 return NOTIFY_DONE;
0172 }
0173
0174 static int watchdog_restart_notifier(struct notifier_block *nb,
0175 unsigned long action, void *data)
0176 {
0177 struct watchdog_device *wdd = container_of(nb, struct watchdog_device,
0178 restart_nb);
0179
0180 int ret;
0181
0182 ret = wdd->ops->restart(wdd, action, data);
0183 if (ret)
0184 return NOTIFY_BAD;
0185
0186 return NOTIFY_DONE;
0187 }
0188
0189 static int watchdog_pm_notifier(struct notifier_block *nb, unsigned long mode,
0190 void *data)
0191 {
0192 struct watchdog_device *wdd;
0193 int ret = 0;
0194
0195 wdd = container_of(nb, struct watchdog_device, pm_nb);
0196
0197 switch (mode) {
0198 case PM_HIBERNATION_PREPARE:
0199 case PM_RESTORE_PREPARE:
0200 case PM_SUSPEND_PREPARE:
0201 ret = watchdog_dev_suspend(wdd);
0202 break;
0203 case PM_POST_HIBERNATION:
0204 case PM_POST_RESTORE:
0205 case PM_POST_SUSPEND:
0206 ret = watchdog_dev_resume(wdd);
0207 break;
0208 }
0209
0210 if (ret)
0211 return NOTIFY_BAD;
0212
0213 return NOTIFY_DONE;
0214 }
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230 void watchdog_set_restart_priority(struct watchdog_device *wdd, int priority)
0231 {
0232 wdd->restart_nb.priority = priority;
0233 }
0234 EXPORT_SYMBOL_GPL(watchdog_set_restart_priority);
0235
0236 static int __watchdog_register_device(struct watchdog_device *wdd)
0237 {
0238 int ret, id = -1;
0239
0240 if (wdd == NULL || wdd->info == NULL || wdd->ops == NULL)
0241 return -EINVAL;
0242
0243
0244 if (!wdd->ops->start || (!wdd->ops->stop && !wdd->max_hw_heartbeat_ms))
0245 return -EINVAL;
0246
0247 watchdog_check_min_max_timeout(wdd);
0248
0249
0250
0251
0252
0253
0254
0255
0256 if (wdd->parent) {
0257 ret = of_alias_get_id(wdd->parent->of_node, "watchdog");
0258 if (ret >= 0)
0259 id = ida_simple_get(&watchdog_ida, ret,
0260 ret + 1, GFP_KERNEL);
0261 }
0262
0263 if (id < 0)
0264 id = ida_simple_get(&watchdog_ida, 0, MAX_DOGS, GFP_KERNEL);
0265
0266 if (id < 0)
0267 return id;
0268 wdd->id = id;
0269
0270 ret = watchdog_dev_register(wdd);
0271 if (ret) {
0272 ida_simple_remove(&watchdog_ida, id);
0273 if (!(id == 0 && ret == -EBUSY))
0274 return ret;
0275
0276
0277 id = ida_simple_get(&watchdog_ida, 1, MAX_DOGS, GFP_KERNEL);
0278 if (id < 0)
0279 return id;
0280 wdd->id = id;
0281
0282 ret = watchdog_dev_register(wdd);
0283 if (ret) {
0284 ida_simple_remove(&watchdog_ida, id);
0285 return ret;
0286 }
0287 }
0288
0289
0290 if (stop_on_reboot != -1) {
0291 if (stop_on_reboot)
0292 set_bit(WDOG_STOP_ON_REBOOT, &wdd->status);
0293 else
0294 clear_bit(WDOG_STOP_ON_REBOOT, &wdd->status);
0295 }
0296
0297 if (test_bit(WDOG_STOP_ON_REBOOT, &wdd->status)) {
0298 if (!wdd->ops->stop)
0299 pr_warn("watchdog%d: stop_on_reboot not supported\n", wdd->id);
0300 else {
0301 wdd->reboot_nb.notifier_call = watchdog_reboot_notifier;
0302
0303 ret = register_reboot_notifier(&wdd->reboot_nb);
0304 if (ret) {
0305 pr_err("watchdog%d: Cannot register reboot notifier (%d)\n",
0306 wdd->id, ret);
0307 watchdog_dev_unregister(wdd);
0308 ida_simple_remove(&watchdog_ida, id);
0309 return ret;
0310 }
0311 }
0312 }
0313
0314 if (wdd->ops->restart) {
0315 wdd->restart_nb.notifier_call = watchdog_restart_notifier;
0316
0317 ret = register_restart_handler(&wdd->restart_nb);
0318 if (ret)
0319 pr_warn("watchdog%d: Cannot register restart handler (%d)\n",
0320 wdd->id, ret);
0321 }
0322
0323 if (test_bit(WDOG_NO_PING_ON_SUSPEND, &wdd->status)) {
0324 wdd->pm_nb.notifier_call = watchdog_pm_notifier;
0325
0326 ret = register_pm_notifier(&wdd->pm_nb);
0327 if (ret)
0328 pr_warn("watchdog%d: Cannot register pm handler (%d)\n",
0329 wdd->id, ret);
0330 }
0331
0332 return 0;
0333 }
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345
0346 int watchdog_register_device(struct watchdog_device *wdd)
0347 {
0348 const char *dev_str;
0349 int ret = 0;
0350
0351 mutex_lock(&wtd_deferred_reg_mutex);
0352 if (wtd_deferred_reg_done)
0353 ret = __watchdog_register_device(wdd);
0354 else
0355 watchdog_deferred_registration_add(wdd);
0356 mutex_unlock(&wtd_deferred_reg_mutex);
0357
0358 if (ret) {
0359 dev_str = wdd->parent ? dev_name(wdd->parent) :
0360 (const char *)wdd->info->identity;
0361 pr_err("%s: failed to register watchdog device (err = %d)\n",
0362 dev_str, ret);
0363 }
0364
0365 return ret;
0366 }
0367 EXPORT_SYMBOL_GPL(watchdog_register_device);
0368
0369 static void __watchdog_unregister_device(struct watchdog_device *wdd)
0370 {
0371 if (wdd == NULL)
0372 return;
0373
0374 if (wdd->ops->restart)
0375 unregister_restart_handler(&wdd->restart_nb);
0376
0377 if (test_bit(WDOG_STOP_ON_REBOOT, &wdd->status))
0378 unregister_reboot_notifier(&wdd->reboot_nb);
0379
0380 watchdog_dev_unregister(wdd);
0381 ida_simple_remove(&watchdog_ida, wdd->id);
0382 }
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392 void watchdog_unregister_device(struct watchdog_device *wdd)
0393 {
0394 mutex_lock(&wtd_deferred_reg_mutex);
0395 if (wtd_deferred_reg_done)
0396 __watchdog_unregister_device(wdd);
0397 else
0398 watchdog_deferred_registration_del(wdd);
0399 mutex_unlock(&wtd_deferred_reg_mutex);
0400 }
0401
0402 EXPORT_SYMBOL_GPL(watchdog_unregister_device);
0403
0404 static void devm_watchdog_unregister_device(struct device *dev, void *res)
0405 {
0406 watchdog_unregister_device(*(struct watchdog_device **)res);
0407 }
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418 int devm_watchdog_register_device(struct device *dev,
0419 struct watchdog_device *wdd)
0420 {
0421 struct watchdog_device **rcwdd;
0422 int ret;
0423
0424 rcwdd = devres_alloc(devm_watchdog_unregister_device, sizeof(*rcwdd),
0425 GFP_KERNEL);
0426 if (!rcwdd)
0427 return -ENOMEM;
0428
0429 ret = watchdog_register_device(wdd);
0430 if (!ret) {
0431 *rcwdd = wdd;
0432 devres_add(dev, rcwdd);
0433 } else {
0434 devres_free(rcwdd);
0435 }
0436
0437 return ret;
0438 }
0439 EXPORT_SYMBOL_GPL(devm_watchdog_register_device);
0440
0441 static int __init watchdog_deferred_registration(void)
0442 {
0443 mutex_lock(&wtd_deferred_reg_mutex);
0444 wtd_deferred_reg_done = true;
0445 while (!list_empty(&wtd_deferred_reg_list)) {
0446 struct watchdog_device *wdd;
0447
0448 wdd = list_first_entry(&wtd_deferred_reg_list,
0449 struct watchdog_device, deferred);
0450 list_del(&wdd->deferred);
0451 __watchdog_register_device(wdd);
0452 }
0453 mutex_unlock(&wtd_deferred_reg_mutex);
0454 return 0;
0455 }
0456
0457 static int __init watchdog_init(void)
0458 {
0459 int err;
0460
0461 err = watchdog_dev_init();
0462 if (err < 0)
0463 return err;
0464
0465 watchdog_deferred_registration();
0466 return 0;
0467 }
0468
0469 static void __exit watchdog_exit(void)
0470 {
0471 watchdog_dev_exit();
0472 ida_destroy(&watchdog_ida);
0473 }
0474
0475 subsys_initcall_sync(watchdog_init);
0476 module_exit(watchdog_exit);
0477
0478 MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>");
0479 MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
0480 MODULE_DESCRIPTION("WatchDog Timer Driver Core");
0481 MODULE_LICENSE("GPL");