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