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