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