gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / arch / s390 / kernel / ipl.c
blob4a71061974fdbe6a0beb8eaf13d0ef7f1ca2d6ed
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * ipl/reipl/dump support for Linux on s390.
5 * Copyright IBM Corp. 2005, 2012
6 * Author(s): Michael Holzheu <holzheu@de.ibm.com>
7 * Heiko Carstens <heiko.carstens@de.ibm.com>
8 * Volker Sameske <sameske@de.ibm.com>
9 */
11 #include <linux/types.h>
12 #include <linux/export.h>
13 #include <linux/init.h>
14 #include <linux/device.h>
15 #include <linux/delay.h>
16 #include <linux/reboot.h>
17 #include <linux/ctype.h>
18 #include <linux/fs.h>
19 #include <linux/gfp.h>
20 #include <linux/crash_dump.h>
21 #include <linux/debug_locks.h>
22 #include <asm/diag.h>
23 #include <asm/ipl.h>
24 #include <asm/smp.h>
25 #include <asm/setup.h>
26 #include <asm/cpcmd.h>
27 #include <asm/ebcdic.h>
28 #include <asm/sclp.h>
29 #include <asm/checksum.h>
30 #include <asm/debug.h>
31 #include <asm/os_info.h>
32 #include <asm/sections.h>
33 #include <asm/boot_data.h>
34 #include "entry.h"
36 #define IPL_PARM_BLOCK_VERSION 0
38 #define IPL_UNKNOWN_STR "unknown"
39 #define IPL_CCW_STR "ccw"
40 #define IPL_FCP_STR "fcp"
41 #define IPL_FCP_DUMP_STR "fcp_dump"
42 #define IPL_NSS_STR "nss"
44 #define DUMP_CCW_STR "ccw"
45 #define DUMP_FCP_STR "fcp"
46 #define DUMP_NONE_STR "none"
49 * Four shutdown trigger types are supported:
50 * - panic
51 * - halt
52 * - power off
53 * - reipl
54 * - restart
56 #define ON_PANIC_STR "on_panic"
57 #define ON_HALT_STR "on_halt"
58 #define ON_POFF_STR "on_poff"
59 #define ON_REIPL_STR "on_reboot"
60 #define ON_RESTART_STR "on_restart"
62 struct shutdown_action;
63 struct shutdown_trigger {
64 char *name;
65 struct shutdown_action *action;
69 * The following shutdown action types are supported:
71 #define SHUTDOWN_ACTION_IPL_STR "ipl"
72 #define SHUTDOWN_ACTION_REIPL_STR "reipl"
73 #define SHUTDOWN_ACTION_DUMP_STR "dump"
74 #define SHUTDOWN_ACTION_VMCMD_STR "vmcmd"
75 #define SHUTDOWN_ACTION_STOP_STR "stop"
76 #define SHUTDOWN_ACTION_DUMP_REIPL_STR "dump_reipl"
78 struct shutdown_action {
79 char *name;
80 void (*fn) (struct shutdown_trigger *trigger);
81 int (*init) (void);
82 int init_rc;
85 static char *ipl_type_str(enum ipl_type type)
87 switch (type) {
88 case IPL_TYPE_CCW:
89 return IPL_CCW_STR;
90 case IPL_TYPE_FCP:
91 return IPL_FCP_STR;
92 case IPL_TYPE_FCP_DUMP:
93 return IPL_FCP_DUMP_STR;
94 case IPL_TYPE_NSS:
95 return IPL_NSS_STR;
96 case IPL_TYPE_UNKNOWN:
97 default:
98 return IPL_UNKNOWN_STR;
102 enum dump_type {
103 DUMP_TYPE_NONE = 1,
104 DUMP_TYPE_CCW = 2,
105 DUMP_TYPE_FCP = 4,
108 static char *dump_type_str(enum dump_type type)
110 switch (type) {
111 case DUMP_TYPE_NONE:
112 return DUMP_NONE_STR;
113 case DUMP_TYPE_CCW:
114 return DUMP_CCW_STR;
115 case DUMP_TYPE_FCP:
116 return DUMP_FCP_STR;
117 default:
118 return NULL;
122 int __bootdata_preserved(ipl_block_valid);
123 struct ipl_parameter_block __bootdata_preserved(ipl_block);
124 int __bootdata_preserved(ipl_secure_flag);
126 unsigned long __bootdata_preserved(ipl_cert_list_addr);
127 unsigned long __bootdata_preserved(ipl_cert_list_size);
129 unsigned long __bootdata(early_ipl_comp_list_addr);
130 unsigned long __bootdata(early_ipl_comp_list_size);
132 static int reipl_capabilities = IPL_TYPE_UNKNOWN;
134 static enum ipl_type reipl_type = IPL_TYPE_UNKNOWN;
135 static struct ipl_parameter_block *reipl_block_fcp;
136 static struct ipl_parameter_block *reipl_block_ccw;
137 static struct ipl_parameter_block *reipl_block_nss;
138 static struct ipl_parameter_block *reipl_block_actual;
140 static int dump_capabilities = DUMP_TYPE_NONE;
141 static enum dump_type dump_type = DUMP_TYPE_NONE;
142 static struct ipl_parameter_block *dump_block_fcp;
143 static struct ipl_parameter_block *dump_block_ccw;
145 static struct sclp_ipl_info sclp_ipl_info;
147 static bool reipl_fcp_clear;
148 static bool reipl_ccw_clear;
150 static inline int __diag308(unsigned long subcode, void *addr)
152 register unsigned long _addr asm("0") = (unsigned long) addr;
153 register unsigned long _rc asm("1") = 0;
155 asm volatile(
156 " diag %0,%2,0x308\n"
157 "0: nopr %%r7\n"
158 EX_TABLE(0b,0b)
159 : "+d" (_addr), "+d" (_rc)
160 : "d" (subcode) : "cc", "memory");
161 return _rc;
164 int diag308(unsigned long subcode, void *addr)
166 if (IS_ENABLED(CONFIG_KASAN))
167 __arch_local_irq_stosm(0x04); /* enable DAT */
168 diag_stat_inc(DIAG_STAT_X308);
169 return __diag308(subcode, addr);
171 EXPORT_SYMBOL_GPL(diag308);
173 /* SYSFS */
175 #define IPL_ATTR_SHOW_FN(_prefix, _name, _format, args...) \
176 static ssize_t sys_##_prefix##_##_name##_show(struct kobject *kobj, \
177 struct kobj_attribute *attr, \
178 char *page) \
180 return snprintf(page, PAGE_SIZE, _format, ##args); \
183 #define IPL_ATTR_CCW_STORE_FN(_prefix, _name, _ipl_blk) \
184 static ssize_t sys_##_prefix##_##_name##_store(struct kobject *kobj, \
185 struct kobj_attribute *attr, \
186 const char *buf, size_t len) \
188 unsigned long long ssid, devno; \
190 if (sscanf(buf, "0.%llx.%llx\n", &ssid, &devno) != 2) \
191 return -EINVAL; \
193 if (ssid > __MAX_SSID || devno > __MAX_SUBCHANNEL) \
194 return -EINVAL; \
196 _ipl_blk.ssid = ssid; \
197 _ipl_blk.devno = devno; \
198 return len; \
201 #define DEFINE_IPL_CCW_ATTR_RW(_prefix, _name, _ipl_blk) \
202 IPL_ATTR_SHOW_FN(_prefix, _name, "0.%x.%04x\n", \
203 _ipl_blk.ssid, _ipl_blk.devno); \
204 IPL_ATTR_CCW_STORE_FN(_prefix, _name, _ipl_blk); \
205 static struct kobj_attribute sys_##_prefix##_##_name##_attr = \
206 __ATTR(_name, (S_IRUGO | S_IWUSR), \
207 sys_##_prefix##_##_name##_show, \
208 sys_##_prefix##_##_name##_store) \
210 #define DEFINE_IPL_ATTR_RO(_prefix, _name, _format, _value) \
211 IPL_ATTR_SHOW_FN(_prefix, _name, _format, _value) \
212 static struct kobj_attribute sys_##_prefix##_##_name##_attr = \
213 __ATTR(_name, S_IRUGO, sys_##_prefix##_##_name##_show, NULL)
215 #define DEFINE_IPL_ATTR_RW(_prefix, _name, _fmt_out, _fmt_in, _value) \
216 IPL_ATTR_SHOW_FN(_prefix, _name, _fmt_out, (unsigned long long) _value) \
217 static ssize_t sys_##_prefix##_##_name##_store(struct kobject *kobj, \
218 struct kobj_attribute *attr, \
219 const char *buf, size_t len) \
221 unsigned long long value; \
222 if (sscanf(buf, _fmt_in, &value) != 1) \
223 return -EINVAL; \
224 _value = value; \
225 return len; \
227 static struct kobj_attribute sys_##_prefix##_##_name##_attr = \
228 __ATTR(_name,(S_IRUGO | S_IWUSR), \
229 sys_##_prefix##_##_name##_show, \
230 sys_##_prefix##_##_name##_store)
232 #define DEFINE_IPL_ATTR_STR_RW(_prefix, _name, _fmt_out, _fmt_in, _value)\
233 IPL_ATTR_SHOW_FN(_prefix, _name, _fmt_out, _value) \
234 static ssize_t sys_##_prefix##_##_name##_store(struct kobject *kobj, \
235 struct kobj_attribute *attr, \
236 const char *buf, size_t len) \
238 strncpy(_value, buf, sizeof(_value) - 1); \
239 strim(_value); \
240 return len; \
242 static struct kobj_attribute sys_##_prefix##_##_name##_attr = \
243 __ATTR(_name,(S_IRUGO | S_IWUSR), \
244 sys_##_prefix##_##_name##_show, \
245 sys_##_prefix##_##_name##_store)
248 * ipl section
251 static __init enum ipl_type get_ipl_type(void)
253 if (!ipl_block_valid)
254 return IPL_TYPE_UNKNOWN;
256 switch (ipl_block.pb0_hdr.pbt) {
257 case IPL_PBT_CCW:
258 return IPL_TYPE_CCW;
259 case IPL_PBT_FCP:
260 if (ipl_block.fcp.opt == IPL_PB0_FCP_OPT_DUMP)
261 return IPL_TYPE_FCP_DUMP;
262 else
263 return IPL_TYPE_FCP;
265 return IPL_TYPE_UNKNOWN;
268 struct ipl_info ipl_info;
269 EXPORT_SYMBOL_GPL(ipl_info);
271 static ssize_t ipl_type_show(struct kobject *kobj, struct kobj_attribute *attr,
272 char *page)
274 return sprintf(page, "%s\n", ipl_type_str(ipl_info.type));
277 static struct kobj_attribute sys_ipl_type_attr = __ATTR_RO(ipl_type);
279 static ssize_t ipl_secure_show(struct kobject *kobj,
280 struct kobj_attribute *attr, char *page)
282 return sprintf(page, "%i\n", !!ipl_secure_flag);
285 static struct kobj_attribute sys_ipl_secure_attr =
286 __ATTR(secure, 0444, ipl_secure_show, NULL);
288 static ssize_t ipl_has_secure_show(struct kobject *kobj,
289 struct kobj_attribute *attr, char *page)
291 return sprintf(page, "%i\n", !!sclp.has_sipl);
294 static struct kobj_attribute sys_ipl_has_secure_attr =
295 __ATTR(has_secure, 0444, ipl_has_secure_show, NULL);
297 static ssize_t ipl_vm_parm_show(struct kobject *kobj,
298 struct kobj_attribute *attr, char *page)
300 char parm[DIAG308_VMPARM_SIZE + 1] = {};
302 if (ipl_block_valid && (ipl_block.pb0_hdr.pbt == IPL_PBT_CCW))
303 ipl_block_get_ascii_vmparm(parm, sizeof(parm), &ipl_block);
304 return sprintf(page, "%s\n", parm);
307 static struct kobj_attribute sys_ipl_vm_parm_attr =
308 __ATTR(parm, S_IRUGO, ipl_vm_parm_show, NULL);
310 static ssize_t sys_ipl_device_show(struct kobject *kobj,
311 struct kobj_attribute *attr, char *page)
313 switch (ipl_info.type) {
314 case IPL_TYPE_CCW:
315 return sprintf(page, "0.%x.%04x\n", ipl_block.ccw.ssid,
316 ipl_block.ccw.devno);
317 case IPL_TYPE_FCP:
318 case IPL_TYPE_FCP_DUMP:
319 return sprintf(page, "0.0.%04x\n", ipl_block.fcp.devno);
320 default:
321 return 0;
325 static struct kobj_attribute sys_ipl_device_attr =
326 __ATTR(device, S_IRUGO, sys_ipl_device_show, NULL);
328 static ssize_t ipl_parameter_read(struct file *filp, struct kobject *kobj,
329 struct bin_attribute *attr, char *buf,
330 loff_t off, size_t count)
332 return memory_read_from_buffer(buf, count, &off, &ipl_block,
333 ipl_block.hdr.len);
335 static struct bin_attribute ipl_parameter_attr =
336 __BIN_ATTR(binary_parameter, S_IRUGO, ipl_parameter_read, NULL,
337 PAGE_SIZE);
339 static ssize_t ipl_scp_data_read(struct file *filp, struct kobject *kobj,
340 struct bin_attribute *attr, char *buf,
341 loff_t off, size_t count)
343 unsigned int size = ipl_block.fcp.scp_data_len;
344 void *scp_data = &ipl_block.fcp.scp_data;
346 return memory_read_from_buffer(buf, count, &off, scp_data, size);
348 static struct bin_attribute ipl_scp_data_attr =
349 __BIN_ATTR(scp_data, S_IRUGO, ipl_scp_data_read, NULL, PAGE_SIZE);
351 static struct bin_attribute *ipl_fcp_bin_attrs[] = {
352 &ipl_parameter_attr,
353 &ipl_scp_data_attr,
354 NULL,
357 /* FCP ipl device attributes */
359 DEFINE_IPL_ATTR_RO(ipl_fcp, wwpn, "0x%016llx\n",
360 (unsigned long long)ipl_block.fcp.wwpn);
361 DEFINE_IPL_ATTR_RO(ipl_fcp, lun, "0x%016llx\n",
362 (unsigned long long)ipl_block.fcp.lun);
363 DEFINE_IPL_ATTR_RO(ipl_fcp, bootprog, "%lld\n",
364 (unsigned long long)ipl_block.fcp.bootprog);
365 DEFINE_IPL_ATTR_RO(ipl_fcp, br_lba, "%lld\n",
366 (unsigned long long)ipl_block.fcp.br_lba);
368 static ssize_t ipl_ccw_loadparm_show(struct kobject *kobj,
369 struct kobj_attribute *attr, char *page)
371 char loadparm[LOADPARM_LEN + 1] = {};
373 if (!sclp_ipl_info.is_valid)
374 return sprintf(page, "#unknown#\n");
375 memcpy(loadparm, &sclp_ipl_info.loadparm, LOADPARM_LEN);
376 EBCASC(loadparm, LOADPARM_LEN);
377 strim(loadparm);
378 return sprintf(page, "%s\n", loadparm);
381 static struct kobj_attribute sys_ipl_ccw_loadparm_attr =
382 __ATTR(loadparm, 0444, ipl_ccw_loadparm_show, NULL);
384 static struct attribute *ipl_fcp_attrs[] = {
385 &sys_ipl_type_attr.attr,
386 &sys_ipl_device_attr.attr,
387 &sys_ipl_fcp_wwpn_attr.attr,
388 &sys_ipl_fcp_lun_attr.attr,
389 &sys_ipl_fcp_bootprog_attr.attr,
390 &sys_ipl_fcp_br_lba_attr.attr,
391 &sys_ipl_ccw_loadparm_attr.attr,
392 &sys_ipl_secure_attr.attr,
393 &sys_ipl_has_secure_attr.attr,
394 NULL,
397 static struct attribute_group ipl_fcp_attr_group = {
398 .attrs = ipl_fcp_attrs,
399 .bin_attrs = ipl_fcp_bin_attrs,
402 /* CCW ipl device attributes */
404 static struct attribute *ipl_ccw_attrs_vm[] = {
405 &sys_ipl_type_attr.attr,
406 &sys_ipl_device_attr.attr,
407 &sys_ipl_ccw_loadparm_attr.attr,
408 &sys_ipl_vm_parm_attr.attr,
409 &sys_ipl_secure_attr.attr,
410 &sys_ipl_has_secure_attr.attr,
411 NULL,
414 static struct attribute *ipl_ccw_attrs_lpar[] = {
415 &sys_ipl_type_attr.attr,
416 &sys_ipl_device_attr.attr,
417 &sys_ipl_ccw_loadparm_attr.attr,
418 &sys_ipl_secure_attr.attr,
419 &sys_ipl_has_secure_attr.attr,
420 NULL,
423 static struct attribute_group ipl_ccw_attr_group_vm = {
424 .attrs = ipl_ccw_attrs_vm,
427 static struct attribute_group ipl_ccw_attr_group_lpar = {
428 .attrs = ipl_ccw_attrs_lpar
431 /* UNKNOWN ipl device attributes */
433 static struct attribute *ipl_unknown_attrs[] = {
434 &sys_ipl_type_attr.attr,
435 NULL,
438 static struct attribute_group ipl_unknown_attr_group = {
439 .attrs = ipl_unknown_attrs,
442 static struct kset *ipl_kset;
444 static void __ipl_run(void *unused)
446 __bpon();
447 diag308(DIAG308_LOAD_CLEAR, NULL);
450 static void ipl_run(struct shutdown_trigger *trigger)
452 smp_call_ipl_cpu(__ipl_run, NULL);
455 static int __init ipl_init(void)
457 int rc;
459 ipl_kset = kset_create_and_add("ipl", NULL, firmware_kobj);
460 if (!ipl_kset) {
461 rc = -ENOMEM;
462 goto out;
464 switch (ipl_info.type) {
465 case IPL_TYPE_CCW:
466 if (MACHINE_IS_VM)
467 rc = sysfs_create_group(&ipl_kset->kobj,
468 &ipl_ccw_attr_group_vm);
469 else
470 rc = sysfs_create_group(&ipl_kset->kobj,
471 &ipl_ccw_attr_group_lpar);
472 break;
473 case IPL_TYPE_FCP:
474 case IPL_TYPE_FCP_DUMP:
475 rc = sysfs_create_group(&ipl_kset->kobj, &ipl_fcp_attr_group);
476 break;
477 default:
478 rc = sysfs_create_group(&ipl_kset->kobj,
479 &ipl_unknown_attr_group);
480 break;
482 out:
483 if (rc)
484 panic("ipl_init failed: rc = %i\n", rc);
486 return 0;
489 static struct shutdown_action __refdata ipl_action = {
490 .name = SHUTDOWN_ACTION_IPL_STR,
491 .fn = ipl_run,
492 .init = ipl_init,
496 * reipl shutdown action: Reboot Linux on shutdown.
499 /* VM IPL PARM attributes */
500 static ssize_t reipl_generic_vmparm_show(struct ipl_parameter_block *ipb,
501 char *page)
503 char vmparm[DIAG308_VMPARM_SIZE + 1] = {};
505 ipl_block_get_ascii_vmparm(vmparm, sizeof(vmparm), ipb);
506 return sprintf(page, "%s\n", vmparm);
509 static ssize_t reipl_generic_vmparm_store(struct ipl_parameter_block *ipb,
510 size_t vmparm_max,
511 const char *buf, size_t len)
513 int i, ip_len;
515 /* ignore trailing newline */
516 ip_len = len;
517 if ((len > 0) && (buf[len - 1] == '\n'))
518 ip_len--;
520 if (ip_len > vmparm_max)
521 return -EINVAL;
523 /* parm is used to store kernel options, check for common chars */
524 for (i = 0; i < ip_len; i++)
525 if (!(isalnum(buf[i]) || isascii(buf[i]) || isprint(buf[i])))
526 return -EINVAL;
528 memset(ipb->ccw.vm_parm, 0, DIAG308_VMPARM_SIZE);
529 ipb->ccw.vm_parm_len = ip_len;
530 if (ip_len > 0) {
531 ipb->ccw.vm_flags |= IPL_PB0_CCW_VM_FLAG_VP;
532 memcpy(ipb->ccw.vm_parm, buf, ip_len);
533 ASCEBC(ipb->ccw.vm_parm, ip_len);
534 } else {
535 ipb->ccw.vm_flags &= ~IPL_PB0_CCW_VM_FLAG_VP;
538 return len;
541 /* NSS wrapper */
542 static ssize_t reipl_nss_vmparm_show(struct kobject *kobj,
543 struct kobj_attribute *attr, char *page)
545 return reipl_generic_vmparm_show(reipl_block_nss, page);
548 static ssize_t reipl_nss_vmparm_store(struct kobject *kobj,
549 struct kobj_attribute *attr,
550 const char *buf, size_t len)
552 return reipl_generic_vmparm_store(reipl_block_nss, 56, buf, len);
555 /* CCW wrapper */
556 static ssize_t reipl_ccw_vmparm_show(struct kobject *kobj,
557 struct kobj_attribute *attr, char *page)
559 return reipl_generic_vmparm_show(reipl_block_ccw, page);
562 static ssize_t reipl_ccw_vmparm_store(struct kobject *kobj,
563 struct kobj_attribute *attr,
564 const char *buf, size_t len)
566 return reipl_generic_vmparm_store(reipl_block_ccw, 64, buf, len);
569 static struct kobj_attribute sys_reipl_nss_vmparm_attr =
570 __ATTR(parm, S_IRUGO | S_IWUSR, reipl_nss_vmparm_show,
571 reipl_nss_vmparm_store);
572 static struct kobj_attribute sys_reipl_ccw_vmparm_attr =
573 __ATTR(parm, S_IRUGO | S_IWUSR, reipl_ccw_vmparm_show,
574 reipl_ccw_vmparm_store);
576 /* FCP reipl device attributes */
578 static ssize_t reipl_fcp_scpdata_read(struct file *filp, struct kobject *kobj,
579 struct bin_attribute *attr,
580 char *buf, loff_t off, size_t count)
582 size_t size = reipl_block_fcp->fcp.scp_data_len;
583 void *scp_data = reipl_block_fcp->fcp.scp_data;
585 return memory_read_from_buffer(buf, count, &off, scp_data, size);
588 static ssize_t reipl_fcp_scpdata_write(struct file *filp, struct kobject *kobj,
589 struct bin_attribute *attr,
590 char *buf, loff_t off, size_t count)
592 size_t scpdata_len = count;
593 size_t padding;
596 if (off)
597 return -EINVAL;
599 memcpy(reipl_block_fcp->fcp.scp_data, buf, count);
600 if (scpdata_len % 8) {
601 padding = 8 - (scpdata_len % 8);
602 memset(reipl_block_fcp->fcp.scp_data + scpdata_len,
603 0, padding);
604 scpdata_len += padding;
607 reipl_block_fcp->hdr.len = IPL_BP_FCP_LEN + scpdata_len;
608 reipl_block_fcp->fcp.len = IPL_BP0_FCP_LEN + scpdata_len;
609 reipl_block_fcp->fcp.scp_data_len = scpdata_len;
611 return count;
613 static struct bin_attribute sys_reipl_fcp_scp_data_attr =
614 __BIN_ATTR(scp_data, (S_IRUGO | S_IWUSR), reipl_fcp_scpdata_read,
615 reipl_fcp_scpdata_write, DIAG308_SCPDATA_SIZE);
617 static struct bin_attribute *reipl_fcp_bin_attrs[] = {
618 &sys_reipl_fcp_scp_data_attr,
619 NULL,
622 DEFINE_IPL_ATTR_RW(reipl_fcp, wwpn, "0x%016llx\n", "%llx\n",
623 reipl_block_fcp->fcp.wwpn);
624 DEFINE_IPL_ATTR_RW(reipl_fcp, lun, "0x%016llx\n", "%llx\n",
625 reipl_block_fcp->fcp.lun);
626 DEFINE_IPL_ATTR_RW(reipl_fcp, bootprog, "%lld\n", "%lld\n",
627 reipl_block_fcp->fcp.bootprog);
628 DEFINE_IPL_ATTR_RW(reipl_fcp, br_lba, "%lld\n", "%lld\n",
629 reipl_block_fcp->fcp.br_lba);
630 DEFINE_IPL_ATTR_RW(reipl_fcp, device, "0.0.%04llx\n", "0.0.%llx\n",
631 reipl_block_fcp->fcp.devno);
633 static void reipl_get_ascii_loadparm(char *loadparm,
634 struct ipl_parameter_block *ibp)
636 memcpy(loadparm, ibp->common.loadparm, LOADPARM_LEN);
637 EBCASC(loadparm, LOADPARM_LEN);
638 loadparm[LOADPARM_LEN] = 0;
639 strim(loadparm);
642 static ssize_t reipl_generic_loadparm_show(struct ipl_parameter_block *ipb,
643 char *page)
645 char buf[LOADPARM_LEN + 1];
647 reipl_get_ascii_loadparm(buf, ipb);
648 return sprintf(page, "%s\n", buf);
651 static ssize_t reipl_generic_loadparm_store(struct ipl_parameter_block *ipb,
652 const char *buf, size_t len)
654 int i, lp_len;
656 /* ignore trailing newline */
657 lp_len = len;
658 if ((len > 0) && (buf[len - 1] == '\n'))
659 lp_len--;
660 /* loadparm can have max 8 characters and must not start with a blank */
661 if ((lp_len > LOADPARM_LEN) || ((lp_len > 0) && (buf[0] == ' ')))
662 return -EINVAL;
663 /* loadparm can only contain "a-z,A-Z,0-9,SP,." */
664 for (i = 0; i < lp_len; i++) {
665 if (isalpha(buf[i]) || isdigit(buf[i]) || (buf[i] == ' ') ||
666 (buf[i] == '.'))
667 continue;
668 return -EINVAL;
670 /* initialize loadparm with blanks */
671 memset(ipb->common.loadparm, ' ', LOADPARM_LEN);
672 /* copy and convert to ebcdic */
673 memcpy(ipb->common.loadparm, buf, lp_len);
674 ASCEBC(ipb->common.loadparm, LOADPARM_LEN);
675 ipb->common.flags |= IPL_PB0_FLAG_LOADPARM;
676 return len;
679 /* FCP wrapper */
680 static ssize_t reipl_fcp_loadparm_show(struct kobject *kobj,
681 struct kobj_attribute *attr, char *page)
683 return reipl_generic_loadparm_show(reipl_block_fcp, page);
686 static ssize_t reipl_fcp_loadparm_store(struct kobject *kobj,
687 struct kobj_attribute *attr,
688 const char *buf, size_t len)
690 return reipl_generic_loadparm_store(reipl_block_fcp, buf, len);
693 static struct kobj_attribute sys_reipl_fcp_loadparm_attr =
694 __ATTR(loadparm, S_IRUGO | S_IWUSR, reipl_fcp_loadparm_show,
695 reipl_fcp_loadparm_store);
697 static ssize_t reipl_fcp_clear_show(struct kobject *kobj,
698 struct kobj_attribute *attr, char *page)
700 return sprintf(page, "%u\n", reipl_fcp_clear);
703 static ssize_t reipl_fcp_clear_store(struct kobject *kobj,
704 struct kobj_attribute *attr,
705 const char *buf, size_t len)
707 if (strtobool(buf, &reipl_fcp_clear) < 0)
708 return -EINVAL;
709 return len;
712 static struct attribute *reipl_fcp_attrs[] = {
713 &sys_reipl_fcp_device_attr.attr,
714 &sys_reipl_fcp_wwpn_attr.attr,
715 &sys_reipl_fcp_lun_attr.attr,
716 &sys_reipl_fcp_bootprog_attr.attr,
717 &sys_reipl_fcp_br_lba_attr.attr,
718 &sys_reipl_fcp_loadparm_attr.attr,
719 NULL,
722 static struct attribute_group reipl_fcp_attr_group = {
723 .attrs = reipl_fcp_attrs,
724 .bin_attrs = reipl_fcp_bin_attrs,
727 static struct kobj_attribute sys_reipl_fcp_clear_attr =
728 __ATTR(clear, 0644, reipl_fcp_clear_show, reipl_fcp_clear_store);
730 /* CCW reipl device attributes */
731 DEFINE_IPL_CCW_ATTR_RW(reipl_ccw, device, reipl_block_ccw->ccw);
733 /* NSS wrapper */
734 static ssize_t reipl_nss_loadparm_show(struct kobject *kobj,
735 struct kobj_attribute *attr, char *page)
737 return reipl_generic_loadparm_show(reipl_block_nss, page);
740 static ssize_t reipl_nss_loadparm_store(struct kobject *kobj,
741 struct kobj_attribute *attr,
742 const char *buf, size_t len)
744 return reipl_generic_loadparm_store(reipl_block_nss, buf, len);
747 /* CCW wrapper */
748 static ssize_t reipl_ccw_loadparm_show(struct kobject *kobj,
749 struct kobj_attribute *attr, char *page)
751 return reipl_generic_loadparm_show(reipl_block_ccw, page);
754 static ssize_t reipl_ccw_loadparm_store(struct kobject *kobj,
755 struct kobj_attribute *attr,
756 const char *buf, size_t len)
758 return reipl_generic_loadparm_store(reipl_block_ccw, buf, len);
761 static struct kobj_attribute sys_reipl_ccw_loadparm_attr =
762 __ATTR(loadparm, S_IRUGO | S_IWUSR, reipl_ccw_loadparm_show,
763 reipl_ccw_loadparm_store);
765 static ssize_t reipl_ccw_clear_show(struct kobject *kobj,
766 struct kobj_attribute *attr, char *page)
768 return sprintf(page, "%u\n", reipl_ccw_clear);
771 static ssize_t reipl_ccw_clear_store(struct kobject *kobj,
772 struct kobj_attribute *attr,
773 const char *buf, size_t len)
775 if (strtobool(buf, &reipl_ccw_clear) < 0)
776 return -EINVAL;
777 return len;
780 static struct kobj_attribute sys_reipl_ccw_clear_attr =
781 __ATTR(clear, 0644, reipl_ccw_clear_show, reipl_ccw_clear_store);
783 static struct attribute *reipl_ccw_attrs_vm[] = {
784 &sys_reipl_ccw_device_attr.attr,
785 &sys_reipl_ccw_loadparm_attr.attr,
786 &sys_reipl_ccw_vmparm_attr.attr,
787 &sys_reipl_ccw_clear_attr.attr,
788 NULL,
791 static struct attribute *reipl_ccw_attrs_lpar[] = {
792 &sys_reipl_ccw_device_attr.attr,
793 &sys_reipl_ccw_loadparm_attr.attr,
794 &sys_reipl_ccw_clear_attr.attr,
795 NULL,
798 static struct attribute_group reipl_ccw_attr_group_vm = {
799 .name = IPL_CCW_STR,
800 .attrs = reipl_ccw_attrs_vm,
803 static struct attribute_group reipl_ccw_attr_group_lpar = {
804 .name = IPL_CCW_STR,
805 .attrs = reipl_ccw_attrs_lpar,
809 /* NSS reipl device attributes */
810 static void reipl_get_ascii_nss_name(char *dst,
811 struct ipl_parameter_block *ipb)
813 memcpy(dst, ipb->ccw.nss_name, NSS_NAME_SIZE);
814 EBCASC(dst, NSS_NAME_SIZE);
815 dst[NSS_NAME_SIZE] = 0;
818 static ssize_t reipl_nss_name_show(struct kobject *kobj,
819 struct kobj_attribute *attr, char *page)
821 char nss_name[NSS_NAME_SIZE + 1] = {};
823 reipl_get_ascii_nss_name(nss_name, reipl_block_nss);
824 return sprintf(page, "%s\n", nss_name);
827 static ssize_t reipl_nss_name_store(struct kobject *kobj,
828 struct kobj_attribute *attr,
829 const char *buf, size_t len)
831 int nss_len;
833 /* ignore trailing newline */
834 nss_len = len;
835 if ((len > 0) && (buf[len - 1] == '\n'))
836 nss_len--;
838 if (nss_len > NSS_NAME_SIZE)
839 return -EINVAL;
841 memset(reipl_block_nss->ccw.nss_name, 0x40, NSS_NAME_SIZE);
842 if (nss_len > 0) {
843 reipl_block_nss->ccw.vm_flags |= IPL_PB0_CCW_VM_FLAG_NSS;
844 memcpy(reipl_block_nss->ccw.nss_name, buf, nss_len);
845 ASCEBC(reipl_block_nss->ccw.nss_name, nss_len);
846 EBC_TOUPPER(reipl_block_nss->ccw.nss_name, nss_len);
847 } else {
848 reipl_block_nss->ccw.vm_flags &= ~IPL_PB0_CCW_VM_FLAG_NSS;
851 return len;
854 static struct kobj_attribute sys_reipl_nss_name_attr =
855 __ATTR(name, S_IRUGO | S_IWUSR, reipl_nss_name_show,
856 reipl_nss_name_store);
858 static struct kobj_attribute sys_reipl_nss_loadparm_attr =
859 __ATTR(loadparm, S_IRUGO | S_IWUSR, reipl_nss_loadparm_show,
860 reipl_nss_loadparm_store);
862 static struct attribute *reipl_nss_attrs[] = {
863 &sys_reipl_nss_name_attr.attr,
864 &sys_reipl_nss_loadparm_attr.attr,
865 &sys_reipl_nss_vmparm_attr.attr,
866 NULL,
869 static struct attribute_group reipl_nss_attr_group = {
870 .name = IPL_NSS_STR,
871 .attrs = reipl_nss_attrs,
874 void set_os_info_reipl_block(void)
876 os_info_entry_add(OS_INFO_REIPL_BLOCK, reipl_block_actual,
877 reipl_block_actual->hdr.len);
880 /* reipl type */
882 static int reipl_set_type(enum ipl_type type)
884 if (!(reipl_capabilities & type))
885 return -EINVAL;
887 switch(type) {
888 case IPL_TYPE_CCW:
889 reipl_block_actual = reipl_block_ccw;
890 break;
891 case IPL_TYPE_FCP:
892 reipl_block_actual = reipl_block_fcp;
893 break;
894 case IPL_TYPE_NSS:
895 reipl_block_actual = reipl_block_nss;
896 break;
897 default:
898 break;
900 reipl_type = type;
901 return 0;
904 static ssize_t reipl_type_show(struct kobject *kobj,
905 struct kobj_attribute *attr, char *page)
907 return sprintf(page, "%s\n", ipl_type_str(reipl_type));
910 static ssize_t reipl_type_store(struct kobject *kobj,
911 struct kobj_attribute *attr,
912 const char *buf, size_t len)
914 int rc = -EINVAL;
916 if (strncmp(buf, IPL_CCW_STR, strlen(IPL_CCW_STR)) == 0)
917 rc = reipl_set_type(IPL_TYPE_CCW);
918 else if (strncmp(buf, IPL_FCP_STR, strlen(IPL_FCP_STR)) == 0)
919 rc = reipl_set_type(IPL_TYPE_FCP);
920 else if (strncmp(buf, IPL_NSS_STR, strlen(IPL_NSS_STR)) == 0)
921 rc = reipl_set_type(IPL_TYPE_NSS);
922 return (rc != 0) ? rc : len;
925 static struct kobj_attribute reipl_type_attr =
926 __ATTR(reipl_type, 0644, reipl_type_show, reipl_type_store);
928 static struct kset *reipl_kset;
929 static struct kset *reipl_fcp_kset;
931 static void __reipl_run(void *unused)
933 switch (reipl_type) {
934 case IPL_TYPE_CCW:
935 diag308(DIAG308_SET, reipl_block_ccw);
936 if (reipl_ccw_clear)
937 diag308(DIAG308_LOAD_CLEAR, NULL);
938 else
939 diag308(DIAG308_LOAD_NORMAL_DUMP, NULL);
940 break;
941 case IPL_TYPE_FCP:
942 diag308(DIAG308_SET, reipl_block_fcp);
943 if (reipl_fcp_clear)
944 diag308(DIAG308_LOAD_CLEAR, NULL);
945 else
946 diag308(DIAG308_LOAD_NORMAL, NULL);
947 break;
948 case IPL_TYPE_NSS:
949 diag308(DIAG308_SET, reipl_block_nss);
950 diag308(DIAG308_LOAD_CLEAR, NULL);
951 break;
952 case IPL_TYPE_UNKNOWN:
953 diag308(DIAG308_LOAD_CLEAR, NULL);
954 break;
955 case IPL_TYPE_FCP_DUMP:
956 break;
958 disabled_wait();
961 static void reipl_run(struct shutdown_trigger *trigger)
963 smp_call_ipl_cpu(__reipl_run, NULL);
966 static void reipl_block_ccw_init(struct ipl_parameter_block *ipb)
968 ipb->hdr.len = IPL_BP_CCW_LEN;
969 ipb->hdr.version = IPL_PARM_BLOCK_VERSION;
970 ipb->pb0_hdr.len = IPL_BP0_CCW_LEN;
971 ipb->pb0_hdr.pbt = IPL_PBT_CCW;
974 static void reipl_block_ccw_fill_parms(struct ipl_parameter_block *ipb)
976 /* LOADPARM */
977 /* check if read scp info worked and set loadparm */
978 if (sclp_ipl_info.is_valid)
979 memcpy(ipb->ccw.loadparm, &sclp_ipl_info.loadparm, LOADPARM_LEN);
980 else
981 /* read scp info failed: set empty loadparm (EBCDIC blanks) */
982 memset(ipb->ccw.loadparm, 0x40, LOADPARM_LEN);
983 ipb->ccw.flags = IPL_PB0_FLAG_LOADPARM;
985 /* VM PARM */
986 if (MACHINE_IS_VM && ipl_block_valid &&
987 (ipl_block.ccw.vm_flags & IPL_PB0_CCW_VM_FLAG_VP)) {
989 ipb->ccw.vm_flags |= IPL_PB0_CCW_VM_FLAG_VP;
990 ipb->ccw.vm_parm_len = ipl_block.ccw.vm_parm_len;
991 memcpy(ipb->ccw.vm_parm,
992 ipl_block.ccw.vm_parm, DIAG308_VMPARM_SIZE);
996 static int __init reipl_nss_init(void)
998 int rc;
1000 if (!MACHINE_IS_VM)
1001 return 0;
1003 reipl_block_nss = (void *) get_zeroed_page(GFP_KERNEL);
1004 if (!reipl_block_nss)
1005 return -ENOMEM;
1007 rc = sysfs_create_group(&reipl_kset->kobj, &reipl_nss_attr_group);
1008 if (rc)
1009 return rc;
1011 reipl_block_ccw_init(reipl_block_nss);
1012 reipl_capabilities |= IPL_TYPE_NSS;
1013 return 0;
1016 static int __init reipl_ccw_init(void)
1018 int rc;
1020 reipl_block_ccw = (void *) get_zeroed_page(GFP_KERNEL);
1021 if (!reipl_block_ccw)
1022 return -ENOMEM;
1024 rc = sysfs_create_group(&reipl_kset->kobj,
1025 MACHINE_IS_VM ? &reipl_ccw_attr_group_vm
1026 : &reipl_ccw_attr_group_lpar);
1027 if (rc)
1028 return rc;
1030 reipl_block_ccw_init(reipl_block_ccw);
1031 if (ipl_info.type == IPL_TYPE_CCW) {
1032 reipl_block_ccw->ccw.ssid = ipl_block.ccw.ssid;
1033 reipl_block_ccw->ccw.devno = ipl_block.ccw.devno;
1034 reipl_block_ccw_fill_parms(reipl_block_ccw);
1037 reipl_capabilities |= IPL_TYPE_CCW;
1038 return 0;
1041 static int __init reipl_fcp_init(void)
1043 int rc;
1045 reipl_block_fcp = (void *) get_zeroed_page(GFP_KERNEL);
1046 if (!reipl_block_fcp)
1047 return -ENOMEM;
1049 /* sysfs: create fcp kset for mixing attr group and bin attrs */
1050 reipl_fcp_kset = kset_create_and_add(IPL_FCP_STR, NULL,
1051 &reipl_kset->kobj);
1052 if (!reipl_fcp_kset) {
1053 free_page((unsigned long) reipl_block_fcp);
1054 return -ENOMEM;
1057 rc = sysfs_create_group(&reipl_fcp_kset->kobj, &reipl_fcp_attr_group);
1058 if (rc)
1059 goto out1;
1061 if (test_facility(141)) {
1062 rc = sysfs_create_file(&reipl_fcp_kset->kobj,
1063 &sys_reipl_fcp_clear_attr.attr);
1064 if (rc)
1065 goto out2;
1066 } else
1067 reipl_fcp_clear = true;
1069 if (ipl_info.type == IPL_TYPE_FCP) {
1070 memcpy(reipl_block_fcp, &ipl_block, sizeof(ipl_block));
1072 * Fix loadparm: There are systems where the (SCSI) LOADPARM
1073 * is invalid in the SCSI IPL parameter block, so take it
1074 * always from sclp_ipl_info.
1076 memcpy(reipl_block_fcp->fcp.loadparm, sclp_ipl_info.loadparm,
1077 LOADPARM_LEN);
1078 } else {
1079 reipl_block_fcp->hdr.len = IPL_BP_FCP_LEN;
1080 reipl_block_fcp->hdr.version = IPL_PARM_BLOCK_VERSION;
1081 reipl_block_fcp->fcp.len = IPL_BP0_FCP_LEN;
1082 reipl_block_fcp->fcp.pbt = IPL_PBT_FCP;
1083 reipl_block_fcp->fcp.opt = IPL_PB0_FCP_OPT_IPL;
1085 reipl_capabilities |= IPL_TYPE_FCP;
1086 return 0;
1088 out2:
1089 sysfs_remove_group(&reipl_fcp_kset->kobj, &reipl_fcp_attr_group);
1090 out1:
1091 kset_unregister(reipl_fcp_kset);
1092 free_page((unsigned long) reipl_block_fcp);
1093 return rc;
1096 static int __init reipl_type_init(void)
1098 enum ipl_type reipl_type = ipl_info.type;
1099 struct ipl_parameter_block *reipl_block;
1100 unsigned long size;
1102 reipl_block = os_info_old_entry(OS_INFO_REIPL_BLOCK, &size);
1103 if (!reipl_block)
1104 goto out;
1106 * If we have an OS info reipl block, this will be used
1108 if (reipl_block->pb0_hdr.pbt == IPL_PBT_FCP) {
1109 memcpy(reipl_block_fcp, reipl_block, size);
1110 reipl_type = IPL_TYPE_FCP;
1111 } else if (reipl_block->pb0_hdr.pbt == IPL_PBT_CCW) {
1112 memcpy(reipl_block_ccw, reipl_block, size);
1113 reipl_type = IPL_TYPE_CCW;
1115 out:
1116 return reipl_set_type(reipl_type);
1119 static int __init reipl_init(void)
1121 int rc;
1123 reipl_kset = kset_create_and_add("reipl", NULL, firmware_kobj);
1124 if (!reipl_kset)
1125 return -ENOMEM;
1126 rc = sysfs_create_file(&reipl_kset->kobj, &reipl_type_attr.attr);
1127 if (rc) {
1128 kset_unregister(reipl_kset);
1129 return rc;
1131 rc = reipl_ccw_init();
1132 if (rc)
1133 return rc;
1134 rc = reipl_fcp_init();
1135 if (rc)
1136 return rc;
1137 rc = reipl_nss_init();
1138 if (rc)
1139 return rc;
1140 return reipl_type_init();
1143 static struct shutdown_action __refdata reipl_action = {
1144 .name = SHUTDOWN_ACTION_REIPL_STR,
1145 .fn = reipl_run,
1146 .init = reipl_init,
1150 * dump shutdown action: Dump Linux on shutdown.
1153 /* FCP dump device attributes */
1155 DEFINE_IPL_ATTR_RW(dump_fcp, wwpn, "0x%016llx\n", "%llx\n",
1156 dump_block_fcp->fcp.wwpn);
1157 DEFINE_IPL_ATTR_RW(dump_fcp, lun, "0x%016llx\n", "%llx\n",
1158 dump_block_fcp->fcp.lun);
1159 DEFINE_IPL_ATTR_RW(dump_fcp, bootprog, "%lld\n", "%lld\n",
1160 dump_block_fcp->fcp.bootprog);
1161 DEFINE_IPL_ATTR_RW(dump_fcp, br_lba, "%lld\n", "%lld\n",
1162 dump_block_fcp->fcp.br_lba);
1163 DEFINE_IPL_ATTR_RW(dump_fcp, device, "0.0.%04llx\n", "0.0.%llx\n",
1164 dump_block_fcp->fcp.devno);
1166 static struct attribute *dump_fcp_attrs[] = {
1167 &sys_dump_fcp_device_attr.attr,
1168 &sys_dump_fcp_wwpn_attr.attr,
1169 &sys_dump_fcp_lun_attr.attr,
1170 &sys_dump_fcp_bootprog_attr.attr,
1171 &sys_dump_fcp_br_lba_attr.attr,
1172 NULL,
1175 static struct attribute_group dump_fcp_attr_group = {
1176 .name = IPL_FCP_STR,
1177 .attrs = dump_fcp_attrs,
1180 /* CCW dump device attributes */
1181 DEFINE_IPL_CCW_ATTR_RW(dump_ccw, device, dump_block_ccw->ccw);
1183 static struct attribute *dump_ccw_attrs[] = {
1184 &sys_dump_ccw_device_attr.attr,
1185 NULL,
1188 static struct attribute_group dump_ccw_attr_group = {
1189 .name = IPL_CCW_STR,
1190 .attrs = dump_ccw_attrs,
1193 /* dump type */
1195 static int dump_set_type(enum dump_type type)
1197 if (!(dump_capabilities & type))
1198 return -EINVAL;
1199 dump_type = type;
1200 return 0;
1203 static ssize_t dump_type_show(struct kobject *kobj,
1204 struct kobj_attribute *attr, char *page)
1206 return sprintf(page, "%s\n", dump_type_str(dump_type));
1209 static ssize_t dump_type_store(struct kobject *kobj,
1210 struct kobj_attribute *attr,
1211 const char *buf, size_t len)
1213 int rc = -EINVAL;
1215 if (strncmp(buf, DUMP_NONE_STR, strlen(DUMP_NONE_STR)) == 0)
1216 rc = dump_set_type(DUMP_TYPE_NONE);
1217 else if (strncmp(buf, DUMP_CCW_STR, strlen(DUMP_CCW_STR)) == 0)
1218 rc = dump_set_type(DUMP_TYPE_CCW);
1219 else if (strncmp(buf, DUMP_FCP_STR, strlen(DUMP_FCP_STR)) == 0)
1220 rc = dump_set_type(DUMP_TYPE_FCP);
1221 return (rc != 0) ? rc : len;
1224 static struct kobj_attribute dump_type_attr =
1225 __ATTR(dump_type, 0644, dump_type_show, dump_type_store);
1227 static struct kset *dump_kset;
1229 static void diag308_dump(void *dump_block)
1231 diag308(DIAG308_SET, dump_block);
1232 while (1) {
1233 if (diag308(DIAG308_LOAD_NORMAL_DUMP, NULL) != 0x302)
1234 break;
1235 udelay_simple(USEC_PER_SEC);
1239 static void __dump_run(void *unused)
1241 switch (dump_type) {
1242 case DUMP_TYPE_CCW:
1243 diag308_dump(dump_block_ccw);
1244 break;
1245 case DUMP_TYPE_FCP:
1246 diag308_dump(dump_block_fcp);
1247 break;
1248 default:
1249 break;
1253 static void dump_run(struct shutdown_trigger *trigger)
1255 if (dump_type == DUMP_TYPE_NONE)
1256 return;
1257 smp_send_stop();
1258 smp_call_ipl_cpu(__dump_run, NULL);
1261 static int __init dump_ccw_init(void)
1263 int rc;
1265 dump_block_ccw = (void *) get_zeroed_page(GFP_KERNEL);
1266 if (!dump_block_ccw)
1267 return -ENOMEM;
1268 rc = sysfs_create_group(&dump_kset->kobj, &dump_ccw_attr_group);
1269 if (rc) {
1270 free_page((unsigned long)dump_block_ccw);
1271 return rc;
1273 dump_block_ccw->hdr.len = IPL_BP_CCW_LEN;
1274 dump_block_ccw->hdr.version = IPL_PARM_BLOCK_VERSION;
1275 dump_block_ccw->ccw.len = IPL_BP0_CCW_LEN;
1276 dump_block_ccw->ccw.pbt = IPL_PBT_CCW;
1277 dump_capabilities |= DUMP_TYPE_CCW;
1278 return 0;
1281 static int __init dump_fcp_init(void)
1283 int rc;
1285 if (!sclp_ipl_info.has_dump)
1286 return 0; /* LDIPL DUMP is not installed */
1287 dump_block_fcp = (void *) get_zeroed_page(GFP_KERNEL);
1288 if (!dump_block_fcp)
1289 return -ENOMEM;
1290 rc = sysfs_create_group(&dump_kset->kobj, &dump_fcp_attr_group);
1291 if (rc) {
1292 free_page((unsigned long)dump_block_fcp);
1293 return rc;
1295 dump_block_fcp->hdr.len = IPL_BP_FCP_LEN;
1296 dump_block_fcp->hdr.version = IPL_PARM_BLOCK_VERSION;
1297 dump_block_fcp->fcp.len = IPL_BP0_FCP_LEN;
1298 dump_block_fcp->fcp.pbt = IPL_PBT_FCP;
1299 dump_block_fcp->fcp.opt = IPL_PB0_FCP_OPT_DUMP;
1300 dump_capabilities |= DUMP_TYPE_FCP;
1301 return 0;
1304 static int __init dump_init(void)
1306 int rc;
1308 dump_kset = kset_create_and_add("dump", NULL, firmware_kobj);
1309 if (!dump_kset)
1310 return -ENOMEM;
1311 rc = sysfs_create_file(&dump_kset->kobj, &dump_type_attr.attr);
1312 if (rc) {
1313 kset_unregister(dump_kset);
1314 return rc;
1316 rc = dump_ccw_init();
1317 if (rc)
1318 return rc;
1319 rc = dump_fcp_init();
1320 if (rc)
1321 return rc;
1322 dump_set_type(DUMP_TYPE_NONE);
1323 return 0;
1326 static struct shutdown_action __refdata dump_action = {
1327 .name = SHUTDOWN_ACTION_DUMP_STR,
1328 .fn = dump_run,
1329 .init = dump_init,
1332 static void dump_reipl_run(struct shutdown_trigger *trigger)
1334 unsigned long ipib = (unsigned long) reipl_block_actual;
1335 unsigned int csum;
1337 csum = (__force unsigned int)
1338 csum_partial(reipl_block_actual, reipl_block_actual->hdr.len, 0);
1339 mem_assign_absolute(S390_lowcore.ipib, ipib);
1340 mem_assign_absolute(S390_lowcore.ipib_checksum, csum);
1341 dump_run(trigger);
1344 static struct shutdown_action __refdata dump_reipl_action = {
1345 .name = SHUTDOWN_ACTION_DUMP_REIPL_STR,
1346 .fn = dump_reipl_run,
1350 * vmcmd shutdown action: Trigger vm command on shutdown.
1353 static char vmcmd_on_reboot[128];
1354 static char vmcmd_on_panic[128];
1355 static char vmcmd_on_halt[128];
1356 static char vmcmd_on_poff[128];
1357 static char vmcmd_on_restart[128];
1359 DEFINE_IPL_ATTR_STR_RW(vmcmd, on_reboot, "%s\n", "%s\n", vmcmd_on_reboot);
1360 DEFINE_IPL_ATTR_STR_RW(vmcmd, on_panic, "%s\n", "%s\n", vmcmd_on_panic);
1361 DEFINE_IPL_ATTR_STR_RW(vmcmd, on_halt, "%s\n", "%s\n", vmcmd_on_halt);
1362 DEFINE_IPL_ATTR_STR_RW(vmcmd, on_poff, "%s\n", "%s\n", vmcmd_on_poff);
1363 DEFINE_IPL_ATTR_STR_RW(vmcmd, on_restart, "%s\n", "%s\n", vmcmd_on_restart);
1365 static struct attribute *vmcmd_attrs[] = {
1366 &sys_vmcmd_on_reboot_attr.attr,
1367 &sys_vmcmd_on_panic_attr.attr,
1368 &sys_vmcmd_on_halt_attr.attr,
1369 &sys_vmcmd_on_poff_attr.attr,
1370 &sys_vmcmd_on_restart_attr.attr,
1371 NULL,
1374 static struct attribute_group vmcmd_attr_group = {
1375 .attrs = vmcmd_attrs,
1378 static struct kset *vmcmd_kset;
1380 static void vmcmd_run(struct shutdown_trigger *trigger)
1382 char *cmd;
1384 if (strcmp(trigger->name, ON_REIPL_STR) == 0)
1385 cmd = vmcmd_on_reboot;
1386 else if (strcmp(trigger->name, ON_PANIC_STR) == 0)
1387 cmd = vmcmd_on_panic;
1388 else if (strcmp(trigger->name, ON_HALT_STR) == 0)
1389 cmd = vmcmd_on_halt;
1390 else if (strcmp(trigger->name, ON_POFF_STR) == 0)
1391 cmd = vmcmd_on_poff;
1392 else if (strcmp(trigger->name, ON_RESTART_STR) == 0)
1393 cmd = vmcmd_on_restart;
1394 else
1395 return;
1397 if (strlen(cmd) == 0)
1398 return;
1399 __cpcmd(cmd, NULL, 0, NULL);
1402 static int vmcmd_init(void)
1404 if (!MACHINE_IS_VM)
1405 return -EOPNOTSUPP;
1406 vmcmd_kset = kset_create_and_add("vmcmd", NULL, firmware_kobj);
1407 if (!vmcmd_kset)
1408 return -ENOMEM;
1409 return sysfs_create_group(&vmcmd_kset->kobj, &vmcmd_attr_group);
1412 static struct shutdown_action vmcmd_action = {SHUTDOWN_ACTION_VMCMD_STR,
1413 vmcmd_run, vmcmd_init};
1416 * stop shutdown action: Stop Linux on shutdown.
1419 static void stop_run(struct shutdown_trigger *trigger)
1421 if (strcmp(trigger->name, ON_PANIC_STR) == 0 ||
1422 strcmp(trigger->name, ON_RESTART_STR) == 0)
1423 disabled_wait();
1424 smp_stop_cpu();
1427 static struct shutdown_action stop_action = {SHUTDOWN_ACTION_STOP_STR,
1428 stop_run, NULL};
1430 /* action list */
1432 static struct shutdown_action *shutdown_actions_list[] = {
1433 &ipl_action, &reipl_action, &dump_reipl_action, &dump_action,
1434 &vmcmd_action, &stop_action};
1435 #define SHUTDOWN_ACTIONS_COUNT (sizeof(shutdown_actions_list) / sizeof(void *))
1438 * Trigger section
1441 static struct kset *shutdown_actions_kset;
1443 static int set_trigger(const char *buf, struct shutdown_trigger *trigger,
1444 size_t len)
1446 int i;
1448 for (i = 0; i < SHUTDOWN_ACTIONS_COUNT; i++) {
1449 if (sysfs_streq(buf, shutdown_actions_list[i]->name)) {
1450 if (shutdown_actions_list[i]->init_rc) {
1451 return shutdown_actions_list[i]->init_rc;
1452 } else {
1453 trigger->action = shutdown_actions_list[i];
1454 return len;
1458 return -EINVAL;
1461 /* on reipl */
1463 static struct shutdown_trigger on_reboot_trigger = {ON_REIPL_STR,
1464 &reipl_action};
1466 static ssize_t on_reboot_show(struct kobject *kobj,
1467 struct kobj_attribute *attr, char *page)
1469 return sprintf(page, "%s\n", on_reboot_trigger.action->name);
1472 static ssize_t on_reboot_store(struct kobject *kobj,
1473 struct kobj_attribute *attr,
1474 const char *buf, size_t len)
1476 return set_trigger(buf, &on_reboot_trigger, len);
1478 static struct kobj_attribute on_reboot_attr = __ATTR_RW(on_reboot);
1480 static void do_machine_restart(char *__unused)
1482 smp_send_stop();
1483 on_reboot_trigger.action->fn(&on_reboot_trigger);
1484 reipl_run(NULL);
1486 void (*_machine_restart)(char *command) = do_machine_restart;
1488 /* on panic */
1490 static struct shutdown_trigger on_panic_trigger = {ON_PANIC_STR, &stop_action};
1492 static ssize_t on_panic_show(struct kobject *kobj,
1493 struct kobj_attribute *attr, char *page)
1495 return sprintf(page, "%s\n", on_panic_trigger.action->name);
1498 static ssize_t on_panic_store(struct kobject *kobj,
1499 struct kobj_attribute *attr,
1500 const char *buf, size_t len)
1502 return set_trigger(buf, &on_panic_trigger, len);
1504 static struct kobj_attribute on_panic_attr = __ATTR_RW(on_panic);
1506 static void do_panic(void)
1508 lgr_info_log();
1509 on_panic_trigger.action->fn(&on_panic_trigger);
1510 stop_run(&on_panic_trigger);
1513 /* on restart */
1515 static struct shutdown_trigger on_restart_trigger = {ON_RESTART_STR,
1516 &stop_action};
1518 static ssize_t on_restart_show(struct kobject *kobj,
1519 struct kobj_attribute *attr, char *page)
1521 return sprintf(page, "%s\n", on_restart_trigger.action->name);
1524 static ssize_t on_restart_store(struct kobject *kobj,
1525 struct kobj_attribute *attr,
1526 const char *buf, size_t len)
1528 return set_trigger(buf, &on_restart_trigger, len);
1530 static struct kobj_attribute on_restart_attr = __ATTR_RW(on_restart);
1532 static void __do_restart(void *ignore)
1534 __arch_local_irq_stosm(0x04); /* enable DAT */
1535 smp_send_stop();
1536 #ifdef CONFIG_CRASH_DUMP
1537 crash_kexec(NULL);
1538 #endif
1539 on_restart_trigger.action->fn(&on_restart_trigger);
1540 stop_run(&on_restart_trigger);
1543 void do_restart(void)
1545 tracing_off();
1546 debug_locks_off();
1547 lgr_info_log();
1548 smp_call_online_cpu(__do_restart, NULL);
1551 /* on halt */
1553 static struct shutdown_trigger on_halt_trigger = {ON_HALT_STR, &stop_action};
1555 static ssize_t on_halt_show(struct kobject *kobj,
1556 struct kobj_attribute *attr, char *page)
1558 return sprintf(page, "%s\n", on_halt_trigger.action->name);
1561 static ssize_t on_halt_store(struct kobject *kobj,
1562 struct kobj_attribute *attr,
1563 const char *buf, size_t len)
1565 return set_trigger(buf, &on_halt_trigger, len);
1567 static struct kobj_attribute on_halt_attr = __ATTR_RW(on_halt);
1569 static void do_machine_halt(void)
1571 smp_send_stop();
1572 on_halt_trigger.action->fn(&on_halt_trigger);
1573 stop_run(&on_halt_trigger);
1575 void (*_machine_halt)(void) = do_machine_halt;
1577 /* on power off */
1579 static struct shutdown_trigger on_poff_trigger = {ON_POFF_STR, &stop_action};
1581 static ssize_t on_poff_show(struct kobject *kobj,
1582 struct kobj_attribute *attr, char *page)
1584 return sprintf(page, "%s\n", on_poff_trigger.action->name);
1587 static ssize_t on_poff_store(struct kobject *kobj,
1588 struct kobj_attribute *attr,
1589 const char *buf, size_t len)
1591 return set_trigger(buf, &on_poff_trigger, len);
1593 static struct kobj_attribute on_poff_attr = __ATTR_RW(on_poff);
1595 static void do_machine_power_off(void)
1597 smp_send_stop();
1598 on_poff_trigger.action->fn(&on_poff_trigger);
1599 stop_run(&on_poff_trigger);
1601 void (*_machine_power_off)(void) = do_machine_power_off;
1603 static struct attribute *shutdown_action_attrs[] = {
1604 &on_restart_attr.attr,
1605 &on_reboot_attr.attr,
1606 &on_panic_attr.attr,
1607 &on_halt_attr.attr,
1608 &on_poff_attr.attr,
1609 NULL,
1612 static struct attribute_group shutdown_action_attr_group = {
1613 .attrs = shutdown_action_attrs,
1616 static void __init shutdown_triggers_init(void)
1618 shutdown_actions_kset = kset_create_and_add("shutdown_actions", NULL,
1619 firmware_kobj);
1620 if (!shutdown_actions_kset)
1621 goto fail;
1622 if (sysfs_create_group(&shutdown_actions_kset->kobj,
1623 &shutdown_action_attr_group))
1624 goto fail;
1625 return;
1626 fail:
1627 panic("shutdown_triggers_init failed\n");
1630 static void __init shutdown_actions_init(void)
1632 int i;
1634 for (i = 0; i < SHUTDOWN_ACTIONS_COUNT; i++) {
1635 if (!shutdown_actions_list[i]->init)
1636 continue;
1637 shutdown_actions_list[i]->init_rc =
1638 shutdown_actions_list[i]->init();
1642 static int __init s390_ipl_init(void)
1644 char str[8] = {0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40};
1646 sclp_early_get_ipl_info(&sclp_ipl_info);
1648 * Fix loadparm: There are systems where the (SCSI) LOADPARM
1649 * returned by read SCP info is invalid (contains EBCDIC blanks)
1650 * when the system has been booted via diag308. In that case we use
1651 * the value from diag308, if available.
1653 * There are also systems where diag308 store does not work in
1654 * case the system is booted from HMC. Fortunately in this case
1655 * READ SCP info provides the correct value.
1657 if (memcmp(sclp_ipl_info.loadparm, str, sizeof(str)) == 0 && ipl_block_valid)
1658 memcpy(sclp_ipl_info.loadparm, ipl_block.ccw.loadparm, LOADPARM_LEN);
1659 shutdown_actions_init();
1660 shutdown_triggers_init();
1661 return 0;
1664 __initcall(s390_ipl_init);
1666 static void __init strncpy_skip_quote(char *dst, char *src, int n)
1668 int sx, dx;
1670 dx = 0;
1671 for (sx = 0; src[sx] != 0; sx++) {
1672 if (src[sx] == '"')
1673 continue;
1674 dst[dx++] = src[sx];
1675 if (dx >= n)
1676 break;
1680 static int __init vmcmd_on_reboot_setup(char *str)
1682 if (!MACHINE_IS_VM)
1683 return 1;
1684 strncpy_skip_quote(vmcmd_on_reboot, str, 127);
1685 vmcmd_on_reboot[127] = 0;
1686 on_reboot_trigger.action = &vmcmd_action;
1687 return 1;
1689 __setup("vmreboot=", vmcmd_on_reboot_setup);
1691 static int __init vmcmd_on_panic_setup(char *str)
1693 if (!MACHINE_IS_VM)
1694 return 1;
1695 strncpy_skip_quote(vmcmd_on_panic, str, 127);
1696 vmcmd_on_panic[127] = 0;
1697 on_panic_trigger.action = &vmcmd_action;
1698 return 1;
1700 __setup("vmpanic=", vmcmd_on_panic_setup);
1702 static int __init vmcmd_on_halt_setup(char *str)
1704 if (!MACHINE_IS_VM)
1705 return 1;
1706 strncpy_skip_quote(vmcmd_on_halt, str, 127);
1707 vmcmd_on_halt[127] = 0;
1708 on_halt_trigger.action = &vmcmd_action;
1709 return 1;
1711 __setup("vmhalt=", vmcmd_on_halt_setup);
1713 static int __init vmcmd_on_poff_setup(char *str)
1715 if (!MACHINE_IS_VM)
1716 return 1;
1717 strncpy_skip_quote(vmcmd_on_poff, str, 127);
1718 vmcmd_on_poff[127] = 0;
1719 on_poff_trigger.action = &vmcmd_action;
1720 return 1;
1722 __setup("vmpoff=", vmcmd_on_poff_setup);
1724 static int on_panic_notify(struct notifier_block *self,
1725 unsigned long event, void *data)
1727 do_panic();
1728 return NOTIFY_OK;
1731 static struct notifier_block on_panic_nb = {
1732 .notifier_call = on_panic_notify,
1733 .priority = INT_MIN,
1736 void __init setup_ipl(void)
1738 BUILD_BUG_ON(sizeof(struct ipl_parameter_block) != PAGE_SIZE);
1740 ipl_info.type = get_ipl_type();
1741 switch (ipl_info.type) {
1742 case IPL_TYPE_CCW:
1743 ipl_info.data.ccw.dev_id.ssid = ipl_block.ccw.ssid;
1744 ipl_info.data.ccw.dev_id.devno = ipl_block.ccw.devno;
1745 break;
1746 case IPL_TYPE_FCP:
1747 case IPL_TYPE_FCP_DUMP:
1748 ipl_info.data.fcp.dev_id.ssid = 0;
1749 ipl_info.data.fcp.dev_id.devno = ipl_block.fcp.devno;
1750 ipl_info.data.fcp.wwpn = ipl_block.fcp.wwpn;
1751 ipl_info.data.fcp.lun = ipl_block.fcp.lun;
1752 break;
1753 case IPL_TYPE_NSS:
1754 case IPL_TYPE_UNKNOWN:
1755 /* We have no info to copy */
1756 break;
1758 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
1761 void s390_reset_system(void)
1763 /* Disable prefixing */
1764 set_prefix(0);
1766 /* Disable lowcore protection */
1767 __ctl_clear_bit(0, 28);
1768 diag_dma_ops.diag308_reset();
1771 #ifdef CONFIG_KEXEC_FILE
1773 int ipl_report_add_component(struct ipl_report *report, struct kexec_buf *kbuf,
1774 unsigned char flags, unsigned short cert)
1776 struct ipl_report_component *comp;
1778 comp = vzalloc(sizeof(*comp));
1779 if (!comp)
1780 return -ENOMEM;
1781 list_add_tail(&comp->list, &report->components);
1783 comp->entry.addr = kbuf->mem;
1784 comp->entry.len = kbuf->memsz;
1785 comp->entry.flags = flags;
1786 comp->entry.certificate_index = cert;
1788 report->size += sizeof(comp->entry);
1790 return 0;
1793 int ipl_report_add_certificate(struct ipl_report *report, void *key,
1794 unsigned long addr, unsigned long len)
1796 struct ipl_report_certificate *cert;
1798 cert = vzalloc(sizeof(*cert));
1799 if (!cert)
1800 return -ENOMEM;
1801 list_add_tail(&cert->list, &report->certificates);
1803 cert->entry.addr = addr;
1804 cert->entry.len = len;
1805 cert->key = key;
1807 report->size += sizeof(cert->entry);
1808 report->size += cert->entry.len;
1810 return 0;
1813 struct ipl_report *ipl_report_init(struct ipl_parameter_block *ipib)
1815 struct ipl_report *report;
1817 report = vzalloc(sizeof(*report));
1818 if (!report)
1819 return ERR_PTR(-ENOMEM);
1821 report->ipib = ipib;
1822 INIT_LIST_HEAD(&report->components);
1823 INIT_LIST_HEAD(&report->certificates);
1825 report->size = ALIGN(ipib->hdr.len, 8);
1826 report->size += sizeof(struct ipl_rl_hdr);
1827 report->size += sizeof(struct ipl_rb_components);
1828 report->size += sizeof(struct ipl_rb_certificates);
1830 return report;
1833 void *ipl_report_finish(struct ipl_report *report)
1835 struct ipl_report_certificate *cert;
1836 struct ipl_report_component *comp;
1837 struct ipl_rb_certificates *certs;
1838 struct ipl_parameter_block *ipib;
1839 struct ipl_rb_components *comps;
1840 struct ipl_rl_hdr *rl_hdr;
1841 void *buf, *ptr;
1843 buf = vzalloc(report->size);
1844 if (!buf)
1845 return ERR_PTR(-ENOMEM);
1846 ptr = buf;
1848 memcpy(ptr, report->ipib, report->ipib->hdr.len);
1849 ipib = ptr;
1850 if (ipl_secure_flag)
1851 ipib->hdr.flags |= IPL_PL_FLAG_SIPL;
1852 ipib->hdr.flags |= IPL_PL_FLAG_IPLSR;
1853 ptr += report->ipib->hdr.len;
1854 ptr = PTR_ALIGN(ptr, 8);
1856 rl_hdr = ptr;
1857 ptr += sizeof(*rl_hdr);
1859 comps = ptr;
1860 comps->rbt = IPL_RBT_COMPONENTS;
1861 ptr += sizeof(*comps);
1862 list_for_each_entry(comp, &report->components, list) {
1863 memcpy(ptr, &comp->entry, sizeof(comp->entry));
1864 ptr += sizeof(comp->entry);
1866 comps->len = ptr - (void *)comps;
1868 certs = ptr;
1869 certs->rbt = IPL_RBT_CERTIFICATES;
1870 ptr += sizeof(*certs);
1871 list_for_each_entry(cert, &report->certificates, list) {
1872 memcpy(ptr, &cert->entry, sizeof(cert->entry));
1873 ptr += sizeof(cert->entry);
1875 certs->len = ptr - (void *)certs;
1876 rl_hdr->len = ptr - (void *)rl_hdr;
1878 list_for_each_entry(cert, &report->certificates, list) {
1879 memcpy(ptr, cert->key, cert->entry.len);
1880 ptr += cert->entry.len;
1883 BUG_ON(ptr > buf + report->size);
1884 return buf;
1887 int ipl_report_free(struct ipl_report *report)
1889 struct ipl_report_component *comp, *ncomp;
1890 struct ipl_report_certificate *cert, *ncert;
1892 list_for_each_entry_safe(comp, ncomp, &report->components, list)
1893 vfree(comp);
1895 list_for_each_entry_safe(cert, ncert, &report->certificates, list)
1896 vfree(cert);
1898 vfree(report);
1900 return 0;
1903 #endif