Linux Audio

Check our new training course

Loading...
v4.6
   1/*
   2 * APEI Error Record Serialization Table support
   3 *
   4 * ERST is a way provided by APEI to save and retrieve hardware error
   5 * information to and from a persistent store.
   6 *
   7 * For more information about ERST, please refer to ACPI Specification
   8 * version 4.0, section 17.4.
   9 *
  10 * Copyright 2010 Intel Corp.
  11 *   Author: Huang Ying <ying.huang@intel.com>
  12 *
  13 * This program is free software; you can redistribute it and/or
  14 * modify it under the terms of the GNU General Public License version
  15 * 2 as published by the Free Software Foundation.
  16 *
  17 * This program is distributed in the hope that it will be useful,
  18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20 * GNU General Public License for more details.
 
 
 
 
  21 */
  22
  23#include <linux/kernel.h>
  24#include <linux/module.h>
  25#include <linux/init.h>
  26#include <linux/delay.h>
  27#include <linux/io.h>
  28#include <linux/acpi.h>
  29#include <linux/uaccess.h>
  30#include <linux/cper.h>
  31#include <linux/nmi.h>
  32#include <linux/hardirq.h>
  33#include <linux/pstore.h>
  34#include <linux/vmalloc.h>
  35#include <linux/mm.h> /* kvfree() */
  36#include <acpi/apei.h>
  37
  38#include "apei-internal.h"
  39
  40#undef pr_fmt
  41#define pr_fmt(fmt) "ERST: " fmt
  42
  43/* ERST command status */
  44#define ERST_STATUS_SUCCESS			0x0
  45#define ERST_STATUS_NOT_ENOUGH_SPACE		0x1
  46#define ERST_STATUS_HARDWARE_NOT_AVAILABLE	0x2
  47#define ERST_STATUS_FAILED			0x3
  48#define ERST_STATUS_RECORD_STORE_EMPTY		0x4
  49#define ERST_STATUS_RECORD_NOT_FOUND		0x5
  50
  51#define ERST_TAB_ENTRY(tab)						\
  52	((struct acpi_whea_header *)((char *)(tab) +			\
  53				     sizeof(struct acpi_table_erst)))
  54
  55#define SPIN_UNIT		100			/* 100ns */
  56/* Firmware should respond within 1 milliseconds */
  57#define FIRMWARE_TIMEOUT	(1 * NSEC_PER_MSEC)
  58#define FIRMWARE_MAX_STALL	50			/* 50us */
  59
  60int erst_disable;
  61EXPORT_SYMBOL_GPL(erst_disable);
  62
  63static struct acpi_table_erst *erst_tab;
  64
  65/* ERST Error Log Address Range atrributes */
  66#define ERST_RANGE_RESERVED	0x0001
  67#define ERST_RANGE_NVRAM	0x0002
  68#define ERST_RANGE_SLOW		0x0004
  69
  70/*
  71 * ERST Error Log Address Range, used as buffer for reading/writing
  72 * error records.
  73 */
  74static struct erst_erange {
  75	u64 base;
  76	u64 size;
  77	void __iomem *vaddr;
  78	u32 attr;
  79} erst_erange;
  80
  81/*
  82 * Prevent ERST interpreter to run simultaneously, because the
  83 * corresponding firmware implementation may not work properly when
  84 * invoked simultaneously.
  85 *
  86 * It is used to provide exclusive accessing for ERST Error Log
  87 * Address Range too.
  88 */
  89static DEFINE_RAW_SPINLOCK(erst_lock);
  90
  91static inline int erst_errno(int command_status)
  92{
  93	switch (command_status) {
  94	case ERST_STATUS_SUCCESS:
  95		return 0;
  96	case ERST_STATUS_HARDWARE_NOT_AVAILABLE:
  97		return -ENODEV;
  98	case ERST_STATUS_NOT_ENOUGH_SPACE:
  99		return -ENOSPC;
 100	case ERST_STATUS_RECORD_STORE_EMPTY:
 101	case ERST_STATUS_RECORD_NOT_FOUND:
 102		return -ENOENT;
 103	default:
 104		return -EINVAL;
 105	}
 106}
 107
 108static int erst_timedout(u64 *t, u64 spin_unit)
 109{
 110	if ((s64)*t < spin_unit) {
 111		pr_warn(FW_WARN "Firmware does not respond in time.\n");
 
 112		return 1;
 113	}
 114	*t -= spin_unit;
 115	ndelay(spin_unit);
 116	touch_nmi_watchdog();
 117	return 0;
 118}
 119
 120static int erst_exec_load_var1(struct apei_exec_context *ctx,
 121			       struct acpi_whea_header *entry)
 122{
 123	return __apei_exec_read_register(entry, &ctx->var1);
 124}
 125
 126static int erst_exec_load_var2(struct apei_exec_context *ctx,
 127			       struct acpi_whea_header *entry)
 128{
 129	return __apei_exec_read_register(entry, &ctx->var2);
 130}
 131
 132static int erst_exec_store_var1(struct apei_exec_context *ctx,
 133				struct acpi_whea_header *entry)
 134{
 135	return __apei_exec_write_register(entry, ctx->var1);
 136}
 137
 138static int erst_exec_add(struct apei_exec_context *ctx,
 139			 struct acpi_whea_header *entry)
 140{
 141	ctx->var1 += ctx->var2;
 142	return 0;
 143}
 144
 145static int erst_exec_subtract(struct apei_exec_context *ctx,
 146			      struct acpi_whea_header *entry)
 147{
 148	ctx->var1 -= ctx->var2;
 149	return 0;
 150}
 151
 152static int erst_exec_add_value(struct apei_exec_context *ctx,
 153			       struct acpi_whea_header *entry)
 154{
 155	int rc;
 156	u64 val;
 157
 158	rc = __apei_exec_read_register(entry, &val);
 159	if (rc)
 160		return rc;
 161	val += ctx->value;
 162	rc = __apei_exec_write_register(entry, val);
 163	return rc;
 164}
 165
 166static int erst_exec_subtract_value(struct apei_exec_context *ctx,
 167				    struct acpi_whea_header *entry)
 168{
 169	int rc;
 170	u64 val;
 171
 172	rc = __apei_exec_read_register(entry, &val);
 173	if (rc)
 174		return rc;
 175	val -= ctx->value;
 176	rc = __apei_exec_write_register(entry, val);
 177	return rc;
 178}
 179
 180static int erst_exec_stall(struct apei_exec_context *ctx,
 181			   struct acpi_whea_header *entry)
 182{
 183	u64 stall_time;
 184
 185	if (ctx->value > FIRMWARE_MAX_STALL) {
 186		if (!in_nmi())
 187			pr_warn(FW_WARN
 188			"Too long stall time for stall instruction: 0x%llx.\n",
 189				   ctx->value);
 190		stall_time = FIRMWARE_MAX_STALL;
 191	} else
 192		stall_time = ctx->value;
 193	udelay(stall_time);
 194	return 0;
 195}
 196
 197static int erst_exec_stall_while_true(struct apei_exec_context *ctx,
 198				      struct acpi_whea_header *entry)
 199{
 200	int rc;
 201	u64 val;
 202	u64 timeout = FIRMWARE_TIMEOUT;
 203	u64 stall_time;
 204
 205	if (ctx->var1 > FIRMWARE_MAX_STALL) {
 206		if (!in_nmi())
 207			pr_warn(FW_WARN
 208		"Too long stall time for stall while true instruction: 0x%llx.\n",
 209				   ctx->var1);
 210		stall_time = FIRMWARE_MAX_STALL;
 211	} else
 212		stall_time = ctx->var1;
 213
 214	for (;;) {
 215		rc = __apei_exec_read_register(entry, &val);
 216		if (rc)
 217			return rc;
 218		if (val != ctx->value)
 219			break;
 220		if (erst_timedout(&timeout, stall_time * NSEC_PER_USEC))
 221			return -EIO;
 222	}
 223	return 0;
 224}
 225
 226static int erst_exec_skip_next_instruction_if_true(
 227	struct apei_exec_context *ctx,
 228	struct acpi_whea_header *entry)
 229{
 230	int rc;
 231	u64 val;
 232
 233	rc = __apei_exec_read_register(entry, &val);
 234	if (rc)
 235		return rc;
 236	if (val == ctx->value) {
 237		ctx->ip += 2;
 238		return APEI_EXEC_SET_IP;
 239	}
 240
 241	return 0;
 242}
 243
 244static int erst_exec_goto(struct apei_exec_context *ctx,
 245			  struct acpi_whea_header *entry)
 246{
 247	ctx->ip = ctx->value;
 248	return APEI_EXEC_SET_IP;
 249}
 250
 251static int erst_exec_set_src_address_base(struct apei_exec_context *ctx,
 252					  struct acpi_whea_header *entry)
 253{
 254	return __apei_exec_read_register(entry, &ctx->src_base);
 255}
 256
 257static int erst_exec_set_dst_address_base(struct apei_exec_context *ctx,
 258					  struct acpi_whea_header *entry)
 259{
 260	return __apei_exec_read_register(entry, &ctx->dst_base);
 261}
 262
 263static int erst_exec_move_data(struct apei_exec_context *ctx,
 264			       struct acpi_whea_header *entry)
 265{
 266	int rc;
 267	u64 offset;
 268	void *src, *dst;
 269
 270	/* ioremap does not work in interrupt context */
 271	if (in_interrupt()) {
 272		pr_warn("MOVE_DATA can not be used in interrupt context.\n");
 
 273		return -EBUSY;
 274	}
 275
 276	rc = __apei_exec_read_register(entry, &offset);
 277	if (rc)
 278		return rc;
 279
 280	src = ioremap(ctx->src_base + offset, ctx->var2);
 281	if (!src)
 282		return -ENOMEM;
 283	dst = ioremap(ctx->dst_base + offset, ctx->var2);
 284	if (!dst) {
 285		iounmap(src);
 286		return -ENOMEM;
 287	}
 288
 289	memmove(dst, src, ctx->var2);
 290
 291	iounmap(src);
 292	iounmap(dst);
 293
 294	return 0;
 295}
 296
 297static struct apei_exec_ins_type erst_ins_type[] = {
 298	[ACPI_ERST_READ_REGISTER] = {
 299		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 300		.run = apei_exec_read_register,
 301	},
 302	[ACPI_ERST_READ_REGISTER_VALUE] = {
 303		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 304		.run = apei_exec_read_register_value,
 305	},
 306	[ACPI_ERST_WRITE_REGISTER] = {
 307		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 308		.run = apei_exec_write_register,
 309	},
 310	[ACPI_ERST_WRITE_REGISTER_VALUE] = {
 311		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 312		.run = apei_exec_write_register_value,
 313	},
 314	[ACPI_ERST_NOOP] = {
 315		.flags = 0,
 316		.run = apei_exec_noop,
 317	},
 318	[ACPI_ERST_LOAD_VAR1] = {
 319		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 320		.run = erst_exec_load_var1,
 321	},
 322	[ACPI_ERST_LOAD_VAR2] = {
 323		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 324		.run = erst_exec_load_var2,
 325	},
 326	[ACPI_ERST_STORE_VAR1] = {
 327		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 328		.run = erst_exec_store_var1,
 329	},
 330	[ACPI_ERST_ADD] = {
 331		.flags = 0,
 332		.run = erst_exec_add,
 333	},
 334	[ACPI_ERST_SUBTRACT] = {
 335		.flags = 0,
 336		.run = erst_exec_subtract,
 337	},
 338	[ACPI_ERST_ADD_VALUE] = {
 339		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 340		.run = erst_exec_add_value,
 341	},
 342	[ACPI_ERST_SUBTRACT_VALUE] = {
 343		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 344		.run = erst_exec_subtract_value,
 345	},
 346	[ACPI_ERST_STALL] = {
 347		.flags = 0,
 348		.run = erst_exec_stall,
 349	},
 350	[ACPI_ERST_STALL_WHILE_TRUE] = {
 351		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 352		.run = erst_exec_stall_while_true,
 353	},
 354	[ACPI_ERST_SKIP_NEXT_IF_TRUE] = {
 355		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 356		.run = erst_exec_skip_next_instruction_if_true,
 357	},
 358	[ACPI_ERST_GOTO] = {
 359		.flags = 0,
 360		.run = erst_exec_goto,
 361	},
 362	[ACPI_ERST_SET_SRC_ADDRESS_BASE] = {
 363		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 364		.run = erst_exec_set_src_address_base,
 365	},
 366	[ACPI_ERST_SET_DST_ADDRESS_BASE] = {
 367		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 368		.run = erst_exec_set_dst_address_base,
 369	},
 370	[ACPI_ERST_MOVE_DATA] = {
 371		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 372		.run = erst_exec_move_data,
 373	},
 374};
 375
 376static inline void erst_exec_ctx_init(struct apei_exec_context *ctx)
 377{
 378	apei_exec_ctx_init(ctx, erst_ins_type, ARRAY_SIZE(erst_ins_type),
 379			   ERST_TAB_ENTRY(erst_tab), erst_tab->entries);
 380}
 381
 382static int erst_get_erange(struct erst_erange *range)
 383{
 384	struct apei_exec_context ctx;
 385	int rc;
 386
 387	erst_exec_ctx_init(&ctx);
 388	rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_RANGE);
 389	if (rc)
 390		return rc;
 391	range->base = apei_exec_ctx_get_output(&ctx);
 392	rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_LENGTH);
 393	if (rc)
 394		return rc;
 395	range->size = apei_exec_ctx_get_output(&ctx);
 396	rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_ATTRIBUTES);
 397	if (rc)
 398		return rc;
 399	range->attr = apei_exec_ctx_get_output(&ctx);
 400
 401	return 0;
 402}
 403
 404static ssize_t __erst_get_record_count(void)
 405{
 406	struct apei_exec_context ctx;
 407	int rc;
 408
 409	erst_exec_ctx_init(&ctx);
 410	rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_COUNT);
 411	if (rc)
 412		return rc;
 413	return apei_exec_ctx_get_output(&ctx);
 414}
 415
 416ssize_t erst_get_record_count(void)
 417{
 418	ssize_t count;
 419	unsigned long flags;
 420
 421	if (erst_disable)
 422		return -ENODEV;
 423
 424	raw_spin_lock_irqsave(&erst_lock, flags);
 425	count = __erst_get_record_count();
 426	raw_spin_unlock_irqrestore(&erst_lock, flags);
 427
 428	return count;
 429}
 430EXPORT_SYMBOL_GPL(erst_get_record_count);
 431
 432#define ERST_RECORD_ID_CACHE_SIZE_MIN	16
 433#define ERST_RECORD_ID_CACHE_SIZE_MAX	1024
 434
 435struct erst_record_id_cache {
 436	struct mutex lock;
 437	u64 *entries;
 438	int len;
 439	int size;
 440	int refcount;
 441};
 442
 443static struct erst_record_id_cache erst_record_id_cache = {
 444	.lock = __MUTEX_INITIALIZER(erst_record_id_cache.lock),
 445	.refcount = 0,
 446};
 447
 448static int __erst_get_next_record_id(u64 *record_id)
 449{
 450	struct apei_exec_context ctx;
 451	int rc;
 452
 453	erst_exec_ctx_init(&ctx);
 454	rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_ID);
 455	if (rc)
 456		return rc;
 457	*record_id = apei_exec_ctx_get_output(&ctx);
 458
 459	return 0;
 460}
 461
 462int erst_get_record_id_begin(int *pos)
 463{
 464	int rc;
 465
 466	if (erst_disable)
 467		return -ENODEV;
 468
 469	rc = mutex_lock_interruptible(&erst_record_id_cache.lock);
 470	if (rc)
 471		return rc;
 472	erst_record_id_cache.refcount++;
 473	mutex_unlock(&erst_record_id_cache.lock);
 474
 475	*pos = 0;
 476
 477	return 0;
 478}
 479EXPORT_SYMBOL_GPL(erst_get_record_id_begin);
 480
 481/* erst_record_id_cache.lock must be held by caller */
 482static int __erst_record_id_cache_add_one(void)
 483{
 484	u64 id, prev_id, first_id;
 485	int i, rc;
 486	u64 *entries;
 487	unsigned long flags;
 488
 489	id = prev_id = first_id = APEI_ERST_INVALID_RECORD_ID;
 490retry:
 491	raw_spin_lock_irqsave(&erst_lock, flags);
 492	rc = __erst_get_next_record_id(&id);
 493	raw_spin_unlock_irqrestore(&erst_lock, flags);
 494	if (rc == -ENOENT)
 495		return 0;
 496	if (rc)
 497		return rc;
 498	if (id == APEI_ERST_INVALID_RECORD_ID)
 499		return 0;
 500	/* can not skip current ID, or loop back to first ID */
 501	if (id == prev_id || id == first_id)
 502		return 0;
 503	if (first_id == APEI_ERST_INVALID_RECORD_ID)
 504		first_id = id;
 505	prev_id = id;
 506
 507	entries = erst_record_id_cache.entries;
 508	for (i = 0; i < erst_record_id_cache.len; i++) {
 509		if (entries[i] == id)
 510			break;
 511	}
 512	/* record id already in cache, try next */
 513	if (i < erst_record_id_cache.len)
 514		goto retry;
 515	if (erst_record_id_cache.len >= erst_record_id_cache.size) {
 516		int new_size, alloc_size;
 517		u64 *new_entries;
 518
 519		new_size = erst_record_id_cache.size * 2;
 520		new_size = clamp_val(new_size, ERST_RECORD_ID_CACHE_SIZE_MIN,
 521				     ERST_RECORD_ID_CACHE_SIZE_MAX);
 522		if (new_size <= erst_record_id_cache.size) {
 523			if (printk_ratelimit())
 524				pr_warn(FW_WARN "too many record IDs!\n");
 
 525			return 0;
 526		}
 527		alloc_size = new_size * sizeof(entries[0]);
 528		if (alloc_size < PAGE_SIZE)
 529			new_entries = kmalloc(alloc_size, GFP_KERNEL);
 530		else
 531			new_entries = vmalloc(alloc_size);
 532		if (!new_entries)
 533			return -ENOMEM;
 534		memcpy(new_entries, entries,
 535		       erst_record_id_cache.len * sizeof(entries[0]));
 536		kvfree(entries);
 
 
 
 537		erst_record_id_cache.entries = entries = new_entries;
 538		erst_record_id_cache.size = new_size;
 539	}
 540	entries[i] = id;
 541	erst_record_id_cache.len++;
 542
 543	return 1;
 544}
 545
 546/*
 547 * Get the record ID of an existing error record on the persistent
 548 * storage. If there is no error record on the persistent storage, the
 549 * returned record_id is APEI_ERST_INVALID_RECORD_ID.
 550 */
 551int erst_get_record_id_next(int *pos, u64 *record_id)
 552{
 553	int rc = 0;
 554	u64 *entries;
 555
 556	if (erst_disable)
 557		return -ENODEV;
 558
 559	/* must be enclosed by erst_get_record_id_begin/end */
 560	BUG_ON(!erst_record_id_cache.refcount);
 561	BUG_ON(*pos < 0 || *pos > erst_record_id_cache.len);
 562
 563	mutex_lock(&erst_record_id_cache.lock);
 564	entries = erst_record_id_cache.entries;
 565	for (; *pos < erst_record_id_cache.len; (*pos)++)
 566		if (entries[*pos] != APEI_ERST_INVALID_RECORD_ID)
 567			break;
 568	/* found next record id in cache */
 569	if (*pos < erst_record_id_cache.len) {
 570		*record_id = entries[*pos];
 571		(*pos)++;
 572		goto out_unlock;
 573	}
 574
 575	/* Try to add one more record ID to cache */
 576	rc = __erst_record_id_cache_add_one();
 577	if (rc < 0)
 578		goto out_unlock;
 579	/* successfully add one new ID */
 580	if (rc == 1) {
 581		*record_id = erst_record_id_cache.entries[*pos];
 582		(*pos)++;
 583		rc = 0;
 584	} else {
 585		*pos = -1;
 586		*record_id = APEI_ERST_INVALID_RECORD_ID;
 587	}
 588out_unlock:
 589	mutex_unlock(&erst_record_id_cache.lock);
 590
 591	return rc;
 592}
 593EXPORT_SYMBOL_GPL(erst_get_record_id_next);
 594
 595/* erst_record_id_cache.lock must be held by caller */
 596static void __erst_record_id_cache_compact(void)
 597{
 598	int i, wpos = 0;
 599	u64 *entries;
 600
 601	if (erst_record_id_cache.refcount)
 602		return;
 603
 604	entries = erst_record_id_cache.entries;
 605	for (i = 0; i < erst_record_id_cache.len; i++) {
 606		if (entries[i] == APEI_ERST_INVALID_RECORD_ID)
 607			continue;
 608		if (wpos != i)
 609			entries[wpos] = entries[i];
 610		wpos++;
 611	}
 612	erst_record_id_cache.len = wpos;
 613}
 614
 615void erst_get_record_id_end(void)
 616{
 617	/*
 618	 * erst_disable != 0 should be detected by invoker via the
 619	 * return value of erst_get_record_id_begin/next, so this
 620	 * function should not be called for erst_disable != 0.
 621	 */
 622	BUG_ON(erst_disable);
 623
 624	mutex_lock(&erst_record_id_cache.lock);
 625	erst_record_id_cache.refcount--;
 626	BUG_ON(erst_record_id_cache.refcount < 0);
 627	__erst_record_id_cache_compact();
 628	mutex_unlock(&erst_record_id_cache.lock);
 629}
 630EXPORT_SYMBOL_GPL(erst_get_record_id_end);
 631
 632static int __erst_write_to_storage(u64 offset)
 633{
 634	struct apei_exec_context ctx;
 635	u64 timeout = FIRMWARE_TIMEOUT;
 636	u64 val;
 637	int rc;
 638
 639	erst_exec_ctx_init(&ctx);
 640	rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_WRITE);
 641	if (rc)
 642		return rc;
 643	apei_exec_ctx_set_input(&ctx, offset);
 644	rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
 645	if (rc)
 646		return rc;
 647	rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
 648	if (rc)
 649		return rc;
 650	for (;;) {
 651		rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
 652		if (rc)
 653			return rc;
 654		val = apei_exec_ctx_get_output(&ctx);
 655		if (!val)
 656			break;
 657		if (erst_timedout(&timeout, SPIN_UNIT))
 658			return -EIO;
 659	}
 660	rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
 661	if (rc)
 662		return rc;
 663	val = apei_exec_ctx_get_output(&ctx);
 664	rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
 665	if (rc)
 666		return rc;
 667
 668	return erst_errno(val);
 669}
 670
 671static int __erst_read_from_storage(u64 record_id, u64 offset)
 672{
 673	struct apei_exec_context ctx;
 674	u64 timeout = FIRMWARE_TIMEOUT;
 675	u64 val;
 676	int rc;
 677
 678	erst_exec_ctx_init(&ctx);
 679	rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_READ);
 680	if (rc)
 681		return rc;
 682	apei_exec_ctx_set_input(&ctx, offset);
 683	rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
 684	if (rc)
 685		return rc;
 686	apei_exec_ctx_set_input(&ctx, record_id);
 687	rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
 688	if (rc)
 689		return rc;
 690	rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
 691	if (rc)
 692		return rc;
 693	for (;;) {
 694		rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
 695		if (rc)
 696			return rc;
 697		val = apei_exec_ctx_get_output(&ctx);
 698		if (!val)
 699			break;
 700		if (erst_timedout(&timeout, SPIN_UNIT))
 701			return -EIO;
 702	};
 703	rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
 704	if (rc)
 705		return rc;
 706	val = apei_exec_ctx_get_output(&ctx);
 707	rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
 708	if (rc)
 709		return rc;
 710
 711	return erst_errno(val);
 712}
 713
 714static int __erst_clear_from_storage(u64 record_id)
 715{
 716	struct apei_exec_context ctx;
 717	u64 timeout = FIRMWARE_TIMEOUT;
 718	u64 val;
 719	int rc;
 720
 721	erst_exec_ctx_init(&ctx);
 722	rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_CLEAR);
 723	if (rc)
 724		return rc;
 725	apei_exec_ctx_set_input(&ctx, record_id);
 726	rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
 727	if (rc)
 728		return rc;
 729	rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
 730	if (rc)
 731		return rc;
 732	for (;;) {
 733		rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
 734		if (rc)
 735			return rc;
 736		val = apei_exec_ctx_get_output(&ctx);
 737		if (!val)
 738			break;
 739		if (erst_timedout(&timeout, SPIN_UNIT))
 740			return -EIO;
 741	}
 742	rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
 743	if (rc)
 744		return rc;
 745	val = apei_exec_ctx_get_output(&ctx);
 746	rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
 747	if (rc)
 748		return rc;
 749
 750	return erst_errno(val);
 751}
 752
 753/* NVRAM ERST Error Log Address Range is not supported yet */
 754static void pr_unimpl_nvram(void)
 755{
 756	if (printk_ratelimit())
 757		pr_warn("NVRAM ERST Log Address Range not implemented yet.\n");
 
 758}
 759
 760static int __erst_write_to_nvram(const struct cper_record_header *record)
 761{
 762	/* do not print message, because printk is not safe for NMI */
 763	return -ENOSYS;
 764}
 765
 766static int __erst_read_to_erange_from_nvram(u64 record_id, u64 *offset)
 767{
 768	pr_unimpl_nvram();
 769	return -ENOSYS;
 770}
 771
 772static int __erst_clear_from_nvram(u64 record_id)
 773{
 774	pr_unimpl_nvram();
 775	return -ENOSYS;
 776}
 777
 778int erst_write(const struct cper_record_header *record)
 779{
 780	int rc;
 781	unsigned long flags;
 782	struct cper_record_header *rcd_erange;
 783
 784	if (erst_disable)
 785		return -ENODEV;
 786
 787	if (memcmp(record->signature, CPER_SIG_RECORD, CPER_SIG_SIZE))
 788		return -EINVAL;
 789
 790	if (erst_erange.attr & ERST_RANGE_NVRAM) {
 791		if (!raw_spin_trylock_irqsave(&erst_lock, flags))
 792			return -EBUSY;
 793		rc = __erst_write_to_nvram(record);
 794		raw_spin_unlock_irqrestore(&erst_lock, flags);
 795		return rc;
 796	}
 797
 798	if (record->record_length > erst_erange.size)
 799		return -EINVAL;
 800
 801	if (!raw_spin_trylock_irqsave(&erst_lock, flags))
 802		return -EBUSY;
 803	memcpy(erst_erange.vaddr, record, record->record_length);
 804	rcd_erange = erst_erange.vaddr;
 805	/* signature for serialization system */
 806	memcpy(&rcd_erange->persistence_information, "ER", 2);
 807
 808	rc = __erst_write_to_storage(0);
 809	raw_spin_unlock_irqrestore(&erst_lock, flags);
 810
 811	return rc;
 812}
 813EXPORT_SYMBOL_GPL(erst_write);
 814
 815static int __erst_read_to_erange(u64 record_id, u64 *offset)
 816{
 817	int rc;
 818
 819	if (erst_erange.attr & ERST_RANGE_NVRAM)
 820		return __erst_read_to_erange_from_nvram(
 821			record_id, offset);
 822
 823	rc = __erst_read_from_storage(record_id, 0);
 824	if (rc)
 825		return rc;
 826	*offset = 0;
 827
 828	return 0;
 829}
 830
 831static ssize_t __erst_read(u64 record_id, struct cper_record_header *record,
 832			   size_t buflen)
 833{
 834	int rc;
 835	u64 offset, len = 0;
 836	struct cper_record_header *rcd_tmp;
 837
 838	rc = __erst_read_to_erange(record_id, &offset);
 839	if (rc)
 840		return rc;
 841	rcd_tmp = erst_erange.vaddr + offset;
 842	len = rcd_tmp->record_length;
 843	if (len <= buflen)
 844		memcpy(record, rcd_tmp, len);
 845
 846	return len;
 847}
 848
 849/*
 850 * If return value > buflen, the buffer size is not big enough,
 851 * else if return value < 0, something goes wrong,
 852 * else everything is OK, and return value is record length
 853 */
 854ssize_t erst_read(u64 record_id, struct cper_record_header *record,
 855		  size_t buflen)
 856{
 857	ssize_t len;
 858	unsigned long flags;
 859
 860	if (erst_disable)
 861		return -ENODEV;
 862
 863	raw_spin_lock_irqsave(&erst_lock, flags);
 864	len = __erst_read(record_id, record, buflen);
 865	raw_spin_unlock_irqrestore(&erst_lock, flags);
 866	return len;
 867}
 868EXPORT_SYMBOL_GPL(erst_read);
 869
 870int erst_clear(u64 record_id)
 871{
 872	int rc, i;
 873	unsigned long flags;
 874	u64 *entries;
 875
 876	if (erst_disable)
 877		return -ENODEV;
 878
 879	rc = mutex_lock_interruptible(&erst_record_id_cache.lock);
 880	if (rc)
 881		return rc;
 882	raw_spin_lock_irqsave(&erst_lock, flags);
 883	if (erst_erange.attr & ERST_RANGE_NVRAM)
 884		rc = __erst_clear_from_nvram(record_id);
 885	else
 886		rc = __erst_clear_from_storage(record_id);
 887	raw_spin_unlock_irqrestore(&erst_lock, flags);
 888	if (rc)
 889		goto out;
 890	entries = erst_record_id_cache.entries;
 891	for (i = 0; i < erst_record_id_cache.len; i++) {
 892		if (entries[i] == record_id)
 893			entries[i] = APEI_ERST_INVALID_RECORD_ID;
 894	}
 895	__erst_record_id_cache_compact();
 896out:
 897	mutex_unlock(&erst_record_id_cache.lock);
 898	return rc;
 899}
 900EXPORT_SYMBOL_GPL(erst_clear);
 901
 902static int __init setup_erst_disable(char *str)
 903{
 904	erst_disable = 1;
 905	return 0;
 906}
 907
 908__setup("erst_disable", setup_erst_disable);
 909
 910static int erst_check_table(struct acpi_table_erst *erst_tab)
 911{
 912	if ((erst_tab->header_length !=
 913	     (sizeof(struct acpi_table_erst) - sizeof(erst_tab->header)))
 914	    && (erst_tab->header_length != sizeof(struct acpi_table_erst)))
 915		return -EINVAL;
 916	if (erst_tab->header.length < sizeof(struct acpi_table_erst))
 917		return -EINVAL;
 918	if (erst_tab->entries !=
 919	    (erst_tab->header.length - sizeof(struct acpi_table_erst)) /
 920	    sizeof(struct acpi_erst_entry))
 921		return -EINVAL;
 922
 923	return 0;
 924}
 925
 926static int erst_open_pstore(struct pstore_info *psi);
 927static int erst_close_pstore(struct pstore_info *psi);
 928static ssize_t erst_reader(u64 *id, enum pstore_type_id *type, int *count,
 929			   struct timespec *time, char **buf,
 930			   bool *compressed, struct pstore_info *psi);
 931static int erst_writer(enum pstore_type_id type, enum kmsg_dump_reason reason,
 932		       u64 *id, unsigned int part, int count, bool compressed,
 933		       size_t size, struct pstore_info *psi);
 934static int erst_clearer(enum pstore_type_id type, u64 id, int count,
 935			struct timespec time, struct pstore_info *psi);
 936
 937static struct pstore_info erst_info = {
 938	.owner		= THIS_MODULE,
 939	.name		= "erst",
 940	.flags		= PSTORE_FLAGS_FRAGILE,
 941	.open		= erst_open_pstore,
 942	.close		= erst_close_pstore,
 943	.read		= erst_reader,
 944	.write		= erst_writer,
 945	.erase		= erst_clearer
 946};
 947
 948#define CPER_CREATOR_PSTORE						\
 949	UUID_LE(0x75a574e3, 0x5052, 0x4b29, 0x8a, 0x8e, 0xbe, 0x2c,	\
 950		0x64, 0x90, 0xb8, 0x9d)
 951#define CPER_SECTION_TYPE_DMESG						\
 952	UUID_LE(0xc197e04e, 0xd545, 0x4a70, 0x9c, 0x17, 0xa5, 0x54,	\
 953		0x94, 0x19, 0xeb, 0x12)
 954#define CPER_SECTION_TYPE_DMESG_Z					\
 955	UUID_LE(0x4f118707, 0x04dd, 0x4055, 0xb5, 0xdd, 0x95, 0x6d,	\
 956		0x34, 0xdd, 0xfa, 0xc6)
 957#define CPER_SECTION_TYPE_MCE						\
 958	UUID_LE(0xfe08ffbe, 0x95e4, 0x4be7, 0xbc, 0x73, 0x40, 0x96,	\
 959		0x04, 0x4a, 0x38, 0xfc)
 960
 961struct cper_pstore_record {
 962	struct cper_record_header hdr;
 963	struct cper_section_descriptor sec_hdr;
 964	char data[];
 965} __packed;
 966
 967static int reader_pos;
 968
 969static int erst_open_pstore(struct pstore_info *psi)
 970{
 971	int rc;
 972
 973	if (erst_disable)
 974		return -ENODEV;
 975
 976	rc = erst_get_record_id_begin(&reader_pos);
 977
 978	return rc;
 979}
 980
 981static int erst_close_pstore(struct pstore_info *psi)
 982{
 983	erst_get_record_id_end();
 984
 985	return 0;
 986}
 987
 988static ssize_t erst_reader(u64 *id, enum pstore_type_id *type, int *count,
 989			   struct timespec *time, char **buf,
 990			   bool *compressed, struct pstore_info *psi)
 991{
 992	int rc;
 993	ssize_t len = 0;
 994	u64 record_id;
 995	struct cper_pstore_record *rcd;
 996	size_t rcd_len = sizeof(*rcd) + erst_info.bufsize;
 997
 998	if (erst_disable)
 999		return -ENODEV;
1000
1001	rcd = kmalloc(rcd_len, GFP_KERNEL);
1002	if (!rcd) {
1003		rc = -ENOMEM;
1004		goto out;
1005	}
1006skip:
1007	rc = erst_get_record_id_next(&reader_pos, &record_id);
1008	if (rc)
1009		goto out;
1010
1011	/* no more record */
1012	if (record_id == APEI_ERST_INVALID_RECORD_ID) {
1013		rc = -EINVAL;
1014		goto out;
1015	}
1016
1017	len = erst_read(record_id, &rcd->hdr, rcd_len);
1018	/* The record may be cleared by others, try read next record */
1019	if (len == -ENOENT)
1020		goto skip;
1021	else if (len < sizeof(*rcd)) {
1022		rc = -EIO;
1023		goto out;
1024	}
1025	if (uuid_le_cmp(rcd->hdr.creator_id, CPER_CREATOR_PSTORE) != 0)
1026		goto skip;
1027
1028	*buf = kmalloc(len, GFP_KERNEL);
1029	if (*buf == NULL) {
1030		rc = -ENOMEM;
1031		goto out;
1032	}
1033	memcpy(*buf, rcd->data, len - sizeof(*rcd));
1034	*id = record_id;
1035	*compressed = false;
1036	if (uuid_le_cmp(rcd->sec_hdr.section_type,
1037			CPER_SECTION_TYPE_DMESG_Z) == 0) {
1038		*type = PSTORE_TYPE_DMESG;
1039		*compressed = true;
1040	} else if (uuid_le_cmp(rcd->sec_hdr.section_type,
1041			CPER_SECTION_TYPE_DMESG) == 0)
1042		*type = PSTORE_TYPE_DMESG;
1043	else if (uuid_le_cmp(rcd->sec_hdr.section_type,
1044			     CPER_SECTION_TYPE_MCE) == 0)
1045		*type = PSTORE_TYPE_MCE;
1046	else
1047		*type = PSTORE_TYPE_UNKNOWN;
1048
1049	if (rcd->hdr.validation_bits & CPER_VALID_TIMESTAMP)
1050		time->tv_sec = rcd->hdr.timestamp;
1051	else
1052		time->tv_sec = 0;
1053	time->tv_nsec = 0;
1054
1055out:
1056	kfree(rcd);
1057	return (rc < 0) ? rc : (len - sizeof(*rcd));
1058}
1059
1060static int erst_writer(enum pstore_type_id type, enum kmsg_dump_reason reason,
1061		       u64 *id, unsigned int part, int count, bool compressed,
1062		       size_t size, struct pstore_info *psi)
1063{
1064	struct cper_pstore_record *rcd = (struct cper_pstore_record *)
1065					(erst_info.buf - sizeof(*rcd));
1066	int ret;
1067
1068	memset(rcd, 0, sizeof(*rcd));
1069	memcpy(rcd->hdr.signature, CPER_SIG_RECORD, CPER_SIG_SIZE);
1070	rcd->hdr.revision = CPER_RECORD_REV;
1071	rcd->hdr.signature_end = CPER_SIG_END;
1072	rcd->hdr.section_count = 1;
1073	rcd->hdr.error_severity = CPER_SEV_FATAL;
1074	/* timestamp valid. platform_id, partition_id are invalid */
1075	rcd->hdr.validation_bits = CPER_VALID_TIMESTAMP;
1076	rcd->hdr.timestamp = get_seconds();
1077	rcd->hdr.record_length = sizeof(*rcd) + size;
1078	rcd->hdr.creator_id = CPER_CREATOR_PSTORE;
1079	rcd->hdr.notification_type = CPER_NOTIFY_MCE;
1080	rcd->hdr.record_id = cper_next_record_id();
1081	rcd->hdr.flags = CPER_HW_ERROR_FLAGS_PREVERR;
1082
1083	rcd->sec_hdr.section_offset = sizeof(*rcd);
1084	rcd->sec_hdr.section_length = size;
1085	rcd->sec_hdr.revision = CPER_SEC_REV;
1086	/* fru_id and fru_text is invalid */
1087	rcd->sec_hdr.validation_bits = 0;
1088	rcd->sec_hdr.flags = CPER_SEC_PRIMARY;
1089	switch (type) {
1090	case PSTORE_TYPE_DMESG:
1091		if (compressed)
1092			rcd->sec_hdr.section_type = CPER_SECTION_TYPE_DMESG_Z;
1093		else
1094			rcd->sec_hdr.section_type = CPER_SECTION_TYPE_DMESG;
1095		break;
1096	case PSTORE_TYPE_MCE:
1097		rcd->sec_hdr.section_type = CPER_SECTION_TYPE_MCE;
1098		break;
1099	default:
1100		return -EINVAL;
1101	}
1102	rcd->sec_hdr.section_severity = CPER_SEV_FATAL;
1103
1104	ret = erst_write(&rcd->hdr);
1105	*id = rcd->hdr.record_id;
1106
1107	return ret;
1108}
1109
1110static int erst_clearer(enum pstore_type_id type, u64 id, int count,
1111			struct timespec time, struct pstore_info *psi)
1112{
1113	return erst_clear(id);
1114}
1115
1116static int __init erst_init(void)
1117{
1118	int rc = 0;
1119	acpi_status status;
1120	struct apei_exec_context ctx;
1121	struct apei_resources erst_resources;
1122	struct resource *r;
1123	char *buf;
1124
1125	if (acpi_disabled)
1126		goto err;
1127
1128	if (erst_disable) {
1129		pr_info(
1130	"Error Record Serialization Table (ERST) support is disabled.\n");
1131		goto err;
1132	}
1133
1134	status = acpi_get_table(ACPI_SIG_ERST, 0,
1135				(struct acpi_table_header **)&erst_tab);
1136	if (status == AE_NOT_FOUND)
1137		goto err;
1138	else if (ACPI_FAILURE(status)) {
1139		const char *msg = acpi_format_exception(status);
1140		pr_err("Failed to get table, %s\n", msg);
1141		rc = -EINVAL;
1142		goto err;
1143	}
1144
1145	rc = erst_check_table(erst_tab);
1146	if (rc) {
1147		pr_err(FW_BUG "ERST table is invalid.\n");
1148		goto err;
1149	}
1150
1151	apei_resources_init(&erst_resources);
1152	erst_exec_ctx_init(&ctx);
1153	rc = apei_exec_collect_resources(&ctx, &erst_resources);
1154	if (rc)
1155		goto err_fini;
1156	rc = apei_resources_request(&erst_resources, "APEI ERST");
1157	if (rc)
1158		goto err_fini;
1159	rc = apei_exec_pre_map_gars(&ctx);
1160	if (rc)
1161		goto err_release;
1162	rc = erst_get_erange(&erst_erange);
1163	if (rc) {
1164		if (rc == -ENODEV)
1165			pr_info(
1166	"The corresponding hardware device or firmware implementation "
1167	"is not available.\n");
1168		else
1169			pr_err("Failed to get Error Log Address Range.\n");
 
1170		goto err_unmap_reg;
1171	}
1172
1173	r = request_mem_region(erst_erange.base, erst_erange.size, "APEI ERST");
1174	if (!r) {
1175		pr_err("Can not request [mem %#010llx-%#010llx] for ERST.\n",
1176		       (unsigned long long)erst_erange.base,
1177		       (unsigned long long)erst_erange.base + erst_erange.size - 1);
 
1178		rc = -EIO;
1179		goto err_unmap_reg;
1180	}
1181	rc = -ENOMEM;
1182	erst_erange.vaddr = ioremap_cache(erst_erange.base,
1183					  erst_erange.size);
1184	if (!erst_erange.vaddr)
1185		goto err_release_erange;
1186
1187	pr_info(
1188	"Error Record Serialization Table (ERST) support is initialized.\n");
1189
1190	buf = kmalloc(erst_erange.size, GFP_KERNEL);
1191	spin_lock_init(&erst_info.buf_lock);
1192	if (buf) {
1193		erst_info.buf = buf + sizeof(struct cper_pstore_record);
1194		erst_info.bufsize = erst_erange.size -
1195				    sizeof(struct cper_pstore_record);
1196		rc = pstore_register(&erst_info);
1197		if (rc) {
1198			if (rc != -EPERM)
1199				pr_info(
1200				"Could not register with persistent store.\n");
1201			erst_info.buf = NULL;
1202			erst_info.bufsize = 0;
1203			kfree(buf);
1204		}
1205	} else
1206		pr_err(
1207		"Failed to allocate %lld bytes for persistent store error log.\n",
1208		erst_erange.size);
1209
1210	/* Cleanup ERST Resources */
1211	apei_resources_fini(&erst_resources);
1212
1213	return 0;
1214
1215err_release_erange:
1216	release_mem_region(erst_erange.base, erst_erange.size);
1217err_unmap_reg:
1218	apei_exec_post_unmap_gars(&ctx);
1219err_release:
1220	apei_resources_release(&erst_resources);
1221err_fini:
1222	apei_resources_fini(&erst_resources);
1223err:
1224	erst_disable = 1;
1225	return rc;
1226}
1227
1228device_initcall(erst_init);
v3.5.6
   1/*
   2 * APEI Error Record Serialization Table support
   3 *
   4 * ERST is a way provided by APEI to save and retrieve hardware error
   5 * information to and from a persistent store.
   6 *
   7 * For more information about ERST, please refer to ACPI Specification
   8 * version 4.0, section 17.4.
   9 *
  10 * Copyright 2010 Intel Corp.
  11 *   Author: Huang Ying <ying.huang@intel.com>
  12 *
  13 * This program is free software; you can redistribute it and/or
  14 * modify it under the terms of the GNU General Public License version
  15 * 2 as published by the Free Software Foundation.
  16 *
  17 * This program is distributed in the hope that it will be useful,
  18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20 * GNU General Public License for more details.
  21 *
  22 * You should have received a copy of the GNU General Public License
  23 * along with this program; if not, write to the Free Software
  24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  25 */
  26
  27#include <linux/kernel.h>
  28#include <linux/module.h>
  29#include <linux/init.h>
  30#include <linux/delay.h>
  31#include <linux/io.h>
  32#include <linux/acpi.h>
  33#include <linux/uaccess.h>
  34#include <linux/cper.h>
  35#include <linux/nmi.h>
  36#include <linux/hardirq.h>
  37#include <linux/pstore.h>
 
 
  38#include <acpi/apei.h>
  39
  40#include "apei-internal.h"
  41
  42#define ERST_PFX "ERST: "
 
  43
  44/* ERST command status */
  45#define ERST_STATUS_SUCCESS			0x0
  46#define ERST_STATUS_NOT_ENOUGH_SPACE		0x1
  47#define ERST_STATUS_HARDWARE_NOT_AVAILABLE	0x2
  48#define ERST_STATUS_FAILED			0x3
  49#define ERST_STATUS_RECORD_STORE_EMPTY		0x4
  50#define ERST_STATUS_RECORD_NOT_FOUND		0x5
  51
  52#define ERST_TAB_ENTRY(tab)						\
  53	((struct acpi_whea_header *)((char *)(tab) +			\
  54				     sizeof(struct acpi_table_erst)))
  55
  56#define SPIN_UNIT		100			/* 100ns */
  57/* Firmware should respond within 1 milliseconds */
  58#define FIRMWARE_TIMEOUT	(1 * NSEC_PER_MSEC)
  59#define FIRMWARE_MAX_STALL	50			/* 50us */
  60
  61int erst_disable;
  62EXPORT_SYMBOL_GPL(erst_disable);
  63
  64static struct acpi_table_erst *erst_tab;
  65
  66/* ERST Error Log Address Range atrributes */
  67#define ERST_RANGE_RESERVED	0x0001
  68#define ERST_RANGE_NVRAM	0x0002
  69#define ERST_RANGE_SLOW		0x0004
  70
  71/*
  72 * ERST Error Log Address Range, used as buffer for reading/writing
  73 * error records.
  74 */
  75static struct erst_erange {
  76	u64 base;
  77	u64 size;
  78	void __iomem *vaddr;
  79	u32 attr;
  80} erst_erange;
  81
  82/*
  83 * Prevent ERST interpreter to run simultaneously, because the
  84 * corresponding firmware implementation may not work properly when
  85 * invoked simultaneously.
  86 *
  87 * It is used to provide exclusive accessing for ERST Error Log
  88 * Address Range too.
  89 */
  90static DEFINE_RAW_SPINLOCK(erst_lock);
  91
  92static inline int erst_errno(int command_status)
  93{
  94	switch (command_status) {
  95	case ERST_STATUS_SUCCESS:
  96		return 0;
  97	case ERST_STATUS_HARDWARE_NOT_AVAILABLE:
  98		return -ENODEV;
  99	case ERST_STATUS_NOT_ENOUGH_SPACE:
 100		return -ENOSPC;
 101	case ERST_STATUS_RECORD_STORE_EMPTY:
 102	case ERST_STATUS_RECORD_NOT_FOUND:
 103		return -ENOENT;
 104	default:
 105		return -EINVAL;
 106	}
 107}
 108
 109static int erst_timedout(u64 *t, u64 spin_unit)
 110{
 111	if ((s64)*t < spin_unit) {
 112		pr_warning(FW_WARN ERST_PFX
 113			   "Firmware does not respond in time\n");
 114		return 1;
 115	}
 116	*t -= spin_unit;
 117	ndelay(spin_unit);
 118	touch_nmi_watchdog();
 119	return 0;
 120}
 121
 122static int erst_exec_load_var1(struct apei_exec_context *ctx,
 123			       struct acpi_whea_header *entry)
 124{
 125	return __apei_exec_read_register(entry, &ctx->var1);
 126}
 127
 128static int erst_exec_load_var2(struct apei_exec_context *ctx,
 129			       struct acpi_whea_header *entry)
 130{
 131	return __apei_exec_read_register(entry, &ctx->var2);
 132}
 133
 134static int erst_exec_store_var1(struct apei_exec_context *ctx,
 135				struct acpi_whea_header *entry)
 136{
 137	return __apei_exec_write_register(entry, ctx->var1);
 138}
 139
 140static int erst_exec_add(struct apei_exec_context *ctx,
 141			 struct acpi_whea_header *entry)
 142{
 143	ctx->var1 += ctx->var2;
 144	return 0;
 145}
 146
 147static int erst_exec_subtract(struct apei_exec_context *ctx,
 148			      struct acpi_whea_header *entry)
 149{
 150	ctx->var1 -= ctx->var2;
 151	return 0;
 152}
 153
 154static int erst_exec_add_value(struct apei_exec_context *ctx,
 155			       struct acpi_whea_header *entry)
 156{
 157	int rc;
 158	u64 val;
 159
 160	rc = __apei_exec_read_register(entry, &val);
 161	if (rc)
 162		return rc;
 163	val += ctx->value;
 164	rc = __apei_exec_write_register(entry, val);
 165	return rc;
 166}
 167
 168static int erst_exec_subtract_value(struct apei_exec_context *ctx,
 169				    struct acpi_whea_header *entry)
 170{
 171	int rc;
 172	u64 val;
 173
 174	rc = __apei_exec_read_register(entry, &val);
 175	if (rc)
 176		return rc;
 177	val -= ctx->value;
 178	rc = __apei_exec_write_register(entry, val);
 179	return rc;
 180}
 181
 182static int erst_exec_stall(struct apei_exec_context *ctx,
 183			   struct acpi_whea_header *entry)
 184{
 185	u64 stall_time;
 186
 187	if (ctx->value > FIRMWARE_MAX_STALL) {
 188		if (!in_nmi())
 189			pr_warning(FW_WARN ERST_PFX
 190			"Too long stall time for stall instruction: %llx.\n",
 191				   ctx->value);
 192		stall_time = FIRMWARE_MAX_STALL;
 193	} else
 194		stall_time = ctx->value;
 195	udelay(stall_time);
 196	return 0;
 197}
 198
 199static int erst_exec_stall_while_true(struct apei_exec_context *ctx,
 200				      struct acpi_whea_header *entry)
 201{
 202	int rc;
 203	u64 val;
 204	u64 timeout = FIRMWARE_TIMEOUT;
 205	u64 stall_time;
 206
 207	if (ctx->var1 > FIRMWARE_MAX_STALL) {
 208		if (!in_nmi())
 209			pr_warning(FW_WARN ERST_PFX
 210		"Too long stall time for stall while true instruction: %llx.\n",
 211				   ctx->var1);
 212		stall_time = FIRMWARE_MAX_STALL;
 213	} else
 214		stall_time = ctx->var1;
 215
 216	for (;;) {
 217		rc = __apei_exec_read_register(entry, &val);
 218		if (rc)
 219			return rc;
 220		if (val != ctx->value)
 221			break;
 222		if (erst_timedout(&timeout, stall_time * NSEC_PER_USEC))
 223			return -EIO;
 224	}
 225	return 0;
 226}
 227
 228static int erst_exec_skip_next_instruction_if_true(
 229	struct apei_exec_context *ctx,
 230	struct acpi_whea_header *entry)
 231{
 232	int rc;
 233	u64 val;
 234
 235	rc = __apei_exec_read_register(entry, &val);
 236	if (rc)
 237		return rc;
 238	if (val == ctx->value) {
 239		ctx->ip += 2;
 240		return APEI_EXEC_SET_IP;
 241	}
 242
 243	return 0;
 244}
 245
 246static int erst_exec_goto(struct apei_exec_context *ctx,
 247			  struct acpi_whea_header *entry)
 248{
 249	ctx->ip = ctx->value;
 250	return APEI_EXEC_SET_IP;
 251}
 252
 253static int erst_exec_set_src_address_base(struct apei_exec_context *ctx,
 254					  struct acpi_whea_header *entry)
 255{
 256	return __apei_exec_read_register(entry, &ctx->src_base);
 257}
 258
 259static int erst_exec_set_dst_address_base(struct apei_exec_context *ctx,
 260					  struct acpi_whea_header *entry)
 261{
 262	return __apei_exec_read_register(entry, &ctx->dst_base);
 263}
 264
 265static int erst_exec_move_data(struct apei_exec_context *ctx,
 266			       struct acpi_whea_header *entry)
 267{
 268	int rc;
 269	u64 offset;
 270	void *src, *dst;
 271
 272	/* ioremap does not work in interrupt context */
 273	if (in_interrupt()) {
 274		pr_warning(ERST_PFX
 275			   "MOVE_DATA can not be used in interrupt context");
 276		return -EBUSY;
 277	}
 278
 279	rc = __apei_exec_read_register(entry, &offset);
 280	if (rc)
 281		return rc;
 282
 283	src = ioremap(ctx->src_base + offset, ctx->var2);
 284	if (!src)
 285		return -ENOMEM;
 286	dst = ioremap(ctx->dst_base + offset, ctx->var2);
 287	if (!dst)
 
 288		return -ENOMEM;
 
 289
 290	memmove(dst, src, ctx->var2);
 291
 292	iounmap(src);
 293	iounmap(dst);
 294
 295	return 0;
 296}
 297
 298static struct apei_exec_ins_type erst_ins_type[] = {
 299	[ACPI_ERST_READ_REGISTER] = {
 300		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 301		.run = apei_exec_read_register,
 302	},
 303	[ACPI_ERST_READ_REGISTER_VALUE] = {
 304		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 305		.run = apei_exec_read_register_value,
 306	},
 307	[ACPI_ERST_WRITE_REGISTER] = {
 308		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 309		.run = apei_exec_write_register,
 310	},
 311	[ACPI_ERST_WRITE_REGISTER_VALUE] = {
 312		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 313		.run = apei_exec_write_register_value,
 314	},
 315	[ACPI_ERST_NOOP] = {
 316		.flags = 0,
 317		.run = apei_exec_noop,
 318	},
 319	[ACPI_ERST_LOAD_VAR1] = {
 320		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 321		.run = erst_exec_load_var1,
 322	},
 323	[ACPI_ERST_LOAD_VAR2] = {
 324		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 325		.run = erst_exec_load_var2,
 326	},
 327	[ACPI_ERST_STORE_VAR1] = {
 328		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 329		.run = erst_exec_store_var1,
 330	},
 331	[ACPI_ERST_ADD] = {
 332		.flags = 0,
 333		.run = erst_exec_add,
 334	},
 335	[ACPI_ERST_SUBTRACT] = {
 336		.flags = 0,
 337		.run = erst_exec_subtract,
 338	},
 339	[ACPI_ERST_ADD_VALUE] = {
 340		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 341		.run = erst_exec_add_value,
 342	},
 343	[ACPI_ERST_SUBTRACT_VALUE] = {
 344		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 345		.run = erst_exec_subtract_value,
 346	},
 347	[ACPI_ERST_STALL] = {
 348		.flags = 0,
 349		.run = erst_exec_stall,
 350	},
 351	[ACPI_ERST_STALL_WHILE_TRUE] = {
 352		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 353		.run = erst_exec_stall_while_true,
 354	},
 355	[ACPI_ERST_SKIP_NEXT_IF_TRUE] = {
 356		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 357		.run = erst_exec_skip_next_instruction_if_true,
 358	},
 359	[ACPI_ERST_GOTO] = {
 360		.flags = 0,
 361		.run = erst_exec_goto,
 362	},
 363	[ACPI_ERST_SET_SRC_ADDRESS_BASE] = {
 364		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 365		.run = erst_exec_set_src_address_base,
 366	},
 367	[ACPI_ERST_SET_DST_ADDRESS_BASE] = {
 368		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 369		.run = erst_exec_set_dst_address_base,
 370	},
 371	[ACPI_ERST_MOVE_DATA] = {
 372		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
 373		.run = erst_exec_move_data,
 374	},
 375};
 376
 377static inline void erst_exec_ctx_init(struct apei_exec_context *ctx)
 378{
 379	apei_exec_ctx_init(ctx, erst_ins_type, ARRAY_SIZE(erst_ins_type),
 380			   ERST_TAB_ENTRY(erst_tab), erst_tab->entries);
 381}
 382
 383static int erst_get_erange(struct erst_erange *range)
 384{
 385	struct apei_exec_context ctx;
 386	int rc;
 387
 388	erst_exec_ctx_init(&ctx);
 389	rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_RANGE);
 390	if (rc)
 391		return rc;
 392	range->base = apei_exec_ctx_get_output(&ctx);
 393	rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_LENGTH);
 394	if (rc)
 395		return rc;
 396	range->size = apei_exec_ctx_get_output(&ctx);
 397	rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_ATTRIBUTES);
 398	if (rc)
 399		return rc;
 400	range->attr = apei_exec_ctx_get_output(&ctx);
 401
 402	return 0;
 403}
 404
 405static ssize_t __erst_get_record_count(void)
 406{
 407	struct apei_exec_context ctx;
 408	int rc;
 409
 410	erst_exec_ctx_init(&ctx);
 411	rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_COUNT);
 412	if (rc)
 413		return rc;
 414	return apei_exec_ctx_get_output(&ctx);
 415}
 416
 417ssize_t erst_get_record_count(void)
 418{
 419	ssize_t count;
 420	unsigned long flags;
 421
 422	if (erst_disable)
 423		return -ENODEV;
 424
 425	raw_spin_lock_irqsave(&erst_lock, flags);
 426	count = __erst_get_record_count();
 427	raw_spin_unlock_irqrestore(&erst_lock, flags);
 428
 429	return count;
 430}
 431EXPORT_SYMBOL_GPL(erst_get_record_count);
 432
 433#define ERST_RECORD_ID_CACHE_SIZE_MIN	16
 434#define ERST_RECORD_ID_CACHE_SIZE_MAX	1024
 435
 436struct erst_record_id_cache {
 437	struct mutex lock;
 438	u64 *entries;
 439	int len;
 440	int size;
 441	int refcount;
 442};
 443
 444static struct erst_record_id_cache erst_record_id_cache = {
 445	.lock = __MUTEX_INITIALIZER(erst_record_id_cache.lock),
 446	.refcount = 0,
 447};
 448
 449static int __erst_get_next_record_id(u64 *record_id)
 450{
 451	struct apei_exec_context ctx;
 452	int rc;
 453
 454	erst_exec_ctx_init(&ctx);
 455	rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_ID);
 456	if (rc)
 457		return rc;
 458	*record_id = apei_exec_ctx_get_output(&ctx);
 459
 460	return 0;
 461}
 462
 463int erst_get_record_id_begin(int *pos)
 464{
 465	int rc;
 466
 467	if (erst_disable)
 468		return -ENODEV;
 469
 470	rc = mutex_lock_interruptible(&erst_record_id_cache.lock);
 471	if (rc)
 472		return rc;
 473	erst_record_id_cache.refcount++;
 474	mutex_unlock(&erst_record_id_cache.lock);
 475
 476	*pos = 0;
 477
 478	return 0;
 479}
 480EXPORT_SYMBOL_GPL(erst_get_record_id_begin);
 481
 482/* erst_record_id_cache.lock must be held by caller */
 483static int __erst_record_id_cache_add_one(void)
 484{
 485	u64 id, prev_id, first_id;
 486	int i, rc;
 487	u64 *entries;
 488	unsigned long flags;
 489
 490	id = prev_id = first_id = APEI_ERST_INVALID_RECORD_ID;
 491retry:
 492	raw_spin_lock_irqsave(&erst_lock, flags);
 493	rc = __erst_get_next_record_id(&id);
 494	raw_spin_unlock_irqrestore(&erst_lock, flags);
 495	if (rc == -ENOENT)
 496		return 0;
 497	if (rc)
 498		return rc;
 499	if (id == APEI_ERST_INVALID_RECORD_ID)
 500		return 0;
 501	/* can not skip current ID, or loop back to first ID */
 502	if (id == prev_id || id == first_id)
 503		return 0;
 504	if (first_id == APEI_ERST_INVALID_RECORD_ID)
 505		first_id = id;
 506	prev_id = id;
 507
 508	entries = erst_record_id_cache.entries;
 509	for (i = 0; i < erst_record_id_cache.len; i++) {
 510		if (entries[i] == id)
 511			break;
 512	}
 513	/* record id already in cache, try next */
 514	if (i < erst_record_id_cache.len)
 515		goto retry;
 516	if (erst_record_id_cache.len >= erst_record_id_cache.size) {
 517		int new_size, alloc_size;
 518		u64 *new_entries;
 519
 520		new_size = erst_record_id_cache.size * 2;
 521		new_size = clamp_val(new_size, ERST_RECORD_ID_CACHE_SIZE_MIN,
 522				     ERST_RECORD_ID_CACHE_SIZE_MAX);
 523		if (new_size <= erst_record_id_cache.size) {
 524			if (printk_ratelimit())
 525				pr_warning(FW_WARN ERST_PFX
 526					   "too many record ID!\n");
 527			return 0;
 528		}
 529		alloc_size = new_size * sizeof(entries[0]);
 530		if (alloc_size < PAGE_SIZE)
 531			new_entries = kmalloc(alloc_size, GFP_KERNEL);
 532		else
 533			new_entries = vmalloc(alloc_size);
 534		if (!new_entries)
 535			return -ENOMEM;
 536		memcpy(new_entries, entries,
 537		       erst_record_id_cache.len * sizeof(entries[0]));
 538		if (erst_record_id_cache.size < PAGE_SIZE)
 539			kfree(entries);
 540		else
 541			vfree(entries);
 542		erst_record_id_cache.entries = entries = new_entries;
 543		erst_record_id_cache.size = new_size;
 544	}
 545	entries[i] = id;
 546	erst_record_id_cache.len++;
 547
 548	return 1;
 549}
 550
 551/*
 552 * Get the record ID of an existing error record on the persistent
 553 * storage. If there is no error record on the persistent storage, the
 554 * returned record_id is APEI_ERST_INVALID_RECORD_ID.
 555 */
 556int erst_get_record_id_next(int *pos, u64 *record_id)
 557{
 558	int rc = 0;
 559	u64 *entries;
 560
 561	if (erst_disable)
 562		return -ENODEV;
 563
 564	/* must be enclosed by erst_get_record_id_begin/end */
 565	BUG_ON(!erst_record_id_cache.refcount);
 566	BUG_ON(*pos < 0 || *pos > erst_record_id_cache.len);
 567
 568	mutex_lock(&erst_record_id_cache.lock);
 569	entries = erst_record_id_cache.entries;
 570	for (; *pos < erst_record_id_cache.len; (*pos)++)
 571		if (entries[*pos] != APEI_ERST_INVALID_RECORD_ID)
 572			break;
 573	/* found next record id in cache */
 574	if (*pos < erst_record_id_cache.len) {
 575		*record_id = entries[*pos];
 576		(*pos)++;
 577		goto out_unlock;
 578	}
 579
 580	/* Try to add one more record ID to cache */
 581	rc = __erst_record_id_cache_add_one();
 582	if (rc < 0)
 583		goto out_unlock;
 584	/* successfully add one new ID */
 585	if (rc == 1) {
 586		*record_id = erst_record_id_cache.entries[*pos];
 587		(*pos)++;
 588		rc = 0;
 589	} else {
 590		*pos = -1;
 591		*record_id = APEI_ERST_INVALID_RECORD_ID;
 592	}
 593out_unlock:
 594	mutex_unlock(&erst_record_id_cache.lock);
 595
 596	return rc;
 597}
 598EXPORT_SYMBOL_GPL(erst_get_record_id_next);
 599
 600/* erst_record_id_cache.lock must be held by caller */
 601static void __erst_record_id_cache_compact(void)
 602{
 603	int i, wpos = 0;
 604	u64 *entries;
 605
 606	if (erst_record_id_cache.refcount)
 607		return;
 608
 609	entries = erst_record_id_cache.entries;
 610	for (i = 0; i < erst_record_id_cache.len; i++) {
 611		if (entries[i] == APEI_ERST_INVALID_RECORD_ID)
 612			continue;
 613		if (wpos != i)
 614			memcpy(&entries[wpos], &entries[i], sizeof(entries[i]));
 615		wpos++;
 616	}
 617	erst_record_id_cache.len = wpos;
 618}
 619
 620void erst_get_record_id_end(void)
 621{
 622	/*
 623	 * erst_disable != 0 should be detected by invoker via the
 624	 * return value of erst_get_record_id_begin/next, so this
 625	 * function should not be called for erst_disable != 0.
 626	 */
 627	BUG_ON(erst_disable);
 628
 629	mutex_lock(&erst_record_id_cache.lock);
 630	erst_record_id_cache.refcount--;
 631	BUG_ON(erst_record_id_cache.refcount < 0);
 632	__erst_record_id_cache_compact();
 633	mutex_unlock(&erst_record_id_cache.lock);
 634}
 635EXPORT_SYMBOL_GPL(erst_get_record_id_end);
 636
 637static int __erst_write_to_storage(u64 offset)
 638{
 639	struct apei_exec_context ctx;
 640	u64 timeout = FIRMWARE_TIMEOUT;
 641	u64 val;
 642	int rc;
 643
 644	erst_exec_ctx_init(&ctx);
 645	rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_WRITE);
 646	if (rc)
 647		return rc;
 648	apei_exec_ctx_set_input(&ctx, offset);
 649	rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
 650	if (rc)
 651		return rc;
 652	rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
 653	if (rc)
 654		return rc;
 655	for (;;) {
 656		rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
 657		if (rc)
 658			return rc;
 659		val = apei_exec_ctx_get_output(&ctx);
 660		if (!val)
 661			break;
 662		if (erst_timedout(&timeout, SPIN_UNIT))
 663			return -EIO;
 664	}
 665	rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
 666	if (rc)
 667		return rc;
 668	val = apei_exec_ctx_get_output(&ctx);
 669	rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
 670	if (rc)
 671		return rc;
 672
 673	return erst_errno(val);
 674}
 675
 676static int __erst_read_from_storage(u64 record_id, u64 offset)
 677{
 678	struct apei_exec_context ctx;
 679	u64 timeout = FIRMWARE_TIMEOUT;
 680	u64 val;
 681	int rc;
 682
 683	erst_exec_ctx_init(&ctx);
 684	rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_READ);
 685	if (rc)
 686		return rc;
 687	apei_exec_ctx_set_input(&ctx, offset);
 688	rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
 689	if (rc)
 690		return rc;
 691	apei_exec_ctx_set_input(&ctx, record_id);
 692	rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
 693	if (rc)
 694		return rc;
 695	rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
 696	if (rc)
 697		return rc;
 698	for (;;) {
 699		rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
 700		if (rc)
 701			return rc;
 702		val = apei_exec_ctx_get_output(&ctx);
 703		if (!val)
 704			break;
 705		if (erst_timedout(&timeout, SPIN_UNIT))
 706			return -EIO;
 707	};
 708	rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
 709	if (rc)
 710		return rc;
 711	val = apei_exec_ctx_get_output(&ctx);
 712	rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
 713	if (rc)
 714		return rc;
 715
 716	return erst_errno(val);
 717}
 718
 719static int __erst_clear_from_storage(u64 record_id)
 720{
 721	struct apei_exec_context ctx;
 722	u64 timeout = FIRMWARE_TIMEOUT;
 723	u64 val;
 724	int rc;
 725
 726	erst_exec_ctx_init(&ctx);
 727	rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_CLEAR);
 728	if (rc)
 729		return rc;
 730	apei_exec_ctx_set_input(&ctx, record_id);
 731	rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
 732	if (rc)
 733		return rc;
 734	rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
 735	if (rc)
 736		return rc;
 737	for (;;) {
 738		rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
 739		if (rc)
 740			return rc;
 741		val = apei_exec_ctx_get_output(&ctx);
 742		if (!val)
 743			break;
 744		if (erst_timedout(&timeout, SPIN_UNIT))
 745			return -EIO;
 746	}
 747	rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
 748	if (rc)
 749		return rc;
 750	val = apei_exec_ctx_get_output(&ctx);
 751	rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
 752	if (rc)
 753		return rc;
 754
 755	return erst_errno(val);
 756}
 757
 758/* NVRAM ERST Error Log Address Range is not supported yet */
 759static void pr_unimpl_nvram(void)
 760{
 761	if (printk_ratelimit())
 762		pr_warning(ERST_PFX
 763		"NVRAM ERST Log Address Range is not implemented yet\n");
 764}
 765
 766static int __erst_write_to_nvram(const struct cper_record_header *record)
 767{
 768	/* do not print message, because printk is not safe for NMI */
 769	return -ENOSYS;
 770}
 771
 772static int __erst_read_to_erange_from_nvram(u64 record_id, u64 *offset)
 773{
 774	pr_unimpl_nvram();
 775	return -ENOSYS;
 776}
 777
 778static int __erst_clear_from_nvram(u64 record_id)
 779{
 780	pr_unimpl_nvram();
 781	return -ENOSYS;
 782}
 783
 784int erst_write(const struct cper_record_header *record)
 785{
 786	int rc;
 787	unsigned long flags;
 788	struct cper_record_header *rcd_erange;
 789
 790	if (erst_disable)
 791		return -ENODEV;
 792
 793	if (memcmp(record->signature, CPER_SIG_RECORD, CPER_SIG_SIZE))
 794		return -EINVAL;
 795
 796	if (erst_erange.attr & ERST_RANGE_NVRAM) {
 797		if (!raw_spin_trylock_irqsave(&erst_lock, flags))
 798			return -EBUSY;
 799		rc = __erst_write_to_nvram(record);
 800		raw_spin_unlock_irqrestore(&erst_lock, flags);
 801		return rc;
 802	}
 803
 804	if (record->record_length > erst_erange.size)
 805		return -EINVAL;
 806
 807	if (!raw_spin_trylock_irqsave(&erst_lock, flags))
 808		return -EBUSY;
 809	memcpy(erst_erange.vaddr, record, record->record_length);
 810	rcd_erange = erst_erange.vaddr;
 811	/* signature for serialization system */
 812	memcpy(&rcd_erange->persistence_information, "ER", 2);
 813
 814	rc = __erst_write_to_storage(0);
 815	raw_spin_unlock_irqrestore(&erst_lock, flags);
 816
 817	return rc;
 818}
 819EXPORT_SYMBOL_GPL(erst_write);
 820
 821static int __erst_read_to_erange(u64 record_id, u64 *offset)
 822{
 823	int rc;
 824
 825	if (erst_erange.attr & ERST_RANGE_NVRAM)
 826		return __erst_read_to_erange_from_nvram(
 827			record_id, offset);
 828
 829	rc = __erst_read_from_storage(record_id, 0);
 830	if (rc)
 831		return rc;
 832	*offset = 0;
 833
 834	return 0;
 835}
 836
 837static ssize_t __erst_read(u64 record_id, struct cper_record_header *record,
 838			   size_t buflen)
 839{
 840	int rc;
 841	u64 offset, len = 0;
 842	struct cper_record_header *rcd_tmp;
 843
 844	rc = __erst_read_to_erange(record_id, &offset);
 845	if (rc)
 846		return rc;
 847	rcd_tmp = erst_erange.vaddr + offset;
 848	len = rcd_tmp->record_length;
 849	if (len <= buflen)
 850		memcpy(record, rcd_tmp, len);
 851
 852	return len;
 853}
 854
 855/*
 856 * If return value > buflen, the buffer size is not big enough,
 857 * else if return value < 0, something goes wrong,
 858 * else everything is OK, and return value is record length
 859 */
 860ssize_t erst_read(u64 record_id, struct cper_record_header *record,
 861		  size_t buflen)
 862{
 863	ssize_t len;
 864	unsigned long flags;
 865
 866	if (erst_disable)
 867		return -ENODEV;
 868
 869	raw_spin_lock_irqsave(&erst_lock, flags);
 870	len = __erst_read(record_id, record, buflen);
 871	raw_spin_unlock_irqrestore(&erst_lock, flags);
 872	return len;
 873}
 874EXPORT_SYMBOL_GPL(erst_read);
 875
 876int erst_clear(u64 record_id)
 877{
 878	int rc, i;
 879	unsigned long flags;
 880	u64 *entries;
 881
 882	if (erst_disable)
 883		return -ENODEV;
 884
 885	rc = mutex_lock_interruptible(&erst_record_id_cache.lock);
 886	if (rc)
 887		return rc;
 888	raw_spin_lock_irqsave(&erst_lock, flags);
 889	if (erst_erange.attr & ERST_RANGE_NVRAM)
 890		rc = __erst_clear_from_nvram(record_id);
 891	else
 892		rc = __erst_clear_from_storage(record_id);
 893	raw_spin_unlock_irqrestore(&erst_lock, flags);
 894	if (rc)
 895		goto out;
 896	entries = erst_record_id_cache.entries;
 897	for (i = 0; i < erst_record_id_cache.len; i++) {
 898		if (entries[i] == record_id)
 899			entries[i] = APEI_ERST_INVALID_RECORD_ID;
 900	}
 901	__erst_record_id_cache_compact();
 902out:
 903	mutex_unlock(&erst_record_id_cache.lock);
 904	return rc;
 905}
 906EXPORT_SYMBOL_GPL(erst_clear);
 907
 908static int __init setup_erst_disable(char *str)
 909{
 910	erst_disable = 1;
 911	return 0;
 912}
 913
 914__setup("erst_disable", setup_erst_disable);
 915
 916static int erst_check_table(struct acpi_table_erst *erst_tab)
 917{
 918	if ((erst_tab->header_length !=
 919	     (sizeof(struct acpi_table_erst) - sizeof(erst_tab->header)))
 920	    && (erst_tab->header_length != sizeof(struct acpi_table_erst)))
 921		return -EINVAL;
 922	if (erst_tab->header.length < sizeof(struct acpi_table_erst))
 923		return -EINVAL;
 924	if (erst_tab->entries !=
 925	    (erst_tab->header.length - sizeof(struct acpi_table_erst)) /
 926	    sizeof(struct acpi_erst_entry))
 927		return -EINVAL;
 928
 929	return 0;
 930}
 931
 932static int erst_open_pstore(struct pstore_info *psi);
 933static int erst_close_pstore(struct pstore_info *psi);
 934static ssize_t erst_reader(u64 *id, enum pstore_type_id *type,
 935			   struct timespec *time, char **buf,
 936			   struct pstore_info *psi);
 937static int erst_writer(enum pstore_type_id type, enum kmsg_dump_reason reason,
 938		       u64 *id, unsigned int part,
 939		       size_t size, struct pstore_info *psi);
 940static int erst_clearer(enum pstore_type_id type, u64 id,
 941			struct pstore_info *psi);
 942
 943static struct pstore_info erst_info = {
 944	.owner		= THIS_MODULE,
 945	.name		= "erst",
 
 946	.open		= erst_open_pstore,
 947	.close		= erst_close_pstore,
 948	.read		= erst_reader,
 949	.write		= erst_writer,
 950	.erase		= erst_clearer
 951};
 952
 953#define CPER_CREATOR_PSTORE						\
 954	UUID_LE(0x75a574e3, 0x5052, 0x4b29, 0x8a, 0x8e, 0xbe, 0x2c,	\
 955		0x64, 0x90, 0xb8, 0x9d)
 956#define CPER_SECTION_TYPE_DMESG						\
 957	UUID_LE(0xc197e04e, 0xd545, 0x4a70, 0x9c, 0x17, 0xa5, 0x54,	\
 958		0x94, 0x19, 0xeb, 0x12)
 
 
 
 959#define CPER_SECTION_TYPE_MCE						\
 960	UUID_LE(0xfe08ffbe, 0x95e4, 0x4be7, 0xbc, 0x73, 0x40, 0x96,	\
 961		0x04, 0x4a, 0x38, 0xfc)
 962
 963struct cper_pstore_record {
 964	struct cper_record_header hdr;
 965	struct cper_section_descriptor sec_hdr;
 966	char data[];
 967} __packed;
 968
 969static int reader_pos;
 970
 971static int erst_open_pstore(struct pstore_info *psi)
 972{
 973	int rc;
 974
 975	if (erst_disable)
 976		return -ENODEV;
 977
 978	rc = erst_get_record_id_begin(&reader_pos);
 979
 980	return rc;
 981}
 982
 983static int erst_close_pstore(struct pstore_info *psi)
 984{
 985	erst_get_record_id_end();
 986
 987	return 0;
 988}
 989
 990static ssize_t erst_reader(u64 *id, enum pstore_type_id *type,
 991			   struct timespec *time, char **buf,
 992			   struct pstore_info *psi)
 993{
 994	int rc;
 995	ssize_t len = 0;
 996	u64 record_id;
 997	struct cper_pstore_record *rcd;
 998	size_t rcd_len = sizeof(*rcd) + erst_info.bufsize;
 999
1000	if (erst_disable)
1001		return -ENODEV;
1002
1003	rcd = kmalloc(rcd_len, GFP_KERNEL);
1004	if (!rcd) {
1005		rc = -ENOMEM;
1006		goto out;
1007	}
1008skip:
1009	rc = erst_get_record_id_next(&reader_pos, &record_id);
1010	if (rc)
1011		goto out;
1012
1013	/* no more record */
1014	if (record_id == APEI_ERST_INVALID_RECORD_ID) {
1015		rc = -EINVAL;
1016		goto out;
1017	}
1018
1019	len = erst_read(record_id, &rcd->hdr, rcd_len);
1020	/* The record may be cleared by others, try read next record */
1021	if (len == -ENOENT)
1022		goto skip;
1023	else if (len < sizeof(*rcd)) {
1024		rc = -EIO;
1025		goto out;
1026	}
1027	if (uuid_le_cmp(rcd->hdr.creator_id, CPER_CREATOR_PSTORE) != 0)
1028		goto skip;
1029
1030	*buf = kmalloc(len, GFP_KERNEL);
1031	if (*buf == NULL) {
1032		rc = -ENOMEM;
1033		goto out;
1034	}
1035	memcpy(*buf, rcd->data, len - sizeof(*rcd));
1036	*id = record_id;
 
1037	if (uuid_le_cmp(rcd->sec_hdr.section_type,
 
 
 
 
1038			CPER_SECTION_TYPE_DMESG) == 0)
1039		*type = PSTORE_TYPE_DMESG;
1040	else if (uuid_le_cmp(rcd->sec_hdr.section_type,
1041			     CPER_SECTION_TYPE_MCE) == 0)
1042		*type = PSTORE_TYPE_MCE;
1043	else
1044		*type = PSTORE_TYPE_UNKNOWN;
1045
1046	if (rcd->hdr.validation_bits & CPER_VALID_TIMESTAMP)
1047		time->tv_sec = rcd->hdr.timestamp;
1048	else
1049		time->tv_sec = 0;
1050	time->tv_nsec = 0;
1051
1052out:
1053	kfree(rcd);
1054	return (rc < 0) ? rc : (len - sizeof(*rcd));
1055}
1056
1057static int erst_writer(enum pstore_type_id type, enum kmsg_dump_reason reason,
1058		       u64 *id, unsigned int part,
1059		       size_t size, struct pstore_info *psi)
1060{
1061	struct cper_pstore_record *rcd = (struct cper_pstore_record *)
1062					(erst_info.buf - sizeof(*rcd));
1063	int ret;
1064
1065	memset(rcd, 0, sizeof(*rcd));
1066	memcpy(rcd->hdr.signature, CPER_SIG_RECORD, CPER_SIG_SIZE);
1067	rcd->hdr.revision = CPER_RECORD_REV;
1068	rcd->hdr.signature_end = CPER_SIG_END;
1069	rcd->hdr.section_count = 1;
1070	rcd->hdr.error_severity = CPER_SEV_FATAL;
1071	/* timestamp valid. platform_id, partition_id are invalid */
1072	rcd->hdr.validation_bits = CPER_VALID_TIMESTAMP;
1073	rcd->hdr.timestamp = get_seconds();
1074	rcd->hdr.record_length = sizeof(*rcd) + size;
1075	rcd->hdr.creator_id = CPER_CREATOR_PSTORE;
1076	rcd->hdr.notification_type = CPER_NOTIFY_MCE;
1077	rcd->hdr.record_id = cper_next_record_id();
1078	rcd->hdr.flags = CPER_HW_ERROR_FLAGS_PREVERR;
1079
1080	rcd->sec_hdr.section_offset = sizeof(*rcd);
1081	rcd->sec_hdr.section_length = size;
1082	rcd->sec_hdr.revision = CPER_SEC_REV;
1083	/* fru_id and fru_text is invalid */
1084	rcd->sec_hdr.validation_bits = 0;
1085	rcd->sec_hdr.flags = CPER_SEC_PRIMARY;
1086	switch (type) {
1087	case PSTORE_TYPE_DMESG:
1088		rcd->sec_hdr.section_type = CPER_SECTION_TYPE_DMESG;
 
 
 
1089		break;
1090	case PSTORE_TYPE_MCE:
1091		rcd->sec_hdr.section_type = CPER_SECTION_TYPE_MCE;
1092		break;
1093	default:
1094		return -EINVAL;
1095	}
1096	rcd->sec_hdr.section_severity = CPER_SEV_FATAL;
1097
1098	ret = erst_write(&rcd->hdr);
1099	*id = rcd->hdr.record_id;
1100
1101	return ret;
1102}
1103
1104static int erst_clearer(enum pstore_type_id type, u64 id,
1105			struct pstore_info *psi)
1106{
1107	return erst_clear(id);
1108}
1109
1110static int __init erst_init(void)
1111{
1112	int rc = 0;
1113	acpi_status status;
1114	struct apei_exec_context ctx;
1115	struct apei_resources erst_resources;
1116	struct resource *r;
1117	char *buf;
1118
1119	if (acpi_disabled)
1120		goto err;
1121
1122	if (erst_disable) {
1123		pr_info(ERST_PFX
1124	"Error Record Serialization Table (ERST) support is disabled.\n");
1125		goto err;
1126	}
1127
1128	status = acpi_get_table(ACPI_SIG_ERST, 0,
1129				(struct acpi_table_header **)&erst_tab);
1130	if (status == AE_NOT_FOUND)
1131		goto err;
1132	else if (ACPI_FAILURE(status)) {
1133		const char *msg = acpi_format_exception(status);
1134		pr_err(ERST_PFX "Failed to get table, %s\n", msg);
1135		rc = -EINVAL;
1136		goto err;
1137	}
1138
1139	rc = erst_check_table(erst_tab);
1140	if (rc) {
1141		pr_err(FW_BUG ERST_PFX "ERST table is invalid\n");
1142		goto err;
1143	}
1144
1145	apei_resources_init(&erst_resources);
1146	erst_exec_ctx_init(&ctx);
1147	rc = apei_exec_collect_resources(&ctx, &erst_resources);
1148	if (rc)
1149		goto err_fini;
1150	rc = apei_resources_request(&erst_resources, "APEI ERST");
1151	if (rc)
1152		goto err_fini;
1153	rc = apei_exec_pre_map_gars(&ctx);
1154	if (rc)
1155		goto err_release;
1156	rc = erst_get_erange(&erst_erange);
1157	if (rc) {
1158		if (rc == -ENODEV)
1159			pr_info(ERST_PFX
1160	"The corresponding hardware device or firmware implementation "
1161	"is not available.\n");
1162		else
1163			pr_err(ERST_PFX
1164			       "Failed to get Error Log Address Range.\n");
1165		goto err_unmap_reg;
1166	}
1167
1168	r = request_mem_region(erst_erange.base, erst_erange.size, "APEI ERST");
1169	if (!r) {
1170		pr_err(ERST_PFX
1171		"Can not request iomem region <0x%16llx-0x%16llx> for ERST.\n",
1172		(unsigned long long)erst_erange.base,
1173		(unsigned long long)erst_erange.base + erst_erange.size);
1174		rc = -EIO;
1175		goto err_unmap_reg;
1176	}
1177	rc = -ENOMEM;
1178	erst_erange.vaddr = ioremap_cache(erst_erange.base,
1179					  erst_erange.size);
1180	if (!erst_erange.vaddr)
1181		goto err_release_erange;
1182
 
 
 
1183	buf = kmalloc(erst_erange.size, GFP_KERNEL);
1184	spin_lock_init(&erst_info.buf_lock);
1185	if (buf) {
1186		erst_info.buf = buf + sizeof(struct cper_pstore_record);
1187		erst_info.bufsize = erst_erange.size -
1188				    sizeof(struct cper_pstore_record);
1189		if (pstore_register(&erst_info)) {
1190			pr_info(ERST_PFX "Could not register with persistent store\n");
 
 
 
 
 
1191			kfree(buf);
1192		}
1193	}
 
 
 
1194
1195	pr_info(ERST_PFX
1196	"Error Record Serialization Table (ERST) support is initialized.\n");
1197
1198	return 0;
1199
1200err_release_erange:
1201	release_mem_region(erst_erange.base, erst_erange.size);
1202err_unmap_reg:
1203	apei_exec_post_unmap_gars(&ctx);
1204err_release:
1205	apei_resources_release(&erst_resources);
1206err_fini:
1207	apei_resources_fini(&erst_resources);
1208err:
1209	erst_disable = 1;
1210	return rc;
1211}
1212
1213device_initcall(erst_init);