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