[S390] Add ipl/reipl loadparm attribute.
[firefly-linux-kernel-4.4.55.git] / arch / s390 / kernel / ipl.c
1 /*
2  *  arch/s390/kernel/ipl.c
3  *    ipl/reipl/dump support for Linux on s390.
4  *
5  *    Copyright (C) IBM Corp. 2005,2006
6  *    Author(s): Michael Holzheu <holzheu@de.ibm.com>
7  *               Heiko Carstens <heiko.carstens@de.ibm.com>
8  *               Volker Sameske <sameske@de.ibm.com>
9  */
10
11 #include <linux/types.h>
12 #include <linux/module.h>
13 #include <linux/device.h>
14 #include <linux/delay.h>
15 #include <linux/reboot.h>
16 #include <linux/ctype.h>
17 #include <asm/smp.h>
18 #include <asm/setup.h>
19 #include <asm/cpcmd.h>
20 #include <asm/cio.h>
21 #include <asm/ebcdic.h>
22
23 #define IPL_PARM_BLOCK_VERSION 0
24 #define LOADPARM_LEN 8
25
26 extern char s390_readinfo_sccb[];
27 #define SCCB_VALID (*((__u16*)&s390_readinfo_sccb[6]) == 0x0010)
28 #define SCCB_LOADPARM (&s390_readinfo_sccb[24])
29 #define SCCB_FLAG (s390_readinfo_sccb[91])
30
31 enum ipl_type {
32         IPL_TYPE_NONE    = 1,
33         IPL_TYPE_UNKNOWN = 2,
34         IPL_TYPE_CCW     = 4,
35         IPL_TYPE_FCP     = 8,
36 };
37
38 #define IPL_NONE_STR     "none"
39 #define IPL_UNKNOWN_STR  "unknown"
40 #define IPL_CCW_STR      "ccw"
41 #define IPL_FCP_STR      "fcp"
42
43 static char *ipl_type_str(enum ipl_type type)
44 {
45         switch (type) {
46         case IPL_TYPE_NONE:
47                 return IPL_NONE_STR;
48         case IPL_TYPE_CCW:
49                 return IPL_CCW_STR;
50         case IPL_TYPE_FCP:
51                 return IPL_FCP_STR;
52         case IPL_TYPE_UNKNOWN:
53         default:
54                 return IPL_UNKNOWN_STR;
55         }
56 }
57
58 enum ipl_method {
59         IPL_METHOD_NONE,
60         IPL_METHOD_CCW_CIO,
61         IPL_METHOD_CCW_DIAG,
62         IPL_METHOD_CCW_VM,
63         IPL_METHOD_FCP_RO_DIAG,
64         IPL_METHOD_FCP_RW_DIAG,
65         IPL_METHOD_FCP_RO_VM,
66 };
67
68 enum shutdown_action {
69         SHUTDOWN_REIPL,
70         SHUTDOWN_DUMP,
71         SHUTDOWN_STOP,
72 };
73
74 #define SHUTDOWN_REIPL_STR "reipl"
75 #define SHUTDOWN_DUMP_STR  "dump"
76 #define SHUTDOWN_STOP_STR  "stop"
77
78 static char *shutdown_action_str(enum shutdown_action action)
79 {
80         switch (action) {
81         case SHUTDOWN_REIPL:
82                 return SHUTDOWN_REIPL_STR;
83         case SHUTDOWN_DUMP:
84                 return SHUTDOWN_DUMP_STR;
85         case SHUTDOWN_STOP:
86                 return SHUTDOWN_STOP_STR;
87         default:
88                 BUG();
89         }
90 }
91
92 enum diag308_subcode  {
93         DIAG308_IPL   = 3,
94         DIAG308_DUMP  = 4,
95         DIAG308_SET   = 5,
96         DIAG308_STORE = 6,
97 };
98
99 enum diag308_ipl_type {
100         DIAG308_IPL_TYPE_FCP = 0,
101         DIAG308_IPL_TYPE_CCW = 2,
102 };
103
104 enum diag308_opt {
105         DIAG308_IPL_OPT_IPL  = 0x10,
106         DIAG308_IPL_OPT_DUMP = 0x20,
107 };
108
109 enum diag308_rc {
110         DIAG308_RC_OK = 1,
111 };
112
113 static int diag308_set_works = 0;
114
115 static int reipl_capabilities = IPL_TYPE_UNKNOWN;
116 static enum ipl_type reipl_type = IPL_TYPE_UNKNOWN;
117 static enum ipl_method reipl_method = IPL_METHOD_NONE;
118 static struct ipl_parameter_block *reipl_block_fcp;
119 static struct ipl_parameter_block *reipl_block_ccw;
120
121 static int dump_capabilities = IPL_TYPE_NONE;
122 static enum ipl_type dump_type = IPL_TYPE_NONE;
123 static enum ipl_method dump_method = IPL_METHOD_NONE;
124 static struct ipl_parameter_block *dump_block_fcp;
125 static struct ipl_parameter_block *dump_block_ccw;
126
127 static enum shutdown_action on_panic_action = SHUTDOWN_STOP;
128
129 static int diag308(unsigned long subcode, void *addr)
130 {
131         register unsigned long _addr asm("0") = (unsigned long) addr;
132         register unsigned long _rc asm("1") = 0;
133
134         asm volatile(
135                 "       diag    %0,%2,0x308\n"
136                 "0:\n"
137                 EX_TABLE(0b,0b)
138                 : "+d" (_addr), "+d" (_rc)
139                 : "d" (subcode) : "cc", "memory");
140         return _rc;
141 }
142
143 /* SYSFS */
144
145 #define DEFINE_IPL_ATTR_RO(_prefix, _name, _format, _value)             \
146 static ssize_t sys_##_prefix##_##_name##_show(struct subsystem *subsys, \
147                 char *page)                                             \
148 {                                                                       \
149         return sprintf(page, _format, _value);                          \
150 }                                                                       \
151 static struct subsys_attribute sys_##_prefix##_##_name##_attr =         \
152         __ATTR(_name, S_IRUGO, sys_##_prefix##_##_name##_show, NULL);
153
154 #define DEFINE_IPL_ATTR_RW(_prefix, _name, _fmt_out, _fmt_in, _value)   \
155 static ssize_t sys_##_prefix##_##_name##_show(struct subsystem *subsys, \
156                 char *page)                                             \
157 {                                                                       \
158         return sprintf(page, _fmt_out,                                  \
159                         (unsigned long long) _value);                   \
160 }                                                                       \
161 static ssize_t sys_##_prefix##_##_name##_store(struct subsystem *subsys,\
162                 const char *buf, size_t len)                            \
163 {                                                                       \
164         unsigned long long value;                                       \
165         if (sscanf(buf, _fmt_in, &value) != 1)                          \
166                 return -EINVAL;                                         \
167         _value = value;                                                 \
168         return len;                                                     \
169 }                                                                       \
170 static struct subsys_attribute sys_##_prefix##_##_name##_attr =         \
171         __ATTR(_name,(S_IRUGO | S_IWUSR),                               \
172                         sys_##_prefix##_##_name##_show,                 \
173                         sys_##_prefix##_##_name##_store);
174
175 static void make_attrs_ro(struct attribute **attrs)
176 {
177         while (*attrs) {
178                 (*attrs)->mode = S_IRUGO;
179                 attrs++;
180         }
181 }
182
183 /*
184  * ipl section
185  */
186
187 static enum ipl_type ipl_get_type(void)
188 {
189         struct ipl_parameter_block *ipl = IPL_PARMBLOCK_START;
190
191         if (!(ipl_flags & IPL_DEVNO_VALID))
192                 return IPL_TYPE_UNKNOWN;
193         if (!(ipl_flags & IPL_PARMBLOCK_VALID))
194                 return IPL_TYPE_CCW;
195         if (ipl->hdr.version > IPL_MAX_SUPPORTED_VERSION)
196                 return IPL_TYPE_UNKNOWN;
197         if (ipl->hdr.pbt != DIAG308_IPL_TYPE_FCP)
198                 return IPL_TYPE_UNKNOWN;
199         return IPL_TYPE_FCP;
200 }
201
202 static ssize_t ipl_type_show(struct subsystem *subsys, char *page)
203 {
204         return sprintf(page, "%s\n", ipl_type_str(ipl_get_type()));
205 }
206
207 static struct subsys_attribute sys_ipl_type_attr = __ATTR_RO(ipl_type);
208
209 static ssize_t sys_ipl_device_show(struct subsystem *subsys, char *page)
210 {
211         struct ipl_parameter_block *ipl = IPL_PARMBLOCK_START;
212
213         switch (ipl_get_type()) {
214         case IPL_TYPE_CCW:
215                 return sprintf(page, "0.0.%04x\n", ipl_devno);
216         case IPL_TYPE_FCP:
217                 return sprintf(page, "0.0.%04x\n", ipl->ipl_info.fcp.devno);
218         default:
219                 return 0;
220         }
221 }
222
223 static struct subsys_attribute sys_ipl_device_attr =
224         __ATTR(device, S_IRUGO, sys_ipl_device_show, NULL);
225
226 static ssize_t ipl_parameter_read(struct kobject *kobj, char *buf, loff_t off,
227                                   size_t count)
228 {
229         unsigned int size = IPL_PARMBLOCK_SIZE;
230
231         if (off > size)
232                 return 0;
233         if (off + count > size)
234                 count = size - off;
235         memcpy(buf, (void *)IPL_PARMBLOCK_START + off, count);
236         return count;
237 }
238
239 static struct bin_attribute ipl_parameter_attr = {
240         .attr = {
241                 .name = "binary_parameter",
242                 .mode = S_IRUGO,
243                 .owner = THIS_MODULE,
244         },
245         .size = PAGE_SIZE,
246         .read = &ipl_parameter_read,
247 };
248
249 static ssize_t ipl_scp_data_read(struct kobject *kobj, char *buf, loff_t off,
250         size_t count)
251 {
252         unsigned int size = IPL_PARMBLOCK_START->ipl_info.fcp.scp_data_len;
253         void *scp_data = &IPL_PARMBLOCK_START->ipl_info.fcp.scp_data;
254
255         if (off > size)
256                 return 0;
257         if (off + count > size)
258                 count = size - off;
259         memcpy(buf, scp_data + off, count);
260         return count;
261 }
262
263 static struct bin_attribute ipl_scp_data_attr = {
264         .attr = {
265                 .name = "scp_data",
266                 .mode = S_IRUGO,
267                 .owner = THIS_MODULE,
268         },
269         .size = PAGE_SIZE,
270         .read = &ipl_scp_data_read,
271 };
272
273 /* FCP ipl device attributes */
274
275 DEFINE_IPL_ATTR_RO(ipl_fcp, wwpn, "0x%016llx\n", (unsigned long long)
276                    IPL_PARMBLOCK_START->ipl_info.fcp.wwpn);
277 DEFINE_IPL_ATTR_RO(ipl_fcp, lun, "0x%016llx\n", (unsigned long long)
278                    IPL_PARMBLOCK_START->ipl_info.fcp.lun);
279 DEFINE_IPL_ATTR_RO(ipl_fcp, bootprog, "%lld\n", (unsigned long long)
280                    IPL_PARMBLOCK_START->ipl_info.fcp.bootprog);
281 DEFINE_IPL_ATTR_RO(ipl_fcp, br_lba, "%lld\n", (unsigned long long)
282                    IPL_PARMBLOCK_START->ipl_info.fcp.br_lba);
283
284 static struct attribute *ipl_fcp_attrs[] = {
285         &sys_ipl_type_attr.attr,
286         &sys_ipl_device_attr.attr,
287         &sys_ipl_fcp_wwpn_attr.attr,
288         &sys_ipl_fcp_lun_attr.attr,
289         &sys_ipl_fcp_bootprog_attr.attr,
290         &sys_ipl_fcp_br_lba_attr.attr,
291         NULL,
292 };
293
294 static struct attribute_group ipl_fcp_attr_group = {
295         .attrs = ipl_fcp_attrs,
296 };
297
298 /* CCW ipl device attributes */
299
300 static ssize_t ipl_ccw_loadparm_show(struct subsystem *subsys, char *page)
301 {
302         char loadparm[LOADPARM_LEN + 1] = {};
303
304         if (!SCCB_VALID)
305                 return sprintf(page, "#unknown#\n");
306         memcpy(loadparm, SCCB_LOADPARM, LOADPARM_LEN);
307         EBCASC(loadparm, LOADPARM_LEN);
308         strstrip(loadparm);
309         return sprintf(page, "%s\n", loadparm);
310 }
311
312 static struct subsys_attribute sys_ipl_ccw_loadparm_attr =
313         __ATTR(loadparm, 0444, ipl_ccw_loadparm_show, NULL);
314
315 static struct attribute *ipl_ccw_attrs[] = {
316         &sys_ipl_type_attr.attr,
317         &sys_ipl_device_attr.attr,
318         &sys_ipl_ccw_loadparm_attr.attr,
319         NULL,
320 };
321
322 static struct attribute_group ipl_ccw_attr_group = {
323         .attrs = ipl_ccw_attrs,
324 };
325
326 /* UNKNOWN ipl device attributes */
327
328 static struct attribute *ipl_unknown_attrs[] = {
329         &sys_ipl_type_attr.attr,
330         NULL,
331 };
332
333 static struct attribute_group ipl_unknown_attr_group = {
334         .attrs = ipl_unknown_attrs,
335 };
336
337 static decl_subsys(ipl, NULL, NULL);
338
339 /*
340  * reipl section
341  */
342
343 /* FCP reipl device attributes */
344
345 DEFINE_IPL_ATTR_RW(reipl_fcp, wwpn, "0x%016llx\n", "%016llx\n",
346                    reipl_block_fcp->ipl_info.fcp.wwpn);
347 DEFINE_IPL_ATTR_RW(reipl_fcp, lun, "0x%016llx\n", "%016llx\n",
348                    reipl_block_fcp->ipl_info.fcp.lun);
349 DEFINE_IPL_ATTR_RW(reipl_fcp, bootprog, "%lld\n", "%lld\n",
350                    reipl_block_fcp->ipl_info.fcp.bootprog);
351 DEFINE_IPL_ATTR_RW(reipl_fcp, br_lba, "%lld\n", "%lld\n",
352                    reipl_block_fcp->ipl_info.fcp.br_lba);
353 DEFINE_IPL_ATTR_RW(reipl_fcp, device, "0.0.%04llx\n", "0.0.%llx\n",
354                    reipl_block_fcp->ipl_info.fcp.devno);
355
356 static struct attribute *reipl_fcp_attrs[] = {
357         &sys_reipl_fcp_device_attr.attr,
358         &sys_reipl_fcp_wwpn_attr.attr,
359         &sys_reipl_fcp_lun_attr.attr,
360         &sys_reipl_fcp_bootprog_attr.attr,
361         &sys_reipl_fcp_br_lba_attr.attr,
362         NULL,
363 };
364
365 static struct attribute_group reipl_fcp_attr_group = {
366         .name  = IPL_FCP_STR,
367         .attrs = reipl_fcp_attrs,
368 };
369
370 /* CCW reipl device attributes */
371
372 DEFINE_IPL_ATTR_RW(reipl_ccw, device, "0.0.%04llx\n", "0.0.%llx\n",
373         reipl_block_ccw->ipl_info.ccw.devno);
374
375 static void reipl_get_ascii_loadparm(char *loadparm)
376 {
377         memcpy(loadparm, &reipl_block_ccw->ipl_info.ccw.load_param,
378                LOADPARM_LEN);
379         EBCASC(loadparm, LOADPARM_LEN);
380         loadparm[LOADPARM_LEN] = 0;
381         strstrip(loadparm);
382 }
383
384 static ssize_t reipl_ccw_loadparm_show(struct subsystem *subsys, char *page)
385 {
386         char buf[LOADPARM_LEN + 1];
387
388         reipl_get_ascii_loadparm(buf);
389         return sprintf(page, "%s\n", buf);
390 }
391
392 static ssize_t reipl_ccw_loadparm_store(struct subsystem *subsys,
393                                         const char *buf, size_t len)
394 {
395         int i, lp_len;
396
397         /* ignore trailing newline */
398         lp_len = len;
399         if ((len > 0) && (buf[len - 1] == '\n'))
400                 lp_len--;
401         /* loadparm can have max 8 characters and must not start with a blank */
402         if ((lp_len > LOADPARM_LEN) || ((lp_len > 0) && (buf[0] == ' ')))
403                 return -EINVAL;
404         /* loadparm can only contain "a-z,A-Z,0-9,SP,." */
405         for (i = 0; i < lp_len; i++) {
406                 if (isalpha(buf[i]) || isdigit(buf[i]) || (buf[i] == ' ') ||
407                     (buf[i] == '.'))
408                         continue;
409                 return -EINVAL;
410         }
411         /* initialize loadparm with blanks */
412         memset(&reipl_block_ccw->ipl_info.ccw.load_param, ' ', LOADPARM_LEN);
413         /* copy and convert to ebcdic */
414         memcpy(&reipl_block_ccw->ipl_info.ccw.load_param, buf, lp_len);
415         ASCEBC(reipl_block_ccw->ipl_info.ccw.load_param, LOADPARM_LEN);
416         return len;
417 }
418
419 static struct subsys_attribute sys_reipl_ccw_loadparm_attr =
420         __ATTR(loadparm, 0644, reipl_ccw_loadparm_show,
421                reipl_ccw_loadparm_store);
422
423 static struct attribute *reipl_ccw_attrs[] = {
424         &sys_reipl_ccw_device_attr.attr,
425         &sys_reipl_ccw_loadparm_attr.attr,
426         NULL,
427 };
428
429 static struct attribute_group reipl_ccw_attr_group = {
430         .name  = IPL_CCW_STR,
431         .attrs = reipl_ccw_attrs,
432 };
433
434 /* reipl type */
435
436 static int reipl_set_type(enum ipl_type type)
437 {
438         if (!(reipl_capabilities & type))
439                 return -EINVAL;
440
441         switch(type) {
442         case IPL_TYPE_CCW:
443                 if (MACHINE_IS_VM)
444                         reipl_method = IPL_METHOD_CCW_VM;
445                 else
446                         reipl_method = IPL_METHOD_CCW_CIO;
447                 break;
448         case IPL_TYPE_FCP:
449                 if (diag308_set_works)
450                         reipl_method = IPL_METHOD_FCP_RW_DIAG;
451                 else if (MACHINE_IS_VM)
452                         reipl_method = IPL_METHOD_FCP_RO_VM;
453                 else
454                         reipl_method = IPL_METHOD_FCP_RO_DIAG;
455                 break;
456         default:
457                 reipl_method = IPL_METHOD_NONE;
458         }
459         reipl_type = type;
460         return 0;
461 }
462
463 static ssize_t reipl_type_show(struct subsystem *subsys, char *page)
464 {
465         return sprintf(page, "%s\n", ipl_type_str(reipl_type));
466 }
467
468 static ssize_t reipl_type_store(struct subsystem *subsys, const char *buf,
469                                 size_t len)
470 {
471         int rc = -EINVAL;
472
473         if (strncmp(buf, IPL_CCW_STR, strlen(IPL_CCW_STR)) == 0)
474                 rc = reipl_set_type(IPL_TYPE_CCW);
475         else if (strncmp(buf, IPL_FCP_STR, strlen(IPL_FCP_STR)) == 0)
476                 rc = reipl_set_type(IPL_TYPE_FCP);
477         return (rc != 0) ? rc : len;
478 }
479
480 static struct subsys_attribute reipl_type_attr =
481                 __ATTR(reipl_type, 0644, reipl_type_show, reipl_type_store);
482
483 static decl_subsys(reipl, NULL, NULL);
484
485 /*
486  * dump section
487  */
488
489 /* FCP dump device attributes */
490
491 DEFINE_IPL_ATTR_RW(dump_fcp, wwpn, "0x%016llx\n", "%016llx\n",
492                    dump_block_fcp->ipl_info.fcp.wwpn);
493 DEFINE_IPL_ATTR_RW(dump_fcp, lun, "0x%016llx\n", "%016llx\n",
494                    dump_block_fcp->ipl_info.fcp.lun);
495 DEFINE_IPL_ATTR_RW(dump_fcp, bootprog, "%lld\n", "%lld\n",
496                    dump_block_fcp->ipl_info.fcp.bootprog);
497 DEFINE_IPL_ATTR_RW(dump_fcp, br_lba, "%lld\n", "%lld\n",
498                    dump_block_fcp->ipl_info.fcp.br_lba);
499 DEFINE_IPL_ATTR_RW(dump_fcp, device, "0.0.%04llx\n", "0.0.%llx\n",
500                    dump_block_fcp->ipl_info.fcp.devno);
501
502 static struct attribute *dump_fcp_attrs[] = {
503         &sys_dump_fcp_device_attr.attr,
504         &sys_dump_fcp_wwpn_attr.attr,
505         &sys_dump_fcp_lun_attr.attr,
506         &sys_dump_fcp_bootprog_attr.attr,
507         &sys_dump_fcp_br_lba_attr.attr,
508         NULL,
509 };
510
511 static struct attribute_group dump_fcp_attr_group = {
512         .name  = IPL_FCP_STR,
513         .attrs = dump_fcp_attrs,
514 };
515
516 /* CCW dump device attributes */
517
518 DEFINE_IPL_ATTR_RW(dump_ccw, device, "0.0.%04llx\n", "0.0.%llx\n",
519                    dump_block_ccw->ipl_info.ccw.devno);
520
521 static struct attribute *dump_ccw_attrs[] = {
522         &sys_dump_ccw_device_attr.attr,
523         NULL,
524 };
525
526 static struct attribute_group dump_ccw_attr_group = {
527         .name  = IPL_CCW_STR,
528         .attrs = dump_ccw_attrs,
529 };
530
531 /* dump type */
532
533 static int dump_set_type(enum ipl_type type)
534 {
535         if (!(dump_capabilities & type))
536                 return -EINVAL;
537         switch(type) {
538         case IPL_TYPE_CCW:
539                 if (MACHINE_IS_VM)
540                         dump_method = IPL_METHOD_CCW_VM;
541                 else
542                         dump_method = IPL_METHOD_CCW_CIO;
543                 break;
544         case IPL_TYPE_FCP:
545                 dump_method = IPL_METHOD_FCP_RW_DIAG;
546                 break;
547         default:
548                 dump_method = IPL_METHOD_NONE;
549         }
550         dump_type = type;
551         return 0;
552 }
553
554 static ssize_t dump_type_show(struct subsystem *subsys, char *page)
555 {
556         return sprintf(page, "%s\n", ipl_type_str(dump_type));
557 }
558
559 static ssize_t dump_type_store(struct subsystem *subsys, const char *buf,
560                                size_t len)
561 {
562         int rc = -EINVAL;
563
564         if (strncmp(buf, IPL_NONE_STR, strlen(IPL_NONE_STR)) == 0)
565                 rc = dump_set_type(IPL_TYPE_NONE);
566         else if (strncmp(buf, IPL_CCW_STR, strlen(IPL_CCW_STR)) == 0)
567                 rc = dump_set_type(IPL_TYPE_CCW);
568         else if (strncmp(buf, IPL_FCP_STR, strlen(IPL_FCP_STR)) == 0)
569                 rc = dump_set_type(IPL_TYPE_FCP);
570         return (rc != 0) ? rc : len;
571 }
572
573 static struct subsys_attribute dump_type_attr =
574                 __ATTR(dump_type, 0644, dump_type_show, dump_type_store);
575
576 static decl_subsys(dump, NULL, NULL);
577
578 #ifdef CONFIG_SMP
579 static void dump_smp_stop_all(void)
580 {
581         int cpu;
582         preempt_disable();
583         for_each_online_cpu(cpu) {
584                 if (cpu == smp_processor_id())
585                         continue;
586                 while (signal_processor(cpu, sigp_stop) == sigp_busy)
587                         udelay(10);
588         }
589         preempt_enable();
590 }
591 #else
592 #define dump_smp_stop_all() do { } while (0)
593 #endif
594
595 /*
596  * Shutdown actions section
597  */
598
599 static decl_subsys(shutdown_actions, NULL, NULL);
600
601 /* on panic */
602
603 static ssize_t on_panic_show(struct subsystem *subsys, char *page)
604 {
605         return sprintf(page, "%s\n", shutdown_action_str(on_panic_action));
606 }
607
608 static ssize_t on_panic_store(struct subsystem *subsys, const char *buf,
609                               size_t len)
610 {
611         if (strncmp(buf, SHUTDOWN_REIPL_STR, strlen(SHUTDOWN_REIPL_STR)) == 0)
612                 on_panic_action = SHUTDOWN_REIPL;
613         else if (strncmp(buf, SHUTDOWN_DUMP_STR,
614                          strlen(SHUTDOWN_DUMP_STR)) == 0)
615                 on_panic_action = SHUTDOWN_DUMP;
616         else if (strncmp(buf, SHUTDOWN_STOP_STR,
617                          strlen(SHUTDOWN_STOP_STR)) == 0)
618                 on_panic_action = SHUTDOWN_STOP;
619         else
620                 return -EINVAL;
621
622         return len;
623 }
624
625 static struct subsys_attribute on_panic_attr =
626                 __ATTR(on_panic, 0644, on_panic_show, on_panic_store);
627
628 static void print_fcp_block(struct ipl_parameter_block *fcp_block)
629 {
630         printk(KERN_EMERG "wwpn:      %016llx\n",
631                 (unsigned long long)fcp_block->ipl_info.fcp.wwpn);
632         printk(KERN_EMERG "lun:       %016llx\n",
633                 (unsigned long long)fcp_block->ipl_info.fcp.lun);
634         printk(KERN_EMERG "bootprog:  %lld\n",
635                 (unsigned long long)fcp_block->ipl_info.fcp.bootprog);
636         printk(KERN_EMERG "br_lba:    %lld\n",
637                 (unsigned long long)fcp_block->ipl_info.fcp.br_lba);
638         printk(KERN_EMERG "device:    %llx\n",
639                 (unsigned long long)fcp_block->ipl_info.fcp.devno);
640         printk(KERN_EMERG "opt:       %x\n", fcp_block->ipl_info.fcp.opt);
641 }
642
643 void do_reipl(void)
644 {
645         struct ccw_dev_id devid;
646         static char buf[100];
647         char loadparm[LOADPARM_LEN + 1];
648
649         switch (reipl_type) {
650         case IPL_TYPE_CCW:
651                 reipl_get_ascii_loadparm(loadparm);
652                 printk(KERN_EMERG "reboot on ccw device: 0.0.%04x\n",
653                         reipl_block_ccw->ipl_info.ccw.devno);
654                 printk(KERN_EMERG "loadparm = '%s'\n", loadparm);
655                 break;
656         case IPL_TYPE_FCP:
657                 printk(KERN_EMERG "reboot on fcp device:\n");
658                 print_fcp_block(reipl_block_fcp);
659                 break;
660         default:
661                 break;
662         }
663
664         switch (reipl_method) {
665         case IPL_METHOD_CCW_CIO:
666                 devid.devno = reipl_block_ccw->ipl_info.ccw.devno;
667                 devid.ssid  = 0;
668                 reipl_ccw_dev(&devid);
669                 break;
670         case IPL_METHOD_CCW_VM:
671                 if (strlen(loadparm) == 0)
672                         sprintf(buf, "IPL %X",
673                                 reipl_block_ccw->ipl_info.ccw.devno);
674                 else
675                         sprintf(buf, "IPL %X LOADPARM '%s'",
676                                 reipl_block_ccw->ipl_info.ccw.devno, loadparm);
677                 cpcmd(buf, NULL, 0, NULL);
678                 break;
679         case IPL_METHOD_CCW_DIAG:
680                 diag308(DIAG308_SET, reipl_block_ccw);
681                 diag308(DIAG308_IPL, NULL);
682                 break;
683         case IPL_METHOD_FCP_RW_DIAG:
684                 diag308(DIAG308_SET, reipl_block_fcp);
685                 diag308(DIAG308_IPL, NULL);
686                 break;
687         case IPL_METHOD_FCP_RO_DIAG:
688                 diag308(DIAG308_IPL, NULL);
689                 break;
690         case IPL_METHOD_FCP_RO_VM:
691                 cpcmd("IPL", NULL, 0, NULL);
692                 break;
693         case IPL_METHOD_NONE:
694         default:
695                 if (MACHINE_IS_VM)
696                         cpcmd("IPL", NULL, 0, NULL);
697                 diag308(DIAG308_IPL, NULL);
698                 break;
699         }
700         panic("reipl failed!\n");
701 }
702
703 static void do_dump(void)
704 {
705         struct ccw_dev_id devid;
706         static char buf[100];
707
708         switch (dump_type) {
709         case IPL_TYPE_CCW:
710                 printk(KERN_EMERG "Automatic dump on ccw device: 0.0.%04x\n",
711                        dump_block_ccw->ipl_info.ccw.devno);
712                 break;
713         case IPL_TYPE_FCP:
714                 printk(KERN_EMERG "Automatic dump on fcp device:\n");
715                 print_fcp_block(dump_block_fcp);
716                 break;
717         default:
718                 return;
719         }
720
721         switch (dump_method) {
722         case IPL_METHOD_CCW_CIO:
723                 dump_smp_stop_all();
724                 devid.devno = dump_block_ccw->ipl_info.ccw.devno;
725                 devid.ssid  = 0;
726                 reipl_ccw_dev(&devid);
727                 break;
728         case IPL_METHOD_CCW_VM:
729                 dump_smp_stop_all();
730                 sprintf(buf, "STORE STATUS");
731                 cpcmd(buf, NULL, 0, NULL);
732                 sprintf(buf, "IPL %X", dump_block_ccw->ipl_info.ccw.devno);
733                 cpcmd(buf, NULL, 0, NULL);
734                 break;
735         case IPL_METHOD_CCW_DIAG:
736                 diag308(DIAG308_SET, dump_block_ccw);
737                 diag308(DIAG308_DUMP, NULL);
738                 break;
739         case IPL_METHOD_FCP_RW_DIAG:
740                 diag308(DIAG308_SET, dump_block_fcp);
741                 diag308(DIAG308_DUMP, NULL);
742                 break;
743         case IPL_METHOD_NONE:
744         default:
745                 return;
746         }
747         printk(KERN_EMERG "Dump failed!\n");
748 }
749
750 /* init functions */
751
752 static int __init ipl_register_fcp_files(void)
753 {
754         int rc;
755
756         rc = sysfs_create_group(&ipl_subsys.kset.kobj,
757                                 &ipl_fcp_attr_group);
758         if (rc)
759                 goto out;
760         rc = sysfs_create_bin_file(&ipl_subsys.kset.kobj,
761                                    &ipl_parameter_attr);
762         if (rc)
763                 goto out_ipl_parm;
764         rc = sysfs_create_bin_file(&ipl_subsys.kset.kobj,
765                                    &ipl_scp_data_attr);
766         if (!rc)
767                 goto out;
768
769         sysfs_remove_bin_file(&ipl_subsys.kset.kobj, &ipl_parameter_attr);
770
771 out_ipl_parm:
772         sysfs_remove_group(&ipl_subsys.kset.kobj, &ipl_fcp_attr_group);
773 out:
774         return rc;
775 }
776
777 static int __init ipl_init(void)
778 {
779         int rc;
780
781         rc = firmware_register(&ipl_subsys);
782         if (rc)
783                 return rc;
784         switch (ipl_get_type()) {
785         case IPL_TYPE_CCW:
786                 rc = sysfs_create_group(&ipl_subsys.kset.kobj,
787                                         &ipl_ccw_attr_group);
788                 break;
789         case IPL_TYPE_FCP:
790                 rc = ipl_register_fcp_files();
791                 break;
792         default:
793                 rc = sysfs_create_group(&ipl_subsys.kset.kobj,
794                                         &ipl_unknown_attr_group);
795                 break;
796         }
797         if (rc)
798                 firmware_unregister(&ipl_subsys);
799         return rc;
800 }
801
802 static void __init reipl_probe(void)
803 {
804         void *buffer;
805
806         buffer = (void *) get_zeroed_page(GFP_KERNEL);
807         if (!buffer)
808                 return;
809         if (diag308(DIAG308_STORE, buffer) == DIAG308_RC_OK)
810                 diag308_set_works = 1;
811         free_page((unsigned long)buffer);
812 }
813
814 static int __init reipl_ccw_init(void)
815 {
816         int rc;
817
818         reipl_block_ccw = (void *) get_zeroed_page(GFP_KERNEL);
819         if (!reipl_block_ccw)
820                 return -ENOMEM;
821         rc = sysfs_create_group(&reipl_subsys.kset.kobj, &reipl_ccw_attr_group);
822         if (rc) {
823                 free_page((unsigned long)reipl_block_ccw);
824                 return rc;
825         }
826         reipl_block_ccw->hdr.len = IPL_PARM_BLK_CCW_LEN;
827         reipl_block_ccw->hdr.version = IPL_PARM_BLOCK_VERSION;
828         reipl_block_ccw->hdr.blk0_len = sizeof(reipl_block_ccw->ipl_info.ccw);
829         reipl_block_ccw->hdr.pbt = DIAG308_IPL_TYPE_CCW;
830         /* check if read scp info worked and set loadparm */
831         if (SCCB_VALID)
832                 memcpy(reipl_block_ccw->ipl_info.ccw.load_param,
833                        SCCB_LOADPARM, LOADPARM_LEN);
834         else
835                 /* read scp info failed: set empty loadparm (EBCDIC blanks) */
836                 memset(reipl_block_ccw->ipl_info.ccw.load_param, 0x40,
837                        LOADPARM_LEN);
838         /* FIXME: check for diag308_set_works when enabling diag ccw reipl */
839         if (!MACHINE_IS_VM)
840                 sys_reipl_ccw_loadparm_attr.attr.mode = S_IRUGO;
841         if (ipl_get_type() == IPL_TYPE_CCW)
842                 reipl_block_ccw->ipl_info.ccw.devno = ipl_devno;
843         reipl_capabilities |= IPL_TYPE_CCW;
844         return 0;
845 }
846
847 static int __init reipl_fcp_init(void)
848 {
849         int rc;
850
851         if ((!diag308_set_works) && (ipl_get_type() != IPL_TYPE_FCP))
852                 return 0;
853         if ((!diag308_set_works) && (ipl_get_type() == IPL_TYPE_FCP))
854                 make_attrs_ro(reipl_fcp_attrs);
855
856         reipl_block_fcp = (void *) get_zeroed_page(GFP_KERNEL);
857         if (!reipl_block_fcp)
858                 return -ENOMEM;
859         rc = sysfs_create_group(&reipl_subsys.kset.kobj, &reipl_fcp_attr_group);
860         if (rc) {
861                 free_page((unsigned long)reipl_block_fcp);
862                 return rc;
863         }
864         if (ipl_get_type() == IPL_TYPE_FCP) {
865                 memcpy(reipl_block_fcp, IPL_PARMBLOCK_START, PAGE_SIZE);
866         } else {
867                 reipl_block_fcp->hdr.len = IPL_PARM_BLK_FCP_LEN;
868                 reipl_block_fcp->hdr.version = IPL_PARM_BLOCK_VERSION;
869                 reipl_block_fcp->hdr.blk0_len =
870                         sizeof(reipl_block_fcp->ipl_info.fcp);
871                 reipl_block_fcp->hdr.pbt = DIAG308_IPL_TYPE_FCP;
872                 reipl_block_fcp->ipl_info.fcp.opt = DIAG308_IPL_OPT_IPL;
873         }
874         reipl_capabilities |= IPL_TYPE_FCP;
875         return 0;
876 }
877
878 static int __init reipl_init(void)
879 {
880         int rc;
881
882         rc = firmware_register(&reipl_subsys);
883         if (rc)
884                 return rc;
885         rc = subsys_create_file(&reipl_subsys, &reipl_type_attr);
886         if (rc) {
887                 firmware_unregister(&reipl_subsys);
888                 return rc;
889         }
890         rc = reipl_ccw_init();
891         if (rc)
892                 return rc;
893         rc = reipl_fcp_init();
894         if (rc)
895                 return rc;
896         rc = reipl_set_type(ipl_get_type());
897         if (rc)
898                 return rc;
899         return 0;
900 }
901
902 static int __init dump_ccw_init(void)
903 {
904         int rc;
905
906         dump_block_ccw = (void *) get_zeroed_page(GFP_KERNEL);
907         if (!dump_block_ccw)
908                 return -ENOMEM;
909         rc = sysfs_create_group(&dump_subsys.kset.kobj, &dump_ccw_attr_group);
910         if (rc) {
911                 free_page((unsigned long)dump_block_ccw);
912                 return rc;
913         }
914         dump_block_ccw->hdr.len = IPL_PARM_BLK_CCW_LEN;
915         dump_block_ccw->hdr.version = IPL_PARM_BLOCK_VERSION;
916         dump_block_ccw->hdr.blk0_len = sizeof(reipl_block_ccw->ipl_info.ccw);
917         dump_block_ccw->hdr.pbt = DIAG308_IPL_TYPE_CCW;
918         dump_capabilities |= IPL_TYPE_CCW;
919         return 0;
920 }
921
922 static int __init dump_fcp_init(void)
923 {
924         int rc;
925
926         if(!(SCCB_FLAG & 0x2) || !SCCB_VALID)
927                 return 0; /* LDIPL DUMP is not installed */
928         if (!diag308_set_works)
929                 return 0;
930         dump_block_fcp = (void *) get_zeroed_page(GFP_KERNEL);
931         if (!dump_block_fcp)
932                 return -ENOMEM;
933         rc = sysfs_create_group(&dump_subsys.kset.kobj, &dump_fcp_attr_group);
934         if (rc) {
935                 free_page((unsigned long)dump_block_fcp);
936                 return rc;
937         }
938         dump_block_fcp->hdr.len = IPL_PARM_BLK_FCP_LEN;
939         dump_block_fcp->hdr.version = IPL_PARM_BLOCK_VERSION;
940         dump_block_fcp->hdr.blk0_len = sizeof(dump_block_fcp->ipl_info.fcp);
941         dump_block_fcp->hdr.pbt = DIAG308_IPL_TYPE_FCP;
942         dump_block_fcp->ipl_info.fcp.opt = DIAG308_IPL_OPT_DUMP;
943         dump_capabilities |= IPL_TYPE_FCP;
944         return 0;
945 }
946
947 #define SHUTDOWN_ON_PANIC_PRIO 0
948
949 static int shutdown_on_panic_notify(struct notifier_block *self,
950                                     unsigned long event, void *data)
951 {
952         if (on_panic_action == SHUTDOWN_DUMP)
953                 do_dump();
954         else if (on_panic_action == SHUTDOWN_REIPL)
955                 do_reipl();
956         return NOTIFY_OK;
957 }
958
959 static struct notifier_block shutdown_on_panic_nb = {
960         .notifier_call = shutdown_on_panic_notify,
961         .priority = SHUTDOWN_ON_PANIC_PRIO
962 };
963
964 static int __init dump_init(void)
965 {
966         int rc;
967
968         rc = firmware_register(&dump_subsys);
969         if (rc)
970                 return rc;
971         rc = subsys_create_file(&dump_subsys, &dump_type_attr);
972         if (rc) {
973                 firmware_unregister(&dump_subsys);
974                 return rc;
975         }
976         rc = dump_ccw_init();
977         if (rc)
978                 return rc;
979         rc = dump_fcp_init();
980         if (rc)
981                 return rc;
982         dump_set_type(IPL_TYPE_NONE);
983         return 0;
984 }
985
986 static int __init shutdown_actions_init(void)
987 {
988         int rc;
989
990         rc = firmware_register(&shutdown_actions_subsys);
991         if (rc)
992                 return rc;
993         rc = subsys_create_file(&shutdown_actions_subsys, &on_panic_attr);
994         if (rc) {
995                 firmware_unregister(&shutdown_actions_subsys);
996                 return rc;
997         }
998         atomic_notifier_chain_register(&panic_notifier_list,
999                                        &shutdown_on_panic_nb);
1000         return 0;
1001 }
1002
1003 static int __init s390_ipl_init(void)
1004 {
1005         int rc;
1006
1007         reipl_probe();
1008         rc = ipl_init();
1009         if (rc)
1010                 return rc;
1011         rc = reipl_init();
1012         if (rc)
1013                 return rc;
1014         rc = dump_init();
1015         if (rc)
1016                 return rc;
1017         rc = shutdown_actions_init();
1018         if (rc)
1019                 return rc;
1020         return 0;
1021 }
1022
1023 __initcall(s390_ipl_init);