ipl.c 50.9 KB
Newer Older
M
Michael Holzheu 已提交
1 2 3 4
/*
 *  arch/s390/kernel/ipl.c
 *    ipl/reipl/dump support for Linux on s390.
 *
M
Martin Schwidefsky 已提交
5
 *    Copyright IBM Corp. 2005,2012
M
Michael Holzheu 已提交
6 7 8 9 10 11 12 13 14 15
 *    Author(s): Michael Holzheu <holzheu@de.ibm.com>
 *		 Heiko Carstens <heiko.carstens@de.ibm.com>
 *		 Volker Sameske <sameske@de.ibm.com>
 */

#include <linux/types.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/delay.h>
#include <linux/reboot.h>
16
#include <linux/ctype.h>
17
#include <linux/fs.h>
18
#include <linux/gfp.h>
M
Michael Holzheu 已提交
19
#include <linux/crash_dump.h>
20
#include <linux/debug_locks.h>
M
Michael Holzheu 已提交
21
#include <asm/ipl.h>
M
Michael Holzheu 已提交
22 23 24 25
#include <asm/smp.h>
#include <asm/setup.h>
#include <asm/cpcmd.h>
#include <asm/cio.h>
26
#include <asm/ebcdic.h>
27
#include <asm/reset.h>
28
#include <asm/sclp.h>
29
#include <asm/checksum.h>
30
#include <asm/debug.h>
31
#include <asm/os_info.h>
32
#include "entry.h"
M
Michael Holzheu 已提交
33 34

#define IPL_PARM_BLOCK_VERSION 0
35

M
Michael Holzheu 已提交
36 37 38 39 40
#define IPL_UNKNOWN_STR		"unknown"
#define IPL_CCW_STR		"ccw"
#define IPL_FCP_STR		"fcp"
#define IPL_FCP_DUMP_STR	"fcp_dump"
#define IPL_NSS_STR		"nss"
41

42 43 44 45 46 47 48 49 50 51
#define DUMP_CCW_STR		"ccw"
#define DUMP_FCP_STR		"fcp"
#define DUMP_NONE_STR		"none"

/*
 * Four shutdown trigger types are supported:
 * - panic
 * - halt
 * - power off
 * - reipl
52
 * - restart
53 54 55 56 57
 */
#define ON_PANIC_STR		"on_panic"
#define ON_HALT_STR		"on_halt"
#define ON_POFF_STR		"on_poff"
#define ON_REIPL_STR		"on_reboot"
58
#define ON_RESTART_STR		"on_restart"
59 60 61 62 63 64 65 66

struct shutdown_action;
struct shutdown_trigger {
	char *name;
	struct shutdown_action *action;
};

/*
F
Frank Munzert 已提交
67
 * The following shutdown action types are supported:
68 69 70 71 72 73
 */
#define SHUTDOWN_ACTION_IPL_STR		"ipl"
#define SHUTDOWN_ACTION_REIPL_STR	"reipl"
#define SHUTDOWN_ACTION_DUMP_STR	"dump"
#define SHUTDOWN_ACTION_VMCMD_STR	"vmcmd"
#define SHUTDOWN_ACTION_STOP_STR	"stop"
F
Frank Munzert 已提交
74
#define SHUTDOWN_ACTION_DUMP_REIPL_STR	"dump_reipl"
75 76 77 78 79

struct shutdown_action {
	char *name;
	void (*fn) (struct shutdown_trigger *trigger);
	int (*init) (void);
80
	int init_rc;
81 82
};

M
Michael Holzheu 已提交
83 84 85 86 87 88 89
static char *ipl_type_str(enum ipl_type type)
{
	switch (type) {
	case IPL_TYPE_CCW:
		return IPL_CCW_STR;
	case IPL_TYPE_FCP:
		return IPL_FCP_STR;
M
Michael Holzheu 已提交
90 91
	case IPL_TYPE_FCP_DUMP:
		return IPL_FCP_DUMP_STR;
H
Hongjie Yang 已提交
92 93
	case IPL_TYPE_NSS:
		return IPL_NSS_STR;
M
Michael Holzheu 已提交
94 95 96 97 98 99
	case IPL_TYPE_UNKNOWN:
	default:
		return IPL_UNKNOWN_STR;
	}
}

M
Michael Holzheu 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
enum dump_type {
	DUMP_TYPE_NONE	= 1,
	DUMP_TYPE_CCW	= 2,
	DUMP_TYPE_FCP	= 4,
};

static char *dump_type_str(enum dump_type type)
{
	switch (type) {
	case DUMP_TYPE_NONE:
		return DUMP_NONE_STR;
	case DUMP_TYPE_CCW:
		return DUMP_CCW_STR;
	case DUMP_TYPE_FCP:
		return DUMP_FCP_STR;
	default:
		return NULL;
	}
}

/*
 * Must be in data section since the bss section
 * is not cleared when these are accessed.
 */
static u16 ipl_devno __attribute__((__section__(".data"))) = 0;
u32 ipl_flags __attribute__((__section__(".data"))) = 0;

M
Michael Holzheu 已提交
127
enum ipl_method {
M
Michael Holzheu 已提交
128 129 130 131 132 133 134 135
	REIPL_METHOD_CCW_CIO,
	REIPL_METHOD_CCW_DIAG,
	REIPL_METHOD_CCW_VM,
	REIPL_METHOD_FCP_RO_DIAG,
	REIPL_METHOD_FCP_RW_DIAG,
	REIPL_METHOD_FCP_RO_VM,
	REIPL_METHOD_FCP_DUMP,
	REIPL_METHOD_NSS,
136
	REIPL_METHOD_NSS_DIAG,
M
Michael Holzheu 已提交
137 138 139 140 141 142 143 144 145
	REIPL_METHOD_DEFAULT,
};

enum dump_method {
	DUMP_METHOD_NONE,
	DUMP_METHOD_CCW_CIO,
	DUMP_METHOD_CCW_DIAG,
	DUMP_METHOD_CCW_VM,
	DUMP_METHOD_FCP_DIAG,
M
Michael Holzheu 已提交
146 147 148 149
};

static int diag308_set_works = 0;

150 151
static struct ipl_parameter_block ipl_block;

M
Michael Holzheu 已提交
152
static int reipl_capabilities = IPL_TYPE_UNKNOWN;
H
Hongjie Yang 已提交
153

M
Michael Holzheu 已提交
154
static enum ipl_type reipl_type = IPL_TYPE_UNKNOWN;
M
Michael Holzheu 已提交
155
static enum ipl_method reipl_method = REIPL_METHOD_DEFAULT;
M
Michael Holzheu 已提交
156 157
static struct ipl_parameter_block *reipl_block_fcp;
static struct ipl_parameter_block *reipl_block_ccw;
158
static struct ipl_parameter_block *reipl_block_nss;
F
Frank Munzert 已提交
159
static struct ipl_parameter_block *reipl_block_actual;
H
Hongjie Yang 已提交
160

M
Michael Holzheu 已提交
161 162 163
static int dump_capabilities = DUMP_TYPE_NONE;
static enum dump_type dump_type = DUMP_TYPE_NONE;
static enum dump_method dump_method = DUMP_METHOD_NONE;
M
Michael Holzheu 已提交
164 165 166
static struct ipl_parameter_block *dump_block_fcp;
static struct ipl_parameter_block *dump_block_ccw;

167 168
static struct sclp_ipl_info sclp_ipl_info;

M
Michael Holzheu 已提交
169
int diag308(unsigned long subcode, void *addr)
M
Michael Holzheu 已提交
170
{
171
	register unsigned long _addr asm("0") = (unsigned long) addr;
M
Michael Holzheu 已提交
172 173
	register unsigned long _rc asm("1") = 0;

174 175 176 177
	asm volatile(
		"	diag	%0,%2,0x308\n"
		"0:\n"
		EX_TABLE(0b,0b)
M
Michael Holzheu 已提交
178
		: "+d" (_addr), "+d" (_rc)
179
		: "d" (subcode) : "cc", "memory");
M
Michael Holzheu 已提交
180 181
	return _rc;
}
M
Michael Holzheu 已提交
182
EXPORT_SYMBOL_GPL(diag308);
M
Michael Holzheu 已提交
183 184 185 186

/* SYSFS */

#define DEFINE_IPL_ATTR_RO(_prefix, _name, _format, _value)		\
187 188
static ssize_t sys_##_prefix##_##_name##_show(struct kobject *kobj,	\
		struct kobj_attribute *attr,				\
M
Michael Holzheu 已提交
189 190 191 192
		char *page)						\
{									\
	return sprintf(page, _format, _value);				\
}									\
193
static struct kobj_attribute sys_##_prefix##_##_name##_attr =		\
M
Michael Holzheu 已提交
194 195 196
	__ATTR(_name, S_IRUGO, sys_##_prefix##_##_name##_show, NULL);

#define DEFINE_IPL_ATTR_RW(_prefix, _name, _fmt_out, _fmt_in, _value)	\
197 198
static ssize_t sys_##_prefix##_##_name##_show(struct kobject *kobj,	\
		struct kobj_attribute *attr,				\
M
Michael Holzheu 已提交
199 200 201 202 203
		char *page)						\
{									\
	return sprintf(page, _fmt_out,					\
			(unsigned long long) _value);			\
}									\
204 205
static ssize_t sys_##_prefix##_##_name##_store(struct kobject *kobj,	\
		struct kobj_attribute *attr,				\
M
Michael Holzheu 已提交
206 207 208 209 210 211 212 213
		const char *buf, size_t len)				\
{									\
	unsigned long long value;					\
	if (sscanf(buf, _fmt_in, &value) != 1)				\
		return -EINVAL;						\
	_value = value;							\
	return len;							\
}									\
214
static struct kobj_attribute sys_##_prefix##_##_name##_attr =		\
M
Michael Holzheu 已提交
215 216 217 218
	__ATTR(_name,(S_IRUGO | S_IWUSR),				\
			sys_##_prefix##_##_name##_show,			\
			sys_##_prefix##_##_name##_store);

H
Hongjie Yang 已提交
219
#define DEFINE_IPL_ATTR_STR_RW(_prefix, _name, _fmt_out, _fmt_in, _value)\
220 221
static ssize_t sys_##_prefix##_##_name##_show(struct kobject *kobj,	\
		struct kobj_attribute *attr,				\
H
Hongjie Yang 已提交
222 223 224 225
		char *page)						\
{									\
	return sprintf(page, _fmt_out, _value);				\
}									\
226 227
static ssize_t sys_##_prefix##_##_name##_store(struct kobject *kobj,	\
		struct kobj_attribute *attr,				\
H
Hongjie Yang 已提交
228 229
		const char *buf, size_t len)				\
{									\
230
	strncpy(_value, buf, sizeof(_value) - 1);			\
231
	strim(_value);							\
H
Hongjie Yang 已提交
232 233
	return len;							\
}									\
234
static struct kobj_attribute sys_##_prefix##_##_name##_attr =		\
H
Hongjie Yang 已提交
235 236 237 238
	__ATTR(_name,(S_IRUGO | S_IWUSR),				\
			sys_##_prefix##_##_name##_show,			\
			sys_##_prefix##_##_name##_store);

M
Michael Holzheu 已提交
239 240 241 242 243 244 245 246 247 248 249 250
static void make_attrs_ro(struct attribute **attrs)
{
	while (*attrs) {
		(*attrs)->mode = S_IRUGO;
		attrs++;
	}
}

/*
 * ipl section
 */

M
Michael Holzheu 已提交
251
static __init enum ipl_type get_ipl_type(void)
M
Michael Holzheu 已提交
252 253 254
{
	struct ipl_parameter_block *ipl = IPL_PARMBLOCK_START;

H
Hongjie Yang 已提交
255 256
	if (ipl_flags & IPL_NSS_VALID)
		return IPL_TYPE_NSS;
257
	if (!(ipl_flags & IPL_DEVNO_VALID))
M
Michael Holzheu 已提交
258
		return IPL_TYPE_UNKNOWN;
259
	if (!(ipl_flags & IPL_PARMBLOCK_VALID))
M
Michael Holzheu 已提交
260 261 262 263 264
		return IPL_TYPE_CCW;
	if (ipl->hdr.version > IPL_MAX_SUPPORTED_VERSION)
		return IPL_TYPE_UNKNOWN;
	if (ipl->hdr.pbt != DIAG308_IPL_TYPE_FCP)
		return IPL_TYPE_UNKNOWN;
M
Michael Holzheu 已提交
265 266
	if (ipl->ipl_info.fcp.opt == DIAG308_IPL_OPT_DUMP)
		return IPL_TYPE_FCP_DUMP;
M
Michael Holzheu 已提交
267 268 269
	return IPL_TYPE_FCP;
}

M
Michael Holzheu 已提交
270 271 272
struct ipl_info ipl_info;
EXPORT_SYMBOL_GPL(ipl_info);

273 274
static ssize_t ipl_type_show(struct kobject *kobj, struct kobj_attribute *attr,
			     char *page)
M
Michael Holzheu 已提交
275
{
M
Michael Holzheu 已提交
276
	return sprintf(page, "%s\n", ipl_type_str(ipl_info.type));
M
Michael Holzheu 已提交
277 278
}

279
static struct kobj_attribute sys_ipl_type_attr = __ATTR_RO(ipl_type);
M
Michael Holzheu 已提交
280

281
/* VM IPL PARM routines */
282 283
static size_t reipl_get_ascii_vmparm(char *dest, size_t size,
				     const struct ipl_parameter_block *ipb)
284 285
{
	int i;
286
	size_t len;
287 288
	char has_lowercase = 0;

289
	len = 0;
290 291 292
	if ((ipb->ipl_info.ccw.vm_flags & DIAG308_VM_FLAGS_VP_VALID) &&
	    (ipb->ipl_info.ccw.vm_parm_len > 0)) {

293
		len = min_t(size_t, size - 1, ipb->ipl_info.ccw.vm_parm_len);
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
		memcpy(dest, ipb->ipl_info.ccw.vm_parm, len);
		/* If at least one character is lowercase, we assume mixed
		 * case; otherwise we convert everything to lowercase.
		 */
		for (i = 0; i < len; i++)
			if ((dest[i] > 0x80 && dest[i] < 0x8a) || /* a-i */
			    (dest[i] > 0x90 && dest[i] < 0x9a) || /* j-r */
			    (dest[i] > 0xa1 && dest[i] < 0xaa)) { /* s-z */
				has_lowercase = 1;
				break;
			}
		if (!has_lowercase)
			EBC_TOLOWER(dest, len);
		EBCASC(dest, len);
	}
	dest[len] = 0;
310 311

	return len;
312 313
}

314
size_t append_ipl_vmparm(char *dest, size_t size)
315
{
316 317 318
	size_t rc;

	rc = 0;
319
	if (diag308_set_works && (ipl_block.hdr.pbt == DIAG308_IPL_TYPE_CCW))
320
		rc = reipl_get_ascii_vmparm(dest, size, &ipl_block);
321 322
	else
		dest[0] = 0;
323
	return rc;
324 325 326 327 328 329 330
}

static ssize_t ipl_vm_parm_show(struct kobject *kobj,
				struct kobj_attribute *attr, char *page)
{
	char parm[DIAG308_VMPARM_SIZE + 1] = {};

331
	append_ipl_vmparm(parm, sizeof(parm));
332 333 334
	return sprintf(page, "%s\n", parm);
}

335 336 337 338 339 340 341 342 343 344
static size_t scpdata_length(const char* buf, size_t count)
{
	while (count) {
		if (buf[count - 1] != '\0' && buf[count - 1] != ' ')
			break;
		count--;
	}
	return count;
}

345 346
static size_t reipl_append_ascii_scpdata(char *dest, size_t size,
					 const struct ipl_parameter_block *ipb)
347 348 349
{
	size_t count;
	size_t i;
350
	int has_lowercase;
351 352 353 354 355 356

	count = min(size - 1, scpdata_length(ipb->ipl_info.fcp.scp_data,
					     ipb->ipl_info.fcp.scp_data_len));
	if (!count)
		goto out;

357 358
	has_lowercase = 0;
	for (i = 0; i < count; i++) {
359 360 361 362
		if (!isascii(ipb->ipl_info.fcp.scp_data[i])) {
			count = 0;
			goto out;
		}
363 364 365
		if (!has_lowercase && islower(ipb->ipl_info.fcp.scp_data[i]))
			has_lowercase = 1;
	}
366

367 368 369 370 371
	if (has_lowercase)
		memcpy(dest, ipb->ipl_info.fcp.scp_data, count);
	else
		for (i = 0; i < count; i++)
			dest[i] = tolower(ipb->ipl_info.fcp.scp_data[i]);
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
out:
	dest[count] = '\0';
	return count;
}

size_t append_ipl_scpdata(char *dest, size_t len)
{
	size_t rc;

	rc = 0;
	if (ipl_block.hdr.pbt == DIAG308_IPL_TYPE_FCP)
		rc = reipl_append_ascii_scpdata(dest, len, &ipl_block);
	else
		dest[0] = 0;
	return rc;
}


390 391 392
static struct kobj_attribute sys_ipl_vm_parm_attr =
	__ATTR(parm, S_IRUGO, ipl_vm_parm_show, NULL);

393 394
static ssize_t sys_ipl_device_show(struct kobject *kobj,
				   struct kobj_attribute *attr, char *page)
M
Michael Holzheu 已提交
395 396 397
{
	struct ipl_parameter_block *ipl = IPL_PARMBLOCK_START;

M
Michael Holzheu 已提交
398
	switch (ipl_info.type) {
M
Michael Holzheu 已提交
399 400 401
	case IPL_TYPE_CCW:
		return sprintf(page, "0.0.%04x\n", ipl_devno);
	case IPL_TYPE_FCP:
M
Michael Holzheu 已提交
402
	case IPL_TYPE_FCP_DUMP:
M
Michael Holzheu 已提交
403 404 405 406 407 408
		return sprintf(page, "0.0.%04x\n", ipl->ipl_info.fcp.devno);
	default:
		return 0;
	}
}

409
static struct kobj_attribute sys_ipl_device_attr =
M
Michael Holzheu 已提交
410 411
	__ATTR(device, S_IRUGO, sys_ipl_device_show, NULL);

412 413 414
static ssize_t ipl_parameter_read(struct file *filp, struct kobject *kobj,
				  struct bin_attribute *attr, char *buf,
				  loff_t off, size_t count)
M
Michael Holzheu 已提交
415
{
416 417
	return memory_read_from_buffer(buf, count, &off, IPL_PARMBLOCK_START,
					IPL_PARMBLOCK_SIZE);
M
Michael Holzheu 已提交
418 419 420 421 422 423 424 425 426 427 428
}

static struct bin_attribute ipl_parameter_attr = {
	.attr = {
		.name = "binary_parameter",
		.mode = S_IRUGO,
	},
	.size = PAGE_SIZE,
	.read = &ipl_parameter_read,
};

429 430 431
static ssize_t ipl_scp_data_read(struct file *filp, struct kobject *kobj,
				 struct bin_attribute *attr, char *buf,
				 loff_t off, size_t count)
M
Michael Holzheu 已提交
432 433 434 435
{
	unsigned int size = IPL_PARMBLOCK_START->ipl_info.fcp.scp_data_len;
	void *scp_data = &IPL_PARMBLOCK_START->ipl_info.fcp.scp_data;

436
	return memory_read_from_buffer(buf, count, &off, scp_data, size);
M
Michael Holzheu 已提交
437 438 439 440 441 442 443 444
}

static struct bin_attribute ipl_scp_data_attr = {
	.attr = {
		.name = "scp_data",
		.mode = S_IRUGO,
	},
	.size = PAGE_SIZE,
445
	.read = ipl_scp_data_read,
M
Michael Holzheu 已提交
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
};

/* FCP ipl device attributes */

DEFINE_IPL_ATTR_RO(ipl_fcp, wwpn, "0x%016llx\n", (unsigned long long)
		   IPL_PARMBLOCK_START->ipl_info.fcp.wwpn);
DEFINE_IPL_ATTR_RO(ipl_fcp, lun, "0x%016llx\n", (unsigned long long)
		   IPL_PARMBLOCK_START->ipl_info.fcp.lun);
DEFINE_IPL_ATTR_RO(ipl_fcp, bootprog, "%lld\n", (unsigned long long)
		   IPL_PARMBLOCK_START->ipl_info.fcp.bootprog);
DEFINE_IPL_ATTR_RO(ipl_fcp, br_lba, "%lld\n", (unsigned long long)
		   IPL_PARMBLOCK_START->ipl_info.fcp.br_lba);

static struct attribute *ipl_fcp_attrs[] = {
	&sys_ipl_type_attr.attr,
	&sys_ipl_device_attr.attr,
	&sys_ipl_fcp_wwpn_attr.attr,
	&sys_ipl_fcp_lun_attr.attr,
	&sys_ipl_fcp_bootprog_attr.attr,
	&sys_ipl_fcp_br_lba_attr.attr,
	NULL,
};

static struct attribute_group ipl_fcp_attr_group = {
	.attrs = ipl_fcp_attrs,
};

/* CCW ipl device attributes */

475 476
static ssize_t ipl_ccw_loadparm_show(struct kobject *kobj,
				     struct kobj_attribute *attr, char *page)
477 478 479
{
	char loadparm[LOADPARM_LEN + 1] = {};

480
	if (!sclp_ipl_info.is_valid)
481
		return sprintf(page, "#unknown#\n");
482
	memcpy(loadparm, &sclp_ipl_info.loadparm, LOADPARM_LEN);
483
	EBCASC(loadparm, LOADPARM_LEN);
484
	strim(loadparm);
485 486 487
	return sprintf(page, "%s\n", loadparm);
}

488
static struct kobj_attribute sys_ipl_ccw_loadparm_attr =
489 490
	__ATTR(loadparm, 0444, ipl_ccw_loadparm_show, NULL);

491 492 493 494 495 496 497 498 499
static struct attribute *ipl_ccw_attrs_vm[] = {
	&sys_ipl_type_attr.attr,
	&sys_ipl_device_attr.attr,
	&sys_ipl_ccw_loadparm_attr.attr,
	&sys_ipl_vm_parm_attr.attr,
	NULL,
};

static struct attribute *ipl_ccw_attrs_lpar[] = {
M
Michael Holzheu 已提交
500 501
	&sys_ipl_type_attr.attr,
	&sys_ipl_device_attr.attr,
502
	&sys_ipl_ccw_loadparm_attr.attr,
M
Michael Holzheu 已提交
503 504 505
	NULL,
};

506 507 508 509 510 511
static struct attribute_group ipl_ccw_attr_group_vm = {
	.attrs = ipl_ccw_attrs_vm,
};

static struct attribute_group ipl_ccw_attr_group_lpar = {
	.attrs = ipl_ccw_attrs_lpar
M
Michael Holzheu 已提交
512 513
};

H
Hongjie Yang 已提交
514 515 516 517 518 519 520
/* NSS ipl device attributes */

DEFINE_IPL_ATTR_RO(ipl_nss, name, "%s\n", kernel_nss_name);

static struct attribute *ipl_nss_attrs[] = {
	&sys_ipl_type_attr.attr,
	&sys_ipl_nss_name_attr.attr,
521 522
	&sys_ipl_ccw_loadparm_attr.attr,
	&sys_ipl_vm_parm_attr.attr,
H
Hongjie Yang 已提交
523 524 525 526 527 528 529
	NULL,
};

static struct attribute_group ipl_nss_attr_group = {
	.attrs = ipl_nss_attrs,
};

M
Michael Holzheu 已提交
530 531 532 533 534 535 536 537 538 539 540
/* UNKNOWN ipl device attributes */

static struct attribute *ipl_unknown_attrs[] = {
	&sys_ipl_type_attr.attr,
	NULL,
};

static struct attribute_group ipl_unknown_attr_group = {
	.attrs = ipl_unknown_attrs,
};

541
static struct kset *ipl_kset;
M
Michael Holzheu 已提交
542

543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
static int __init ipl_register_fcp_files(void)
{
	int rc;

	rc = sysfs_create_group(&ipl_kset->kobj, &ipl_fcp_attr_group);
	if (rc)
		goto out;
	rc = sysfs_create_bin_file(&ipl_kset->kobj, &ipl_parameter_attr);
	if (rc)
		goto out_ipl_parm;
	rc = sysfs_create_bin_file(&ipl_kset->kobj, &ipl_scp_data_attr);
	if (!rc)
		goto out;

	sysfs_remove_bin_file(&ipl_kset->kobj, &ipl_parameter_attr);

out_ipl_parm:
	sysfs_remove_group(&ipl_kset->kobj, &ipl_fcp_attr_group);
out:
	return rc;
}

565
static void __ipl_run(void *unused)
566 567 568 569 570 571 572 573
{
	diag308(DIAG308_IPL, NULL);
	if (MACHINE_IS_VM)
		__cpcmd("IPL", NULL, 0, NULL);
	else if (ipl_info.type == IPL_TYPE_CCW)
		reipl_ccw_dev(&ipl_info.data.ccw.dev_id);
}

574 575
static void ipl_run(struct shutdown_trigger *trigger)
{
M
Martin Schwidefsky 已提交
576
	smp_call_ipl_cpu(__ipl_run, NULL);
577 578
}

579
static int __init ipl_init(void)
580 581 582 583 584 585 586 587 588 589
{
	int rc;

	ipl_kset = kset_create_and_add("ipl", NULL, firmware_kobj);
	if (!ipl_kset) {
		rc = -ENOMEM;
		goto out;
	}
	switch (ipl_info.type) {
	case IPL_TYPE_CCW:
590 591 592 593 594 595
		if (MACHINE_IS_VM)
			rc = sysfs_create_group(&ipl_kset->kobj,
						&ipl_ccw_attr_group_vm);
		else
			rc = sysfs_create_group(&ipl_kset->kobj,
						&ipl_ccw_attr_group_lpar);
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
		break;
	case IPL_TYPE_FCP:
	case IPL_TYPE_FCP_DUMP:
		rc = ipl_register_fcp_files();
		break;
	case IPL_TYPE_NSS:
		rc = sysfs_create_group(&ipl_kset->kobj, &ipl_nss_attr_group);
		break;
	default:
		rc = sysfs_create_group(&ipl_kset->kobj,
					&ipl_unknown_attr_group);
		break;
	}
out:
	if (rc)
		panic("ipl_init failed: rc = %i\n", rc);

	return 0;
}

616 617 618 619 620
static struct shutdown_action __refdata ipl_action = {
	.name	= SHUTDOWN_ACTION_IPL_STR,
	.fn	= ipl_run,
	.init	= ipl_init,
};
621

M
Michael Holzheu 已提交
622
/*
623
 * reipl shutdown action: Reboot Linux on shutdown.
M
Michael Holzheu 已提交
624 625
 */

626 627 628 629 630 631
/* VM IPL PARM attributes */
static ssize_t reipl_generic_vmparm_show(struct ipl_parameter_block *ipb,
					  char *page)
{
	char vmparm[DIAG308_VMPARM_SIZE + 1] = {};

632
	reipl_get_ascii_vmparm(vmparm, sizeof(vmparm), ipb);
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
	return sprintf(page, "%s\n", vmparm);
}

static ssize_t reipl_generic_vmparm_store(struct ipl_parameter_block *ipb,
					  size_t vmparm_max,
					  const char *buf, size_t len)
{
	int i, ip_len;

	/* ignore trailing newline */
	ip_len = len;
	if ((len > 0) && (buf[len - 1] == '\n'))
		ip_len--;

	if (ip_len > vmparm_max)
		return -EINVAL;

	/* parm is used to store kernel options, check for common chars */
	for (i = 0; i < ip_len; i++)
		if (!(isalnum(buf[i]) || isascii(buf[i]) || isprint(buf[i])))
			return -EINVAL;

	memset(ipb->ipl_info.ccw.vm_parm, 0, DIAG308_VMPARM_SIZE);
	ipb->ipl_info.ccw.vm_parm_len = ip_len;
	if (ip_len > 0) {
		ipb->ipl_info.ccw.vm_flags |= DIAG308_VM_FLAGS_VP_VALID;
		memcpy(ipb->ipl_info.ccw.vm_parm, buf, ip_len);
		ASCEBC(ipb->ipl_info.ccw.vm_parm, ip_len);
	} else {
		ipb->ipl_info.ccw.vm_flags &= ~DIAG308_VM_FLAGS_VP_VALID;
	}

	return len;
}

/* NSS wrapper */
static ssize_t reipl_nss_vmparm_show(struct kobject *kobj,
				     struct kobj_attribute *attr, char *page)
{
	return reipl_generic_vmparm_show(reipl_block_nss, page);
}

static ssize_t reipl_nss_vmparm_store(struct kobject *kobj,
				      struct kobj_attribute *attr,
				      const char *buf, size_t len)
{
	return reipl_generic_vmparm_store(reipl_block_nss, 56, buf, len);
}

/* CCW wrapper */
static ssize_t reipl_ccw_vmparm_show(struct kobject *kobj,
				     struct kobj_attribute *attr, char *page)
{
	return reipl_generic_vmparm_show(reipl_block_ccw, page);
}

static ssize_t reipl_ccw_vmparm_store(struct kobject *kobj,
				      struct kobj_attribute *attr,
				      const char *buf, size_t len)
{
	return reipl_generic_vmparm_store(reipl_block_ccw, 64, buf, len);
}

static struct kobj_attribute sys_reipl_nss_vmparm_attr =
	__ATTR(parm, S_IRUGO | S_IWUSR, reipl_nss_vmparm_show,
					reipl_nss_vmparm_store);
static struct kobj_attribute sys_reipl_ccw_vmparm_attr =
	__ATTR(parm, S_IRUGO | S_IWUSR, reipl_ccw_vmparm_show,
					reipl_ccw_vmparm_store);

M
Michael Holzheu 已提交
703 704
/* FCP reipl device attributes */

705
static ssize_t reipl_fcp_scpdata_read(struct file *filp, struct kobject *kobj,
706 707 708 709 710 711 712 713 714
				      struct bin_attribute *attr,
				      char *buf, loff_t off, size_t count)
{
	size_t size = reipl_block_fcp->ipl_info.fcp.scp_data_len;
	void *scp_data = reipl_block_fcp->ipl_info.fcp.scp_data;

	return memory_read_from_buffer(buf, count, &off, scp_data, size);
}

715
static ssize_t reipl_fcp_scpdata_write(struct file *filp, struct kobject *kobj,
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
				       struct bin_attribute *attr,
				       char *buf, loff_t off, size_t count)
{
	size_t padding;
	size_t scpdata_len;

	if (off < 0)
		return -EINVAL;

	if (off >= DIAG308_SCPDATA_SIZE)
		return -ENOSPC;

	if (count > DIAG308_SCPDATA_SIZE - off)
		count = DIAG308_SCPDATA_SIZE - off;

	memcpy(reipl_block_fcp->ipl_info.fcp.scp_data, buf + off, count);
	scpdata_len = off + count;

	if (scpdata_len % 8) {
		padding = 8 - (scpdata_len % 8);
		memset(reipl_block_fcp->ipl_info.fcp.scp_data + scpdata_len,
		       0, padding);
		scpdata_len += padding;
	}

	reipl_block_fcp->ipl_info.fcp.scp_data_len = scpdata_len;
	reipl_block_fcp->hdr.len = IPL_PARM_BLK_FCP_LEN + scpdata_len;
	reipl_block_fcp->hdr.blk0_len = IPL_PARM_BLK0_FCP_LEN + scpdata_len;

	return count;
}

static struct bin_attribute sys_reipl_fcp_scp_data_attr = {
	.attr = {
		.name = "scp_data",
		.mode = S_IRUGO | S_IWUSR,
	},
	.size = PAGE_SIZE,
	.read = reipl_fcp_scpdata_read,
	.write = reipl_fcp_scpdata_write,
};

M
Michael Holzheu 已提交
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
DEFINE_IPL_ATTR_RW(reipl_fcp, wwpn, "0x%016llx\n", "%016llx\n",
		   reipl_block_fcp->ipl_info.fcp.wwpn);
DEFINE_IPL_ATTR_RW(reipl_fcp, lun, "0x%016llx\n", "%016llx\n",
		   reipl_block_fcp->ipl_info.fcp.lun);
DEFINE_IPL_ATTR_RW(reipl_fcp, bootprog, "%lld\n", "%lld\n",
		   reipl_block_fcp->ipl_info.fcp.bootprog);
DEFINE_IPL_ATTR_RW(reipl_fcp, br_lba, "%lld\n", "%lld\n",
		   reipl_block_fcp->ipl_info.fcp.br_lba);
DEFINE_IPL_ATTR_RW(reipl_fcp, device, "0.0.%04llx\n", "0.0.%llx\n",
		   reipl_block_fcp->ipl_info.fcp.devno);

static struct attribute *reipl_fcp_attrs[] = {
	&sys_reipl_fcp_device_attr.attr,
	&sys_reipl_fcp_wwpn_attr.attr,
	&sys_reipl_fcp_lun_attr.attr,
	&sys_reipl_fcp_bootprog_attr.attr,
	&sys_reipl_fcp_br_lba_attr.attr,
	NULL,
};

static struct attribute_group reipl_fcp_attr_group = {
	.attrs = reipl_fcp_attrs,
};

/* CCW reipl device attributes */

DEFINE_IPL_ATTR_RW(reipl_ccw, device, "0.0.%04llx\n", "0.0.%llx\n",
	reipl_block_ccw->ipl_info.ccw.devno);

787 788
static void reipl_get_ascii_loadparm(char *loadparm,
				     struct ipl_parameter_block *ibp)
789
{
790
	memcpy(loadparm, ibp->ipl_info.ccw.load_parm, LOADPARM_LEN);
791 792
	EBCASC(loadparm, LOADPARM_LEN);
	loadparm[LOADPARM_LEN] = 0;
793
	strim(loadparm);
794 795
}

796 797
static ssize_t reipl_generic_loadparm_show(struct ipl_parameter_block *ipb,
					   char *page)
798 799 800
{
	char buf[LOADPARM_LEN + 1];

801
	reipl_get_ascii_loadparm(buf, ipb);
802 803 804
	return sprintf(page, "%s\n", buf);
}

805 806
static ssize_t reipl_generic_loadparm_store(struct ipl_parameter_block *ipb,
					    const char *buf, size_t len)
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
{
	int i, lp_len;

	/* ignore trailing newline */
	lp_len = len;
	if ((len > 0) && (buf[len - 1] == '\n'))
		lp_len--;
	/* loadparm can have max 8 characters and must not start with a blank */
	if ((lp_len > LOADPARM_LEN) || ((lp_len > 0) && (buf[0] == ' ')))
		return -EINVAL;
	/* loadparm can only contain "a-z,A-Z,0-9,SP,." */
	for (i = 0; i < lp_len; i++) {
		if (isalpha(buf[i]) || isdigit(buf[i]) || (buf[i] == ' ') ||
		    (buf[i] == '.'))
			continue;
		return -EINVAL;
	}
	/* initialize loadparm with blanks */
825
	memset(ipb->ipl_info.ccw.load_parm, ' ', LOADPARM_LEN);
826
	/* copy and convert to ebcdic */
827 828
	memcpy(ipb->ipl_info.ccw.load_parm, buf, lp_len);
	ASCEBC(ipb->ipl_info.ccw.load_parm, LOADPARM_LEN);
829 830 831
	return len;
}

832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
/* NSS wrapper */
static ssize_t reipl_nss_loadparm_show(struct kobject *kobj,
				       struct kobj_attribute *attr, char *page)
{
	return reipl_generic_loadparm_show(reipl_block_nss, page);
}

static ssize_t reipl_nss_loadparm_store(struct kobject *kobj,
					struct kobj_attribute *attr,
					const char *buf, size_t len)
{
	return reipl_generic_loadparm_store(reipl_block_nss, buf, len);
}

/* CCW wrapper */
static ssize_t reipl_ccw_loadparm_show(struct kobject *kobj,
				       struct kobj_attribute *attr, char *page)
{
	return reipl_generic_loadparm_show(reipl_block_ccw, page);
}

static ssize_t reipl_ccw_loadparm_store(struct kobject *kobj,
					struct kobj_attribute *attr,
					const char *buf, size_t len)
{
	return reipl_generic_loadparm_store(reipl_block_ccw, buf, len);
}

860
static struct kobj_attribute sys_reipl_ccw_loadparm_attr =
861 862 863 864 865 866 867 868 869
	__ATTR(loadparm, S_IRUGO | S_IWUSR, reipl_ccw_loadparm_show,
					    reipl_ccw_loadparm_store);

static struct attribute *reipl_ccw_attrs_vm[] = {
	&sys_reipl_ccw_device_attr.attr,
	&sys_reipl_ccw_loadparm_attr.attr,
	&sys_reipl_ccw_vmparm_attr.attr,
	NULL,
};
870

871
static struct attribute *reipl_ccw_attrs_lpar[] = {
M
Michael Holzheu 已提交
872
	&sys_reipl_ccw_device_attr.attr,
873
	&sys_reipl_ccw_loadparm_attr.attr,
M
Michael Holzheu 已提交
874 875 876
	NULL,
};

877
static struct attribute_group reipl_ccw_attr_group_vm = {
M
Michael Holzheu 已提交
878
	.name  = IPL_CCW_STR,
879 880 881 882 883 884
	.attrs = reipl_ccw_attrs_vm,
};

static struct attribute_group reipl_ccw_attr_group_lpar = {
	.name  = IPL_CCW_STR,
	.attrs = reipl_ccw_attrs_lpar,
M
Michael Holzheu 已提交
885 886
};

H
Hongjie Yang 已提交
887 888

/* NSS reipl device attributes */
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
static void reipl_get_ascii_nss_name(char *dst,
				     struct ipl_parameter_block *ipb)
{
	memcpy(dst, ipb->ipl_info.ccw.nss_name, NSS_NAME_SIZE);
	EBCASC(dst, NSS_NAME_SIZE);
	dst[NSS_NAME_SIZE] = 0;
}

static ssize_t reipl_nss_name_show(struct kobject *kobj,
				   struct kobj_attribute *attr, char *page)
{
	char nss_name[NSS_NAME_SIZE + 1] = {};

	reipl_get_ascii_nss_name(nss_name, reipl_block_nss);
	return sprintf(page, "%s\n", nss_name);
}

static ssize_t reipl_nss_name_store(struct kobject *kobj,
				    struct kobj_attribute *attr,
				    const char *buf, size_t len)
{
	int nss_len;

	/* ignore trailing newline */
	nss_len = len;
	if ((len > 0) && (buf[len - 1] == '\n'))
		nss_len--;
H
Hongjie Yang 已提交
916

917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
	if (nss_len > NSS_NAME_SIZE)
		return -EINVAL;

	memset(reipl_block_nss->ipl_info.ccw.nss_name, 0x40, NSS_NAME_SIZE);
	if (nss_len > 0) {
		reipl_block_nss->ipl_info.ccw.vm_flags |=
			DIAG308_VM_FLAGS_NSS_VALID;
		memcpy(reipl_block_nss->ipl_info.ccw.nss_name, buf, nss_len);
		ASCEBC(reipl_block_nss->ipl_info.ccw.nss_name, nss_len);
		EBC_TOUPPER(reipl_block_nss->ipl_info.ccw.nss_name, nss_len);
	} else {
		reipl_block_nss->ipl_info.ccw.vm_flags &=
			~DIAG308_VM_FLAGS_NSS_VALID;
	}

	return len;
}

static struct kobj_attribute sys_reipl_nss_name_attr =
	__ATTR(name, S_IRUGO | S_IWUSR, reipl_nss_name_show,
					reipl_nss_name_store);

static struct kobj_attribute sys_reipl_nss_loadparm_attr =
	__ATTR(loadparm, S_IRUGO | S_IWUSR, reipl_nss_loadparm_show,
					    reipl_nss_loadparm_store);
H
Hongjie Yang 已提交
942 943 944

static struct attribute *reipl_nss_attrs[] = {
	&sys_reipl_nss_name_attr.attr,
945 946
	&sys_reipl_nss_loadparm_attr.attr,
	&sys_reipl_nss_vmparm_attr.attr,
H
Hongjie Yang 已提交
947 948 949 950 951 952 953 954
	NULL,
};

static struct attribute_group reipl_nss_attr_group = {
	.name  = IPL_NSS_STR,
	.attrs = reipl_nss_attrs,
};

955 956 957 958 959 960 961
static void set_reipl_block_actual(struct ipl_parameter_block *reipl_block)
{
	reipl_block_actual = reipl_block;
	os_info_entry_add(OS_INFO_REIPL_BLOCK, reipl_block_actual,
			  reipl_block->hdr.len);
}

M
Michael Holzheu 已提交
962 963 964 965 966 967 968 969 970
/* reipl type */

static int reipl_set_type(enum ipl_type type)
{
	if (!(reipl_capabilities & type))
		return -EINVAL;

	switch(type) {
	case IPL_TYPE_CCW:
971 972 973
		if (diag308_set_works)
			reipl_method = REIPL_METHOD_CCW_DIAG;
		else if (MACHINE_IS_VM)
M
Michael Holzheu 已提交
974
			reipl_method = REIPL_METHOD_CCW_VM;
M
Michael Holzheu 已提交
975
		else
M
Michael Holzheu 已提交
976
			reipl_method = REIPL_METHOD_CCW_CIO;
977
		set_reipl_block_actual(reipl_block_ccw);
M
Michael Holzheu 已提交
978 979 980
		break;
	case IPL_TYPE_FCP:
		if (diag308_set_works)
M
Michael Holzheu 已提交
981
			reipl_method = REIPL_METHOD_FCP_RW_DIAG;
M
Michael Holzheu 已提交
982
		else if (MACHINE_IS_VM)
M
Michael Holzheu 已提交
983
			reipl_method = REIPL_METHOD_FCP_RO_VM;
M
Michael Holzheu 已提交
984
		else
M
Michael Holzheu 已提交
985
			reipl_method = REIPL_METHOD_FCP_RO_DIAG;
986
		set_reipl_block_actual(reipl_block_fcp);
M
Michael Holzheu 已提交
987 988 989
		break;
	case IPL_TYPE_FCP_DUMP:
		reipl_method = REIPL_METHOD_FCP_DUMP;
M
Michael Holzheu 已提交
990
		break;
H
Hongjie Yang 已提交
991
	case IPL_TYPE_NSS:
992 993 994 995
		if (diag308_set_works)
			reipl_method = REIPL_METHOD_NSS_DIAG;
		else
			reipl_method = REIPL_METHOD_NSS;
996
		set_reipl_block_actual(reipl_block_nss);
M
Michael Holzheu 已提交
997 998 999
		break;
	case IPL_TYPE_UNKNOWN:
		reipl_method = REIPL_METHOD_DEFAULT;
H
Hongjie Yang 已提交
1000
		break;
M
Michael Holzheu 已提交
1001
	default:
M
Michael Holzheu 已提交
1002
		BUG();
M
Michael Holzheu 已提交
1003 1004 1005 1006 1007
	}
	reipl_type = type;
	return 0;
}

1008 1009
static ssize_t reipl_type_show(struct kobject *kobj,
			       struct kobj_attribute *attr, char *page)
M
Michael Holzheu 已提交
1010 1011 1012 1013
{
	return sprintf(page, "%s\n", ipl_type_str(reipl_type));
}

1014 1015 1016
static ssize_t reipl_type_store(struct kobject *kobj,
				struct kobj_attribute *attr,
				const char *buf, size_t len)
M
Michael Holzheu 已提交
1017 1018 1019 1020 1021 1022 1023
{
	int rc = -EINVAL;

	if (strncmp(buf, IPL_CCW_STR, strlen(IPL_CCW_STR)) == 0)
		rc = reipl_set_type(IPL_TYPE_CCW);
	else if (strncmp(buf, IPL_FCP_STR, strlen(IPL_FCP_STR)) == 0)
		rc = reipl_set_type(IPL_TYPE_FCP);
H
Hongjie Yang 已提交
1024 1025
	else if (strncmp(buf, IPL_NSS_STR, strlen(IPL_NSS_STR)) == 0)
		rc = reipl_set_type(IPL_TYPE_NSS);
M
Michael Holzheu 已提交
1026 1027 1028
	return (rc != 0) ? rc : len;
}

1029
static struct kobj_attribute reipl_type_attr =
1030
	__ATTR(reipl_type, 0644, reipl_type_show, reipl_type_store);
M
Michael Holzheu 已提交
1031

1032
static struct kset *reipl_kset;
1033
static struct kset *reipl_fcp_kset;
M
Michael Holzheu 已提交
1034

1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
static void get_ipl_string(char *dst, struct ipl_parameter_block *ipb,
			   const enum ipl_method m)
{
	char loadparm[LOADPARM_LEN + 1] = {};
	char vmparm[DIAG308_VMPARM_SIZE + 1] = {};
	char nss_name[NSS_NAME_SIZE + 1] = {};
	size_t pos = 0;

	reipl_get_ascii_loadparm(loadparm, ipb);
	reipl_get_ascii_nss_name(nss_name, ipb);
1045
	reipl_get_ascii_vmparm(vmparm, sizeof(vmparm), ipb);
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062

	switch (m) {
	case REIPL_METHOD_CCW_VM:
		pos = sprintf(dst, "IPL %X CLEAR", ipb->ipl_info.ccw.devno);
		break;
	case REIPL_METHOD_NSS:
		pos = sprintf(dst, "IPL %s", nss_name);
		break;
	default:
		break;
	}
	if (strlen(loadparm) > 0)
		pos += sprintf(dst + pos, " LOADPARM '%s'", loadparm);
	if (strlen(vmparm) > 0)
		sprintf(dst + pos, " PARM %s", vmparm);
}

1063
static void __reipl_run(void *unused)
M
Michael Holzheu 已提交
1064 1065
{
	struct ccw_dev_id devid;
1066
	static char buf[128];
M
Michael Holzheu 已提交
1067 1068

	switch (reipl_method) {
M
Michael Holzheu 已提交
1069
	case REIPL_METHOD_CCW_CIO:
M
Michael Holzheu 已提交
1070 1071 1072 1073
		devid.devno = reipl_block_ccw->ipl_info.ccw.devno;
		devid.ssid  = 0;
		reipl_ccw_dev(&devid);
		break;
M
Michael Holzheu 已提交
1074
	case REIPL_METHOD_CCW_VM:
1075
		get_ipl_string(buf, reipl_block_ccw, REIPL_METHOD_CCW_VM);
1076
		__cpcmd(buf, NULL, 0, NULL);
M
Michael Holzheu 已提交
1077
		break;
M
Michael Holzheu 已提交
1078
	case REIPL_METHOD_CCW_DIAG:
M
Michael Holzheu 已提交
1079 1080 1081
		diag308(DIAG308_SET, reipl_block_ccw);
		diag308(DIAG308_IPL, NULL);
		break;
M
Michael Holzheu 已提交
1082
	case REIPL_METHOD_FCP_RW_DIAG:
M
Michael Holzheu 已提交
1083 1084 1085
		diag308(DIAG308_SET, reipl_block_fcp);
		diag308(DIAG308_IPL, NULL);
		break;
M
Michael Holzheu 已提交
1086
	case REIPL_METHOD_FCP_RO_DIAG:
M
Michael Holzheu 已提交
1087 1088
		diag308(DIAG308_IPL, NULL);
		break;
M
Michael Holzheu 已提交
1089
	case REIPL_METHOD_FCP_RO_VM:
1090
		__cpcmd("IPL", NULL, 0, NULL);
M
Michael Holzheu 已提交
1091
		break;
1092 1093 1094 1095
	case REIPL_METHOD_NSS_DIAG:
		diag308(DIAG308_SET, reipl_block_nss);
		diag308(DIAG308_IPL, NULL);
		break;
M
Michael Holzheu 已提交
1096
	case REIPL_METHOD_NSS:
1097
		get_ipl_string(buf, reipl_block_nss, REIPL_METHOD_NSS);
H
Hongjie Yang 已提交
1098 1099
		__cpcmd(buf, NULL, 0, NULL);
		break;
M
Michael Holzheu 已提交
1100
	case REIPL_METHOD_DEFAULT:
M
Michael Holzheu 已提交
1101
		if (MACHINE_IS_VM)
1102
			__cpcmd("IPL", NULL, 0, NULL);
M
Michael Holzheu 已提交
1103 1104
		diag308(DIAG308_IPL, NULL);
		break;
M
Michael Holzheu 已提交
1105 1106
	case REIPL_METHOD_FCP_DUMP:
		break;
M
Michael Holzheu 已提交
1107
	}
1108
	disabled_wait((unsigned long) __builtin_return_address(0));
M
Michael Holzheu 已提交
1109 1110
}

1111 1112
static void reipl_run(struct shutdown_trigger *trigger)
{
M
Martin Schwidefsky 已提交
1113
	smp_call_ipl_cpu(__reipl_run, NULL);
1114 1115
}

1116
static void reipl_block_ccw_init(struct ipl_parameter_block *ipb)
M
Michael Holzheu 已提交
1117
{
1118 1119 1120 1121 1122
	ipb->hdr.len = IPL_PARM_BLK_CCW_LEN;
	ipb->hdr.version = IPL_PARM_BLOCK_VERSION;
	ipb->hdr.blk0_len = IPL_PARM_BLK0_CCW_LEN;
	ipb->hdr.pbt = DIAG308_IPL_TYPE_CCW;
}
M
Michael Holzheu 已提交
1123

1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
static void reipl_block_ccw_fill_parms(struct ipl_parameter_block *ipb)
{
	/* LOADPARM */
	/* check if read scp info worked and set loadparm */
	if (sclp_ipl_info.is_valid)
		memcpy(ipb->ipl_info.ccw.load_parm,
				&sclp_ipl_info.loadparm, LOADPARM_LEN);
	else
		/* read scp info failed: set empty loadparm (EBCDIC blanks) */
		memset(ipb->ipl_info.ccw.load_parm, 0x40, LOADPARM_LEN);
	ipb->hdr.flags = DIAG308_FLAGS_LP_VALID;

	/* VM PARM */
	if (MACHINE_IS_VM && diag308_set_works &&
	    (ipl_block.ipl_info.ccw.vm_flags & DIAG308_VM_FLAGS_VP_VALID)) {

		ipb->ipl_info.ccw.vm_flags |= DIAG308_VM_FLAGS_VP_VALID;
		ipb->ipl_info.ccw.vm_parm_len =
					ipl_block.ipl_info.ccw.vm_parm_len;
		memcpy(ipb->ipl_info.ccw.vm_parm,
		       ipl_block.ipl_info.ccw.vm_parm, DIAG308_VMPARM_SIZE);
	}
M
Michael Holzheu 已提交
1146 1147
}

1148
static int __init reipl_nss_init(void)
M
Michael Holzheu 已提交
1149 1150 1151
{
	int rc;

1152 1153
	if (!MACHINE_IS_VM)
		return 0;
1154 1155 1156 1157 1158 1159 1160 1161

	reipl_block_nss = (void *) get_zeroed_page(GFP_KERNEL);
	if (!reipl_block_nss)
		return -ENOMEM;

	if (!diag308_set_works)
		sys_reipl_nss_vmparm_attr.attr.mode = S_IRUGO;

1162
	rc = sysfs_create_group(&reipl_kset->kobj, &reipl_nss_attr_group);
H
Hongjie Yang 已提交
1163 1164
	if (rc)
		return rc;
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178

	reipl_block_ccw_init(reipl_block_nss);
	if (ipl_info.type == IPL_TYPE_NSS) {
		memset(reipl_block_nss->ipl_info.ccw.nss_name,
			' ', NSS_NAME_SIZE);
		memcpy(reipl_block_nss->ipl_info.ccw.nss_name,
			kernel_nss_name, strlen(kernel_nss_name));
		ASCEBC(reipl_block_nss->ipl_info.ccw.nss_name, NSS_NAME_SIZE);
		reipl_block_nss->ipl_info.ccw.vm_flags |=
			DIAG308_VM_FLAGS_NSS_VALID;

		reipl_block_ccw_fill_parms(reipl_block_nss);
	}

H
Hongjie Yang 已提交
1179 1180 1181 1182
	reipl_capabilities |= IPL_TYPE_NSS;
	return 0;
}

M
Michael Holzheu 已提交
1183 1184 1185 1186 1187 1188 1189
static int __init reipl_ccw_init(void)
{
	int rc;

	reipl_block_ccw = (void *) get_zeroed_page(GFP_KERNEL);
	if (!reipl_block_ccw)
		return -ENOMEM;
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200

	if (MACHINE_IS_VM) {
		if (!diag308_set_works)
			sys_reipl_ccw_vmparm_attr.attr.mode = S_IRUGO;
		rc = sysfs_create_group(&reipl_kset->kobj,
					&reipl_ccw_attr_group_vm);
	} else {
		if(!diag308_set_works)
			sys_reipl_ccw_loadparm_attr.attr.mode = S_IRUGO;
		rc = sysfs_create_group(&reipl_kset->kobj,
					&reipl_ccw_attr_group_lpar);
M
Michael Holzheu 已提交
1201
	}
1202 1203 1204 1205 1206
	if (rc)
		return rc;

	reipl_block_ccw_init(reipl_block_ccw);
	if (ipl_info.type == IPL_TYPE_CCW) {
M
Michael Holzheu 已提交
1207
		reipl_block_ccw->ipl_info.ccw.devno = ipl_devno;
1208 1209 1210
		reipl_block_ccw_fill_parms(reipl_block_ccw);
	}

M
Michael Holzheu 已提交
1211 1212 1213 1214 1215 1216 1217 1218
	reipl_capabilities |= IPL_TYPE_CCW;
	return 0;
}

static int __init reipl_fcp_init(void)
{
	int rc;

1219
	if (!diag308_set_works) {
1220
		if (ipl_info.type == IPL_TYPE_FCP) {
1221
			make_attrs_ro(reipl_fcp_attrs);
1222 1223
			sys_reipl_fcp_scp_data_attr.attr.mode = S_IRUGO;
		} else
1224 1225
			return 0;
	}
M
Michael Holzheu 已提交
1226 1227 1228 1229

	reipl_block_fcp = (void *) get_zeroed_page(GFP_KERNEL);
	if (!reipl_block_fcp)
		return -ENOMEM;
1230 1231 1232 1233

	/* sysfs: create fcp kset for mixing attr group and bin attrs */
	reipl_fcp_kset = kset_create_and_add(IPL_FCP_STR, NULL,
					     &reipl_kset->kobj);
1234
	if (!reipl_fcp_kset) {
1235 1236 1237 1238 1239
		free_page((unsigned long) reipl_block_fcp);
		return -ENOMEM;
	}

	rc = sysfs_create_group(&reipl_fcp_kset->kobj, &reipl_fcp_attr_group);
M
Michael Holzheu 已提交
1240
	if (rc) {
1241 1242
		kset_unregister(reipl_fcp_kset);
		free_page((unsigned long) reipl_block_fcp);
M
Michael Holzheu 已提交
1243 1244
		return rc;
	}
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255

	rc = sysfs_create_bin_file(&reipl_fcp_kset->kobj,
				   &sys_reipl_fcp_scp_data_attr);
	if (rc) {
		sysfs_remove_group(&reipl_fcp_kset->kobj, &reipl_fcp_attr_group);
		kset_unregister(reipl_fcp_kset);
		free_page((unsigned long) reipl_block_fcp);
		return rc;
	}

	if (ipl_info.type == IPL_TYPE_FCP)
M
Michael Holzheu 已提交
1256
		memcpy(reipl_block_fcp, IPL_PARMBLOCK_START, PAGE_SIZE);
1257
	else {
M
Michael Holzheu 已提交
1258 1259
		reipl_block_fcp->hdr.len = IPL_PARM_BLK_FCP_LEN;
		reipl_block_fcp->hdr.version = IPL_PARM_BLOCK_VERSION;
1260
		reipl_block_fcp->hdr.blk0_len = IPL_PARM_BLK0_FCP_LEN;
M
Michael Holzheu 已提交
1261 1262 1263 1264 1265 1266 1267
		reipl_block_fcp->hdr.pbt = DIAG308_IPL_TYPE_FCP;
		reipl_block_fcp->ipl_info.fcp.opt = DIAG308_IPL_OPT_IPL;
	}
	reipl_capabilities |= IPL_TYPE_FCP;
	return 0;
}

1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
static int __init reipl_type_init(void)
{
	enum ipl_type reipl_type = ipl_info.type;
	struct ipl_parameter_block *reipl_block;
	unsigned long size;

	reipl_block = os_info_old_entry(OS_INFO_REIPL_BLOCK, &size);
	if (!reipl_block)
		goto out;
	/*
	 * If we have an OS info reipl block, this will be used
	 */
	if (reipl_block->hdr.pbt == DIAG308_IPL_TYPE_FCP) {
		memcpy(reipl_block_fcp, reipl_block, size);
		reipl_type = IPL_TYPE_FCP;
	} else if (reipl_block->hdr.pbt == DIAG308_IPL_TYPE_CCW) {
		memcpy(reipl_block_ccw, reipl_block, size);
		reipl_type = IPL_TYPE_CCW;
	}
out:
	return reipl_set_type(reipl_type);
}

1291
static int __init reipl_init(void)
M
Michael Holzheu 已提交
1292 1293 1294
{
	int rc;

1295
	reipl_kset = kset_create_and_add("reipl", NULL, firmware_kobj);
1296 1297 1298
	if (!reipl_kset)
		return -ENOMEM;
	rc = sysfs_create_file(&reipl_kset->kobj, &reipl_type_attr.attr);
M
Michael Holzheu 已提交
1299
	if (rc) {
1300
		kset_unregister(reipl_kset);
M
Michael Holzheu 已提交
1301 1302 1303 1304 1305 1306
		return rc;
	}
	rc = reipl_ccw_init();
	if (rc)
		return rc;
	rc = reipl_fcp_init();
H
Hongjie Yang 已提交
1307 1308 1309
	if (rc)
		return rc;
	rc = reipl_nss_init();
M
Michael Holzheu 已提交
1310 1311
	if (rc)
		return rc;
1312
	return reipl_type_init();
M
Michael Holzheu 已提交
1313 1314
}

1315 1316 1317 1318 1319
static struct shutdown_action __refdata reipl_action = {
	.name	= SHUTDOWN_ACTION_REIPL_STR,
	.fn	= reipl_run,
	.init	= reipl_init,
};
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374

/*
 * dump shutdown action: Dump Linux on shutdown.
 */

/* FCP dump device attributes */

DEFINE_IPL_ATTR_RW(dump_fcp, wwpn, "0x%016llx\n", "%016llx\n",
		   dump_block_fcp->ipl_info.fcp.wwpn);
DEFINE_IPL_ATTR_RW(dump_fcp, lun, "0x%016llx\n", "%016llx\n",
		   dump_block_fcp->ipl_info.fcp.lun);
DEFINE_IPL_ATTR_RW(dump_fcp, bootprog, "%lld\n", "%lld\n",
		   dump_block_fcp->ipl_info.fcp.bootprog);
DEFINE_IPL_ATTR_RW(dump_fcp, br_lba, "%lld\n", "%lld\n",
		   dump_block_fcp->ipl_info.fcp.br_lba);
DEFINE_IPL_ATTR_RW(dump_fcp, device, "0.0.%04llx\n", "0.0.%llx\n",
		   dump_block_fcp->ipl_info.fcp.devno);

static struct attribute *dump_fcp_attrs[] = {
	&sys_dump_fcp_device_attr.attr,
	&sys_dump_fcp_wwpn_attr.attr,
	&sys_dump_fcp_lun_attr.attr,
	&sys_dump_fcp_bootprog_attr.attr,
	&sys_dump_fcp_br_lba_attr.attr,
	NULL,
};

static struct attribute_group dump_fcp_attr_group = {
	.name  = IPL_FCP_STR,
	.attrs = dump_fcp_attrs,
};

/* CCW dump device attributes */

DEFINE_IPL_ATTR_RW(dump_ccw, device, "0.0.%04llx\n", "0.0.%llx\n",
		   dump_block_ccw->ipl_info.ccw.devno);

static struct attribute *dump_ccw_attrs[] = {
	&sys_dump_ccw_device_attr.attr,
	NULL,
};

static struct attribute_group dump_ccw_attr_group = {
	.name  = IPL_CCW_STR,
	.attrs = dump_ccw_attrs,
};

/* dump type */

static int dump_set_type(enum dump_type type)
{
	if (!(dump_capabilities & type))
		return -EINVAL;
	switch (type) {
	case DUMP_TYPE_CCW:
1375 1376 1377
		if (diag308_set_works)
			dump_method = DUMP_METHOD_CCW_DIAG;
		else if (MACHINE_IS_VM)
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
			dump_method = DUMP_METHOD_CCW_VM;
		else
			dump_method = DUMP_METHOD_CCW_CIO;
		break;
	case DUMP_TYPE_FCP:
		dump_method = DUMP_METHOD_FCP_DIAG;
		break;
	default:
		dump_method = DUMP_METHOD_NONE;
	}
	dump_type = type;
	return 0;
}

static ssize_t dump_type_show(struct kobject *kobj,
			      struct kobj_attribute *attr, char *page)
{
	return sprintf(page, "%s\n", dump_type_str(dump_type));
}

static ssize_t dump_type_store(struct kobject *kobj,
			       struct kobj_attribute *attr,
			       const char *buf, size_t len)
{
	int rc = -EINVAL;

	if (strncmp(buf, DUMP_NONE_STR, strlen(DUMP_NONE_STR)) == 0)
		rc = dump_set_type(DUMP_TYPE_NONE);
	else if (strncmp(buf, DUMP_CCW_STR, strlen(DUMP_CCW_STR)) == 0)
		rc = dump_set_type(DUMP_TYPE_CCW);
	else if (strncmp(buf, DUMP_FCP_STR, strlen(DUMP_FCP_STR)) == 0)
		rc = dump_set_type(DUMP_TYPE_FCP);
	return (rc != 0) ? rc : len;
}

static struct kobj_attribute dump_type_attr =
	__ATTR(dump_type, 0644, dump_type_show, dump_type_store);

static struct kset *dump_kset;

1418
static void __dump_run(void *unused)
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
{
	struct ccw_dev_id devid;
	static char buf[100];

	switch (dump_method) {
	case DUMP_METHOD_CCW_CIO:
		devid.devno = dump_block_ccw->ipl_info.ccw.devno;
		devid.ssid  = 0;
		reipl_ccw_dev(&devid);
		break;
	case DUMP_METHOD_CCW_VM:
		sprintf(buf, "STORE STATUS");
		__cpcmd(buf, NULL, 0, NULL);
		sprintf(buf, "IPL %X", dump_block_ccw->ipl_info.ccw.devno);
		__cpcmd(buf, NULL, 0, NULL);
		break;
	case DUMP_METHOD_CCW_DIAG:
		diag308(DIAG308_SET, dump_block_ccw);
		diag308(DIAG308_DUMP, NULL);
		break;
	case DUMP_METHOD_FCP_DIAG:
		diag308(DIAG308_SET, dump_block_fcp);
		diag308(DIAG308_DUMP, NULL);
		break;
1443 1444
	default:
		break;
1445
	}
1446 1447 1448 1449 1450 1451 1452
}

static void dump_run(struct shutdown_trigger *trigger)
{
	if (dump_method == DUMP_METHOD_NONE)
		return;
	smp_send_stop();
M
Martin Schwidefsky 已提交
1453
	smp_call_ipl_cpu(__dump_run, NULL);
1454 1455
}

M
Michael Holzheu 已提交
1456 1457 1458 1459 1460 1461 1462
static int __init dump_ccw_init(void)
{
	int rc;

	dump_block_ccw = (void *) get_zeroed_page(GFP_KERNEL);
	if (!dump_block_ccw)
		return -ENOMEM;
1463
	rc = sysfs_create_group(&dump_kset->kobj, &dump_ccw_attr_group);
M
Michael Holzheu 已提交
1464 1465 1466 1467 1468 1469
	if (rc) {
		free_page((unsigned long)dump_block_ccw);
		return rc;
	}
	dump_block_ccw->hdr.len = IPL_PARM_BLK_CCW_LEN;
	dump_block_ccw->hdr.version = IPL_PARM_BLOCK_VERSION;
1470
	dump_block_ccw->hdr.blk0_len = IPL_PARM_BLK0_CCW_LEN;
M
Michael Holzheu 已提交
1471
	dump_block_ccw->hdr.pbt = DIAG308_IPL_TYPE_CCW;
M
Michael Holzheu 已提交
1472
	dump_capabilities |= DUMP_TYPE_CCW;
M
Michael Holzheu 已提交
1473 1474 1475 1476 1477 1478 1479
	return 0;
}

static int __init dump_fcp_init(void)
{
	int rc;

1480
	if (!sclp_ipl_info.has_dump)
M
Michael Holzheu 已提交
1481 1482 1483 1484 1485 1486
		return 0; /* LDIPL DUMP is not installed */
	if (!diag308_set_works)
		return 0;
	dump_block_fcp = (void *) get_zeroed_page(GFP_KERNEL);
	if (!dump_block_fcp)
		return -ENOMEM;
1487
	rc = sysfs_create_group(&dump_kset->kobj, &dump_fcp_attr_group);
M
Michael Holzheu 已提交
1488 1489 1490 1491 1492 1493
	if (rc) {
		free_page((unsigned long)dump_block_fcp);
		return rc;
	}
	dump_block_fcp->hdr.len = IPL_PARM_BLK_FCP_LEN;
	dump_block_fcp->hdr.version = IPL_PARM_BLOCK_VERSION;
1494
	dump_block_fcp->hdr.blk0_len = IPL_PARM_BLK0_FCP_LEN;
M
Michael Holzheu 已提交
1495 1496
	dump_block_fcp->hdr.pbt = DIAG308_IPL_TYPE_FCP;
	dump_block_fcp->ipl_info.fcp.opt = DIAG308_IPL_OPT_DUMP;
M
Michael Holzheu 已提交
1497
	dump_capabilities |= DUMP_TYPE_FCP;
M
Michael Holzheu 已提交
1498 1499 1500
	return 0;
}

1501
static int __init dump_init(void)
M
Michael Holzheu 已提交
1502 1503 1504
{
	int rc;

1505
	dump_kset = kset_create_and_add("dump", NULL, firmware_kobj);
1506 1507
	if (!dump_kset)
		return -ENOMEM;
1508
	rc = sysfs_create_file(&dump_kset->kobj, &dump_type_attr.attr);
M
Michael Holzheu 已提交
1509
	if (rc) {
1510
		kset_unregister(dump_kset);
M
Michael Holzheu 已提交
1511 1512 1513 1514 1515 1516 1517 1518
		return rc;
	}
	rc = dump_ccw_init();
	if (rc)
		return rc;
	rc = dump_fcp_init();
	if (rc)
		return rc;
M
Michael Holzheu 已提交
1519
	dump_set_type(DUMP_TYPE_NONE);
M
Michael Holzheu 已提交
1520 1521 1522
	return 0;
}

1523 1524 1525 1526 1527
static struct shutdown_action __refdata dump_action = {
	.name	= SHUTDOWN_ACTION_DUMP_STR,
	.fn	= dump_run,
	.init	= dump_init,
};
1528

F
Frank Munzert 已提交
1529 1530
static void dump_reipl_run(struct shutdown_trigger *trigger)
{
1531 1532 1533 1534 1535 1536
	unsigned long ipib = (unsigned long) &reipl_block_actual;
	unsigned int csum;

	csum = csum_partial(reipl_block_actual, reipl_block_actual->hdr.len, 0);
	mem_assign_absolute(S390_lowcore.ipib, ipib);
	mem_assign_absolute(S390_lowcore.ipib_checksum, csum);
F
Frank Munzert 已提交
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553
	dump_run(trigger);
}

static int __init dump_reipl_init(void)
{
	if (!diag308_set_works)
		return -EOPNOTSUPP;
	else
		return 0;
}

static struct shutdown_action __refdata dump_reipl_action = {
	.name	= SHUTDOWN_ACTION_DUMP_REIPL_STR,
	.fn	= dump_reipl_run,
	.init	= dump_reipl_init,
};

1554 1555 1556 1557 1558 1559 1560 1561
/*
 * vmcmd shutdown action: Trigger vm command on shutdown.
 */

static char vmcmd_on_reboot[128];
static char vmcmd_on_panic[128];
static char vmcmd_on_halt[128];
static char vmcmd_on_poff[128];
1562
static char vmcmd_on_restart[128];
1563 1564 1565 1566 1567

DEFINE_IPL_ATTR_STR_RW(vmcmd, on_reboot, "%s\n", "%s\n", vmcmd_on_reboot);
DEFINE_IPL_ATTR_STR_RW(vmcmd, on_panic, "%s\n", "%s\n", vmcmd_on_panic);
DEFINE_IPL_ATTR_STR_RW(vmcmd, on_halt, "%s\n", "%s\n", vmcmd_on_halt);
DEFINE_IPL_ATTR_STR_RW(vmcmd, on_poff, "%s\n", "%s\n", vmcmd_on_poff);
1568
DEFINE_IPL_ATTR_STR_RW(vmcmd, on_restart, "%s\n", "%s\n", vmcmd_on_restart);
1569 1570 1571 1572 1573 1574

static struct attribute *vmcmd_attrs[] = {
	&sys_vmcmd_on_reboot_attr.attr,
	&sys_vmcmd_on_panic_attr.attr,
	&sys_vmcmd_on_halt_attr.attr,
	&sys_vmcmd_on_poff_attr.attr,
1575
	&sys_vmcmd_on_restart_attr.attr,
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
	NULL,
};

static struct attribute_group vmcmd_attr_group = {
	.attrs = vmcmd_attrs,
};

static struct kset *vmcmd_kset;

static void vmcmd_run(struct shutdown_trigger *trigger)
M
Michael Holzheu 已提交
1586
{
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
	char *cmd, *next_cmd;

	if (strcmp(trigger->name, ON_REIPL_STR) == 0)
		cmd = vmcmd_on_reboot;
	else if (strcmp(trigger->name, ON_PANIC_STR) == 0)
		cmd = vmcmd_on_panic;
	else if (strcmp(trigger->name, ON_HALT_STR) == 0)
		cmd = vmcmd_on_halt;
	else if (strcmp(trigger->name, ON_POFF_STR) == 0)
		cmd = vmcmd_on_poff;
1597 1598
	else if (strcmp(trigger->name, ON_RESTART_STR) == 0)
		cmd = vmcmd_on_restart;
1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
	else
		return;

	if (strlen(cmd) == 0)
		return;
	do {
		next_cmd = strchr(cmd, '\n');
		if (next_cmd) {
			next_cmd[0] = 0;
			next_cmd += 1;
		}
		__cpcmd(cmd, NULL, 0, NULL);
		cmd = next_cmd;
	} while (cmd != NULL);
}

static int vmcmd_init(void)
{
	if (!MACHINE_IS_VM)
1618
		return -EOPNOTSUPP;
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
	vmcmd_kset = kset_create_and_add("vmcmd", NULL, firmware_kobj);
	if (!vmcmd_kset)
		return -ENOMEM;
	return sysfs_create_group(&vmcmd_kset->kobj, &vmcmd_attr_group);
}

static struct shutdown_action vmcmd_action = {SHUTDOWN_ACTION_VMCMD_STR,
					      vmcmd_run, vmcmd_init};

/*
 * stop shutdown action: Stop Linux on shutdown.
 */

static void stop_run(struct shutdown_trigger *trigger)
{
1634 1635
	if (strcmp(trigger->name, ON_PANIC_STR) == 0 ||
	    strcmp(trigger->name, ON_RESTART_STR) == 0)
1636
		disabled_wait((unsigned long) __builtin_return_address(0));
M
Martin Schwidefsky 已提交
1637
	smp_stop_cpu();
1638 1639 1640 1641 1642 1643 1644 1645
}

static struct shutdown_action stop_action = {SHUTDOWN_ACTION_STOP_STR,
					     stop_run, NULL};

/* action list */

static struct shutdown_action *shutdown_actions_list[] = {
F
Frank Munzert 已提交
1646 1647
	&ipl_action, &reipl_action, &dump_reipl_action, &dump_action,
	&vmcmd_action, &stop_action};
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
#define SHUTDOWN_ACTIONS_COUNT (sizeof(shutdown_actions_list) / sizeof(void *))

/*
 * Trigger section
 */

static struct kset *shutdown_actions_kset;

static int set_trigger(const char *buf, struct shutdown_trigger *trigger,
		       size_t len)
{
	int i;
F
Frank Munzert 已提交
1660

1661
	for (i = 0; i < SHUTDOWN_ACTIONS_COUNT; i++) {
F
Frank Munzert 已提交
1662
		if (sysfs_streq(buf, shutdown_actions_list[i]->name)) {
1663 1664 1665 1666 1667 1668
			if (shutdown_actions_list[i]->init_rc) {
				return shutdown_actions_list[i]->init_rc;
			} else {
				trigger->action = shutdown_actions_list[i];
				return len;
			}
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724
		}
	}
	return -EINVAL;
}

/* on reipl */

static struct shutdown_trigger on_reboot_trigger = {ON_REIPL_STR,
						    &reipl_action};

static ssize_t on_reboot_show(struct kobject *kobj,
			      struct kobj_attribute *attr, char *page)
{
	return sprintf(page, "%s\n", on_reboot_trigger.action->name);
}

static ssize_t on_reboot_store(struct kobject *kobj,
			       struct kobj_attribute *attr,
			       const char *buf, size_t len)
{
	return set_trigger(buf, &on_reboot_trigger, len);
}

static struct kobj_attribute on_reboot_attr =
	__ATTR(on_reboot, 0644, on_reboot_show, on_reboot_store);

static void do_machine_restart(char *__unused)
{
	smp_send_stop();
	on_reboot_trigger.action->fn(&on_reboot_trigger);
	reipl_run(NULL);
}
void (*_machine_restart)(char *command) = do_machine_restart;

/* on panic */

static struct shutdown_trigger on_panic_trigger = {ON_PANIC_STR, &stop_action};

static ssize_t on_panic_show(struct kobject *kobj,
			     struct kobj_attribute *attr, char *page)
{
	return sprintf(page, "%s\n", on_panic_trigger.action->name);
}

static ssize_t on_panic_store(struct kobject *kobj,
			      struct kobj_attribute *attr,
			      const char *buf, size_t len)
{
	return set_trigger(buf, &on_panic_trigger, len);
}

static struct kobj_attribute on_panic_attr =
	__ATTR(on_panic, 0644, on_panic_show, on_panic_store);

static void do_panic(void)
{
1725
	lgr_info_log();
1726 1727 1728 1729
	on_panic_trigger.action->fn(&on_panic_trigger);
	stop_run(&on_panic_trigger);
}

1730 1731 1732
/* on restart */

static struct shutdown_trigger on_restart_trigger = {ON_RESTART_STR,
1733
	&stop_action};
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750

static ssize_t on_restart_show(struct kobject *kobj,
			       struct kobj_attribute *attr, char *page)
{
	return sprintf(page, "%s\n", on_restart_trigger.action->name);
}

static ssize_t on_restart_store(struct kobject *kobj,
				struct kobj_attribute *attr,
				const char *buf, size_t len)
{
	return set_trigger(buf, &on_restart_trigger, len);
}

static struct kobj_attribute on_restart_attr =
	__ATTR(on_restart, 0644, on_restart_show, on_restart_store);

M
Martin Schwidefsky 已提交
1751
static void __do_restart(void *ignore)
1752
{
1753
	__arch_local_irq_stosm(0x04); /* enable DAT */
1754
	smp_send_stop();
M
Michael Holzheu 已提交
1755 1756 1757
#ifdef CONFIG_CRASH_DUMP
	crash_kexec(NULL);
#endif
1758 1759 1760 1761
	on_restart_trigger.action->fn(&on_restart_trigger);
	stop_run(&on_restart_trigger);
}

M
Martin Schwidefsky 已提交
1762 1763
void do_restart(void)
{
1764 1765 1766
	tracing_off();
	debug_locks_off();
	lgr_info_log();
M
Martin Schwidefsky 已提交
1767 1768 1769
	smp_call_online_cpu(__do_restart, NULL);
}

1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788
/* on halt */

static struct shutdown_trigger on_halt_trigger = {ON_HALT_STR, &stop_action};

static ssize_t on_halt_show(struct kobject *kobj,
			    struct kobj_attribute *attr, char *page)
{
	return sprintf(page, "%s\n", on_halt_trigger.action->name);
}

static ssize_t on_halt_store(struct kobject *kobj,
			     struct kobj_attribute *attr,
			     const char *buf, size_t len)
{
	return set_trigger(buf, &on_halt_trigger, len);
}

static struct kobj_attribute on_halt_attr =
	__ATTR(on_halt, 0644, on_halt_show, on_halt_store);
M
Michael Holzheu 已提交
1789

1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829

static void do_machine_halt(void)
{
	smp_send_stop();
	on_halt_trigger.action->fn(&on_halt_trigger);
	stop_run(&on_halt_trigger);
}
void (*_machine_halt)(void) = do_machine_halt;

/* on power off */

static struct shutdown_trigger on_poff_trigger = {ON_POFF_STR, &stop_action};

static ssize_t on_poff_show(struct kobject *kobj,
			    struct kobj_attribute *attr, char *page)
{
	return sprintf(page, "%s\n", on_poff_trigger.action->name);
}

static ssize_t on_poff_store(struct kobject *kobj,
			     struct kobj_attribute *attr,
			     const char *buf, size_t len)
{
	return set_trigger(buf, &on_poff_trigger, len);
}

static struct kobj_attribute on_poff_attr =
	__ATTR(on_poff, 0644, on_poff_show, on_poff_store);


static void do_machine_power_off(void)
{
	smp_send_stop();
	on_poff_trigger.action->fn(&on_poff_trigger);
	stop_run(&on_poff_trigger);
}
void (*_machine_power_off)(void) = do_machine_power_off;

static void __init shutdown_triggers_init(void)
{
1830
	shutdown_actions_kset = kset_create_and_add("shutdown_actions", NULL,
1831
						    firmware_kobj);
1832
	if (!shutdown_actions_kset)
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845
		goto fail;
	if (sysfs_create_file(&shutdown_actions_kset->kobj,
			      &on_reboot_attr.attr))
		goto fail;
	if (sysfs_create_file(&shutdown_actions_kset->kobj,
			      &on_panic_attr.attr))
		goto fail;
	if (sysfs_create_file(&shutdown_actions_kset->kobj,
			      &on_halt_attr.attr))
		goto fail;
	if (sysfs_create_file(&shutdown_actions_kset->kobj,
			      &on_poff_attr.attr))
		goto fail;
1846 1847 1848
	if (sysfs_create_file(&shutdown_actions_kset->kobj,
			      &on_restart_attr.attr))
		goto fail;
1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860
	return;
fail:
	panic("shutdown_triggers_init failed\n");
}

static void __init shutdown_actions_init(void)
{
	int i;

	for (i = 0; i < SHUTDOWN_ACTIONS_COUNT; i++) {
		if (!shutdown_actions_list[i]->init)
			continue;
1861 1862
		shutdown_actions_list[i]->init_rc =
			shutdown_actions_list[i]->init();
M
Michael Holzheu 已提交
1863 1864 1865 1866 1867
	}
}

static int __init s390_ipl_init(void)
{
1868
	sclp_get_ipl_info(&sclp_ipl_info);
1869 1870
	shutdown_actions_init();
	shutdown_triggers_init();
M
Michael Holzheu 已提交
1871 1872 1873 1874
	return 0;
}

__initcall(s390_ipl_init);
1875

1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942
static void __init strncpy_skip_quote(char *dst, char *src, int n)
{
	int sx, dx;

	dx = 0;
	for (sx = 0; src[sx] != 0; sx++) {
		if (src[sx] == '"')
			continue;
		dst[dx++] = src[sx];
		if (dx >= n)
			break;
	}
}

static int __init vmcmd_on_reboot_setup(char *str)
{
	if (!MACHINE_IS_VM)
		return 1;
	strncpy_skip_quote(vmcmd_on_reboot, str, 127);
	vmcmd_on_reboot[127] = 0;
	on_reboot_trigger.action = &vmcmd_action;
	return 1;
}
__setup("vmreboot=", vmcmd_on_reboot_setup);

static int __init vmcmd_on_panic_setup(char *str)
{
	if (!MACHINE_IS_VM)
		return 1;
	strncpy_skip_quote(vmcmd_on_panic, str, 127);
	vmcmd_on_panic[127] = 0;
	on_panic_trigger.action = &vmcmd_action;
	return 1;
}
__setup("vmpanic=", vmcmd_on_panic_setup);

static int __init vmcmd_on_halt_setup(char *str)
{
	if (!MACHINE_IS_VM)
		return 1;
	strncpy_skip_quote(vmcmd_on_halt, str, 127);
	vmcmd_on_halt[127] = 0;
	on_halt_trigger.action = &vmcmd_action;
	return 1;
}
__setup("vmhalt=", vmcmd_on_halt_setup);

static int __init vmcmd_on_poff_setup(char *str)
{
	if (!MACHINE_IS_VM)
		return 1;
	strncpy_skip_quote(vmcmd_on_poff, str, 127);
	vmcmd_on_poff[127] = 0;
	on_poff_trigger.action = &vmcmd_action;
	return 1;
}
__setup("vmpoff=", vmcmd_on_poff_setup);

static int on_panic_notify(struct notifier_block *self,
			   unsigned long event, void *data)
{
	do_panic();
	return NOTIFY_OK;
}

static struct notifier_block on_panic_nb = {
	.notifier_call = on_panic_notify,
1943
	.priority = INT_MIN,
1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972
};

void __init setup_ipl(void)
{
	ipl_info.type = get_ipl_type();
	switch (ipl_info.type) {
	case IPL_TYPE_CCW:
		ipl_info.data.ccw.dev_id.devno = ipl_devno;
		ipl_info.data.ccw.dev_id.ssid = 0;
		break;
	case IPL_TYPE_FCP:
	case IPL_TYPE_FCP_DUMP:
		ipl_info.data.fcp.dev_id.devno =
			IPL_PARMBLOCK_START->ipl_info.fcp.devno;
		ipl_info.data.fcp.dev_id.ssid = 0;
		ipl_info.data.fcp.wwpn = IPL_PARMBLOCK_START->ipl_info.fcp.wwpn;
		ipl_info.data.fcp.lun = IPL_PARMBLOCK_START->ipl_info.fcp.lun;
		break;
	case IPL_TYPE_NSS:
		strncpy(ipl_info.data.nss.name, kernel_nss_name,
			sizeof(ipl_info.data.nss.name));
		break;
	case IPL_TYPE_UNKNOWN:
		/* We have no info to copy */
		break;
	}
	atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
}

1973 1974
void __init ipl_update_parameters(void)
{
1975 1976 1977 1978
	int rc;

	rc = diag308(DIAG308_STORE, &ipl_block);
	if ((rc == DIAG308_RC_OK) || (rc == DIAG308_RC_NOCONFIG))
1979 1980 1981
		diag308_set_works = 1;
}

1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994
void __init ipl_save_parameters(void)
{
	struct cio_iplinfo iplinfo;
	void *src, *dst;

	if (cio_get_iplinfo(&iplinfo))
		return;

	ipl_devno = iplinfo.devno;
	ipl_flags |= IPL_DEVNO_VALID;
	if (!iplinfo.is_qdio)
		return;
	ipl_flags |= IPL_PARMBLOCK_VALID;
1995
	src = (void *)(unsigned long)S390_lowcore.ipl_parmblock_ptr;
1996 1997
	dst = (void *)IPL_PARMBLOCK_ORIGIN;
	memmove(dst, src, PAGE_SIZE);
1998
	S390_lowcore.ipl_parmblock_ptr = IPL_PARMBLOCK_ORIGIN;
1999 2000
}

2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023
static LIST_HEAD(rcall);
static DEFINE_MUTEX(rcall_mutex);

void register_reset_call(struct reset_call *reset)
{
	mutex_lock(&rcall_mutex);
	list_add(&reset->list, &rcall);
	mutex_unlock(&rcall_mutex);
}
EXPORT_SYMBOL_GPL(register_reset_call);

void unregister_reset_call(struct reset_call *reset)
{
	mutex_lock(&rcall_mutex);
	list_del(&reset->list);
	mutex_unlock(&rcall_mutex);
}
EXPORT_SYMBOL_GPL(unregister_reset_call);

static void do_reset_calls(void)
{
	struct reset_call *reset;

2024 2025 2026 2027 2028 2029
#ifdef CONFIG_64BIT
	if (diag308_set_works) {
		diag308_reset();
		return;
	}
#endif
2030 2031 2032 2033
	list_for_each_entry(reset, &rcall, list)
		reset->fn();
}

2034
u32 dump_prefix_page;
2035

M
Michael Holzheu 已提交
2036
void s390_reset_system(void (*func)(void *), void *data)
2037 2038 2039 2040
{
	struct _lowcore *lc;

	lc = (struct _lowcore *)(unsigned long) store_prefix();
M
Michael Holzheu 已提交
2041 2042

	/* Stack for interrupt/machine check handler */
2043 2044
	lc->panic_stack = S390_lowcore.panic_stack;

2045
	/* Save prefix page address for dump case */
2046
	dump_prefix_page = (u32)(unsigned long) lc;
2047

2048 2049 2050 2051 2052 2053 2054
	/* Disable prefixing */
	set_prefix(0);

	/* Disable lowcore protection */
	__ctl_clear_bit(0,28);

	/* 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
		PSW_ADDR_AMODE | (unsigned long) s390_base_mcck_handler;
M
Michael Holzheu 已提交
2058 2059

	/* Set new program check handler */
2060
	S390_lowcore.program_new_psw.mask = psw_kernel_bits | PSW_MASK_DAT;
M
Michael Holzheu 已提交
2061
	S390_lowcore.program_new_psw.addr =
2062
		PSW_ADDR_AMODE | (unsigned long) s390_base_pgm_handler;
M
Michael Holzheu 已提交
2063

M
Michael Holzheu 已提交
2064 2065 2066
	/* Store status at absolute zero */
	store_status();

2067
	do_reset_calls();
M
Michael Holzheu 已提交
2068 2069
	if (func)
		func(data);
2070
}