watchdog.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Generic watchdog defines. Derived from..
  3. *
  4. * Berkshire PC Watchdog Defines
  5. * by Ken Hollis <khollis@bitgate.com>
  6. *
  7. */
  8. #ifndef _LINUX_WATCHDOG_H
  9. #define _LINUX_WATCHDOG_H
  10. #include <linux/bitops.h>
  11. #include <linux/cdev.h>
  12. #include <linux/device.h>
  13. #include <linux/kernel.h>
  14. #include <linux/notifier.h>
  15. #include <uapi/linux/watchdog.h>
  16. struct watchdog_ops;
  17. struct watchdog_device;
  18. struct watchdog_core_data;
  19. struct watchdog_governor;
  20. /** struct watchdog_ops - The watchdog-devices operations
  21. *
  22. * @owner: The module owner.
  23. * @start: The routine for starting the watchdog device.
  24. * @stop: The routine for stopping the watchdog device.
  25. * @ping: The routine that sends a keepalive ping to the watchdog device.
  26. * @status: The routine that shows the status of the watchdog device.
  27. * @set_timeout:The routine for setting the watchdog devices timeout value (in seconds).
  28. * @set_pretimeout:The routine for setting the watchdog devices pretimeout.
  29. * @get_timeleft:The routine that gets the time left before a reset (in seconds).
  30. * @restart: The routine for restarting the machine.
  31. * @ioctl: The routines that handles extra ioctl calls.
  32. *
  33. * The watchdog_ops structure contains a list of low-level operations
  34. * that control a watchdog device. It also contains the module that owns
  35. * these operations. The start and stop function are mandatory, all other
  36. * functions are optional.
  37. */
  38. struct watchdog_ops {
  39. struct module *owner;
  40. /* mandatory operations */
  41. int (*start)(struct watchdog_device *);
  42. int (*stop)(struct watchdog_device *);
  43. /* optional operations */
  44. int (*ping)(struct watchdog_device *);
  45. unsigned int (*status)(struct watchdog_device *);
  46. int (*set_timeout)(struct watchdog_device *, unsigned int);
  47. int (*set_pretimeout)(struct watchdog_device *, unsigned int);
  48. unsigned int (*get_timeleft)(struct watchdog_device *);
  49. int (*restart)(struct watchdog_device *, unsigned long, void *);
  50. long (*ioctl)(struct watchdog_device *, unsigned int, unsigned long);
  51. };
  52. /** struct watchdog_device - The structure that defines a watchdog device
  53. *
  54. * @id: The watchdog's ID. (Allocated by watchdog_register_device)
  55. * @parent: The parent bus device
  56. * @groups: List of sysfs attribute groups to create when creating the
  57. * watchdog device.
  58. * @info: Pointer to a watchdog_info structure.
  59. * @ops: Pointer to the list of watchdog operations.
  60. * @gov: Pointer to watchdog pretimeout governor.
  61. * @bootstatus: Status of the watchdog device at boot.
  62. * @timeout: The watchdog devices timeout value (in seconds).
  63. * @pretimeout: The watchdog devices pre_timeout value.
  64. * @min_timeout:The watchdog devices minimum timeout value (in seconds).
  65. * @max_timeout:The watchdog devices maximum timeout value (in seconds)
  66. * as configurable from user space. Only relevant if
  67. * max_hw_heartbeat_ms is not provided.
  68. * @min_hw_heartbeat_ms:
  69. * Hardware limit for minimum time between heartbeats,
  70. * in milli-seconds.
  71. * @max_hw_heartbeat_ms:
  72. * Hardware limit for maximum timeout, in milli-seconds.
  73. * Replaces max_timeout if specified.
  74. * @reboot_nb: The notifier block to stop watchdog on reboot.
  75. * @restart_nb: The notifier block to register a restart function.
  76. * @driver_data:Pointer to the drivers private data.
  77. * @wd_data: Pointer to watchdog core internal data.
  78. * @status: Field that contains the devices internal status bits.
  79. * @deferred: Entry in wtd_deferred_reg_list which is used to
  80. * register early initialized watchdogs.
  81. *
  82. * The watchdog_device structure contains all information about a
  83. * watchdog timer device.
  84. *
  85. * The driver-data field may not be accessed directly. It must be accessed
  86. * via the watchdog_set_drvdata and watchdog_get_drvdata helpers.
  87. *
  88. * The lock field is for watchdog core internal use only and should not be
  89. * touched.
  90. */
  91. struct watchdog_device {
  92. int id;
  93. struct device *parent;
  94. const struct attribute_group **groups;
  95. const struct watchdog_info *info;
  96. const struct watchdog_ops *ops;
  97. const struct watchdog_governor *gov;
  98. unsigned int bootstatus;
  99. unsigned int timeout;
  100. unsigned int pretimeout;
  101. unsigned int min_timeout;
  102. unsigned int max_timeout;
  103. unsigned int min_hw_heartbeat_ms;
  104. unsigned int max_hw_heartbeat_ms;
  105. struct notifier_block reboot_nb;
  106. struct notifier_block restart_nb;
  107. void *driver_data;
  108. struct watchdog_core_data *wd_data;
  109. unsigned long status;
  110. /* Bit numbers for status flags */
  111. #define WDOG_ACTIVE 0 /* Is the watchdog running/active */
  112. #define WDOG_NO_WAY_OUT 1 /* Is 'nowayout' feature set ? */
  113. #define WDOG_STOP_ON_REBOOT 2 /* Should be stopped on reboot */
  114. #define WDOG_HW_RUNNING 3 /* True if HW watchdog running */
  115. struct list_head deferred;
  116. };
  117. #define WATCHDOG_NOWAYOUT IS_BUILTIN(CONFIG_WATCHDOG_NOWAYOUT)
  118. #define WATCHDOG_NOWAYOUT_INIT_STATUS (WATCHDOG_NOWAYOUT << WDOG_NO_WAY_OUT)
  119. /* Use the following function to check whether or not the watchdog is active */
  120. static inline bool watchdog_active(struct watchdog_device *wdd)
  121. {
  122. return test_bit(WDOG_ACTIVE, &wdd->status);
  123. }
  124. /*
  125. * Use the following function to check whether or not the hardware watchdog
  126. * is running
  127. */
  128. static inline bool watchdog_hw_running(struct watchdog_device *wdd)
  129. {
  130. return test_bit(WDOG_HW_RUNNING, &wdd->status);
  131. }
  132. /* Use the following function to set the nowayout feature */
  133. static inline void watchdog_set_nowayout(struct watchdog_device *wdd, bool nowayout)
  134. {
  135. if (nowayout)
  136. set_bit(WDOG_NO_WAY_OUT, &wdd->status);
  137. }
  138. /* Use the following function to stop the watchdog on reboot */
  139. static inline void watchdog_stop_on_reboot(struct watchdog_device *wdd)
  140. {
  141. set_bit(WDOG_STOP_ON_REBOOT, &wdd->status);
  142. }
  143. /* Use the following function to check if a timeout value is invalid */
  144. static inline bool watchdog_timeout_invalid(struct watchdog_device *wdd, unsigned int t)
  145. {
  146. /*
  147. * The timeout is invalid if
  148. * - the requested value is larger than UINT_MAX / 1000
  149. * (since internal calculations are done in milli-seconds),
  150. * or
  151. * - the requested value is smaller than the configured minimum timeout,
  152. * or
  153. * - a maximum hardware timeout is not configured, a maximum timeout
  154. * is configured, and the requested value is larger than the
  155. * configured maximum timeout.
  156. */
  157. return t > UINT_MAX / 1000 || t < wdd->min_timeout ||
  158. (!wdd->max_hw_heartbeat_ms && wdd->max_timeout &&
  159. t > wdd->max_timeout);
  160. }
  161. /* Use the following function to check if a pretimeout value is invalid */
  162. static inline bool watchdog_pretimeout_invalid(struct watchdog_device *wdd,
  163. unsigned int t)
  164. {
  165. return t && wdd->timeout && t >= wdd->timeout;
  166. }
  167. /* Use the following functions to manipulate watchdog driver specific data */
  168. static inline void watchdog_set_drvdata(struct watchdog_device *wdd, void *data)
  169. {
  170. wdd->driver_data = data;
  171. }
  172. static inline void *watchdog_get_drvdata(struct watchdog_device *wdd)
  173. {
  174. return wdd->driver_data;
  175. }
  176. /* Use the following functions to report watchdog pretimeout event */
  177. #if IS_ENABLED(CONFIG_WATCHDOG_PRETIMEOUT_GOV)
  178. void watchdog_notify_pretimeout(struct watchdog_device *wdd);
  179. #else
  180. static inline void watchdog_notify_pretimeout(struct watchdog_device *wdd)
  181. {
  182. pr_alert("watchdog%d: pretimeout event\n", wdd->id);
  183. }
  184. #endif
  185. /* drivers/watchdog/watchdog_core.c */
  186. void watchdog_set_restart_priority(struct watchdog_device *wdd, int priority);
  187. extern int watchdog_init_timeout(struct watchdog_device *wdd,
  188. unsigned int timeout_parm, struct device *dev);
  189. extern int watchdog_register_device(struct watchdog_device *);
  190. extern void watchdog_unregister_device(struct watchdog_device *);
  191. /* devres register variant */
  192. int devm_watchdog_register_device(struct device *dev, struct watchdog_device *);
  193. #endif /* ifndef _LINUX_WATCHDOG_H */