perf annotate: Don't return -1 for error when doing BPF disassembly
[linux-2.6-microblaze.git] / drivers / watchdog / diag288_wdt.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Watchdog driver for z/VM and LPAR using the diag 288 interface.
4  *
5  * Under z/VM, expiration of the watchdog will send a "system restart" command
6  * to CP.
7  *
8  * The command can be altered using the module parameter "cmd". This is
9  * not recommended because it's only supported on z/VM but not whith LPAR.
10  *
11  * On LPAR, the watchdog will always trigger a system restart. the module
12  * paramter cmd is meaningless here.
13  *
14  *
15  * Copyright IBM Corp. 2004, 2013
16  * Author(s): Arnd Bergmann (arndb@de.ibm.com)
17  *            Philipp Hachtmann (phacht@de.ibm.com)
18  *
19  */
20
21 #define KMSG_COMPONENT "diag288_wdt"
22 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
23
24 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/slab.h>
29 #include <linux/miscdevice.h>
30 #include <linux/watchdog.h>
31 #include <linux/suspend.h>
32 #include <asm/ebcdic.h>
33 #include <asm/diag.h>
34 #include <linux/io.h>
35 #include <linux/uaccess.h>
36
37 #define MAX_CMDLEN 240
38 #define DEFAULT_CMD "SYSTEM RESTART"
39
40 #define MIN_INTERVAL 15     /* Minimal time supported by diag88 */
41 #define MAX_INTERVAL 3600   /* One hour should be enough - pure estimation */
42
43 #define WDT_DEFAULT_TIMEOUT 30
44
45 /* Function codes - init, change, cancel */
46 #define WDT_FUNC_INIT 0
47 #define WDT_FUNC_CHANGE 1
48 #define WDT_FUNC_CANCEL 2
49 #define WDT_FUNC_CONCEAL 0x80000000
50
51 /* Action codes for LPAR watchdog */
52 #define LPARWDT_RESTART 0
53
54 static char wdt_cmd[MAX_CMDLEN] = DEFAULT_CMD;
55 static bool conceal_on;
56 static bool nowayout_info = WATCHDOG_NOWAYOUT;
57
58 MODULE_LICENSE("GPL");
59 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
60 MODULE_AUTHOR("Philipp Hachtmann <phacht@de.ibm.com>");
61
62 MODULE_DESCRIPTION("System z diag288  Watchdog Timer");
63
64 module_param_string(cmd, wdt_cmd, MAX_CMDLEN, 0644);
65 MODULE_PARM_DESC(cmd, "CP command that is run when the watchdog triggers (z/VM only)");
66
67 module_param_named(conceal, conceal_on, bool, 0644);
68 MODULE_PARM_DESC(conceal, "Enable the CONCEAL CP option while the watchdog is active (z/VM only)");
69
70 module_param_named(nowayout, nowayout_info, bool, 0444);
71 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default = CONFIG_WATCHDOG_NOWAYOUT)");
72
73 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
74 MODULE_ALIAS("vmwatchdog");
75
76 static int __diag288(unsigned int func, unsigned int timeout,
77                      unsigned long action, unsigned int len)
78 {
79         register unsigned long __func asm("2") = func;
80         register unsigned long __timeout asm("3") = timeout;
81         register unsigned long __action asm("4") = action;
82         register unsigned long __len asm("5") = len;
83         int err;
84
85         err = -EINVAL;
86         asm volatile(
87                 "       diag    %1, %3, 0x288\n"
88                 "0:     la      %0, 0\n"
89                 "1:\n"
90                 EX_TABLE(0b, 1b)
91                 : "+d" (err) : "d"(__func), "d"(__timeout),
92                   "d"(__action), "d"(__len) : "1", "cc");
93         return err;
94 }
95
96 static int __diag288_vm(unsigned int  func, unsigned int timeout,
97                         char *cmd, size_t len)
98 {
99         diag_stat_inc(DIAG_STAT_X288);
100         return __diag288(func, timeout, virt_to_phys(cmd), len);
101 }
102
103 static int __diag288_lpar(unsigned int func, unsigned int timeout,
104                           unsigned long action)
105 {
106         diag_stat_inc(DIAG_STAT_X288);
107         return __diag288(func, timeout, action, 0);
108 }
109
110 static unsigned long wdt_status;
111
112 #define DIAG_WDOG_BUSY  0
113
114 static int wdt_start(struct watchdog_device *dev)
115 {
116         char *ebc_cmd;
117         size_t len;
118         int ret;
119         unsigned int func;
120
121         if (test_and_set_bit(DIAG_WDOG_BUSY, &wdt_status))
122                 return -EBUSY;
123
124         ret = -ENODEV;
125
126         if (MACHINE_IS_VM) {
127                 ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL);
128                 if (!ebc_cmd) {
129                         clear_bit(DIAG_WDOG_BUSY, &wdt_status);
130                         return -ENOMEM;
131                 }
132                 len = strlcpy(ebc_cmd, wdt_cmd, MAX_CMDLEN);
133                 ASCEBC(ebc_cmd, MAX_CMDLEN);
134                 EBC_TOUPPER(ebc_cmd, MAX_CMDLEN);
135
136                 func = conceal_on ? (WDT_FUNC_INIT | WDT_FUNC_CONCEAL)
137                         : WDT_FUNC_INIT;
138                 ret = __diag288_vm(func, dev->timeout, ebc_cmd, len);
139                 WARN_ON(ret != 0);
140                 kfree(ebc_cmd);
141         } else {
142                 ret = __diag288_lpar(WDT_FUNC_INIT,
143                                      dev->timeout, LPARWDT_RESTART);
144         }
145
146         if (ret) {
147                 pr_err("The watchdog cannot be activated\n");
148                 clear_bit(DIAG_WDOG_BUSY, &wdt_status);
149                 return ret;
150         }
151         return 0;
152 }
153
154 static int wdt_stop(struct watchdog_device *dev)
155 {
156         int ret;
157
158         diag_stat_inc(DIAG_STAT_X288);
159         ret = __diag288(WDT_FUNC_CANCEL, 0, 0, 0);
160
161         clear_bit(DIAG_WDOG_BUSY, &wdt_status);
162
163         return ret;
164 }
165
166 static int wdt_ping(struct watchdog_device *dev)
167 {
168         char *ebc_cmd;
169         size_t len;
170         int ret;
171         unsigned int func;
172
173         ret = -ENODEV;
174
175         if (MACHINE_IS_VM) {
176                 ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL);
177                 if (!ebc_cmd)
178                         return -ENOMEM;
179                 len = strlcpy(ebc_cmd, wdt_cmd, MAX_CMDLEN);
180                 ASCEBC(ebc_cmd, MAX_CMDLEN);
181                 EBC_TOUPPER(ebc_cmd, MAX_CMDLEN);
182
183                 /*
184                  * It seems to be ok to z/VM to use the init function to
185                  * retrigger the watchdog. On LPAR WDT_FUNC_CHANGE must
186                  * be used when the watchdog is running.
187                  */
188                 func = conceal_on ? (WDT_FUNC_INIT | WDT_FUNC_CONCEAL)
189                         : WDT_FUNC_INIT;
190
191                 ret = __diag288_vm(func, dev->timeout, ebc_cmd, len);
192                 WARN_ON(ret != 0);
193                 kfree(ebc_cmd);
194         } else {
195                 ret = __diag288_lpar(WDT_FUNC_CHANGE, dev->timeout, 0);
196         }
197
198         if (ret)
199                 pr_err("The watchdog timer cannot be started or reset\n");
200         return ret;
201 }
202
203 static int wdt_set_timeout(struct watchdog_device * dev, unsigned int new_to)
204 {
205         dev->timeout = new_to;
206         return wdt_ping(dev);
207 }
208
209 static const struct watchdog_ops wdt_ops = {
210         .owner = THIS_MODULE,
211         .start = wdt_start,
212         .stop = wdt_stop,
213         .ping = wdt_ping,
214         .set_timeout = wdt_set_timeout,
215 };
216
217 static const struct watchdog_info wdt_info = {
218         .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
219         .firmware_version = 0,
220         .identity = "z Watchdog",
221 };
222
223 static struct watchdog_device wdt_dev = {
224         .parent = NULL,
225         .info = &wdt_info,
226         .ops = &wdt_ops,
227         .bootstatus = 0,
228         .timeout = WDT_DEFAULT_TIMEOUT,
229         .min_timeout = MIN_INTERVAL,
230         .max_timeout = MAX_INTERVAL,
231 };
232
233 /*
234  * It makes no sense to go into suspend while the watchdog is running.
235  * Depending on the memory size, the watchdog might trigger, while we
236  * are still saving the memory.
237  */
238 static int wdt_suspend(void)
239 {
240         if (test_and_set_bit(DIAG_WDOG_BUSY, &wdt_status)) {
241                 pr_err("Linux cannot be suspended while the watchdog is in use\n");
242                 return notifier_from_errno(-EBUSY);
243         }
244         return NOTIFY_DONE;
245 }
246
247 static int wdt_resume(void)
248 {
249         clear_bit(DIAG_WDOG_BUSY, &wdt_status);
250         return NOTIFY_DONE;
251 }
252
253 static int wdt_power_event(struct notifier_block *this, unsigned long event,
254                            void *ptr)
255 {
256         switch (event) {
257         case PM_POST_HIBERNATION:
258         case PM_POST_SUSPEND:
259                 return wdt_resume();
260         case PM_HIBERNATION_PREPARE:
261         case PM_SUSPEND_PREPARE:
262                 return wdt_suspend();
263         default:
264                 return NOTIFY_DONE;
265         }
266 }
267
268 static struct notifier_block wdt_power_notifier = {
269         .notifier_call = wdt_power_event,
270 };
271
272 static int __init diag288_init(void)
273 {
274         int ret;
275         char ebc_begin[] = {
276                 194, 197, 199, 201, 213
277         };
278
279         watchdog_set_nowayout(&wdt_dev, nowayout_info);
280
281         if (MACHINE_IS_VM) {
282                 if (__diag288_vm(WDT_FUNC_INIT, 15,
283                                  ebc_begin, sizeof(ebc_begin)) != 0) {
284                         pr_err("The watchdog cannot be initialized\n");
285                         return -EINVAL;
286                 }
287         } else {
288                 if (__diag288_lpar(WDT_FUNC_INIT, 30, LPARWDT_RESTART)) {
289                         pr_err("The watchdog cannot be initialized\n");
290                         return -EINVAL;
291                 }
292         }
293
294         if (__diag288_lpar(WDT_FUNC_CANCEL, 0, 0)) {
295                 pr_err("The watchdog cannot be deactivated\n");
296                 return -EINVAL;
297         }
298
299         ret = register_pm_notifier(&wdt_power_notifier);
300         if (ret)
301                 return ret;
302
303         ret = watchdog_register_device(&wdt_dev);
304         if (ret)
305                 unregister_pm_notifier(&wdt_power_notifier);
306
307         return ret;
308 }
309
310 static void __exit diag288_exit(void)
311 {
312         watchdog_unregister_device(&wdt_dev);
313         unregister_pm_notifier(&wdt_power_notifier);
314 }
315
316 module_init(diag288_init);
317 module_exit(diag288_exit);