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