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