2afa4e566b09612b468ba135639f8ca73b95e14c
[librecmc/librecmc.git] / target / linux / ipq806x / patches-4.4 / 009-2-watchdog-core-add-reboot-notifier-support.patch
1 From e131319669e0ef5e6fcd75174daeffa40492135c Mon Sep 17 00:00:00 2001
2 From: Damien Riegel <damien.riegel@savoirfairelinux.com>
3 Date: Fri, 20 Nov 2015 16:54:51 -0500
4 Subject: watchdog: core: add reboot notifier support
5
6 Many watchdog drivers register a reboot notifier in order to stop the
7 watchdog on system reboot. Thus we can factorize this code in the
8 watchdog core.
9
10 For that purpose, a new notifier block is added in watchdog_device for
11 internal use only, as well as a new watchdog_stop_on_reboot helper
12 function.
13
14 If this helper is called, watchdog core registers the related notifier
15 block and will stop the watchdog when SYS_HALT or SYS_DOWN is received.
16
17 Since this operation can be critical on some platforms, abort the device
18 registration if the reboot notifier registration fails.
19
20 Suggested-by: Vivien Didelot <vivien.didelot@savoirfairelinux.com>
21 Signed-off-by: Damien Riegel <damien.riegel@savoirfairelinux.com>
22 Reviewed-by: Vivien Didelot <vivien.didelot@savoirfairelinux.com>
23 Signed-off-by: Guenter Roeck <linux@roeck-us.net>
24 Signed-off-by: Wim Van Sebroeck <wim@iguana.be>
25 ---
26  Documentation/watchdog/watchdog-kernel-api.txt |  8 ++++++
27  drivers/watchdog/watchdog_core.c               | 37 ++++++++++++++++++++++++++
28  include/linux/watchdog.h                       |  9 +++++++
29  3 files changed, 54 insertions(+)
30
31 --- a/Documentation/watchdog/watchdog-kernel-api.txt
32 +++ b/Documentation/watchdog/watchdog-kernel-api.txt
33 @@ -53,6 +53,7 @@ struct watchdog_device {
34         unsigned int timeout;
35         unsigned int min_timeout;
36         unsigned int max_timeout;
37 +       struct notifier_block reboot_nb;
38         struct notifier_block restart_nb;
39         void *driver_data;
40         struct mutex lock;
41 @@ -76,6 +77,9 @@ It contains following fields:
42  * timeout: the watchdog timer's timeout value (in seconds).
43  * min_timeout: the watchdog timer's minimum timeout value (in seconds).
44  * max_timeout: the watchdog timer's maximum timeout value (in seconds).
45 +* reboot_nb: notifier block that is registered for reboot notifications, for
46 +  internal use only. If the driver calls watchdog_stop_on_reboot, watchdog core
47 +  will stop the watchdog on such notifications.
48  * restart_nb: notifier block that is registered for machine restart, for
49    internal use only. If a watchdog is capable of restarting the machine, it
50    should define ops->restart. Priority can be changed through
51 @@ -240,6 +244,10 @@ to set the default timeout value as time
52  then use this function to set the user "preferred" timeout value.
53  This routine returns zero on success and a negative errno code for failure.
54  
55 +To disable the watchdog on reboot, the user must call the following helper:
56 +
57 +static inline void watchdog_stop_on_reboot(struct watchdog_device *wdd);
58 +
59  To change the priority of the restart handler the following helper should be
60  used:
61  
62 --- a/drivers/watchdog/watchdog_core.c
63 +++ b/drivers/watchdog/watchdog_core.c
64 @@ -138,6 +138,25 @@ int watchdog_init_timeout(struct watchdo
65  }
66  EXPORT_SYMBOL_GPL(watchdog_init_timeout);
67  
68 +static int watchdog_reboot_notifier(struct notifier_block *nb,
69 +                                   unsigned long code, void *data)
70 +{
71 +       struct watchdog_device *wdd = container_of(nb, struct watchdog_device,
72 +                                                  reboot_nb);
73 +
74 +       if (code == SYS_DOWN || code == SYS_HALT) {
75 +               if (watchdog_active(wdd)) {
76 +                       int ret;
77 +
78 +                       ret = wdd->ops->stop(wdd);
79 +                       if (ret)
80 +                               return NOTIFY_BAD;
81 +               }
82 +       }
83 +
84 +       return NOTIFY_DONE;
85 +}
86 +
87  static int watchdog_restart_notifier(struct notifier_block *nb,
88                                      unsigned long action, void *data)
89  {
90 @@ -238,6 +257,21 @@ static int __watchdog_register_device(st
91                 return ret;
92         }
93  
94 +       if (test_bit(WDOG_STOP_ON_REBOOT, &wdd->status)) {
95 +               wdd->reboot_nb.notifier_call = watchdog_reboot_notifier;
96 +
97 +               ret = register_reboot_notifier(&wdd->reboot_nb);
98 +               if (ret) {
99 +                       dev_err(wdd->dev, "Cannot register reboot notifier (%d)\n",
100 +                               ret);
101 +                       watchdog_dev_unregister(wdd);
102 +                       device_destroy(watchdog_class, devno);
103 +                       ida_simple_remove(&watchdog_ida, wdd->id);
104 +                       wdd->dev = NULL;
105 +                       return ret;
106 +               }
107 +       }
108 +
109         if (wdd->ops->restart) {
110                 wdd->restart_nb.notifier_call = watchdog_restart_notifier;
111  
112 @@ -286,6 +320,9 @@ static void __watchdog_unregister_device
113         if (wdd->ops->restart)
114                 unregister_restart_handler(&wdd->restart_nb);
115  
116 +       if (test_bit(WDOG_STOP_ON_REBOOT, &wdd->status))
117 +               unregister_reboot_notifier(&wdd->reboot_nb);
118 +
119         devno = wdd->cdev.dev;
120         ret = watchdog_dev_unregister(wdd);
121         if (ret)
122 --- a/include/linux/watchdog.h
123 +++ b/include/linux/watchdog.h
124 @@ -65,6 +65,7 @@ struct watchdog_ops {
125   * @timeout:   The watchdog devices timeout value (in seconds).
126   * @min_timeout:The watchdog devices minimum timeout value (in seconds).
127   * @max_timeout:The watchdog devices maximum timeout value (in seconds).
128 + * @reboot_nb: The notifier block to stop watchdog on reboot.
129   * @restart_nb:        The notifier block to register a restart function.
130   * @driver-data:Pointer to the drivers private data.
131   * @lock:      Lock for watchdog core internal use only.
132 @@ -92,6 +93,7 @@ struct watchdog_device {
133         unsigned int timeout;
134         unsigned int min_timeout;
135         unsigned int max_timeout;
136 +       struct notifier_block reboot_nb;
137         struct notifier_block restart_nb;
138         void *driver_data;
139         struct mutex lock;
140 @@ -102,6 +104,7 @@ struct watchdog_device {
141  #define WDOG_ALLOW_RELEASE     2       /* Did we receive the magic char ? */
142  #define WDOG_NO_WAY_OUT                3       /* Is 'nowayout' feature set ? */
143  #define WDOG_UNREGISTERED      4       /* Has the device been unregistered */
144 +#define WDOG_STOP_ON_REBOOT    5       /* Should be stopped on reboot */
145         struct list_head deferred;
146  };
147  
148 @@ -121,6 +124,12 @@ static inline void watchdog_set_nowayout
149                 set_bit(WDOG_NO_WAY_OUT, &wdd->status);
150  }
151  
152 +/* Use the following function to stop the watchdog on reboot */
153 +static inline void watchdog_stop_on_reboot(struct watchdog_device *wdd)
154 +{
155 +       set_bit(WDOG_STOP_ON_REBOOT, &wdd->status);
156 +}
157 +
158  /* Use the following function to check if a timeout value is invalid */
159  static inline bool watchdog_timeout_invalid(struct watchdog_device *wdd, unsigned int t)
160  {