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