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