watchdog_core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29. #include <linux/module.h> /* For EXPORT_SYMBOL/module stuff/... */
  30. #include <linux/types.h> /* For standard types */
  31. #include <linux/errno.h> /* For the -ENODEV/... values */
  32. #include <linux/kernel.h> /* For printk/panic/... */
  33. #include <linux/reboot.h> /* For restart handler */
  34. #include <linux/watchdog.h> /* For watchdog specific items */
  35. #include <linux/init.h> /* For __init/__exit/... */
  36. #include <linux/idr.h> /* For ida_* macros */
  37. #include <linux/err.h> /* For IS_ERR macros */
  38. #include <linux/of.h> /* For of_get_timeout_sec */
  39. #include "watchdog_core.h" /* For watchdog_dev_register/... */
  40. static DEFINE_IDA(watchdog_ida);
  41. /*
  42. * Deferred Registration infrastructure.
  43. *
  44. * Sometimes watchdog drivers needs to be loaded as soon as possible,
  45. * for example when it's impossible to disable it. To do so,
  46. * raising the initcall level of the watchdog driver is a solution.
  47. * But in such case, the miscdev is maybe not ready (subsys_initcall), and
  48. * watchdog_core need miscdev to register the watchdog as a char device.
  49. *
  50. * The deferred registration infrastructure offer a way for the watchdog
  51. * subsystem to register a watchdog properly, even before miscdev is ready.
  52. */
  53. static DEFINE_MUTEX(wtd_deferred_reg_mutex);
  54. static LIST_HEAD(wtd_deferred_reg_list);
  55. static bool wtd_deferred_reg_done;
  56. static int watchdog_deferred_registration_add(struct watchdog_device *wdd)
  57. {
  58. list_add_tail(&wdd->deferred,
  59. &wtd_deferred_reg_list);
  60. return 0;
  61. }
  62. static void watchdog_deferred_registration_del(struct watchdog_device *wdd)
  63. {
  64. struct list_head *p, *n;
  65. struct watchdog_device *wdd_tmp;
  66. list_for_each_safe(p, n, &wtd_deferred_reg_list) {
  67. wdd_tmp = list_entry(p, struct watchdog_device,
  68. deferred);
  69. if (wdd_tmp == wdd) {
  70. list_del(&wdd_tmp->deferred);
  71. break;
  72. }
  73. }
  74. }
  75. static void watchdog_check_min_max_timeout(struct watchdog_device *wdd)
  76. {
  77. /*
  78. * Check that we have valid min and max timeout values, if
  79. * not reset them both to 0 (=not used or unknown)
  80. */
  81. if (!wdd->max_hw_heartbeat_ms && wdd->min_timeout > wdd->max_timeout) {
  82. pr_info("Invalid min and max timeout values, resetting to 0!\n");
  83. wdd->min_timeout = 0;
  84. wdd->max_timeout = 0;
  85. }
  86. }
  87. /**
  88. * watchdog_init_timeout() - initialize the timeout field
  89. * @wdd: watchdog device
  90. * @timeout_parm: timeout module parameter
  91. * @dev: Device that stores the timeout-sec property
  92. *
  93. * Initialize the timeout field of the watchdog_device struct with either the
  94. * timeout module parameter (if it is valid value) or the timeout-sec property
  95. * (only if it is a valid value and the timeout_parm is out of bounds).
  96. * If none of them are valid then we keep the old value (which should normally
  97. * be the default timeout value).
  98. *
  99. * A zero is returned on success and -EINVAL for failure.
  100. */
  101. int watchdog_init_timeout(struct watchdog_device *wdd,
  102. unsigned int timeout_parm, struct device *dev)
  103. {
  104. unsigned int t = 0;
  105. int ret = 0;
  106. watchdog_check_min_max_timeout(wdd);
  107. /* try to get the timeout module parameter first */
  108. if (!watchdog_timeout_invalid(wdd, timeout_parm) && timeout_parm) {
  109. wdd->timeout = timeout_parm;
  110. return ret;
  111. }
  112. if (timeout_parm)
  113. ret = -EINVAL;
  114. /* try to get the timeout_sec property */
  115. if (dev == NULL || dev->of_node == NULL)
  116. return ret;
  117. of_property_read_u32(dev->of_node, "timeout-sec", &t);
  118. if (!watchdog_timeout_invalid(wdd, t) && t)
  119. wdd->timeout = t;
  120. else
  121. ret = -EINVAL;
  122. return ret;
  123. }
  124. EXPORT_SYMBOL_GPL(watchdog_init_timeout);
  125. static int watchdog_reboot_notifier(struct notifier_block *nb,
  126. unsigned long code, void *data)
  127. {
  128. struct watchdog_device *wdd;
  129. wdd = container_of(nb, struct watchdog_device, reboot_nb);
  130. if (code == SYS_DOWN || code == SYS_HALT) {
  131. if (watchdog_active(wdd)) {
  132. int ret;
  133. ret = wdd->ops->stop(wdd);
  134. if (ret)
  135. return NOTIFY_BAD;
  136. }
  137. }
  138. return NOTIFY_DONE;
  139. }
  140. static int watchdog_restart_notifier(struct notifier_block *nb,
  141. unsigned long action, void *data)
  142. {
  143. struct watchdog_device *wdd = container_of(nb, struct watchdog_device,
  144. restart_nb);
  145. int ret;
  146. ret = wdd->ops->restart(wdd, action, data);
  147. if (ret)
  148. return NOTIFY_BAD;
  149. return NOTIFY_DONE;
  150. }
  151. /**
  152. * watchdog_set_restart_priority - Change priority of restart handler
  153. * @wdd: watchdog device
  154. * @priority: priority of the restart handler, should follow these guidelines:
  155. * 0: use watchdog's restart function as last resort, has limited restart
  156. * capabilies
  157. * 128: default restart handler, use if no other handler is expected to be
  158. * available and/or if restart is sufficient to restart the entire system
  159. * 255: preempt all other handlers
  160. *
  161. * If a wdd->ops->restart function is provided when watchdog_register_device is
  162. * called, it will be registered as a restart handler with the priority given
  163. * here.
  164. */
  165. void watchdog_set_restart_priority(struct watchdog_device *wdd, int priority)
  166. {
  167. wdd->restart_nb.priority = priority;
  168. }
  169. EXPORT_SYMBOL_GPL(watchdog_set_restart_priority);
  170. static int __watchdog_register_device(struct watchdog_device *wdd)
  171. {
  172. int ret, id = -1;
  173. if (wdd == NULL || wdd->info == NULL || wdd->ops == NULL)
  174. return -EINVAL;
  175. /* Mandatory operations need to be supported */
  176. if (!wdd->ops->start || (!wdd->ops->stop && !wdd->max_hw_heartbeat_ms))
  177. return -EINVAL;
  178. watchdog_check_min_max_timeout(wdd);
  179. /*
  180. * Note: now that all watchdog_device data has been verified, we
  181. * will not check this anymore in other functions. If data gets
  182. * corrupted in a later stage then we expect a kernel panic!
  183. */
  184. /* Use alias for watchdog id if possible */
  185. if (wdd->parent) {
  186. ret = of_alias_get_id(wdd->parent->of_node, "watchdog");
  187. if (ret >= 0)
  188. id = ida_simple_get(&watchdog_ida, ret,
  189. ret + 1, GFP_KERNEL);
  190. }
  191. if (id < 0)
  192. id = ida_simple_get(&watchdog_ida, 0, MAX_DOGS, GFP_KERNEL);
  193. if (id < 0)
  194. return id;
  195. wdd->id = id;
  196. ret = watchdog_dev_register(wdd);
  197. if (ret) {
  198. ida_simple_remove(&watchdog_ida, id);
  199. if (!(id == 0 && ret == -EBUSY))
  200. return ret;
  201. /* Retry in case a legacy watchdog module exists */
  202. id = ida_simple_get(&watchdog_ida, 1, MAX_DOGS, GFP_KERNEL);
  203. if (id < 0)
  204. return id;
  205. wdd->id = id;
  206. ret = watchdog_dev_register(wdd);
  207. if (ret) {
  208. ida_simple_remove(&watchdog_ida, id);
  209. return ret;
  210. }
  211. }
  212. if (test_bit(WDOG_STOP_ON_REBOOT, &wdd->status)) {
  213. if (!wdd->ops->stop)
  214. pr_warn("watchdog%d: stop_on_reboot not supported\n", wdd->id);
  215. else {
  216. wdd->reboot_nb.notifier_call = watchdog_reboot_notifier;
  217. ret = register_reboot_notifier(&wdd->reboot_nb);
  218. if (ret) {
  219. pr_err("watchdog%d: Cannot register reboot notifier (%d)\n",
  220. wdd->id, ret);
  221. watchdog_dev_unregister(wdd);
  222. ida_simple_remove(&watchdog_ida, id);
  223. return ret;
  224. }
  225. }
  226. }
  227. if (wdd->ops->restart) {
  228. wdd->restart_nb.notifier_call = watchdog_restart_notifier;
  229. ret = register_restart_handler(&wdd->restart_nb);
  230. if (ret)
  231. pr_warn("watchdog%d: Cannot register restart handler (%d)\n",
  232. wdd->id, ret);
  233. }
  234. return 0;
  235. }
  236. /**
  237. * watchdog_register_device() - register a watchdog device
  238. * @wdd: watchdog device
  239. *
  240. * Register a watchdog device with the kernel so that the
  241. * watchdog timer can be accessed from userspace.
  242. *
  243. * A zero is returned on success and a negative errno code for
  244. * failure.
  245. */
  246. int watchdog_register_device(struct watchdog_device *wdd)
  247. {
  248. int ret;
  249. mutex_lock(&wtd_deferred_reg_mutex);
  250. if (wtd_deferred_reg_done)
  251. ret = __watchdog_register_device(wdd);
  252. else
  253. ret = watchdog_deferred_registration_add(wdd);
  254. mutex_unlock(&wtd_deferred_reg_mutex);
  255. return ret;
  256. }
  257. EXPORT_SYMBOL_GPL(watchdog_register_device);
  258. static void __watchdog_unregister_device(struct watchdog_device *wdd)
  259. {
  260. if (wdd == NULL)
  261. return;
  262. if (wdd->ops->restart)
  263. unregister_restart_handler(&wdd->restart_nb);
  264. if (test_bit(WDOG_STOP_ON_REBOOT, &wdd->status))
  265. unregister_reboot_notifier(&wdd->reboot_nb);
  266. watchdog_dev_unregister(wdd);
  267. ida_simple_remove(&watchdog_ida, wdd->id);
  268. }
  269. /**
  270. * watchdog_unregister_device() - unregister a watchdog device
  271. * @wdd: watchdog device to unregister
  272. *
  273. * Unregister a watchdog device that was previously successfully
  274. * registered with watchdog_register_device().
  275. */
  276. void watchdog_unregister_device(struct watchdog_device *wdd)
  277. {
  278. mutex_lock(&wtd_deferred_reg_mutex);
  279. if (wtd_deferred_reg_done)
  280. __watchdog_unregister_device(wdd);
  281. else
  282. watchdog_deferred_registration_del(wdd);
  283. mutex_unlock(&wtd_deferred_reg_mutex);
  284. }
  285. EXPORT_SYMBOL_GPL(watchdog_unregister_device);
  286. static void devm_watchdog_unregister_device(struct device *dev, void *res)
  287. {
  288. watchdog_unregister_device(*(struct watchdog_device **)res);
  289. }
  290. /**
  291. * devm_watchdog_register_device() - resource managed watchdog_register_device()
  292. * @dev: device that is registering this watchdog device
  293. * @wdd: watchdog device
  294. *
  295. * Managed watchdog_register_device(). For watchdog device registered by this
  296. * function, watchdog_unregister_device() is automatically called on driver
  297. * detach. See watchdog_register_device() for more information.
  298. */
  299. int devm_watchdog_register_device(struct device *dev,
  300. struct watchdog_device *wdd)
  301. {
  302. struct watchdog_device **rcwdd;
  303. int ret;
  304. rcwdd = devres_alloc(devm_watchdog_unregister_device, sizeof(*rcwdd),
  305. GFP_KERNEL);
  306. if (!rcwdd)
  307. return -ENOMEM;
  308. ret = watchdog_register_device(wdd);
  309. if (!ret) {
  310. *rcwdd = wdd;
  311. devres_add(dev, rcwdd);
  312. } else {
  313. devres_free(rcwdd);
  314. }
  315. return ret;
  316. }
  317. EXPORT_SYMBOL_GPL(devm_watchdog_register_device);
  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. wdd = list_first_entry(&wtd_deferred_reg_list,
  325. struct watchdog_device, deferred);
  326. list_del(&wdd->deferred);
  327. __watchdog_register_device(wdd);
  328. }
  329. mutex_unlock(&wtd_deferred_reg_mutex);
  330. return 0;
  331. }
  332. static int __init watchdog_init(void)
  333. {
  334. int err;
  335. err = watchdog_dev_init();
  336. if (err < 0)
  337. return err;
  338. watchdog_deferred_registration();
  339. return 0;
  340. }
  341. static void __exit watchdog_exit(void)
  342. {
  343. watchdog_dev_exit();
  344. ida_destroy(&watchdog_ida);
  345. }
  346. subsys_initcall_sync(watchdog_init);
  347. module_exit(watchdog_exit);
  348. MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>");
  349. MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
  350. MODULE_DESCRIPTION("WatchDog Timer Driver Core");
  351. MODULE_LICENSE("GPL");