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