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