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