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