Blame view

drivers/watchdog/diag288_wdt.c 7.43 KB
09c434b8a   Thomas Gleixner   treewide: Add SPD...
1
  // SPDX-License-Identifier: GPL-2.0-only
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
2
  /*
646f919e9   Philipp Hachtmann   s390/watchdog: ad...
3
   * Watchdog driver for z/VM and LPAR using the diag 288 interface.
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
4
5
6
7
   *
   * Under z/VM, expiration of the watchdog will send a "system restart" command
   * to CP.
   *
646f919e9   Philipp Hachtmann   s390/watchdog: ad...
8
9
10
11
12
13
   * The command can be altered using the module parameter "cmd". This is
   * not recommended because it's only supported on z/VM but not whith LPAR.
   *
   * On LPAR, the watchdog will always trigger a system restart. the module
   * paramter cmd is meaningless here.
   *
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
   *
   * Copyright IBM Corp. 2004, 2013
   * Author(s): Arnd Bergmann (arndb@de.ibm.com)
   *	      Philipp Hachtmann (phacht@de.ibm.com)
   *
   */
  
  #define KMSG_COMPONENT "diag288_wdt"
  #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  
  #include <linux/init.h>
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/moduleparam.h>
  #include <linux/slab.h>
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
29
30
31
  #include <linux/watchdog.h>
  #include <linux/suspend.h>
  #include <asm/ebcdic.h>
1ec2772e0   Martin Schwidefsky   s390/diag: add a ...
32
  #include <asm/diag.h>
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
33
  #include <linux/io.h>
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  
  #define MAX_CMDLEN 240
  #define DEFAULT_CMD "SYSTEM RESTART"
  
  #define MIN_INTERVAL 15     /* Minimal time supported by diag88 */
  #define MAX_INTERVAL 3600   /* One hour should be enough - pure estimation */
  
  #define WDT_DEFAULT_TIMEOUT 30
  
  /* Function codes - init, change, cancel */
  #define WDT_FUNC_INIT 0
  #define WDT_FUNC_CHANGE 1
  #define WDT_FUNC_CANCEL 2
  #define WDT_FUNC_CONCEAL 0x80000000
646f919e9   Philipp Hachtmann   s390/watchdog: ad...
48
49
  /* Action codes for LPAR watchdog */
  #define LPARWDT_RESTART 0
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  static char wdt_cmd[MAX_CMDLEN] = DEFAULT_CMD;
  static bool conceal_on;
  static bool nowayout_info = WATCHDOG_NOWAYOUT;
  
  MODULE_LICENSE("GPL");
  MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
  MODULE_AUTHOR("Philipp Hachtmann <phacht@de.ibm.com>");
  
  MODULE_DESCRIPTION("System z diag288  Watchdog Timer");
  
  module_param_string(cmd, wdt_cmd, MAX_CMDLEN, 0644);
  MODULE_PARM_DESC(cmd, "CP command that is run when the watchdog triggers (z/VM only)");
  
  module_param_named(conceal, conceal_on, bool, 0644);
  MODULE_PARM_DESC(conceal, "Enable the CONCEAL CP option while the watchdog is active (z/VM only)");
  
  module_param_named(nowayout, nowayout_info, bool, 0444);
  MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default = CONFIG_WATCHDOG_NOWAYOUT)");
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  MODULE_ALIAS("vmwatchdog");
  
  static int __diag288(unsigned int func, unsigned int timeout,
  		     unsigned long action, unsigned int len)
  {
  	register unsigned long __func asm("2") = func;
  	register unsigned long __timeout asm("3") = timeout;
  	register unsigned long __action asm("4") = action;
  	register unsigned long __len asm("5") = len;
  	int err;
  
  	err = -EINVAL;
  	asm volatile(
  		"	diag	%1, %3, 0x288
  "
  		"0:	la	%0, 0
  "
  		"1:
  "
  		EX_TABLE(0b, 1b)
  		: "+d" (err) : "d"(__func), "d"(__timeout),
  		  "d"(__action), "d"(__len) : "1", "cc");
  	return err;
  }
  
  static int __diag288_vm(unsigned int  func, unsigned int timeout,
  			char *cmd, size_t len)
  {
1ec2772e0   Martin Schwidefsky   s390/diag: add a ...
96
  	diag_stat_inc(DIAG_STAT_X288);
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
97
98
  	return __diag288(func, timeout, virt_to_phys(cmd), len);
  }
646f919e9   Philipp Hachtmann   s390/watchdog: ad...
99
100
101
  static int __diag288_lpar(unsigned int func, unsigned int timeout,
  			  unsigned long action)
  {
1ec2772e0   Martin Schwidefsky   s390/diag: add a ...
102
  	diag_stat_inc(DIAG_STAT_X288);
646f919e9   Philipp Hachtmann   s390/watchdog: ad...
103
104
  	return __diag288(func, timeout, action, 0);
  }
e7d162faa   Guenter Roeck   watchdog: diag288...
105
106
107
  static unsigned long wdt_status;
  
  #define DIAG_WDOG_BUSY	0
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
108
109
110
111
112
113
  static int wdt_start(struct watchdog_device *dev)
  {
  	char *ebc_cmd;
  	size_t len;
  	int ret;
  	unsigned int func;
e7d162faa   Guenter Roeck   watchdog: diag288...
114
115
  	if (test_and_set_bit(DIAG_WDOG_BUSY, &wdt_status))
  		return -EBUSY;
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
116
117
118
119
  	ret = -ENODEV;
  
  	if (MACHINE_IS_VM) {
  		ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL);
e7d162faa   Guenter Roeck   watchdog: diag288...
120
121
  		if (!ebc_cmd) {
  			clear_bit(DIAG_WDOG_BUSY, &wdt_status);
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
122
  			return -ENOMEM;
e7d162faa   Guenter Roeck   watchdog: diag288...
123
  		}
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
124
125
126
127
128
129
130
131
132
  		len = strlcpy(ebc_cmd, wdt_cmd, MAX_CMDLEN);
  		ASCEBC(ebc_cmd, MAX_CMDLEN);
  		EBC_TOUPPER(ebc_cmd, MAX_CMDLEN);
  
  		func = conceal_on ? (WDT_FUNC_INIT | WDT_FUNC_CONCEAL)
  			: WDT_FUNC_INIT;
  		ret = __diag288_vm(func, dev->timeout, ebc_cmd, len);
  		WARN_ON(ret != 0);
  		kfree(ebc_cmd);
b2527d200   Xu Wang   s390/watchdog: su...
133
  	} else {
646f919e9   Philipp Hachtmann   s390/watchdog: ad...
134
135
136
  		ret = __diag288_lpar(WDT_FUNC_INIT,
  				     dev->timeout, LPARWDT_RESTART);
  	}
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
137
138
139
  	if (ret) {
  		pr_err("The watchdog cannot be activated
  ");
e7d162faa   Guenter Roeck   watchdog: diag288...
140
  		clear_bit(DIAG_WDOG_BUSY, &wdt_status);
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
141
142
  		return ret;
  	}
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
143
144
145
146
147
148
  	return 0;
  }
  
  static int wdt_stop(struct watchdog_device *dev)
  {
  	int ret;
1ec2772e0   Martin Schwidefsky   s390/diag: add a ...
149
  	diag_stat_inc(DIAG_STAT_X288);
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
150
  	ret = __diag288(WDT_FUNC_CANCEL, 0, 0, 0);
e7d162faa   Guenter Roeck   watchdog: diag288...
151
152
  
  	clear_bit(DIAG_WDOG_BUSY, &wdt_status);
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
  	return ret;
  }
  
  static int wdt_ping(struct watchdog_device *dev)
  {
  	char *ebc_cmd;
  	size_t len;
  	int ret;
  	unsigned int func;
  
  	ret = -ENODEV;
  
  	if (MACHINE_IS_VM) {
  		ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL);
  		if (!ebc_cmd)
  			return -ENOMEM;
  		len = strlcpy(ebc_cmd, wdt_cmd, MAX_CMDLEN);
  		ASCEBC(ebc_cmd, MAX_CMDLEN);
  		EBC_TOUPPER(ebc_cmd, MAX_CMDLEN);
  
  		/*
  		 * It seems to be ok to z/VM to use the init function to
646f919e9   Philipp Hachtmann   s390/watchdog: ad...
175
176
  		 * retrigger the watchdog. On LPAR WDT_FUNC_CHANGE must
  		 * be used when the watchdog is running.
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
177
178
179
180
181
182
183
  		 */
  		func = conceal_on ? (WDT_FUNC_INIT | WDT_FUNC_CONCEAL)
  			: WDT_FUNC_INIT;
  
  		ret = __diag288_vm(func, dev->timeout, ebc_cmd, len);
  		WARN_ON(ret != 0);
  		kfree(ebc_cmd);
b2527d200   Xu Wang   s390/watchdog: su...
184
  	} else {
646f919e9   Philipp Hachtmann   s390/watchdog: ad...
185
  		ret = __diag288_lpar(WDT_FUNC_CHANGE, dev->timeout, 0);
b2527d200   Xu Wang   s390/watchdog: su...
186
  	}
646f919e9   Philipp Hachtmann   s390/watchdog: ad...
187

f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
188
189
190
191
192
193
194
195
196
197
198
  	if (ret)
  		pr_err("The watchdog timer cannot be started or reset
  ");
  	return ret;
  }
  
  static int wdt_set_timeout(struct watchdog_device * dev, unsigned int new_to)
  {
  	dev->timeout = new_to;
  	return wdt_ping(dev);
  }
b893e344b   Bhumika Goyal   watchdog: constif...
199
  static const struct watchdog_ops wdt_ops = {
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
200
201
202
203
204
205
  	.owner = THIS_MODULE,
  	.start = wdt_start,
  	.stop = wdt_stop,
  	.ping = wdt_ping,
  	.set_timeout = wdt_set_timeout,
  };
323edb2e2   Julia Lawall   watchdog: constif...
206
  static const struct watchdog_info wdt_info = {
9ec6cb80c   Xu Wang   s390/watchdog: en...
207
  	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
  	.firmware_version = 0,
  	.identity = "z Watchdog",
  };
  
  static struct watchdog_device wdt_dev = {
  	.parent = NULL,
  	.info = &wdt_info,
  	.ops = &wdt_ops,
  	.bootstatus = 0,
  	.timeout = WDT_DEFAULT_TIMEOUT,
  	.min_timeout = MIN_INTERVAL,
  	.max_timeout = MAX_INTERVAL,
  };
  
  /*
   * It makes no sense to go into suspend while the watchdog is running.
   * Depending on the memory size, the watchdog might trigger, while we
   * are still saving the memory.
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
226
227
228
   */
  static int wdt_suspend(void)
  {
e7d162faa   Guenter Roeck   watchdog: diag288...
229
  	if (test_and_set_bit(DIAG_WDOG_BUSY, &wdt_status)) {
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
230
231
232
233
234
235
236
237
238
  		pr_err("Linux cannot be suspended while the watchdog is in use
  ");
  		return notifier_from_errno(-EBUSY);
  	}
  	return NOTIFY_DONE;
  }
  
  static int wdt_resume(void)
  {
e7d162faa   Guenter Roeck   watchdog: diag288...
239
  	clear_bit(DIAG_WDOG_BUSY, &wdt_status);
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
  	return NOTIFY_DONE;
  }
  
  static int wdt_power_event(struct notifier_block *this, unsigned long event,
  			   void *ptr)
  {
  	switch (event) {
  	case PM_POST_HIBERNATION:
  	case PM_POST_SUSPEND:
  		return wdt_resume();
  	case PM_HIBERNATION_PREPARE:
  	case PM_SUSPEND_PREPARE:
  		return wdt_suspend();
  	default:
  		return NOTIFY_DONE;
  	}
  }
  
  static struct notifier_block wdt_power_notifier = {
  	.notifier_call = wdt_power_event,
  };
  
  static int __init diag288_init(void)
  {
  	int ret;
  	char ebc_begin[] = {
  		194, 197, 199, 201, 213
  	};
  
  	watchdog_set_nowayout(&wdt_dev, nowayout_info);
  
  	if (MACHINE_IS_VM) {
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
272
273
274
275
276
277
  		if (__diag288_vm(WDT_FUNC_INIT, 15,
  				 ebc_begin, sizeof(ebc_begin)) != 0) {
  			pr_err("The watchdog cannot be initialized
  ");
  			return -EINVAL;
  		}
b2527d200   Xu Wang   s390/watchdog: su...
278
  	} else {
646f919e9   Philipp Hachtmann   s390/watchdog: ad...
279
280
281
282
283
  		if (__diag288_lpar(WDT_FUNC_INIT, 30, LPARWDT_RESTART)) {
  			pr_err("The watchdog cannot be initialized
  ");
  			return -EINVAL;
  		}
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
284
  	}
646f919e9   Philipp Hachtmann   s390/watchdog: ad...
285
  	if (__diag288_lpar(WDT_FUNC_CANCEL, 0, 0)) {
f7a94db4e   Philipp Hachtmann   s390/watchdog: us...
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
  		pr_err("The watchdog cannot be deactivated
  ");
  		return -EINVAL;
  	}
  
  	ret = register_pm_notifier(&wdt_power_notifier);
  	if (ret)
  		return ret;
  
  	ret = watchdog_register_device(&wdt_dev);
  	if (ret)
  		unregister_pm_notifier(&wdt_power_notifier);
  
  	return ret;
  }
  
  static void __exit diag288_exit(void)
  {
  	watchdog_unregister_device(&wdt_dev);
  	unregister_pm_notifier(&wdt_power_notifier);
  }
  
  module_init(diag288_init);
  module_exit(diag288_exit);