watchdog_core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. * @timeout_parm: timeout module parameter
  90. * @dev: Device that stores the timeout-sec property
  91. *
  92. * Initialize the timeout field of the watchdog_device struct with either the
  93. * timeout module parameter (if it is valid value) or the timeout-sec property
  94. * (only if it is a valid value and the timeout_parm is out of bounds).
  95. * If none of them are valid then we keep the old value (which should normally
  96. * be the default timeout value).
  97. *
  98. * A zero is returned on success and -EINVAL for failure.
  99. */
  100. int watchdog_init_timeout(struct watchdog_device *wdd,
  101. unsigned int timeout_parm, struct device *dev)
  102. {
  103. unsigned int t = 0;
  104. int ret = 0;
  105. watchdog_check_min_max_timeout(wdd);
  106. /* try to get the timeout module parameter first */
  107. if (!watchdog_timeout_invalid(wdd, timeout_parm) && timeout_parm) {
  108. wdd->timeout = timeout_parm;
  109. return ret;
  110. }
  111. if (timeout_parm)
  112. ret = -EINVAL;
  113. /* try to get the timeout_sec property */
  114. if (dev == NULL || dev->of_node == NULL)
  115. return ret;
  116. of_property_read_u32(dev->of_node, "timeout-sec", &t);
  117. if (!watchdog_timeout_invalid(wdd, t) && t)
  118. wdd->timeout = t;
  119. else
  120. ret = -EINVAL;
  121. return ret;
  122. }
  123. EXPORT_SYMBOL_GPL(watchdog_init_timeout);
  124. static int watchdog_reboot_notifier(struct notifier_block *nb,
  125. unsigned long code, void *data)
  126. {
  127. struct watchdog_device *wdd = container_of(nb, struct watchdog_device,
  128. reboot_nb);
  129. if (code == SYS_DOWN || code == SYS_HALT) {
  130. if (watchdog_active(wdd)) {
  131. int ret;
  132. ret = wdd->ops->stop(wdd);
  133. if (ret)
  134. return NOTIFY_BAD;
  135. }
  136. }
  137. return NOTIFY_DONE;
  138. }
  139. static int watchdog_restart_notifier(struct notifier_block *nb,
  140. unsigned long action, void *data)
  141. {
  142. struct watchdog_device *wdd = container_of(nb, struct watchdog_device,
  143. restart_nb);
  144. int ret;
  145. ret = wdd->ops->restart(wdd, action, data);
  146. if (ret)
  147. return NOTIFY_BAD;
  148. return NOTIFY_DONE;
  149. }
  150. /**
  151. * watchdog_set_restart_priority - Change priority of restart handler
  152. * @wdd: watchdog device
  153. * @priority: priority of the restart handler, should follow these guidelines:
  154. * 0: use watchdog's restart function as last resort, has limited restart
  155. * capabilies
  156. * 128: default restart handler, use if no other handler is expected to be
  157. * available and/or if restart is sufficient to restart the entire system
  158. * 255: preempt all other handlers
  159. *
  160. * If a wdd->ops->restart function is provided when watchdog_register_device is
  161. * called, it will be registered as a restart handler with the priority given
  162. * here.
  163. */
  164. void watchdog_set_restart_priority(struct watchdog_device *wdd, int priority)
  165. {
  166. wdd->restart_nb.priority = priority;
  167. }
  168. EXPORT_SYMBOL_GPL(watchdog_set_restart_priority);
  169. static int __watchdog_register_device(struct watchdog_device *wdd)
  170. {
  171. int ret, id = -1;
  172. if (wdd == NULL || wdd->info == NULL || wdd->ops == NULL)
  173. return -EINVAL;
  174. /* Mandatory operations need to be supported */
  175. if (!wdd->ops->start || (!wdd->ops->stop && !wdd->max_hw_heartbeat_ms))
  176. return -EINVAL;
  177. watchdog_check_min_max_timeout(wdd);
  178. /*
  179. * Note: now that all watchdog_device data has been verified, we
  180. * will not check this anymore in other functions. If data gets
  181. * corrupted in a later stage then we expect a kernel panic!
  182. */
  183. /* Use alias for watchdog id if possible */
  184. if (wdd->parent) {
  185. ret = of_alias_get_id(wdd->parent->of_node, "watchdog");
  186. if (ret >= 0)
  187. id = ida_simple_get(&watchdog_ida, ret,
  188. ret + 1, GFP_KERNEL);
  189. }
  190. if (id < 0)
  191. id = ida_simple_get(&watchdog_ida, 0, MAX_DOGS, GFP_KERNEL);
  192. if (id < 0)
  193. return id;
  194. wdd->id = id;
  195. ret = watchdog_dev_register(wdd);
  196. if (ret) {
  197. ida_simple_remove(&watchdog_ida, id);
  198. if (!(id == 0 && ret == -EBUSY))
  199. return ret;
  200. /* Retry in case a legacy watchdog module exists */
  201. id = ida_simple_get(&watchdog_ida, 1, MAX_DOGS, GFP_KERNEL);
  202. if (id < 0)
  203. return id;
  204. wdd->id = id;
  205. ret = watchdog_dev_register(wdd);
  206. if (ret) {
  207. ida_simple_remove(&watchdog_ida, id);
  208. return ret;
  209. }
  210. }
  211. if (test_bit(WDOG_STOP_ON_REBOOT, &wdd->status)) {
  212. wdd->reboot_nb.notifier_call = watchdog_reboot_notifier;
  213. ret = register_reboot_notifier(&wdd->reboot_nb);
  214. if (ret) {
  215. pr_err("watchdog%d: Cannot register reboot notifier (%d)\n",
  216. wdd->id, ret);
  217. watchdog_dev_unregister(wdd);
  218. ida_simple_remove(&watchdog_ida, wdd->id);
  219. return ret;
  220. }
  221. }
  222. if (wdd->ops->restart) {
  223. wdd->restart_nb.notifier_call = watchdog_restart_notifier;
  224. ret = register_restart_handler(&wdd->restart_nb);
  225. if (ret)
  226. pr_warn("watchdog%d: Cannot register restart handler (%d)\n",
  227. wdd->id, ret);
  228. }
  229. return 0;
  230. }
  231. /**
  232. * watchdog_register_device() - register a watchdog device
  233. * @wdd: watchdog device
  234. *
  235. * Register a watchdog device with the kernel so that the
  236. * watchdog timer can be accessed from userspace.
  237. *
  238. * A zero is returned on success and a negative errno code for
  239. * failure.
  240. */
  241. int watchdog_register_device(struct watchdog_device *wdd)
  242. {
  243. int ret;
  244. mutex_lock(&wtd_deferred_reg_mutex);
  245. if (wtd_deferred_reg_done)
  246. ret = __watchdog_register_device(wdd);
  247. else
  248. ret = watchdog_deferred_registration_add(wdd);
  249. mutex_unlock(&wtd_deferred_reg_mutex);
  250. return ret;
  251. }
  252. EXPORT_SYMBOL_GPL(watchdog_register_device);
  253. static void __watchdog_unregister_device(struct watchdog_device *wdd)
  254. {
  255. if (wdd == NULL)
  256. return;
  257. if (wdd->ops->restart)
  258. unregister_restart_handler(&wdd->restart_nb);
  259. if (test_bit(WDOG_STOP_ON_REBOOT, &wdd->status))
  260. unregister_reboot_notifier(&wdd->reboot_nb);
  261. watchdog_dev_unregister(wdd);
  262. ida_simple_remove(&watchdog_ida, wdd->id);
  263. }
  264. /**
  265. * watchdog_unregister_device() - unregister a watchdog device
  266. * @wdd: watchdog device to unregister
  267. *
  268. * Unregister a watchdog device that was previously successfully
  269. * registered with watchdog_register_device().
  270. */
  271. void watchdog_unregister_device(struct watchdog_device *wdd)
  272. {
  273. mutex_lock(&wtd_deferred_reg_mutex);
  274. if (wtd_deferred_reg_done)
  275. __watchdog_unregister_device(wdd);
  276. else
  277. watchdog_deferred_registration_del(wdd);
  278. mutex_unlock(&wtd_deferred_reg_mutex);
  279. }
  280. EXPORT_SYMBOL_GPL(watchdog_unregister_device);
  281. static void devm_watchdog_unregister_device(struct device *dev, void *res)
  282. {
  283. watchdog_unregister_device(*(struct watchdog_device **)res);
  284. }
  285. /**
  286. * devm_watchdog_register_device() - resource managed watchdog_register_device()
  287. * @dev: device that is registering this watchdog device
  288. * @wdd: watchdog device
  289. *
  290. * Managed watchdog_register_device(). For watchdog device registered by this
  291. * function, watchdog_unregister_device() is automatically called on driver
  292. * detach. See watchdog_register_device() for more information.
  293. */
  294. int devm_watchdog_register_device(struct device *dev,
  295. struct watchdog_device *wdd)
  296. {
  297. struct watchdog_device **rcwdd;
  298. int ret;
  299. rcwdd = devres_alloc(devm_watchdog_unregister_device, sizeof(*rcwdd),
  300. GFP_KERNEL);
  301. if (!rcwdd)
  302. return -ENOMEM;
  303. ret = watchdog_register_device(wdd);
  304. if (!ret) {
  305. *rcwdd = wdd;
  306. devres_add(dev, rcwdd);
  307. } else {
  308. devres_free(rcwdd);
  309. }
  310. return ret;
  311. }
  312. EXPORT_SYMBOL_GPL(devm_watchdog_register_device);
  313. static int __init watchdog_deferred_registration(void)
  314. {
  315. mutex_lock(&wtd_deferred_reg_mutex);
  316. wtd_deferred_reg_done = true;
  317. while (!list_empty(&wtd_deferred_reg_list)) {
  318. struct watchdog_device *wdd;
  319. wdd = list_first_entry(&wtd_deferred_reg_list,
  320. struct watchdog_device, deferred);
  321. list_del(&wdd->deferred);
  322. __watchdog_register_device(wdd);
  323. }
  324. mutex_unlock(&wtd_deferred_reg_mutex);
  325. return 0;
  326. }
  327. static int __init watchdog_init(void)
  328. {
  329. int err;
  330. err = watchdog_dev_init();
  331. if (err < 0)
  332. return err;
  333. watchdog_deferred_registration();
  334. return 0;
  335. }
  336. static void __exit watchdog_exit(void)
  337. {
  338. watchdog_dev_exit();
  339. ida_destroy(&watchdog_ida);
  340. }
  341. subsys_initcall_sync(watchdog_init);
  342. module_exit(watchdog_exit);
  343. MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>");
  344. MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
  345. MODULE_DESCRIPTION("WatchDog Timer Driver Core");
  346. MODULE_LICENSE("GPL");