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