xref: /openbmc/linux/arch/s390/kernel/ipl.c (revision 8730046c)
1 /*
2  *    ipl/reipl/dump support for Linux on s390.
3  *
4  *    Copyright IBM Corp. 2005, 2012
5  *    Author(s): Michael Holzheu <holzheu@de.ibm.com>
6  *		 Heiko Carstens <heiko.carstens@de.ibm.com>
7  *		 Volker Sameske <sameske@de.ibm.com>
8  */
9 
10 #include <linux/types.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/delay.h>
14 #include <linux/reboot.h>
15 #include <linux/ctype.h>
16 #include <linux/fs.h>
17 #include <linux/gfp.h>
18 #include <linux/crash_dump.h>
19 #include <linux/debug_locks.h>
20 #include <asm/diag.h>
21 #include <asm/ipl.h>
22 #include <asm/smp.h>
23 #include <asm/setup.h>
24 #include <asm/cpcmd.h>
25 #include <asm/cio.h>
26 #include <asm/ebcdic.h>
27 #include <asm/reset.h>
28 #include <asm/sclp.h>
29 #include <asm/checksum.h>
30 #include <asm/debug.h>
31 #include <asm/os_info.h>
32 #include "entry.h"
33 
34 #define IPL_PARM_BLOCK_VERSION 0
35 
36 #define IPL_UNKNOWN_STR		"unknown"
37 #define IPL_CCW_STR		"ccw"
38 #define IPL_FCP_STR		"fcp"
39 #define IPL_FCP_DUMP_STR	"fcp_dump"
40 #define IPL_NSS_STR		"nss"
41 
42 #define DUMP_CCW_STR		"ccw"
43 #define DUMP_FCP_STR		"fcp"
44 #define DUMP_NONE_STR		"none"
45 
46 /*
47  * Four shutdown trigger types are supported:
48  * - panic
49  * - halt
50  * - power off
51  * - reipl
52  * - restart
53  */
54 #define ON_PANIC_STR		"on_panic"
55 #define ON_HALT_STR		"on_halt"
56 #define ON_POFF_STR		"on_poff"
57 #define ON_REIPL_STR		"on_reboot"
58 #define ON_RESTART_STR		"on_restart"
59 
60 struct shutdown_action;
61 struct shutdown_trigger {
62 	char *name;
63 	struct shutdown_action *action;
64 };
65 
66 /*
67  * The following shutdown action types are supported:
68  */
69 #define SHUTDOWN_ACTION_IPL_STR		"ipl"
70 #define SHUTDOWN_ACTION_REIPL_STR	"reipl"
71 #define SHUTDOWN_ACTION_DUMP_STR	"dump"
72 #define SHUTDOWN_ACTION_VMCMD_STR	"vmcmd"
73 #define SHUTDOWN_ACTION_STOP_STR	"stop"
74 #define SHUTDOWN_ACTION_DUMP_REIPL_STR	"dump_reipl"
75 
76 struct shutdown_action {
77 	char *name;
78 	void (*fn) (struct shutdown_trigger *trigger);
79 	int (*init) (void);
80 	int init_rc;
81 };
82 
83 static char *ipl_type_str(enum ipl_type type)
84 {
85 	switch (type) {
86 	case IPL_TYPE_CCW:
87 		return IPL_CCW_STR;
88 	case IPL_TYPE_FCP:
89 		return IPL_FCP_STR;
90 	case IPL_TYPE_FCP_DUMP:
91 		return IPL_FCP_DUMP_STR;
92 	case IPL_TYPE_NSS:
93 		return IPL_NSS_STR;
94 	case IPL_TYPE_UNKNOWN:
95 	default:
96 		return IPL_UNKNOWN_STR;
97 	}
98 }
99 
100 enum dump_type {
101 	DUMP_TYPE_NONE	= 1,
102 	DUMP_TYPE_CCW	= 2,
103 	DUMP_TYPE_FCP	= 4,
104 };
105 
106 static char *dump_type_str(enum dump_type type)
107 {
108 	switch (type) {
109 	case DUMP_TYPE_NONE:
110 		return DUMP_NONE_STR;
111 	case DUMP_TYPE_CCW:
112 		return DUMP_CCW_STR;
113 	case DUMP_TYPE_FCP:
114 		return DUMP_FCP_STR;
115 	default:
116 		return NULL;
117 	}
118 }
119 
120 /*
121  * Must be in data section since the bss section
122  * is not cleared when these are accessed.
123  */
124 static u8 ipl_ssid __section(.data) = 0;
125 static u16 ipl_devno __section(.data) = 0;
126 u32 ipl_flags __section(.data) = 0;
127 
128 enum ipl_method {
129 	REIPL_METHOD_CCW_CIO,
130 	REIPL_METHOD_CCW_DIAG,
131 	REIPL_METHOD_CCW_VM,
132 	REIPL_METHOD_FCP_RO_DIAG,
133 	REIPL_METHOD_FCP_RW_DIAG,
134 	REIPL_METHOD_FCP_RO_VM,
135 	REIPL_METHOD_FCP_DUMP,
136 	REIPL_METHOD_NSS,
137 	REIPL_METHOD_NSS_DIAG,
138 	REIPL_METHOD_DEFAULT,
139 };
140 
141 enum dump_method {
142 	DUMP_METHOD_NONE,
143 	DUMP_METHOD_CCW_CIO,
144 	DUMP_METHOD_CCW_DIAG,
145 	DUMP_METHOD_CCW_VM,
146 	DUMP_METHOD_FCP_DIAG,
147 };
148 
149 static int diag308_set_works = 0;
150 
151 static struct ipl_parameter_block ipl_block;
152 
153 static int reipl_capabilities = IPL_TYPE_UNKNOWN;
154 
155 static enum ipl_type reipl_type = IPL_TYPE_UNKNOWN;
156 static enum ipl_method reipl_method = REIPL_METHOD_DEFAULT;
157 static struct ipl_parameter_block *reipl_block_fcp;
158 static struct ipl_parameter_block *reipl_block_ccw;
159 static struct ipl_parameter_block *reipl_block_nss;
160 static struct ipl_parameter_block *reipl_block_actual;
161 
162 static int dump_capabilities = DUMP_TYPE_NONE;
163 static enum dump_type dump_type = DUMP_TYPE_NONE;
164 static enum dump_method dump_method = DUMP_METHOD_NONE;
165 static struct ipl_parameter_block *dump_block_fcp;
166 static struct ipl_parameter_block *dump_block_ccw;
167 
168 static struct sclp_ipl_info sclp_ipl_info;
169 
170 static inline int __diag308(unsigned long subcode, void *addr)
171 {
172 	register unsigned long _addr asm("0") = (unsigned long) addr;
173 	register unsigned long _rc asm("1") = 0;
174 
175 	asm volatile(
176 		"	diag	%0,%2,0x308\n"
177 		"0:	nopr	%%r7\n"
178 		EX_TABLE(0b,0b)
179 		: "+d" (_addr), "+d" (_rc)
180 		: "d" (subcode) : "cc", "memory");
181 	return _rc;
182 }
183 
184 int diag308(unsigned long subcode, void *addr)
185 {
186 	diag_stat_inc(DIAG_STAT_X308);
187 	return __diag308(subcode, addr);
188 }
189 EXPORT_SYMBOL_GPL(diag308);
190 
191 /* SYSFS */
192 
193 #define IPL_ATTR_SHOW_FN(_prefix, _name, _format, args...)		\
194 static ssize_t sys_##_prefix##_##_name##_show(struct kobject *kobj,	\
195 		struct kobj_attribute *attr,				\
196 		char *page)						\
197 {									\
198 	return snprintf(page, PAGE_SIZE, _format, ##args);		\
199 }
200 
201 #define IPL_ATTR_CCW_STORE_FN(_prefix, _name, _ipl_blk)			\
202 static ssize_t sys_##_prefix##_##_name##_store(struct kobject *kobj,	\
203 		struct kobj_attribute *attr,				\
204 		const char *buf, size_t len)				\
205 {									\
206 	unsigned long long ssid, devno;					\
207 									\
208 	if (sscanf(buf, "0.%llx.%llx\n", &ssid, &devno) != 2)		\
209 		return -EINVAL;						\
210 									\
211 	if (ssid > __MAX_SSID || devno > __MAX_SUBCHANNEL)		\
212 		return -EINVAL;						\
213 									\
214 	_ipl_blk.ssid = ssid;						\
215 	_ipl_blk.devno = devno;						\
216 	return len;							\
217 }
218 
219 #define DEFINE_IPL_CCW_ATTR_RW(_prefix, _name, _ipl_blk)		\
220 IPL_ATTR_SHOW_FN(_prefix, _name, "0.%x.%04x\n",				\
221 		 _ipl_blk.ssid, _ipl_blk.devno);			\
222 IPL_ATTR_CCW_STORE_FN(_prefix, _name, _ipl_blk);			\
223 static struct kobj_attribute sys_##_prefix##_##_name##_attr =		\
224 	__ATTR(_name, (S_IRUGO | S_IWUSR),				\
225 	       sys_##_prefix##_##_name##_show,				\
226 	       sys_##_prefix##_##_name##_store)				\
227 
228 #define DEFINE_IPL_ATTR_RO(_prefix, _name, _format, _value)		\
229 IPL_ATTR_SHOW_FN(_prefix, _name, _format, _value)			\
230 static struct kobj_attribute sys_##_prefix##_##_name##_attr =		\
231 	__ATTR(_name, S_IRUGO, sys_##_prefix##_##_name##_show, NULL)
232 
233 #define DEFINE_IPL_ATTR_RW(_prefix, _name, _fmt_out, _fmt_in, _value)	\
234 IPL_ATTR_SHOW_FN(_prefix, _name, _fmt_out, (unsigned long long) _value)	\
235 static ssize_t sys_##_prefix##_##_name##_store(struct kobject *kobj,	\
236 		struct kobj_attribute *attr,				\
237 		const char *buf, size_t len)				\
238 {									\
239 	unsigned long long value;					\
240 	if (sscanf(buf, _fmt_in, &value) != 1)				\
241 		return -EINVAL;						\
242 	_value = value;							\
243 	return len;							\
244 }									\
245 static struct kobj_attribute sys_##_prefix##_##_name##_attr =		\
246 	__ATTR(_name,(S_IRUGO | S_IWUSR),				\
247 			sys_##_prefix##_##_name##_show,			\
248 			sys_##_prefix##_##_name##_store)
249 
250 #define DEFINE_IPL_ATTR_STR_RW(_prefix, _name, _fmt_out, _fmt_in, _value)\
251 IPL_ATTR_SHOW_FN(_prefix, _name, _fmt_out, _value)			\
252 static ssize_t sys_##_prefix##_##_name##_store(struct kobject *kobj,	\
253 		struct kobj_attribute *attr,				\
254 		const char *buf, size_t len)				\
255 {									\
256 	strncpy(_value, buf, sizeof(_value) - 1);			\
257 	strim(_value);							\
258 	return len;							\
259 }									\
260 static struct kobj_attribute sys_##_prefix##_##_name##_attr =		\
261 	__ATTR(_name,(S_IRUGO | S_IWUSR),				\
262 			sys_##_prefix##_##_name##_show,			\
263 			sys_##_prefix##_##_name##_store)
264 
265 static void make_attrs_ro(struct attribute **attrs)
266 {
267 	while (*attrs) {
268 		(*attrs)->mode = S_IRUGO;
269 		attrs++;
270 	}
271 }
272 
273 /*
274  * ipl section
275  */
276 
277 static __init enum ipl_type get_ipl_type(void)
278 {
279 	struct ipl_parameter_block *ipl = IPL_PARMBLOCK_START;
280 
281 	if (ipl_flags & IPL_NSS_VALID)
282 		return IPL_TYPE_NSS;
283 	if (!(ipl_flags & IPL_DEVNO_VALID))
284 		return IPL_TYPE_UNKNOWN;
285 	if (!(ipl_flags & IPL_PARMBLOCK_VALID))
286 		return IPL_TYPE_CCW;
287 	if (ipl->hdr.version > IPL_MAX_SUPPORTED_VERSION)
288 		return IPL_TYPE_UNKNOWN;
289 	if (ipl->hdr.pbt != DIAG308_IPL_TYPE_FCP)
290 		return IPL_TYPE_UNKNOWN;
291 	if (ipl->ipl_info.fcp.opt == DIAG308_IPL_OPT_DUMP)
292 		return IPL_TYPE_FCP_DUMP;
293 	return IPL_TYPE_FCP;
294 }
295 
296 struct ipl_info ipl_info;
297 EXPORT_SYMBOL_GPL(ipl_info);
298 
299 static ssize_t ipl_type_show(struct kobject *kobj, struct kobj_attribute *attr,
300 			     char *page)
301 {
302 	return sprintf(page, "%s\n", ipl_type_str(ipl_info.type));
303 }
304 
305 static struct kobj_attribute sys_ipl_type_attr = __ATTR_RO(ipl_type);
306 
307 /* VM IPL PARM routines */
308 static size_t reipl_get_ascii_vmparm(char *dest, size_t size,
309 				     const struct ipl_parameter_block *ipb)
310 {
311 	int i;
312 	size_t len;
313 	char has_lowercase = 0;
314 
315 	len = 0;
316 	if ((ipb->ipl_info.ccw.vm_flags & DIAG308_VM_FLAGS_VP_VALID) &&
317 	    (ipb->ipl_info.ccw.vm_parm_len > 0)) {
318 
319 		len = min_t(size_t, size - 1, ipb->ipl_info.ccw.vm_parm_len);
320 		memcpy(dest, ipb->ipl_info.ccw.vm_parm, len);
321 		/* If at least one character is lowercase, we assume mixed
322 		 * case; otherwise we convert everything to lowercase.
323 		 */
324 		for (i = 0; i < len; i++)
325 			if ((dest[i] > 0x80 && dest[i] < 0x8a) || /* a-i */
326 			    (dest[i] > 0x90 && dest[i] < 0x9a) || /* j-r */
327 			    (dest[i] > 0xa1 && dest[i] < 0xaa)) { /* s-z */
328 				has_lowercase = 1;
329 				break;
330 			}
331 		if (!has_lowercase)
332 			EBC_TOLOWER(dest, len);
333 		EBCASC(dest, len);
334 	}
335 	dest[len] = 0;
336 
337 	return len;
338 }
339 
340 size_t append_ipl_vmparm(char *dest, size_t size)
341 {
342 	size_t rc;
343 
344 	rc = 0;
345 	if (diag308_set_works && (ipl_block.hdr.pbt == DIAG308_IPL_TYPE_CCW))
346 		rc = reipl_get_ascii_vmparm(dest, size, &ipl_block);
347 	else
348 		dest[0] = 0;
349 	return rc;
350 }
351 
352 static ssize_t ipl_vm_parm_show(struct kobject *kobj,
353 				struct kobj_attribute *attr, char *page)
354 {
355 	char parm[DIAG308_VMPARM_SIZE + 1] = {};
356 
357 	append_ipl_vmparm(parm, sizeof(parm));
358 	return sprintf(page, "%s\n", parm);
359 }
360 
361 static size_t scpdata_length(const char* buf, size_t count)
362 {
363 	while (count) {
364 		if (buf[count - 1] != '\0' && buf[count - 1] != ' ')
365 			break;
366 		count--;
367 	}
368 	return count;
369 }
370 
371 static size_t reipl_append_ascii_scpdata(char *dest, size_t size,
372 					 const struct ipl_parameter_block *ipb)
373 {
374 	size_t count;
375 	size_t i;
376 	int has_lowercase;
377 
378 	count = min(size - 1, scpdata_length(ipb->ipl_info.fcp.scp_data,
379 					     ipb->ipl_info.fcp.scp_data_len));
380 	if (!count)
381 		goto out;
382 
383 	has_lowercase = 0;
384 	for (i = 0; i < count; i++) {
385 		if (!isascii(ipb->ipl_info.fcp.scp_data[i])) {
386 			count = 0;
387 			goto out;
388 		}
389 		if (!has_lowercase && islower(ipb->ipl_info.fcp.scp_data[i]))
390 			has_lowercase = 1;
391 	}
392 
393 	if (has_lowercase)
394 		memcpy(dest, ipb->ipl_info.fcp.scp_data, count);
395 	else
396 		for (i = 0; i < count; i++)
397 			dest[i] = tolower(ipb->ipl_info.fcp.scp_data[i]);
398 out:
399 	dest[count] = '\0';
400 	return count;
401 }
402 
403 size_t append_ipl_scpdata(char *dest, size_t len)
404 {
405 	size_t rc;
406 
407 	rc = 0;
408 	if (ipl_block.hdr.pbt == DIAG308_IPL_TYPE_FCP)
409 		rc = reipl_append_ascii_scpdata(dest, len, &ipl_block);
410 	else
411 		dest[0] = 0;
412 	return rc;
413 }
414 
415 
416 static struct kobj_attribute sys_ipl_vm_parm_attr =
417 	__ATTR(parm, S_IRUGO, ipl_vm_parm_show, NULL);
418 
419 static ssize_t sys_ipl_device_show(struct kobject *kobj,
420 				   struct kobj_attribute *attr, char *page)
421 {
422 	struct ipl_parameter_block *ipl = IPL_PARMBLOCK_START;
423 
424 	switch (ipl_info.type) {
425 	case IPL_TYPE_CCW:
426 		return sprintf(page, "0.%x.%04x\n", ipl_ssid, ipl_devno);
427 	case IPL_TYPE_FCP:
428 	case IPL_TYPE_FCP_DUMP:
429 		return sprintf(page, "0.0.%04x\n", ipl->ipl_info.fcp.devno);
430 	default:
431 		return 0;
432 	}
433 }
434 
435 static struct kobj_attribute sys_ipl_device_attr =
436 	__ATTR(device, S_IRUGO, sys_ipl_device_show, NULL);
437 
438 static ssize_t ipl_parameter_read(struct file *filp, struct kobject *kobj,
439 				  struct bin_attribute *attr, char *buf,
440 				  loff_t off, size_t count)
441 {
442 	return memory_read_from_buffer(buf, count, &off, IPL_PARMBLOCK_START,
443 					IPL_PARMBLOCK_SIZE);
444 }
445 static struct bin_attribute ipl_parameter_attr =
446 	__BIN_ATTR(binary_parameter, S_IRUGO, ipl_parameter_read, NULL,
447 		   PAGE_SIZE);
448 
449 static ssize_t ipl_scp_data_read(struct file *filp, struct kobject *kobj,
450 				 struct bin_attribute *attr, char *buf,
451 				 loff_t off, size_t count)
452 {
453 	unsigned int size = IPL_PARMBLOCK_START->ipl_info.fcp.scp_data_len;
454 	void *scp_data = &IPL_PARMBLOCK_START->ipl_info.fcp.scp_data;
455 
456 	return memory_read_from_buffer(buf, count, &off, scp_data, size);
457 }
458 static struct bin_attribute ipl_scp_data_attr =
459 	__BIN_ATTR(scp_data, S_IRUGO, ipl_scp_data_read, NULL, PAGE_SIZE);
460 
461 static struct bin_attribute *ipl_fcp_bin_attrs[] = {
462 	&ipl_parameter_attr,
463 	&ipl_scp_data_attr,
464 	NULL,
465 };
466 
467 /* FCP ipl device attributes */
468 
469 DEFINE_IPL_ATTR_RO(ipl_fcp, wwpn, "0x%016llx\n", (unsigned long long)
470 		   IPL_PARMBLOCK_START->ipl_info.fcp.wwpn);
471 DEFINE_IPL_ATTR_RO(ipl_fcp, lun, "0x%016llx\n", (unsigned long long)
472 		   IPL_PARMBLOCK_START->ipl_info.fcp.lun);
473 DEFINE_IPL_ATTR_RO(ipl_fcp, bootprog, "%lld\n", (unsigned long long)
474 		   IPL_PARMBLOCK_START->ipl_info.fcp.bootprog);
475 DEFINE_IPL_ATTR_RO(ipl_fcp, br_lba, "%lld\n", (unsigned long long)
476 		   IPL_PARMBLOCK_START->ipl_info.fcp.br_lba);
477 
478 static ssize_t ipl_ccw_loadparm_show(struct kobject *kobj,
479 				     struct kobj_attribute *attr, char *page)
480 {
481 	char loadparm[LOADPARM_LEN + 1] = {};
482 
483 	if (!sclp_ipl_info.is_valid)
484 		return sprintf(page, "#unknown#\n");
485 	memcpy(loadparm, &sclp_ipl_info.loadparm, LOADPARM_LEN);
486 	EBCASC(loadparm, LOADPARM_LEN);
487 	strim(loadparm);
488 	return sprintf(page, "%s\n", loadparm);
489 }
490 
491 static struct kobj_attribute sys_ipl_ccw_loadparm_attr =
492 	__ATTR(loadparm, 0444, ipl_ccw_loadparm_show, NULL);
493 
494 static struct attribute *ipl_fcp_attrs[] = {
495 	&sys_ipl_type_attr.attr,
496 	&sys_ipl_device_attr.attr,
497 	&sys_ipl_fcp_wwpn_attr.attr,
498 	&sys_ipl_fcp_lun_attr.attr,
499 	&sys_ipl_fcp_bootprog_attr.attr,
500 	&sys_ipl_fcp_br_lba_attr.attr,
501 	&sys_ipl_ccw_loadparm_attr.attr,
502 	NULL,
503 };
504 
505 static struct attribute_group ipl_fcp_attr_group = {
506 	.attrs = ipl_fcp_attrs,
507 	.bin_attrs = ipl_fcp_bin_attrs,
508 };
509 
510 /* CCW ipl device attributes */
511 
512 static struct attribute *ipl_ccw_attrs_vm[] = {
513 	&sys_ipl_type_attr.attr,
514 	&sys_ipl_device_attr.attr,
515 	&sys_ipl_ccw_loadparm_attr.attr,
516 	&sys_ipl_vm_parm_attr.attr,
517 	NULL,
518 };
519 
520 static struct attribute *ipl_ccw_attrs_lpar[] = {
521 	&sys_ipl_type_attr.attr,
522 	&sys_ipl_device_attr.attr,
523 	&sys_ipl_ccw_loadparm_attr.attr,
524 	NULL,
525 };
526 
527 static struct attribute_group ipl_ccw_attr_group_vm = {
528 	.attrs = ipl_ccw_attrs_vm,
529 };
530 
531 static struct attribute_group ipl_ccw_attr_group_lpar = {
532 	.attrs = ipl_ccw_attrs_lpar
533 };
534 
535 /* NSS ipl device attributes */
536 
537 DEFINE_IPL_ATTR_RO(ipl_nss, name, "%s\n", kernel_nss_name);
538 
539 static struct attribute *ipl_nss_attrs[] = {
540 	&sys_ipl_type_attr.attr,
541 	&sys_ipl_nss_name_attr.attr,
542 	&sys_ipl_ccw_loadparm_attr.attr,
543 	&sys_ipl_vm_parm_attr.attr,
544 	NULL,
545 };
546 
547 static struct attribute_group ipl_nss_attr_group = {
548 	.attrs = ipl_nss_attrs,
549 };
550 
551 /* UNKNOWN ipl device attributes */
552 
553 static struct attribute *ipl_unknown_attrs[] = {
554 	&sys_ipl_type_attr.attr,
555 	NULL,
556 };
557 
558 static struct attribute_group ipl_unknown_attr_group = {
559 	.attrs = ipl_unknown_attrs,
560 };
561 
562 static struct kset *ipl_kset;
563 
564 static void __ipl_run(void *unused)
565 {
566 	diag308(DIAG308_LOAD_CLEAR, NULL);
567 	if (MACHINE_IS_VM)
568 		__cpcmd("IPL", NULL, 0, NULL);
569 	else if (ipl_info.type == IPL_TYPE_CCW)
570 		reipl_ccw_dev(&ipl_info.data.ccw.dev_id);
571 }
572 
573 static void ipl_run(struct shutdown_trigger *trigger)
574 {
575 	smp_call_ipl_cpu(__ipl_run, NULL);
576 }
577 
578 static int __init ipl_init(void)
579 {
580 	int rc;
581 
582 	ipl_kset = kset_create_and_add("ipl", NULL, firmware_kobj);
583 	if (!ipl_kset) {
584 		rc = -ENOMEM;
585 		goto out;
586 	}
587 	switch (ipl_info.type) {
588 	case IPL_TYPE_CCW:
589 		if (MACHINE_IS_VM)
590 			rc = sysfs_create_group(&ipl_kset->kobj,
591 						&ipl_ccw_attr_group_vm);
592 		else
593 			rc = sysfs_create_group(&ipl_kset->kobj,
594 						&ipl_ccw_attr_group_lpar);
595 		break;
596 	case IPL_TYPE_FCP:
597 	case IPL_TYPE_FCP_DUMP:
598 		rc = sysfs_create_group(&ipl_kset->kobj, &ipl_fcp_attr_group);
599 		break;
600 	case IPL_TYPE_NSS:
601 		rc = sysfs_create_group(&ipl_kset->kobj, &ipl_nss_attr_group);
602 		break;
603 	default:
604 		rc = sysfs_create_group(&ipl_kset->kobj,
605 					&ipl_unknown_attr_group);
606 		break;
607 	}
608 out:
609 	if (rc)
610 		panic("ipl_init failed: rc = %i\n", rc);
611 
612 	return 0;
613 }
614 
615 static struct shutdown_action __refdata ipl_action = {
616 	.name	= SHUTDOWN_ACTION_IPL_STR,
617 	.fn	= ipl_run,
618 	.init	= ipl_init,
619 };
620 
621 /*
622  * reipl shutdown action: Reboot Linux on shutdown.
623  */
624 
625 /* VM IPL PARM attributes */
626 static ssize_t reipl_generic_vmparm_show(struct ipl_parameter_block *ipb,
627 					  char *page)
628 {
629 	char vmparm[DIAG308_VMPARM_SIZE + 1] = {};
630 
631 	reipl_get_ascii_vmparm(vmparm, sizeof(vmparm), ipb);
632 	return sprintf(page, "%s\n", vmparm);
633 }
634 
635 static ssize_t reipl_generic_vmparm_store(struct ipl_parameter_block *ipb,
636 					  size_t vmparm_max,
637 					  const char *buf, size_t len)
638 {
639 	int i, ip_len;
640 
641 	/* ignore trailing newline */
642 	ip_len = len;
643 	if ((len > 0) && (buf[len - 1] == '\n'))
644 		ip_len--;
645 
646 	if (ip_len > vmparm_max)
647 		return -EINVAL;
648 
649 	/* parm is used to store kernel options, check for common chars */
650 	for (i = 0; i < ip_len; i++)
651 		if (!(isalnum(buf[i]) || isascii(buf[i]) || isprint(buf[i])))
652 			return -EINVAL;
653 
654 	memset(ipb->ipl_info.ccw.vm_parm, 0, DIAG308_VMPARM_SIZE);
655 	ipb->ipl_info.ccw.vm_parm_len = ip_len;
656 	if (ip_len > 0) {
657 		ipb->ipl_info.ccw.vm_flags |= DIAG308_VM_FLAGS_VP_VALID;
658 		memcpy(ipb->ipl_info.ccw.vm_parm, buf, ip_len);
659 		ASCEBC(ipb->ipl_info.ccw.vm_parm, ip_len);
660 	} else {
661 		ipb->ipl_info.ccw.vm_flags &= ~DIAG308_VM_FLAGS_VP_VALID;
662 	}
663 
664 	return len;
665 }
666 
667 /* NSS wrapper */
668 static ssize_t reipl_nss_vmparm_show(struct kobject *kobj,
669 				     struct kobj_attribute *attr, char *page)
670 {
671 	return reipl_generic_vmparm_show(reipl_block_nss, page);
672 }
673 
674 static ssize_t reipl_nss_vmparm_store(struct kobject *kobj,
675 				      struct kobj_attribute *attr,
676 				      const char *buf, size_t len)
677 {
678 	return reipl_generic_vmparm_store(reipl_block_nss, 56, buf, len);
679 }
680 
681 /* CCW wrapper */
682 static ssize_t reipl_ccw_vmparm_show(struct kobject *kobj,
683 				     struct kobj_attribute *attr, char *page)
684 {
685 	return reipl_generic_vmparm_show(reipl_block_ccw, page);
686 }
687 
688 static ssize_t reipl_ccw_vmparm_store(struct kobject *kobj,
689 				      struct kobj_attribute *attr,
690 				      const char *buf, size_t len)
691 {
692 	return reipl_generic_vmparm_store(reipl_block_ccw, 64, buf, len);
693 }
694 
695 static struct kobj_attribute sys_reipl_nss_vmparm_attr =
696 	__ATTR(parm, S_IRUGO | S_IWUSR, reipl_nss_vmparm_show,
697 					reipl_nss_vmparm_store);
698 static struct kobj_attribute sys_reipl_ccw_vmparm_attr =
699 	__ATTR(parm, S_IRUGO | S_IWUSR, reipl_ccw_vmparm_show,
700 					reipl_ccw_vmparm_store);
701 
702 /* FCP reipl device attributes */
703 
704 static ssize_t reipl_fcp_scpdata_read(struct file *filp, struct kobject *kobj,
705 				      struct bin_attribute *attr,
706 				      char *buf, loff_t off, size_t count)
707 {
708 	size_t size = reipl_block_fcp->ipl_info.fcp.scp_data_len;
709 	void *scp_data = reipl_block_fcp->ipl_info.fcp.scp_data;
710 
711 	return memory_read_from_buffer(buf, count, &off, scp_data, size);
712 }
713 
714 static ssize_t reipl_fcp_scpdata_write(struct file *filp, struct kobject *kobj,
715 				       struct bin_attribute *attr,
716 				       char *buf, loff_t off, size_t count)
717 {
718 	size_t scpdata_len = count;
719 	size_t padding;
720 
721 
722 	if (off)
723 		return -EINVAL;
724 
725 	memcpy(reipl_block_fcp->ipl_info.fcp.scp_data, buf, count);
726 	if (scpdata_len % 8) {
727 		padding = 8 - (scpdata_len % 8);
728 		memset(reipl_block_fcp->ipl_info.fcp.scp_data + scpdata_len,
729 		       0, padding);
730 		scpdata_len += padding;
731 	}
732 
733 	reipl_block_fcp->ipl_info.fcp.scp_data_len = scpdata_len;
734 	reipl_block_fcp->hdr.len = IPL_PARM_BLK_FCP_LEN + scpdata_len;
735 	reipl_block_fcp->hdr.blk0_len = IPL_PARM_BLK0_FCP_LEN + scpdata_len;
736 
737 	return count;
738 }
739 static struct bin_attribute sys_reipl_fcp_scp_data_attr =
740 	__BIN_ATTR(scp_data, (S_IRUGO | S_IWUSR), reipl_fcp_scpdata_read,
741 		   reipl_fcp_scpdata_write, DIAG308_SCPDATA_SIZE);
742 
743 static struct bin_attribute *reipl_fcp_bin_attrs[] = {
744 	&sys_reipl_fcp_scp_data_attr,
745 	NULL,
746 };
747 
748 DEFINE_IPL_ATTR_RW(reipl_fcp, wwpn, "0x%016llx\n", "%llx\n",
749 		   reipl_block_fcp->ipl_info.fcp.wwpn);
750 DEFINE_IPL_ATTR_RW(reipl_fcp, lun, "0x%016llx\n", "%llx\n",
751 		   reipl_block_fcp->ipl_info.fcp.lun);
752 DEFINE_IPL_ATTR_RW(reipl_fcp, bootprog, "%lld\n", "%lld\n",
753 		   reipl_block_fcp->ipl_info.fcp.bootprog);
754 DEFINE_IPL_ATTR_RW(reipl_fcp, br_lba, "%lld\n", "%lld\n",
755 		   reipl_block_fcp->ipl_info.fcp.br_lba);
756 DEFINE_IPL_ATTR_RW(reipl_fcp, device, "0.0.%04llx\n", "0.0.%llx\n",
757 		   reipl_block_fcp->ipl_info.fcp.devno);
758 
759 static void reipl_get_ascii_loadparm(char *loadparm,
760 				     struct ipl_parameter_block *ibp)
761 {
762 	memcpy(loadparm, ibp->hdr.loadparm, LOADPARM_LEN);
763 	EBCASC(loadparm, LOADPARM_LEN);
764 	loadparm[LOADPARM_LEN] = 0;
765 	strim(loadparm);
766 }
767 
768 static ssize_t reipl_generic_loadparm_show(struct ipl_parameter_block *ipb,
769 					   char *page)
770 {
771 	char buf[LOADPARM_LEN + 1];
772 
773 	reipl_get_ascii_loadparm(buf, ipb);
774 	return sprintf(page, "%s\n", buf);
775 }
776 
777 static ssize_t reipl_generic_loadparm_store(struct ipl_parameter_block *ipb,
778 					    const char *buf, size_t len)
779 {
780 	int i, lp_len;
781 
782 	/* ignore trailing newline */
783 	lp_len = len;
784 	if ((len > 0) && (buf[len - 1] == '\n'))
785 		lp_len--;
786 	/* loadparm can have max 8 characters and must not start with a blank */
787 	if ((lp_len > LOADPARM_LEN) || ((lp_len > 0) && (buf[0] == ' ')))
788 		return -EINVAL;
789 	/* loadparm can only contain "a-z,A-Z,0-9,SP,." */
790 	for (i = 0; i < lp_len; i++) {
791 		if (isalpha(buf[i]) || isdigit(buf[i]) || (buf[i] == ' ') ||
792 		    (buf[i] == '.'))
793 			continue;
794 		return -EINVAL;
795 	}
796 	/* initialize loadparm with blanks */
797 	memset(ipb->hdr.loadparm, ' ', LOADPARM_LEN);
798 	/* copy and convert to ebcdic */
799 	memcpy(ipb->hdr.loadparm, buf, lp_len);
800 	ASCEBC(ipb->hdr.loadparm, LOADPARM_LEN);
801 	return len;
802 }
803 
804 /* FCP wrapper */
805 static ssize_t reipl_fcp_loadparm_show(struct kobject *kobj,
806 				       struct kobj_attribute *attr, char *page)
807 {
808 	return reipl_generic_loadparm_show(reipl_block_fcp, page);
809 }
810 
811 static ssize_t reipl_fcp_loadparm_store(struct kobject *kobj,
812 					struct kobj_attribute *attr,
813 					const char *buf, size_t len)
814 {
815 	return reipl_generic_loadparm_store(reipl_block_fcp, buf, len);
816 }
817 
818 static struct kobj_attribute sys_reipl_fcp_loadparm_attr =
819 	__ATTR(loadparm, S_IRUGO | S_IWUSR, reipl_fcp_loadparm_show,
820 					    reipl_fcp_loadparm_store);
821 
822 static struct attribute *reipl_fcp_attrs[] = {
823 	&sys_reipl_fcp_device_attr.attr,
824 	&sys_reipl_fcp_wwpn_attr.attr,
825 	&sys_reipl_fcp_lun_attr.attr,
826 	&sys_reipl_fcp_bootprog_attr.attr,
827 	&sys_reipl_fcp_br_lba_attr.attr,
828 	&sys_reipl_fcp_loadparm_attr.attr,
829 	NULL,
830 };
831 
832 static struct attribute_group reipl_fcp_attr_group = {
833 	.attrs = reipl_fcp_attrs,
834 	.bin_attrs = reipl_fcp_bin_attrs,
835 };
836 
837 /* CCW reipl device attributes */
838 DEFINE_IPL_CCW_ATTR_RW(reipl_ccw, device, reipl_block_ccw->ipl_info.ccw);
839 
840 /* NSS wrapper */
841 static ssize_t reipl_nss_loadparm_show(struct kobject *kobj,
842 				       struct kobj_attribute *attr, char *page)
843 {
844 	return reipl_generic_loadparm_show(reipl_block_nss, page);
845 }
846 
847 static ssize_t reipl_nss_loadparm_store(struct kobject *kobj,
848 					struct kobj_attribute *attr,
849 					const char *buf, size_t len)
850 {
851 	return reipl_generic_loadparm_store(reipl_block_nss, buf, len);
852 }
853 
854 /* CCW wrapper */
855 static ssize_t reipl_ccw_loadparm_show(struct kobject *kobj,
856 				       struct kobj_attribute *attr, char *page)
857 {
858 	return reipl_generic_loadparm_show(reipl_block_ccw, page);
859 }
860 
861 static ssize_t reipl_ccw_loadparm_store(struct kobject *kobj,
862 					struct kobj_attribute *attr,
863 					const char *buf, size_t len)
864 {
865 	return reipl_generic_loadparm_store(reipl_block_ccw, buf, len);
866 }
867 
868 static struct kobj_attribute sys_reipl_ccw_loadparm_attr =
869 	__ATTR(loadparm, S_IRUGO | S_IWUSR, reipl_ccw_loadparm_show,
870 					    reipl_ccw_loadparm_store);
871 
872 static struct attribute *reipl_ccw_attrs_vm[] = {
873 	&sys_reipl_ccw_device_attr.attr,
874 	&sys_reipl_ccw_loadparm_attr.attr,
875 	&sys_reipl_ccw_vmparm_attr.attr,
876 	NULL,
877 };
878 
879 static struct attribute *reipl_ccw_attrs_lpar[] = {
880 	&sys_reipl_ccw_device_attr.attr,
881 	&sys_reipl_ccw_loadparm_attr.attr,
882 	NULL,
883 };
884 
885 static struct attribute_group reipl_ccw_attr_group_vm = {
886 	.name  = IPL_CCW_STR,
887 	.attrs = reipl_ccw_attrs_vm,
888 };
889 
890 static struct attribute_group reipl_ccw_attr_group_lpar = {
891 	.name  = IPL_CCW_STR,
892 	.attrs = reipl_ccw_attrs_lpar,
893 };
894 
895 
896 /* NSS reipl device attributes */
897 static void reipl_get_ascii_nss_name(char *dst,
898 				     struct ipl_parameter_block *ipb)
899 {
900 	memcpy(dst, ipb->ipl_info.ccw.nss_name, NSS_NAME_SIZE);
901 	EBCASC(dst, NSS_NAME_SIZE);
902 	dst[NSS_NAME_SIZE] = 0;
903 }
904 
905 static ssize_t reipl_nss_name_show(struct kobject *kobj,
906 				   struct kobj_attribute *attr, char *page)
907 {
908 	char nss_name[NSS_NAME_SIZE + 1] = {};
909 
910 	reipl_get_ascii_nss_name(nss_name, reipl_block_nss);
911 	return sprintf(page, "%s\n", nss_name);
912 }
913 
914 static ssize_t reipl_nss_name_store(struct kobject *kobj,
915 				    struct kobj_attribute *attr,
916 				    const char *buf, size_t len)
917 {
918 	int nss_len;
919 
920 	/* ignore trailing newline */
921 	nss_len = len;
922 	if ((len > 0) && (buf[len - 1] == '\n'))
923 		nss_len--;
924 
925 	if (nss_len > NSS_NAME_SIZE)
926 		return -EINVAL;
927 
928 	memset(reipl_block_nss->ipl_info.ccw.nss_name, 0x40, NSS_NAME_SIZE);
929 	if (nss_len > 0) {
930 		reipl_block_nss->ipl_info.ccw.vm_flags |=
931 			DIAG308_VM_FLAGS_NSS_VALID;
932 		memcpy(reipl_block_nss->ipl_info.ccw.nss_name, buf, nss_len);
933 		ASCEBC(reipl_block_nss->ipl_info.ccw.nss_name, nss_len);
934 		EBC_TOUPPER(reipl_block_nss->ipl_info.ccw.nss_name, nss_len);
935 	} else {
936 		reipl_block_nss->ipl_info.ccw.vm_flags &=
937 			~DIAG308_VM_FLAGS_NSS_VALID;
938 	}
939 
940 	return len;
941 }
942 
943 static struct kobj_attribute sys_reipl_nss_name_attr =
944 	__ATTR(name, S_IRUGO | S_IWUSR, reipl_nss_name_show,
945 					reipl_nss_name_store);
946 
947 static struct kobj_attribute sys_reipl_nss_loadparm_attr =
948 	__ATTR(loadparm, S_IRUGO | S_IWUSR, reipl_nss_loadparm_show,
949 					    reipl_nss_loadparm_store);
950 
951 static struct attribute *reipl_nss_attrs[] = {
952 	&sys_reipl_nss_name_attr.attr,
953 	&sys_reipl_nss_loadparm_attr.attr,
954 	&sys_reipl_nss_vmparm_attr.attr,
955 	NULL,
956 };
957 
958 static struct attribute_group reipl_nss_attr_group = {
959 	.name  = IPL_NSS_STR,
960 	.attrs = reipl_nss_attrs,
961 };
962 
963 static void set_reipl_block_actual(struct ipl_parameter_block *reipl_block)
964 {
965 	reipl_block_actual = reipl_block;
966 	os_info_entry_add(OS_INFO_REIPL_BLOCK, reipl_block_actual,
967 			  reipl_block->hdr.len);
968 }
969 
970 /* reipl type */
971 
972 static int reipl_set_type(enum ipl_type type)
973 {
974 	if (!(reipl_capabilities & type))
975 		return -EINVAL;
976 
977 	switch(type) {
978 	case IPL_TYPE_CCW:
979 		if (diag308_set_works)
980 			reipl_method = REIPL_METHOD_CCW_DIAG;
981 		else if (MACHINE_IS_VM)
982 			reipl_method = REIPL_METHOD_CCW_VM;
983 		else
984 			reipl_method = REIPL_METHOD_CCW_CIO;
985 		set_reipl_block_actual(reipl_block_ccw);
986 		break;
987 	case IPL_TYPE_FCP:
988 		if (diag308_set_works)
989 			reipl_method = REIPL_METHOD_FCP_RW_DIAG;
990 		else if (MACHINE_IS_VM)
991 			reipl_method = REIPL_METHOD_FCP_RO_VM;
992 		else
993 			reipl_method = REIPL_METHOD_FCP_RO_DIAG;
994 		set_reipl_block_actual(reipl_block_fcp);
995 		break;
996 	case IPL_TYPE_FCP_DUMP:
997 		reipl_method = REIPL_METHOD_FCP_DUMP;
998 		break;
999 	case IPL_TYPE_NSS:
1000 		if (diag308_set_works)
1001 			reipl_method = REIPL_METHOD_NSS_DIAG;
1002 		else
1003 			reipl_method = REIPL_METHOD_NSS;
1004 		set_reipl_block_actual(reipl_block_nss);
1005 		break;
1006 	case IPL_TYPE_UNKNOWN:
1007 		reipl_method = REIPL_METHOD_DEFAULT;
1008 		break;
1009 	default:
1010 		BUG();
1011 	}
1012 	reipl_type = type;
1013 	return 0;
1014 }
1015 
1016 static ssize_t reipl_type_show(struct kobject *kobj,
1017 			       struct kobj_attribute *attr, char *page)
1018 {
1019 	return sprintf(page, "%s\n", ipl_type_str(reipl_type));
1020 }
1021 
1022 static ssize_t reipl_type_store(struct kobject *kobj,
1023 				struct kobj_attribute *attr,
1024 				const char *buf, size_t len)
1025 {
1026 	int rc = -EINVAL;
1027 
1028 	if (strncmp(buf, IPL_CCW_STR, strlen(IPL_CCW_STR)) == 0)
1029 		rc = reipl_set_type(IPL_TYPE_CCW);
1030 	else if (strncmp(buf, IPL_FCP_STR, strlen(IPL_FCP_STR)) == 0)
1031 		rc = reipl_set_type(IPL_TYPE_FCP);
1032 	else if (strncmp(buf, IPL_NSS_STR, strlen(IPL_NSS_STR)) == 0)
1033 		rc = reipl_set_type(IPL_TYPE_NSS);
1034 	return (rc != 0) ? rc : len;
1035 }
1036 
1037 static struct kobj_attribute reipl_type_attr =
1038 	__ATTR(reipl_type, 0644, reipl_type_show, reipl_type_store);
1039 
1040 static struct kset *reipl_kset;
1041 static struct kset *reipl_fcp_kset;
1042 
1043 static void get_ipl_string(char *dst, struct ipl_parameter_block *ipb,
1044 			   const enum ipl_method m)
1045 {
1046 	char loadparm[LOADPARM_LEN + 1] = {};
1047 	char vmparm[DIAG308_VMPARM_SIZE + 1] = {};
1048 	char nss_name[NSS_NAME_SIZE + 1] = {};
1049 	size_t pos = 0;
1050 
1051 	reipl_get_ascii_loadparm(loadparm, ipb);
1052 	reipl_get_ascii_nss_name(nss_name, ipb);
1053 	reipl_get_ascii_vmparm(vmparm, sizeof(vmparm), ipb);
1054 
1055 	switch (m) {
1056 	case REIPL_METHOD_CCW_VM:
1057 		pos = sprintf(dst, "IPL %X CLEAR", ipb->ipl_info.ccw.devno);
1058 		break;
1059 	case REIPL_METHOD_NSS:
1060 		pos = sprintf(dst, "IPL %s", nss_name);
1061 		break;
1062 	default:
1063 		break;
1064 	}
1065 	if (strlen(loadparm) > 0)
1066 		pos += sprintf(dst + pos, " LOADPARM '%s'", loadparm);
1067 	if (strlen(vmparm) > 0)
1068 		sprintf(dst + pos, " PARM %s", vmparm);
1069 }
1070 
1071 static void __reipl_run(void *unused)
1072 {
1073 	struct ccw_dev_id devid;
1074 	static char buf[128];
1075 
1076 	switch (reipl_method) {
1077 	case REIPL_METHOD_CCW_CIO:
1078 		devid.ssid  = reipl_block_ccw->ipl_info.ccw.ssid;
1079 		devid.devno = reipl_block_ccw->ipl_info.ccw.devno;
1080 		reipl_ccw_dev(&devid);
1081 		break;
1082 	case REIPL_METHOD_CCW_VM:
1083 		get_ipl_string(buf, reipl_block_ccw, REIPL_METHOD_CCW_VM);
1084 		__cpcmd(buf, NULL, 0, NULL);
1085 		break;
1086 	case REIPL_METHOD_CCW_DIAG:
1087 		diag308(DIAG308_SET, reipl_block_ccw);
1088 		if (MACHINE_IS_LPAR)
1089 			diag308(DIAG308_LOAD_NORMAL_DUMP, NULL);
1090 		else
1091 			diag308(DIAG308_LOAD_CLEAR, NULL);
1092 		break;
1093 	case REIPL_METHOD_FCP_RW_DIAG:
1094 		diag308(DIAG308_SET, reipl_block_fcp);
1095 		diag308(DIAG308_LOAD_CLEAR, NULL);
1096 		break;
1097 	case REIPL_METHOD_FCP_RO_DIAG:
1098 		diag308(DIAG308_LOAD_CLEAR, NULL);
1099 		break;
1100 	case REIPL_METHOD_FCP_RO_VM:
1101 		__cpcmd("IPL", NULL, 0, NULL);
1102 		break;
1103 	case REIPL_METHOD_NSS_DIAG:
1104 		diag308(DIAG308_SET, reipl_block_nss);
1105 		diag308(DIAG308_LOAD_CLEAR, NULL);
1106 		break;
1107 	case REIPL_METHOD_NSS:
1108 		get_ipl_string(buf, reipl_block_nss, REIPL_METHOD_NSS);
1109 		__cpcmd(buf, NULL, 0, NULL);
1110 		break;
1111 	case REIPL_METHOD_DEFAULT:
1112 		if (MACHINE_IS_VM)
1113 			__cpcmd("IPL", NULL, 0, NULL);
1114 		diag308(DIAG308_LOAD_CLEAR, NULL);
1115 		break;
1116 	case REIPL_METHOD_FCP_DUMP:
1117 		break;
1118 	}
1119 	disabled_wait((unsigned long) __builtin_return_address(0));
1120 }
1121 
1122 static void reipl_run(struct shutdown_trigger *trigger)
1123 {
1124 	smp_call_ipl_cpu(__reipl_run, NULL);
1125 }
1126 
1127 static void reipl_block_ccw_init(struct ipl_parameter_block *ipb)
1128 {
1129 	ipb->hdr.len = IPL_PARM_BLK_CCW_LEN;
1130 	ipb->hdr.version = IPL_PARM_BLOCK_VERSION;
1131 	ipb->hdr.blk0_len = IPL_PARM_BLK0_CCW_LEN;
1132 	ipb->hdr.pbt = DIAG308_IPL_TYPE_CCW;
1133 }
1134 
1135 static void reipl_block_ccw_fill_parms(struct ipl_parameter_block *ipb)
1136 {
1137 	/* LOADPARM */
1138 	/* check if read scp info worked and set loadparm */
1139 	if (sclp_ipl_info.is_valid)
1140 		memcpy(ipb->hdr.loadparm, &sclp_ipl_info.loadparm, LOADPARM_LEN);
1141 	else
1142 		/* read scp info failed: set empty loadparm (EBCDIC blanks) */
1143 		memset(ipb->hdr.loadparm, 0x40, LOADPARM_LEN);
1144 	ipb->hdr.flags = DIAG308_FLAGS_LP_VALID;
1145 
1146 	/* VM PARM */
1147 	if (MACHINE_IS_VM && diag308_set_works &&
1148 	    (ipl_block.ipl_info.ccw.vm_flags & DIAG308_VM_FLAGS_VP_VALID)) {
1149 
1150 		ipb->ipl_info.ccw.vm_flags |= DIAG308_VM_FLAGS_VP_VALID;
1151 		ipb->ipl_info.ccw.vm_parm_len =
1152 					ipl_block.ipl_info.ccw.vm_parm_len;
1153 		memcpy(ipb->ipl_info.ccw.vm_parm,
1154 		       ipl_block.ipl_info.ccw.vm_parm, DIAG308_VMPARM_SIZE);
1155 	}
1156 }
1157 
1158 static int __init reipl_nss_init(void)
1159 {
1160 	int rc;
1161 
1162 	if (!MACHINE_IS_VM)
1163 		return 0;
1164 
1165 	reipl_block_nss = (void *) get_zeroed_page(GFP_KERNEL);
1166 	if (!reipl_block_nss)
1167 		return -ENOMEM;
1168 
1169 	if (!diag308_set_works)
1170 		sys_reipl_nss_vmparm_attr.attr.mode = S_IRUGO;
1171 
1172 	rc = sysfs_create_group(&reipl_kset->kobj, &reipl_nss_attr_group);
1173 	if (rc)
1174 		return rc;
1175 
1176 	reipl_block_ccw_init(reipl_block_nss);
1177 	if (ipl_info.type == IPL_TYPE_NSS) {
1178 		memset(reipl_block_nss->ipl_info.ccw.nss_name,
1179 			' ', NSS_NAME_SIZE);
1180 		memcpy(reipl_block_nss->ipl_info.ccw.nss_name,
1181 			kernel_nss_name, strlen(kernel_nss_name));
1182 		ASCEBC(reipl_block_nss->ipl_info.ccw.nss_name, NSS_NAME_SIZE);
1183 		reipl_block_nss->ipl_info.ccw.vm_flags |=
1184 			DIAG308_VM_FLAGS_NSS_VALID;
1185 
1186 		reipl_block_ccw_fill_parms(reipl_block_nss);
1187 	}
1188 
1189 	reipl_capabilities |= IPL_TYPE_NSS;
1190 	return 0;
1191 }
1192 
1193 static int __init reipl_ccw_init(void)
1194 {
1195 	int rc;
1196 
1197 	reipl_block_ccw = (void *) get_zeroed_page(GFP_KERNEL);
1198 	if (!reipl_block_ccw)
1199 		return -ENOMEM;
1200 
1201 	if (MACHINE_IS_VM) {
1202 		if (!diag308_set_works)
1203 			sys_reipl_ccw_vmparm_attr.attr.mode = S_IRUGO;
1204 		rc = sysfs_create_group(&reipl_kset->kobj,
1205 					&reipl_ccw_attr_group_vm);
1206 	} else {
1207 		if(!diag308_set_works)
1208 			sys_reipl_ccw_loadparm_attr.attr.mode = S_IRUGO;
1209 		rc = sysfs_create_group(&reipl_kset->kobj,
1210 					&reipl_ccw_attr_group_lpar);
1211 	}
1212 	if (rc)
1213 		return rc;
1214 
1215 	reipl_block_ccw_init(reipl_block_ccw);
1216 	if (ipl_info.type == IPL_TYPE_CCW) {
1217 		reipl_block_ccw->ipl_info.ccw.ssid = ipl_ssid;
1218 		reipl_block_ccw->ipl_info.ccw.devno = ipl_devno;
1219 		reipl_block_ccw_fill_parms(reipl_block_ccw);
1220 	}
1221 
1222 	reipl_capabilities |= IPL_TYPE_CCW;
1223 	return 0;
1224 }
1225 
1226 static int __init reipl_fcp_init(void)
1227 {
1228 	int rc;
1229 
1230 	if (!diag308_set_works) {
1231 		if (ipl_info.type == IPL_TYPE_FCP) {
1232 			make_attrs_ro(reipl_fcp_attrs);
1233 			sys_reipl_fcp_scp_data_attr.attr.mode = S_IRUGO;
1234 		} else
1235 			return 0;
1236 	}
1237 
1238 	reipl_block_fcp = (void *) get_zeroed_page(GFP_KERNEL);
1239 	if (!reipl_block_fcp)
1240 		return -ENOMEM;
1241 
1242 	/* sysfs: create fcp kset for mixing attr group and bin attrs */
1243 	reipl_fcp_kset = kset_create_and_add(IPL_FCP_STR, NULL,
1244 					     &reipl_kset->kobj);
1245 	if (!reipl_fcp_kset) {
1246 		free_page((unsigned long) reipl_block_fcp);
1247 		return -ENOMEM;
1248 	}
1249 
1250 	rc = sysfs_create_group(&reipl_fcp_kset->kobj, &reipl_fcp_attr_group);
1251 	if (rc) {
1252 		kset_unregister(reipl_fcp_kset);
1253 		free_page((unsigned long) reipl_block_fcp);
1254 		return rc;
1255 	}
1256 
1257 	if (ipl_info.type == IPL_TYPE_FCP) {
1258 		memcpy(reipl_block_fcp, IPL_PARMBLOCK_START, PAGE_SIZE);
1259 		/*
1260 		 * Fix loadparm: There are systems where the (SCSI) LOADPARM
1261 		 * is invalid in the SCSI IPL parameter block, so take it
1262 		 * always from sclp_ipl_info.
1263 		 */
1264 		memcpy(reipl_block_fcp->hdr.loadparm, sclp_ipl_info.loadparm,
1265 		       LOADPARM_LEN);
1266 	} else {
1267 		reipl_block_fcp->hdr.len = IPL_PARM_BLK_FCP_LEN;
1268 		reipl_block_fcp->hdr.version = IPL_PARM_BLOCK_VERSION;
1269 		reipl_block_fcp->hdr.blk0_len = IPL_PARM_BLK0_FCP_LEN;
1270 		reipl_block_fcp->hdr.pbt = DIAG308_IPL_TYPE_FCP;
1271 		reipl_block_fcp->ipl_info.fcp.opt = DIAG308_IPL_OPT_IPL;
1272 	}
1273 	reipl_capabilities |= IPL_TYPE_FCP;
1274 	return 0;
1275 }
1276 
1277 static int __init reipl_type_init(void)
1278 {
1279 	enum ipl_type reipl_type = ipl_info.type;
1280 	struct ipl_parameter_block *reipl_block;
1281 	unsigned long size;
1282 
1283 	reipl_block = os_info_old_entry(OS_INFO_REIPL_BLOCK, &size);
1284 	if (!reipl_block)
1285 		goto out;
1286 	/*
1287 	 * If we have an OS info reipl block, this will be used
1288 	 */
1289 	if (reipl_block->hdr.pbt == DIAG308_IPL_TYPE_FCP) {
1290 		memcpy(reipl_block_fcp, reipl_block, size);
1291 		reipl_type = IPL_TYPE_FCP;
1292 	} else if (reipl_block->hdr.pbt == DIAG308_IPL_TYPE_CCW) {
1293 		memcpy(reipl_block_ccw, reipl_block, size);
1294 		reipl_type = IPL_TYPE_CCW;
1295 	}
1296 out:
1297 	return reipl_set_type(reipl_type);
1298 }
1299 
1300 static int __init reipl_init(void)
1301 {
1302 	int rc;
1303 
1304 	reipl_kset = kset_create_and_add("reipl", NULL, firmware_kobj);
1305 	if (!reipl_kset)
1306 		return -ENOMEM;
1307 	rc = sysfs_create_file(&reipl_kset->kobj, &reipl_type_attr.attr);
1308 	if (rc) {
1309 		kset_unregister(reipl_kset);
1310 		return rc;
1311 	}
1312 	rc = reipl_ccw_init();
1313 	if (rc)
1314 		return rc;
1315 	rc = reipl_fcp_init();
1316 	if (rc)
1317 		return rc;
1318 	rc = reipl_nss_init();
1319 	if (rc)
1320 		return rc;
1321 	return reipl_type_init();
1322 }
1323 
1324 static struct shutdown_action __refdata reipl_action = {
1325 	.name	= SHUTDOWN_ACTION_REIPL_STR,
1326 	.fn	= reipl_run,
1327 	.init	= reipl_init,
1328 };
1329 
1330 /*
1331  * dump shutdown action: Dump Linux on shutdown.
1332  */
1333 
1334 /* FCP dump device attributes */
1335 
1336 DEFINE_IPL_ATTR_RW(dump_fcp, wwpn, "0x%016llx\n", "%llx\n",
1337 		   dump_block_fcp->ipl_info.fcp.wwpn);
1338 DEFINE_IPL_ATTR_RW(dump_fcp, lun, "0x%016llx\n", "%llx\n",
1339 		   dump_block_fcp->ipl_info.fcp.lun);
1340 DEFINE_IPL_ATTR_RW(dump_fcp, bootprog, "%lld\n", "%lld\n",
1341 		   dump_block_fcp->ipl_info.fcp.bootprog);
1342 DEFINE_IPL_ATTR_RW(dump_fcp, br_lba, "%lld\n", "%lld\n",
1343 		   dump_block_fcp->ipl_info.fcp.br_lba);
1344 DEFINE_IPL_ATTR_RW(dump_fcp, device, "0.0.%04llx\n", "0.0.%llx\n",
1345 		   dump_block_fcp->ipl_info.fcp.devno);
1346 
1347 static struct attribute *dump_fcp_attrs[] = {
1348 	&sys_dump_fcp_device_attr.attr,
1349 	&sys_dump_fcp_wwpn_attr.attr,
1350 	&sys_dump_fcp_lun_attr.attr,
1351 	&sys_dump_fcp_bootprog_attr.attr,
1352 	&sys_dump_fcp_br_lba_attr.attr,
1353 	NULL,
1354 };
1355 
1356 static struct attribute_group dump_fcp_attr_group = {
1357 	.name  = IPL_FCP_STR,
1358 	.attrs = dump_fcp_attrs,
1359 };
1360 
1361 /* CCW dump device attributes */
1362 DEFINE_IPL_CCW_ATTR_RW(dump_ccw, device, dump_block_ccw->ipl_info.ccw);
1363 
1364 static struct attribute *dump_ccw_attrs[] = {
1365 	&sys_dump_ccw_device_attr.attr,
1366 	NULL,
1367 };
1368 
1369 static struct attribute_group dump_ccw_attr_group = {
1370 	.name  = IPL_CCW_STR,
1371 	.attrs = dump_ccw_attrs,
1372 };
1373 
1374 /* dump type */
1375 
1376 static int dump_set_type(enum dump_type type)
1377 {
1378 	if (!(dump_capabilities & type))
1379 		return -EINVAL;
1380 	switch (type) {
1381 	case DUMP_TYPE_CCW:
1382 		if (diag308_set_works)
1383 			dump_method = DUMP_METHOD_CCW_DIAG;
1384 		else if (MACHINE_IS_VM)
1385 			dump_method = DUMP_METHOD_CCW_VM;
1386 		else
1387 			dump_method = DUMP_METHOD_CCW_CIO;
1388 		break;
1389 	case DUMP_TYPE_FCP:
1390 		dump_method = DUMP_METHOD_FCP_DIAG;
1391 		break;
1392 	default:
1393 		dump_method = DUMP_METHOD_NONE;
1394 	}
1395 	dump_type = type;
1396 	return 0;
1397 }
1398 
1399 static ssize_t dump_type_show(struct kobject *kobj,
1400 			      struct kobj_attribute *attr, char *page)
1401 {
1402 	return sprintf(page, "%s\n", dump_type_str(dump_type));
1403 }
1404 
1405 static ssize_t dump_type_store(struct kobject *kobj,
1406 			       struct kobj_attribute *attr,
1407 			       const char *buf, size_t len)
1408 {
1409 	int rc = -EINVAL;
1410 
1411 	if (strncmp(buf, DUMP_NONE_STR, strlen(DUMP_NONE_STR)) == 0)
1412 		rc = dump_set_type(DUMP_TYPE_NONE);
1413 	else if (strncmp(buf, DUMP_CCW_STR, strlen(DUMP_CCW_STR)) == 0)
1414 		rc = dump_set_type(DUMP_TYPE_CCW);
1415 	else if (strncmp(buf, DUMP_FCP_STR, strlen(DUMP_FCP_STR)) == 0)
1416 		rc = dump_set_type(DUMP_TYPE_FCP);
1417 	return (rc != 0) ? rc : len;
1418 }
1419 
1420 static struct kobj_attribute dump_type_attr =
1421 	__ATTR(dump_type, 0644, dump_type_show, dump_type_store);
1422 
1423 static struct kset *dump_kset;
1424 
1425 static void diag308_dump(void *dump_block)
1426 {
1427 	diag308(DIAG308_SET, dump_block);
1428 	while (1) {
1429 		if (diag308(DIAG308_LOAD_NORMAL_DUMP, NULL) != 0x302)
1430 			break;
1431 		udelay_simple(USEC_PER_SEC);
1432 	}
1433 }
1434 
1435 static void __dump_run(void *unused)
1436 {
1437 	struct ccw_dev_id devid;
1438 	static char buf[100];
1439 
1440 	switch (dump_method) {
1441 	case DUMP_METHOD_CCW_CIO:
1442 		devid.ssid  = dump_block_ccw->ipl_info.ccw.ssid;
1443 		devid.devno = dump_block_ccw->ipl_info.ccw.devno;
1444 		reipl_ccw_dev(&devid);
1445 		break;
1446 	case DUMP_METHOD_CCW_VM:
1447 		sprintf(buf, "STORE STATUS");
1448 		__cpcmd(buf, NULL, 0, NULL);
1449 		sprintf(buf, "IPL %X", dump_block_ccw->ipl_info.ccw.devno);
1450 		__cpcmd(buf, NULL, 0, NULL);
1451 		break;
1452 	case DUMP_METHOD_CCW_DIAG:
1453 		diag308_dump(dump_block_ccw);
1454 		break;
1455 	case DUMP_METHOD_FCP_DIAG:
1456 		diag308_dump(dump_block_fcp);
1457 		break;
1458 	default:
1459 		break;
1460 	}
1461 }
1462 
1463 static void dump_run(struct shutdown_trigger *trigger)
1464 {
1465 	if (dump_method == DUMP_METHOD_NONE)
1466 		return;
1467 	smp_send_stop();
1468 	smp_call_ipl_cpu(__dump_run, NULL);
1469 }
1470 
1471 static int __init dump_ccw_init(void)
1472 {
1473 	int rc;
1474 
1475 	dump_block_ccw = (void *) get_zeroed_page(GFP_KERNEL);
1476 	if (!dump_block_ccw)
1477 		return -ENOMEM;
1478 	rc = sysfs_create_group(&dump_kset->kobj, &dump_ccw_attr_group);
1479 	if (rc) {
1480 		free_page((unsigned long)dump_block_ccw);
1481 		return rc;
1482 	}
1483 	dump_block_ccw->hdr.len = IPL_PARM_BLK_CCW_LEN;
1484 	dump_block_ccw->hdr.version = IPL_PARM_BLOCK_VERSION;
1485 	dump_block_ccw->hdr.blk0_len = IPL_PARM_BLK0_CCW_LEN;
1486 	dump_block_ccw->hdr.pbt = DIAG308_IPL_TYPE_CCW;
1487 	dump_capabilities |= DUMP_TYPE_CCW;
1488 	return 0;
1489 }
1490 
1491 static int __init dump_fcp_init(void)
1492 {
1493 	int rc;
1494 
1495 	if (!sclp_ipl_info.has_dump)
1496 		return 0; /* LDIPL DUMP is not installed */
1497 	if (!diag308_set_works)
1498 		return 0;
1499 	dump_block_fcp = (void *) get_zeroed_page(GFP_KERNEL);
1500 	if (!dump_block_fcp)
1501 		return -ENOMEM;
1502 	rc = sysfs_create_group(&dump_kset->kobj, &dump_fcp_attr_group);
1503 	if (rc) {
1504 		free_page((unsigned long)dump_block_fcp);
1505 		return rc;
1506 	}
1507 	dump_block_fcp->hdr.len = IPL_PARM_BLK_FCP_LEN;
1508 	dump_block_fcp->hdr.version = IPL_PARM_BLOCK_VERSION;
1509 	dump_block_fcp->hdr.blk0_len = IPL_PARM_BLK0_FCP_LEN;
1510 	dump_block_fcp->hdr.pbt = DIAG308_IPL_TYPE_FCP;
1511 	dump_block_fcp->ipl_info.fcp.opt = DIAG308_IPL_OPT_DUMP;
1512 	dump_capabilities |= DUMP_TYPE_FCP;
1513 	return 0;
1514 }
1515 
1516 static int __init dump_init(void)
1517 {
1518 	int rc;
1519 
1520 	dump_kset = kset_create_and_add("dump", NULL, firmware_kobj);
1521 	if (!dump_kset)
1522 		return -ENOMEM;
1523 	rc = sysfs_create_file(&dump_kset->kobj, &dump_type_attr.attr);
1524 	if (rc) {
1525 		kset_unregister(dump_kset);
1526 		return rc;
1527 	}
1528 	rc = dump_ccw_init();
1529 	if (rc)
1530 		return rc;
1531 	rc = dump_fcp_init();
1532 	if (rc)
1533 		return rc;
1534 	dump_set_type(DUMP_TYPE_NONE);
1535 	return 0;
1536 }
1537 
1538 static struct shutdown_action __refdata dump_action = {
1539 	.name	= SHUTDOWN_ACTION_DUMP_STR,
1540 	.fn	= dump_run,
1541 	.init	= dump_init,
1542 };
1543 
1544 static void dump_reipl_run(struct shutdown_trigger *trigger)
1545 {
1546 	unsigned long ipib = (unsigned long) reipl_block_actual;
1547 	unsigned int csum;
1548 
1549 	csum = csum_partial(reipl_block_actual, reipl_block_actual->hdr.len, 0);
1550 	mem_assign_absolute(S390_lowcore.ipib, ipib);
1551 	mem_assign_absolute(S390_lowcore.ipib_checksum, csum);
1552 	dump_run(trigger);
1553 }
1554 
1555 static int __init dump_reipl_init(void)
1556 {
1557 	if (!diag308_set_works)
1558 		return -EOPNOTSUPP;
1559 	else
1560 		return 0;
1561 }
1562 
1563 static struct shutdown_action __refdata dump_reipl_action = {
1564 	.name	= SHUTDOWN_ACTION_DUMP_REIPL_STR,
1565 	.fn	= dump_reipl_run,
1566 	.init	= dump_reipl_init,
1567 };
1568 
1569 /*
1570  * vmcmd shutdown action: Trigger vm command on shutdown.
1571  */
1572 
1573 static char vmcmd_on_reboot[128];
1574 static char vmcmd_on_panic[128];
1575 static char vmcmd_on_halt[128];
1576 static char vmcmd_on_poff[128];
1577 static char vmcmd_on_restart[128];
1578 
1579 DEFINE_IPL_ATTR_STR_RW(vmcmd, on_reboot, "%s\n", "%s\n", vmcmd_on_reboot);
1580 DEFINE_IPL_ATTR_STR_RW(vmcmd, on_panic, "%s\n", "%s\n", vmcmd_on_panic);
1581 DEFINE_IPL_ATTR_STR_RW(vmcmd, on_halt, "%s\n", "%s\n", vmcmd_on_halt);
1582 DEFINE_IPL_ATTR_STR_RW(vmcmd, on_poff, "%s\n", "%s\n", vmcmd_on_poff);
1583 DEFINE_IPL_ATTR_STR_RW(vmcmd, on_restart, "%s\n", "%s\n", vmcmd_on_restart);
1584 
1585 static struct attribute *vmcmd_attrs[] = {
1586 	&sys_vmcmd_on_reboot_attr.attr,
1587 	&sys_vmcmd_on_panic_attr.attr,
1588 	&sys_vmcmd_on_halt_attr.attr,
1589 	&sys_vmcmd_on_poff_attr.attr,
1590 	&sys_vmcmd_on_restart_attr.attr,
1591 	NULL,
1592 };
1593 
1594 static struct attribute_group vmcmd_attr_group = {
1595 	.attrs = vmcmd_attrs,
1596 };
1597 
1598 static struct kset *vmcmd_kset;
1599 
1600 static void vmcmd_run(struct shutdown_trigger *trigger)
1601 {
1602 	char *cmd;
1603 
1604 	if (strcmp(trigger->name, ON_REIPL_STR) == 0)
1605 		cmd = vmcmd_on_reboot;
1606 	else if (strcmp(trigger->name, ON_PANIC_STR) == 0)
1607 		cmd = vmcmd_on_panic;
1608 	else if (strcmp(trigger->name, ON_HALT_STR) == 0)
1609 		cmd = vmcmd_on_halt;
1610 	else if (strcmp(trigger->name, ON_POFF_STR) == 0)
1611 		cmd = vmcmd_on_poff;
1612 	else if (strcmp(trigger->name, ON_RESTART_STR) == 0)
1613 		cmd = vmcmd_on_restart;
1614 	else
1615 		return;
1616 
1617 	if (strlen(cmd) == 0)
1618 		return;
1619 	__cpcmd(cmd, NULL, 0, NULL);
1620 }
1621 
1622 static int vmcmd_init(void)
1623 {
1624 	if (!MACHINE_IS_VM)
1625 		return -EOPNOTSUPP;
1626 	vmcmd_kset = kset_create_and_add("vmcmd", NULL, firmware_kobj);
1627 	if (!vmcmd_kset)
1628 		return -ENOMEM;
1629 	return sysfs_create_group(&vmcmd_kset->kobj, &vmcmd_attr_group);
1630 }
1631 
1632 static struct shutdown_action vmcmd_action = {SHUTDOWN_ACTION_VMCMD_STR,
1633 					      vmcmd_run, vmcmd_init};
1634 
1635 /*
1636  * stop shutdown action: Stop Linux on shutdown.
1637  */
1638 
1639 static void stop_run(struct shutdown_trigger *trigger)
1640 {
1641 	if (strcmp(trigger->name, ON_PANIC_STR) == 0 ||
1642 	    strcmp(trigger->name, ON_RESTART_STR) == 0)
1643 		disabled_wait((unsigned long) __builtin_return_address(0));
1644 	smp_stop_cpu();
1645 }
1646 
1647 static struct shutdown_action stop_action = {SHUTDOWN_ACTION_STOP_STR,
1648 					     stop_run, NULL};
1649 
1650 /* action list */
1651 
1652 static struct shutdown_action *shutdown_actions_list[] = {
1653 	&ipl_action, &reipl_action, &dump_reipl_action, &dump_action,
1654 	&vmcmd_action, &stop_action};
1655 #define SHUTDOWN_ACTIONS_COUNT (sizeof(shutdown_actions_list) / sizeof(void *))
1656 
1657 /*
1658  * Trigger section
1659  */
1660 
1661 static struct kset *shutdown_actions_kset;
1662 
1663 static int set_trigger(const char *buf, struct shutdown_trigger *trigger,
1664 		       size_t len)
1665 {
1666 	int i;
1667 
1668 	for (i = 0; i < SHUTDOWN_ACTIONS_COUNT; i++) {
1669 		if (sysfs_streq(buf, shutdown_actions_list[i]->name)) {
1670 			if (shutdown_actions_list[i]->init_rc) {
1671 				return shutdown_actions_list[i]->init_rc;
1672 			} else {
1673 				trigger->action = shutdown_actions_list[i];
1674 				return len;
1675 			}
1676 		}
1677 	}
1678 	return -EINVAL;
1679 }
1680 
1681 /* on reipl */
1682 
1683 static struct shutdown_trigger on_reboot_trigger = {ON_REIPL_STR,
1684 						    &reipl_action};
1685 
1686 static ssize_t on_reboot_show(struct kobject *kobj,
1687 			      struct kobj_attribute *attr, char *page)
1688 {
1689 	return sprintf(page, "%s\n", on_reboot_trigger.action->name);
1690 }
1691 
1692 static ssize_t on_reboot_store(struct kobject *kobj,
1693 			       struct kobj_attribute *attr,
1694 			       const char *buf, size_t len)
1695 {
1696 	return set_trigger(buf, &on_reboot_trigger, len);
1697 }
1698 static struct kobj_attribute on_reboot_attr = __ATTR_RW(on_reboot);
1699 
1700 static void do_machine_restart(char *__unused)
1701 {
1702 	smp_send_stop();
1703 	on_reboot_trigger.action->fn(&on_reboot_trigger);
1704 	reipl_run(NULL);
1705 }
1706 void (*_machine_restart)(char *command) = do_machine_restart;
1707 
1708 /* on panic */
1709 
1710 static struct shutdown_trigger on_panic_trigger = {ON_PANIC_STR, &stop_action};
1711 
1712 static ssize_t on_panic_show(struct kobject *kobj,
1713 			     struct kobj_attribute *attr, char *page)
1714 {
1715 	return sprintf(page, "%s\n", on_panic_trigger.action->name);
1716 }
1717 
1718 static ssize_t on_panic_store(struct kobject *kobj,
1719 			      struct kobj_attribute *attr,
1720 			      const char *buf, size_t len)
1721 {
1722 	return set_trigger(buf, &on_panic_trigger, len);
1723 }
1724 static struct kobj_attribute on_panic_attr = __ATTR_RW(on_panic);
1725 
1726 static void do_panic(void)
1727 {
1728 	lgr_info_log();
1729 	on_panic_trigger.action->fn(&on_panic_trigger);
1730 	stop_run(&on_panic_trigger);
1731 }
1732 
1733 /* on restart */
1734 
1735 static struct shutdown_trigger on_restart_trigger = {ON_RESTART_STR,
1736 	&stop_action};
1737 
1738 static ssize_t on_restart_show(struct kobject *kobj,
1739 			       struct kobj_attribute *attr, char *page)
1740 {
1741 	return sprintf(page, "%s\n", on_restart_trigger.action->name);
1742 }
1743 
1744 static ssize_t on_restart_store(struct kobject *kobj,
1745 				struct kobj_attribute *attr,
1746 				const char *buf, size_t len)
1747 {
1748 	return set_trigger(buf, &on_restart_trigger, len);
1749 }
1750 static struct kobj_attribute on_restart_attr = __ATTR_RW(on_restart);
1751 
1752 static void __do_restart(void *ignore)
1753 {
1754 	__arch_local_irq_stosm(0x04); /* enable DAT */
1755 	smp_send_stop();
1756 #ifdef CONFIG_CRASH_DUMP
1757 	crash_kexec(NULL);
1758 #endif
1759 	on_restart_trigger.action->fn(&on_restart_trigger);
1760 	stop_run(&on_restart_trigger);
1761 }
1762 
1763 void do_restart(void)
1764 {
1765 	tracing_off();
1766 	debug_locks_off();
1767 	lgr_info_log();
1768 	smp_call_online_cpu(__do_restart, NULL);
1769 }
1770 
1771 /* on halt */
1772 
1773 static struct shutdown_trigger on_halt_trigger = {ON_HALT_STR, &stop_action};
1774 
1775 static ssize_t on_halt_show(struct kobject *kobj,
1776 			    struct kobj_attribute *attr, char *page)
1777 {
1778 	return sprintf(page, "%s\n", on_halt_trigger.action->name);
1779 }
1780 
1781 static ssize_t on_halt_store(struct kobject *kobj,
1782 			     struct kobj_attribute *attr,
1783 			     const char *buf, size_t len)
1784 {
1785 	return set_trigger(buf, &on_halt_trigger, len);
1786 }
1787 static struct kobj_attribute on_halt_attr = __ATTR_RW(on_halt);
1788 
1789 static void do_machine_halt(void)
1790 {
1791 	smp_send_stop();
1792 	on_halt_trigger.action->fn(&on_halt_trigger);
1793 	stop_run(&on_halt_trigger);
1794 }
1795 void (*_machine_halt)(void) = do_machine_halt;
1796 
1797 /* on power off */
1798 
1799 static struct shutdown_trigger on_poff_trigger = {ON_POFF_STR, &stop_action};
1800 
1801 static ssize_t on_poff_show(struct kobject *kobj,
1802 			    struct kobj_attribute *attr, char *page)
1803 {
1804 	return sprintf(page, "%s\n", on_poff_trigger.action->name);
1805 }
1806 
1807 static ssize_t on_poff_store(struct kobject *kobj,
1808 			     struct kobj_attribute *attr,
1809 			     const char *buf, size_t len)
1810 {
1811 	return set_trigger(buf, &on_poff_trigger, len);
1812 }
1813 static struct kobj_attribute on_poff_attr = __ATTR_RW(on_poff);
1814 
1815 static void do_machine_power_off(void)
1816 {
1817 	smp_send_stop();
1818 	on_poff_trigger.action->fn(&on_poff_trigger);
1819 	stop_run(&on_poff_trigger);
1820 }
1821 void (*_machine_power_off)(void) = do_machine_power_off;
1822 
1823 static struct attribute *shutdown_action_attrs[] = {
1824 	&on_restart_attr.attr,
1825 	&on_reboot_attr.attr,
1826 	&on_panic_attr.attr,
1827 	&on_halt_attr.attr,
1828 	&on_poff_attr.attr,
1829 	NULL,
1830 };
1831 
1832 static struct attribute_group shutdown_action_attr_group = {
1833 	.attrs = shutdown_action_attrs,
1834 };
1835 
1836 static void __init shutdown_triggers_init(void)
1837 {
1838 	shutdown_actions_kset = kset_create_and_add("shutdown_actions", NULL,
1839 						    firmware_kobj);
1840 	if (!shutdown_actions_kset)
1841 		goto fail;
1842 	if (sysfs_create_group(&shutdown_actions_kset->kobj,
1843 			       &shutdown_action_attr_group))
1844 		goto fail;
1845 	return;
1846 fail:
1847 	panic("shutdown_triggers_init failed\n");
1848 }
1849 
1850 static void __init shutdown_actions_init(void)
1851 {
1852 	int i;
1853 
1854 	for (i = 0; i < SHUTDOWN_ACTIONS_COUNT; i++) {
1855 		if (!shutdown_actions_list[i]->init)
1856 			continue;
1857 		shutdown_actions_list[i]->init_rc =
1858 			shutdown_actions_list[i]->init();
1859 	}
1860 }
1861 
1862 static int __init s390_ipl_init(void)
1863 {
1864 	char str[8] = {0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40};
1865 
1866 	sclp_get_ipl_info(&sclp_ipl_info);
1867 	/*
1868 	 * Fix loadparm: There are systems where the (SCSI) LOADPARM
1869 	 * returned by read SCP info is invalid (contains EBCDIC blanks)
1870 	 * when the system has been booted via diag308. In that case we use
1871 	 * the value from diag308, if available.
1872 	 *
1873 	 * There are also systems where diag308 store does not work in
1874 	 * case the system is booted from HMC. Fortunately in this case
1875 	 * READ SCP info provides the correct value.
1876 	 */
1877 	if (memcmp(sclp_ipl_info.loadparm, str, sizeof(str)) == 0 &&
1878 	    diag308_set_works)
1879 		memcpy(sclp_ipl_info.loadparm, ipl_block.hdr.loadparm,
1880 		       LOADPARM_LEN);
1881 	shutdown_actions_init();
1882 	shutdown_triggers_init();
1883 	return 0;
1884 }
1885 
1886 __initcall(s390_ipl_init);
1887 
1888 static void __init strncpy_skip_quote(char *dst, char *src, int n)
1889 {
1890 	int sx, dx;
1891 
1892 	dx = 0;
1893 	for (sx = 0; src[sx] != 0; sx++) {
1894 		if (src[sx] == '"')
1895 			continue;
1896 		dst[dx++] = src[sx];
1897 		if (dx >= n)
1898 			break;
1899 	}
1900 }
1901 
1902 static int __init vmcmd_on_reboot_setup(char *str)
1903 {
1904 	if (!MACHINE_IS_VM)
1905 		return 1;
1906 	strncpy_skip_quote(vmcmd_on_reboot, str, 127);
1907 	vmcmd_on_reboot[127] = 0;
1908 	on_reboot_trigger.action = &vmcmd_action;
1909 	return 1;
1910 }
1911 __setup("vmreboot=", vmcmd_on_reboot_setup);
1912 
1913 static int __init vmcmd_on_panic_setup(char *str)
1914 {
1915 	if (!MACHINE_IS_VM)
1916 		return 1;
1917 	strncpy_skip_quote(vmcmd_on_panic, str, 127);
1918 	vmcmd_on_panic[127] = 0;
1919 	on_panic_trigger.action = &vmcmd_action;
1920 	return 1;
1921 }
1922 __setup("vmpanic=", vmcmd_on_panic_setup);
1923 
1924 static int __init vmcmd_on_halt_setup(char *str)
1925 {
1926 	if (!MACHINE_IS_VM)
1927 		return 1;
1928 	strncpy_skip_quote(vmcmd_on_halt, str, 127);
1929 	vmcmd_on_halt[127] = 0;
1930 	on_halt_trigger.action = &vmcmd_action;
1931 	return 1;
1932 }
1933 __setup("vmhalt=", vmcmd_on_halt_setup);
1934 
1935 static int __init vmcmd_on_poff_setup(char *str)
1936 {
1937 	if (!MACHINE_IS_VM)
1938 		return 1;
1939 	strncpy_skip_quote(vmcmd_on_poff, str, 127);
1940 	vmcmd_on_poff[127] = 0;
1941 	on_poff_trigger.action = &vmcmd_action;
1942 	return 1;
1943 }
1944 __setup("vmpoff=", vmcmd_on_poff_setup);
1945 
1946 static int on_panic_notify(struct notifier_block *self,
1947 			   unsigned long event, void *data)
1948 {
1949 	do_panic();
1950 	return NOTIFY_OK;
1951 }
1952 
1953 static struct notifier_block on_panic_nb = {
1954 	.notifier_call = on_panic_notify,
1955 	.priority = INT_MIN,
1956 };
1957 
1958 void __init setup_ipl(void)
1959 {
1960 	ipl_info.type = get_ipl_type();
1961 	switch (ipl_info.type) {
1962 	case IPL_TYPE_CCW:
1963 		ipl_info.data.ccw.dev_id.ssid = ipl_ssid;
1964 		ipl_info.data.ccw.dev_id.devno = ipl_devno;
1965 		break;
1966 	case IPL_TYPE_FCP:
1967 	case IPL_TYPE_FCP_DUMP:
1968 		ipl_info.data.fcp.dev_id.ssid = 0;
1969 		ipl_info.data.fcp.dev_id.devno =
1970 			IPL_PARMBLOCK_START->ipl_info.fcp.devno;
1971 		ipl_info.data.fcp.wwpn = IPL_PARMBLOCK_START->ipl_info.fcp.wwpn;
1972 		ipl_info.data.fcp.lun = IPL_PARMBLOCK_START->ipl_info.fcp.lun;
1973 		break;
1974 	case IPL_TYPE_NSS:
1975 		strncpy(ipl_info.data.nss.name, kernel_nss_name,
1976 			sizeof(ipl_info.data.nss.name));
1977 		break;
1978 	case IPL_TYPE_UNKNOWN:
1979 		/* We have no info to copy */
1980 		break;
1981 	}
1982 	atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
1983 }
1984 
1985 void __init ipl_update_parameters(void)
1986 {
1987 	int rc;
1988 
1989 	rc = diag308(DIAG308_STORE, &ipl_block);
1990 	if ((rc == DIAG308_RC_OK) || (rc == DIAG308_RC_NOCONFIG))
1991 		diag308_set_works = 1;
1992 }
1993 
1994 void __init ipl_verify_parameters(void)
1995 {
1996 	struct cio_iplinfo iplinfo;
1997 
1998 	if (cio_get_iplinfo(&iplinfo))
1999 		return;
2000 
2001 	ipl_ssid = iplinfo.ssid;
2002 	ipl_devno = iplinfo.devno;
2003 	ipl_flags |= IPL_DEVNO_VALID;
2004 	if (!iplinfo.is_qdio)
2005 		return;
2006 	ipl_flags |= IPL_PARMBLOCK_VALID;
2007 }
2008 
2009 static LIST_HEAD(rcall);
2010 static DEFINE_MUTEX(rcall_mutex);
2011 
2012 void register_reset_call(struct reset_call *reset)
2013 {
2014 	mutex_lock(&rcall_mutex);
2015 	list_add(&reset->list, &rcall);
2016 	mutex_unlock(&rcall_mutex);
2017 }
2018 EXPORT_SYMBOL_GPL(register_reset_call);
2019 
2020 void unregister_reset_call(struct reset_call *reset)
2021 {
2022 	mutex_lock(&rcall_mutex);
2023 	list_del(&reset->list);
2024 	mutex_unlock(&rcall_mutex);
2025 }
2026 EXPORT_SYMBOL_GPL(unregister_reset_call);
2027 
2028 static void do_reset_calls(void)
2029 {
2030 	struct reset_call *reset;
2031 
2032 	if (diag308_set_works) {
2033 		diag308_reset();
2034 		return;
2035 	}
2036 	list_for_each_entry(reset, &rcall, list)
2037 		reset->fn();
2038 }
2039 
2040 void s390_reset_system(void)
2041 {
2042 	struct lowcore *lc;
2043 
2044 	lc = (struct lowcore *)(unsigned long) store_prefix();
2045 
2046 	/* Stack for interrupt/machine check handler */
2047 	lc->panic_stack = S390_lowcore.panic_stack;
2048 
2049 	/* Disable prefixing */
2050 	set_prefix(0);
2051 
2052 	/* Disable lowcore protection */
2053 	__ctl_clear_bit(0,28);
2054 
2055 	/* Set new machine check handler */
2056 	S390_lowcore.mcck_new_psw.mask = PSW_KERNEL_BITS | PSW_MASK_DAT;
2057 	S390_lowcore.mcck_new_psw.addr =
2058 		(unsigned long) s390_base_mcck_handler;
2059 
2060 	/* Set new program check handler */
2061 	S390_lowcore.program_new_psw.mask = PSW_KERNEL_BITS | PSW_MASK_DAT;
2062 	S390_lowcore.program_new_psw.addr =
2063 		(unsigned long) s390_base_pgm_handler;
2064 
2065 	do_reset_calls();
2066 }
2067