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