mca_drv.c 20.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6
/*
 * File:	mca_drv.c
 * Purpose:	Generic MCA handling layer
 *
 * Copyright (C) 2004 FUJITSU LIMITED
 * Copyright (C) Hidetoshi Seto (seto.hidetoshi@jp.fujitsu.com)
7 8
 * Copyright (C) 2005 Silicon Graphics, Inc
 * Copyright (C) 2005 Keith Owens <kaos@sgi.com>
9
 * Copyright (C) 2006 Russ Anderson <rja@sgi.com>
L
Linus Torvalds 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
 */
#include <linux/types.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/kallsyms.h>
#include <linux/smp_lock.h>
#include <linux/bootmem.h>
#include <linux/acpi.h>
#include <linux/timer.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/smp.h>
#include <linux/workqueue.h>
#include <linux/mm.h>

#include <asm/delay.h>
#include <asm/machvec.h>
#include <asm/page.h>
#include <asm/ptrace.h>
#include <asm/system.h>
#include <asm/sal.h>
#include <asm/mca.h>

#include <asm/irq.h>
#include <asm/hw_irq.h>

#include "mca_drv.h"

/* max size of SAL error record (default) */
static int sal_rec_max = 10000;

/* from mca_drv_asm.S */
extern void *mca_handler_bhhook(void);

static DEFINE_SPINLOCK(mca_bh_lock);

typedef enum {
	MCA_IS_LOCAL  = 0,
	MCA_IS_GLOBAL = 1
} mca_type_t;

#define MAX_PAGE_ISOLATE 1024

static struct page *page_isolate[MAX_PAGE_ISOLATE];
static int num_page_isolate = 0;

typedef enum {
59 60 61
	ISOLATE_NG,
	ISOLATE_OK,
	ISOLATE_NONE
L
Linus Torvalds 已提交
62 63
} isolate_status_t;

64 65 66 67 68
typedef enum {
	MCA_NOT_RECOVERED = 0,
	MCA_RECOVERED	  = 1
} recovery_status_t;

L
Linus Torvalds 已提交
69 70 71 72 73 74 75 76 77
/*
 *  This pool keeps pointers to the section part of SAL error record
 */
static struct {
	slidx_list_t *buffer; /* section pointer list pool */
	int	     cur_idx; /* Current index of section pointer list pool */
	int	     max_idx; /* Maximum index of section pointer list pool */
} slidx_pool;

78 79 80 81
static int
fatal_mca(const char *fmt, ...)
{
	va_list args;
82
	char buf[256];
83 84

	va_start(args, fmt);
85
	vsnprintf(buf, sizeof(buf), fmt, args);
86
	va_end(args);
87
	ia64_mca_printk(KERN_ALERT "MCA: %s\n", buf);
88 89 90 91

	return MCA_NOT_RECOVERED;
}

92 93 94 95 96 97 98 99 100 101 102 103 104 105
static int
mca_recovered(const char *fmt, ...)
{
	va_list args;
	char buf[256];

	va_start(args, fmt);
	vsnprintf(buf, sizeof(buf), fmt, args);
	va_end(args);
	ia64_mca_printk(KERN_INFO "MCA: %s\n", buf);

	return MCA_RECOVERED;
}

L
Linus Torvalds 已提交
106 107 108 109 110
/**
 * mca_page_isolate - isolate a poisoned page in order not to use it later
 * @paddr:	poisoned memory location
 *
 * Return value:
111
 *	one of isolate_status_t, ISOLATE_OK/NG/NONE.
L
Linus Torvalds 已提交
112 113 114 115 116 117 118 119 120
 */

static isolate_status_t
mca_page_isolate(unsigned long paddr)
{
	int i;
	struct page *p;

	/* whether physical address is valid or not */
H
Hidetoshi Seto 已提交
121
	if (!ia64_phys_addr_valid(paddr))
122 123
		return ISOLATE_NONE;

124
	if (!pfn_valid(paddr >> PAGE_SHIFT))
125
		return ISOLATE_NONE;
L
Linus Torvalds 已提交
126 127 128 129 130

	/* convert physical address to physical page number */
	p = pfn_to_page(paddr>>PAGE_SHIFT);

	/* check whether a page number have been already registered or not */
H
Hidetoshi Seto 已提交
131 132
	for (i = 0; i < num_page_isolate; i++)
		if (page_isolate[i] == p)
L
Linus Torvalds 已提交
133 134 135
			return ISOLATE_OK; /* already listed */

	/* limitation check */
H
Hidetoshi Seto 已提交
136
	if (num_page_isolate == MAX_PAGE_ISOLATE)
L
Linus Torvalds 已提交
137 138 139
		return ISOLATE_NG;

	/* kick pages having attribute 'SLAB' or 'Reserved' */
H
Hidetoshi Seto 已提交
140
	if (PageSlab(p) || PageReserved(p))
L
Linus Torvalds 已提交
141 142 143
		return ISOLATE_NG;

	/* add attribute 'Reserved' and register the page */
144
	get_page(p);
L
Linus Torvalds 已提交
145 146 147 148 149 150 151 152 153 154 155 156
	SetPageReserved(p);
	page_isolate[num_page_isolate++] = p;

	return ISOLATE_OK;
}

/**
 * mca_hanlder_bh - Kill the process which occurred memory read error
 * @paddr:	poisoned address received from MCA Handler
 */

void
157
mca_handler_bh(unsigned long paddr, void *iip, unsigned long ipsr)
L
Linus Torvalds 已提交
158
{
159
	ia64_mlogbuf_dump();
160 161 162 163
	printk(KERN_ERR "OS_MCA: process [cpu %d, pid: %d, uid: %d, "
		"iip: %p, psr: 0x%lx,paddr: 0x%lx](%s) encounters MCA.\n",
		raw_smp_processor_id(), current->pid, current->uid,
		iip, ipsr, paddr, current->comm);
L
Linus Torvalds 已提交
164 165

	spin_lock(&mca_bh_lock);
166 167
	switch (mca_page_isolate(paddr)) {
	case ISOLATE_OK:
L
Linus Torvalds 已提交
168
		printk(KERN_DEBUG "Page isolation: ( %lx ) success.\n", paddr);
169 170
		break;
	case ISOLATE_NG:
171
		printk(KERN_CRIT "Page isolation: ( %lx ) failure.\n", paddr);
172 173 174
		break;
	default:
		break;
L
Linus Torvalds 已提交
175 176 177 178
	}
	spin_unlock(&mca_bh_lock);

	/* This process is about to be killed itself */
R
Russ Anderson 已提交
179
	do_exit(SIGKILL);
L
Linus Torvalds 已提交
180 181 182 183 184 185 186 187
}

/**
 * mca_make_peidx - Make index of processor error section
 * @slpi:	pointer to record of processor error section
 * @peidx:	pointer to index of processor error section
 */

H
Hidetoshi Seto 已提交
188
static void
L
Linus Torvalds 已提交
189 190
mca_make_peidx(sal_log_processor_info_t *slpi, peidx_table_t *peidx)
{
H
Hidetoshi Seto 已提交
191
	/*
L
Linus Torvalds 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	 * calculate the start address of
	 *   "struct cpuid_info" and "sal_processor_static_info_t".
	 */
	u64 total_check_num = slpi->valid.num_cache_check
				+ slpi->valid.num_tlb_check
				+ slpi->valid.num_bus_check
				+ slpi->valid.num_reg_file_check
				+ slpi->valid.num_ms_check;
	u64 head_size =	sizeof(sal_log_mod_error_info_t) * total_check_num
			+ sizeof(sal_log_processor_info_t);
	u64 mid_size  = slpi->valid.cpuid_info * sizeof(struct sal_cpuid_info);

	peidx_head(peidx)   = slpi;
	peidx_mid(peidx)    = (struct sal_cpuid_info *)
		(slpi->valid.cpuid_info ? ((char*)slpi + head_size) : NULL);
	peidx_bottom(peidx) = (sal_processor_static_info_t *)
		(slpi->valid.psi_static_struct ?
			((char*)slpi + head_size + mid_size) : NULL);
}

/**
H
Hidetoshi Seto 已提交
213
 * mca_make_slidx -  Make index of SAL error record
L
Linus Torvalds 已提交
214 215 216 217 218 219 220
 * @buffer:	pointer to SAL error record
 * @slidx:	pointer to index of SAL error record
 *
 * Return value:
 *	1 if record has platform error / 0 if not
 */
#define LOG_INDEX_ADD_SECT_PTR(sect, ptr) \
H
Hidetoshi Seto 已提交
221 222 223 224
	{slidx_list_t *hl = &slidx_pool.buffer[slidx_pool.cur_idx]; \
	hl->hdr = ptr; \
	list_add(&hl->list, &(sect)); \
	slidx_pool.cur_idx = (slidx_pool.cur_idx + 1)%slidx_pool.max_idx; }
L
Linus Torvalds 已提交
225

H
Hidetoshi Seto 已提交
226
static int
L
Linus Torvalds 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
mca_make_slidx(void *buffer, slidx_table_t *slidx)
{
	int platform_err = 0;
	int record_len = ((sal_log_record_header_t*)buffer)->len;
	u32 ercd_pos;
	int sects;
	sal_log_section_hdr_t *sp;

	/*
	 * Initialize index referring current record
	 */
	INIT_LIST_HEAD(&(slidx->proc_err));
	INIT_LIST_HEAD(&(slidx->mem_dev_err));
	INIT_LIST_HEAD(&(slidx->sel_dev_err));
	INIT_LIST_HEAD(&(slidx->pci_bus_err));
	INIT_LIST_HEAD(&(slidx->smbios_dev_err));
	INIT_LIST_HEAD(&(slidx->pci_comp_err));
	INIT_LIST_HEAD(&(slidx->plat_specific_err));
	INIT_LIST_HEAD(&(slidx->host_ctlr_err));
	INIT_LIST_HEAD(&(slidx->plat_bus_err));
	INIT_LIST_HEAD(&(slidx->unsupported));

	/*
	 * Extract a Record Header
	 */
	slidx->header = buffer;

	/*
	 * Extract each section records
	 * (arranged from "int ia64_log_platform_info_print()")
	 */
	for (ercd_pos = sizeof(sal_log_record_header_t), sects = 0;
		ercd_pos < record_len; ercd_pos += sp->len, sects++) {
		sp = (sal_log_section_hdr_t *)((char*)buffer + ercd_pos);
		if (!efi_guidcmp(sp->guid, SAL_PROC_DEV_ERR_SECT_GUID)) {
			LOG_INDEX_ADD_SECT_PTR(slidx->proc_err, sp);
H
Hidetoshi Seto 已提交
263 264
		} else if (!efi_guidcmp(sp->guid,
				SAL_PLAT_MEM_DEV_ERR_SECT_GUID)) {
L
Linus Torvalds 已提交
265 266
			platform_err = 1;
			LOG_INDEX_ADD_SECT_PTR(slidx->mem_dev_err, sp);
H
Hidetoshi Seto 已提交
267 268
		} else if (!efi_guidcmp(sp->guid,
				SAL_PLAT_SEL_DEV_ERR_SECT_GUID)) {
L
Linus Torvalds 已提交
269 270
			platform_err = 1;
			LOG_INDEX_ADD_SECT_PTR(slidx->sel_dev_err, sp);
H
Hidetoshi Seto 已提交
271 272
		} else if (!efi_guidcmp(sp->guid,
				SAL_PLAT_PCI_BUS_ERR_SECT_GUID)) {
L
Linus Torvalds 已提交
273 274
			platform_err = 1;
			LOG_INDEX_ADD_SECT_PTR(slidx->pci_bus_err, sp);
H
Hidetoshi Seto 已提交
275 276
		} else if (!efi_guidcmp(sp->guid,
				SAL_PLAT_SMBIOS_DEV_ERR_SECT_GUID)) {
L
Linus Torvalds 已提交
277 278
			platform_err = 1;
			LOG_INDEX_ADD_SECT_PTR(slidx->smbios_dev_err, sp);
H
Hidetoshi Seto 已提交
279 280
		} else if (!efi_guidcmp(sp->guid,
				SAL_PLAT_PCI_COMP_ERR_SECT_GUID)) {
L
Linus Torvalds 已提交
281 282
			platform_err = 1;
			LOG_INDEX_ADD_SECT_PTR(slidx->pci_comp_err, sp);
H
Hidetoshi Seto 已提交
283 284
		} else if (!efi_guidcmp(sp->guid,
				SAL_PLAT_SPECIFIC_ERR_SECT_GUID)) {
L
Linus Torvalds 已提交
285 286
			platform_err = 1;
			LOG_INDEX_ADD_SECT_PTR(slidx->plat_specific_err, sp);
H
Hidetoshi Seto 已提交
287 288
		} else if (!efi_guidcmp(sp->guid,
				SAL_PLAT_HOST_CTLR_ERR_SECT_GUID)) {
L
Linus Torvalds 已提交
289 290
			platform_err = 1;
			LOG_INDEX_ADD_SECT_PTR(slidx->host_ctlr_err, sp);
H
Hidetoshi Seto 已提交
291 292
		} else if (!efi_guidcmp(sp->guid,
				SAL_PLAT_BUS_ERR_SECT_GUID)) {
L
Linus Torvalds 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
			platform_err = 1;
			LOG_INDEX_ADD_SECT_PTR(slidx->plat_bus_err, sp);
		} else {
			LOG_INDEX_ADD_SECT_PTR(slidx->unsupported, sp);
		}
	}
	slidx->n_sections = sects;

	return platform_err;
}

/**
 * init_record_index_pools - Initialize pool of lists for SAL record index
 *
 * Return value:
 *	0 on Success / -ENOMEM on Failure
 */
H
Hidetoshi Seto 已提交
310
static int
L
Linus Torvalds 已提交
311 312 313 314 315 316
init_record_index_pools(void)
{
	int i;
	int rec_max_size;  /* Maximum size of SAL error records */
	int sect_min_size; /* Minimum size of SAL error sections */
	/* minimum size table of each section */
H
Hidetoshi Seto 已提交
317 318 319
	static int sal_log_sect_min_sizes[] = {
		sizeof(sal_log_processor_info_t)
		+ sizeof(sal_processor_static_info_t),
L
Linus Torvalds 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
		sizeof(sal_log_mem_dev_err_info_t),
		sizeof(sal_log_sel_dev_err_info_t),
		sizeof(sal_log_pci_bus_err_info_t),
		sizeof(sal_log_smbios_dev_err_info_t),
		sizeof(sal_log_pci_comp_err_info_t),
		sizeof(sal_log_plat_specific_err_info_t),
		sizeof(sal_log_host_ctlr_err_info_t),
		sizeof(sal_log_plat_bus_err_info_t),
	};

	/*
	 * MCA handler cannot allocate new memory on flight,
	 * so we preallocate enough memory to handle a SAL record.
	 *
	 * Initialize a handling set of slidx_pool:
	 *   1. Pick up the max size of SAL error records
	 *   2. Pick up the min size of SAL error sections
	 *   3. Allocate the pool as enough to 2 SAL records
	 *     (now we can estimate the maxinum of section in a record.)
	 */

	/* - 1 - */
	rec_max_size = sal_rec_max;

	/* - 2 - */
	sect_min_size = sal_log_sect_min_sizes[0];
	for (i = 1; i < sizeof sal_log_sect_min_sizes/sizeof(size_t); i++)
		if (sect_min_size > sal_log_sect_min_sizes[i])
			sect_min_size = sal_log_sect_min_sizes[i];

	/* - 3 - */
	slidx_pool.max_idx = (rec_max_size/sect_min_size) * 2 + 1;
H
Hidetoshi Seto 已提交
352 353
	slidx_pool.buffer = (slidx_list_t *)
		kmalloc(slidx_pool.max_idx * sizeof(slidx_list_t), GFP_KERNEL);
L
Linus Torvalds 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366

	return slidx_pool.buffer ? 0 : -ENOMEM;
}


/*****************************************************************************
 * Recovery functions                                                        *
 *****************************************************************************/

/**
 * is_mca_global - Check whether this MCA is global or not
 * @peidx:	pointer of index of processor error section
 * @pbci:	pointer to pal_bus_check_info_t
H
Hidetoshi Seto 已提交
367
 * @sos:	pointer to hand off struct between SAL and OS
L
Linus Torvalds 已提交
368 369 370 371 372 373
 *
 * Return value:
 *	MCA_IS_LOCAL / MCA_IS_GLOBAL
 */

static mca_type_t
374 375
is_mca_global(peidx_table_t *peidx, pal_bus_check_info_t *pbci,
	      struct ia64_sal_os_state *sos)
L
Linus Torvalds 已提交
376
{
H
Hidetoshi Seto 已提交
377 378
	pal_processor_state_info_t *psp =
		(pal_processor_state_info_t*)peidx_psp(peidx);
L
Linus Torvalds 已提交
379

H
Hidetoshi Seto 已提交
380
	/*
L
Linus Torvalds 已提交
381
	 * PAL can request a rendezvous, if the MCA has a global scope.
H
Hidetoshi Seto 已提交
382
	 * If "rz_always" flag is set, SAL requests MCA rendezvous
L
Linus Torvalds 已提交
383 384 385 386
	 * in spite of global MCA.
	 * Therefore it is local MCA when rendezvous has not been requested.
	 * Failed to rendezvous, the system must be down.
	 */
387
	switch (sos->rv_rc) {
L
Linus Torvalds 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
		case -1: /* SAL rendezvous unsuccessful */
			return MCA_IS_GLOBAL;
		case  0: /* SAL rendezvous not required */
			return MCA_IS_LOCAL;
		case  1: /* SAL rendezvous successful int */
		case  2: /* SAL rendezvous successful int with init */
		default:
			break;
	}

	/*
	 * If One or more Cache/TLB/Reg_File/Uarch_Check is here,
	 * it would be a local MCA. (i.e. processor internal error)
	 */
	if (psp->tc || psp->cc || psp->rc || psp->uc)
		return MCA_IS_LOCAL;
	
	/*
	 * Bus_Check structure with Bus_Check.ib (internal bus error) flag set
	 * would be a global MCA. (e.g. a system bus address parity error)
	 */
	if (!pbci || pbci->ib)
		return MCA_IS_GLOBAL;

	/*
	 * Bus_Check structure with Bus_Check.eb (external bus error) flag set
	 * could be either a local MCA or a global MCA.
	 *
	 * Referring Bus_Check.bsi:
	 *   0: Unknown/unclassified
	 *   1: BERR#
	 *   2: BINIT#
	 *   3: Hard Fail
	 * (FIXME: Are these SGI specific or generic bsi values?)
	 */
	if (pbci->eb)
		switch (pbci->bsi) {
			case 0:
				/* e.g. a load from poisoned memory */
				return MCA_IS_LOCAL;
			case 1:
			case 2:
			case 3:
				return MCA_IS_GLOBAL;
		}

	return MCA_IS_GLOBAL;
}

437 438 439 440 441 442 443 444 445 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 475 476 477 478 479 480
/**
 * get_target_identifier - Get the valid Cache or Bus check target identifier.
 * @peidx:	pointer of index of processor error section
 *
 * Return value:
 *	target address on Success / 0 on Failue
 */
static u64
get_target_identifier(peidx_table_t *peidx)
{
	u64 target_address = 0;
	sal_log_mod_error_info_t *smei;
	pal_cache_check_info_t *pcci;
	int i, level = 9;

	/*
	 * Look through the cache checks for a valid target identifier
	 * If more than one valid target identifier, return the one
	 * with the lowest cache level.
	 */
	for (i = 0; i < peidx_cache_check_num(peidx); i++) {
		smei = (sal_log_mod_error_info_t *)peidx_cache_check(peidx, i);
		if (smei->valid.target_identifier && smei->target_identifier) {
			pcci = (pal_cache_check_info_t *)&(smei->check_info);
			if (!target_address || (pcci->level < level)) {
				target_address = smei->target_identifier;
				level = pcci->level;
				continue;
			}
		}
	}
	if (target_address)
		return target_address;

	/*
	 * Look at the bus check for a valid target identifier
	 */
	smei = peidx_bus_check(peidx, 0);
	if (smei && smei->valid.target_identifier)
		return smei->target_identifier;

	return 0;
}

L
Linus Torvalds 已提交
481 482 483 484 485
/**
 * recover_from_read_error - Try to recover the errors which type are "read"s.
 * @slidx:	pointer of index of SAL error record
 * @peidx:	pointer of index of processor error section
 * @pbci:	pointer of pal_bus_check_info
H
Hidetoshi Seto 已提交
486
 * @sos:	pointer to hand off struct between SAL and OS
L
Linus Torvalds 已提交
487 488 489 490 491 492
 *
 * Return value:
 *	1 on Success / 0 on Failure
 */

static int
H
Hidetoshi Seto 已提交
493 494
recover_from_read_error(slidx_table_t *slidx,
			peidx_table_t *peidx, pal_bus_check_info_t *pbci,
495
			struct ia64_sal_os_state *sos)
L
Linus Torvalds 已提交
496
{
497
	u64 target_identifier;
L
Linus Torvalds 已提交
498 499 500 501 502
	pal_min_state_area_t *pmsa;
	struct ia64_psr *psr1, *psr2;
	ia64_fptr_t *mca_hdlr_bh = (ia64_fptr_t*)mca_handler_bhhook;

	/* Is target address valid? */
503 504
	target_identifier = get_target_identifier(peidx);
	if (!target_identifier)
505
		return fatal_mca("target address not valid");
L
Linus Torvalds 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520

	/*
	 * cpu read or memory-mapped io read
	 *
	 *    offending process  affected process  OS MCA do
	 *     kernel mode        kernel mode       down system
	 *     kernel mode        user   mode       kill the process
	 *     user   mode        kernel mode       down system (*)
	 *     user   mode        user   mode       kill the process
	 *
	 * (*) You could terminate offending user-mode process
	 *    if (pbci->pv && pbci->pl != 0) *and* if you sure
	 *    the process not have any locks of kernel.
	 */

521 522
	/* Is minstate valid? */
	if (!peidx_bottom(peidx) || !(peidx_bottom(peidx)->valid.minstate))
523
		return fatal_mca("minstate not valid");
L
Linus Torvalds 已提交
524
	psr1 =(struct ia64_psr *)&(peidx_minstate_area(peidx)->pmsa_ipsr);
525
	psr2 =(struct ia64_psr *)&(peidx_minstate_area(peidx)->pmsa_xpsr);
L
Linus Torvalds 已提交
526 527 528 529 530

	/*
	 *  Check the privilege level of interrupted context.
	 *   If it is user-mode, then terminate affected process.
	 */
531 532 533 534

	pmsa = sos->pal_min_state;
	if (psr1->cpl != 0 ||
	   ((psr2->cpl != 0) && mca_recover_range(pmsa->pmsa_iip))) {
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
		/*
		 *  setup for resume to bottom half of MCA,
		 * "mca_handler_bhhook"
		 */
		/* pass to bhhook as argument (gr8, ...) */
		pmsa->pmsa_gr[8-1] = target_identifier;
		pmsa->pmsa_gr[9-1] = pmsa->pmsa_iip;
		pmsa->pmsa_gr[10-1] = pmsa->pmsa_ipsr;
		/* set interrupted return address (but no use) */
		pmsa->pmsa_br0 = pmsa->pmsa_iip;
		/* change resume address to bottom half */
		pmsa->pmsa_iip = mca_hdlr_bh->fp;
		pmsa->pmsa_gr[1-1] = mca_hdlr_bh->gp;
		/* set cpl with kernel mode */
		psr2 = (struct ia64_psr *)&pmsa->pmsa_ipsr;
		psr2->cpl = 0;
		psr2->ri  = 0;
		psr2->bn  = 1;
		psr2->i  = 0;

		return mca_recovered("user memory corruption. "
556
				"kill affected process - recovered.");
L
Linus Torvalds 已提交
557 558
	}

559 560
	return fatal_mca("kernel context not recovered, iip 0x%lx\n",
			 pmsa->pmsa_iip);
L
Linus Torvalds 已提交
561 562 563 564 565 566 567
}

/**
 * recover_from_platform_error - Recover from platform error.
 * @slidx:	pointer of index of SAL error record
 * @peidx:	pointer of index of processor error section
 * @pbci:	pointer of pal_bus_check_info
H
Hidetoshi Seto 已提交
568
 * @sos:	pointer to hand off struct between SAL and OS
L
Linus Torvalds 已提交
569 570 571 572 573 574
 *
 * Return value:
 *	1 on Success / 0 on Failure
 */

static int
H
Hidetoshi Seto 已提交
575 576
recover_from_platform_error(slidx_table_t *slidx, peidx_table_t *peidx,
			    pal_bus_check_info_t *pbci,
577
			    struct ia64_sal_os_state *sos)
L
Linus Torvalds 已提交
578 579
{
	int status = 0;
H
Hidetoshi Seto 已提交
580 581
	pal_processor_state_info_t *psp =
		(pal_processor_state_info_t*)peidx_psp(peidx);
L
Linus Torvalds 已提交
582 583 584 585 586 587

	if (psp->bc && pbci->eb && pbci->bsi == 0) {
		switch(pbci->type) {
		case 1: /* partial read */
		case 3: /* full line(cpu) read */
		case 9: /* I/O space read */
H
Hidetoshi Seto 已提交
588 589
			status = recover_from_read_error(slidx, peidx, pbci,
							 sos);
L
Linus Torvalds 已提交
590 591 592 593 594 595 596 597 598 599
			break;
		case 0: /* unknown */
		case 2: /* partial write */
		case 4: /* full line write */
		case 5: /* implicit or explicit write-back operation */
		case 6: /* snoop probe */
		case 7: /* incoming or outgoing ptc.g */
		case 8: /* write coalescing transactions */
		case 10: /* I/O space write */
		case 11: /* inter-processor interrupt message(IPI) */
H
Hidetoshi Seto 已提交
600 601
		case 12: /* interrupt acknowledge or
				external task priority cycle */
L
Linus Torvalds 已提交
602 603 604 605 606 607 608 609 610 611 612 613 614 615
		default:
			break;
		}
	}

	return status;
}

/**
 * recover_from_processor_error
 * @platform:	whether there are some platform error section or not
 * @slidx:	pointer of index of SAL error record
 * @peidx:	pointer of index of processor error section
 * @pbci:	pointer of pal_bus_check_info
H
Hidetoshi Seto 已提交
616
 * @sos:	pointer to hand off struct between SAL and OS
L
Linus Torvalds 已提交
617 618 619 620 621 622 623 624 625 626 627 628 629
 *
 * Return value:
 *	1 on Success / 0 on Failure
 */
/*
 *  Later we try to recover when below all conditions are satisfied.
 *   1. Only one processor error section is exist.
 *   2. BUS_CHECK is exist and the others are not exist.(Except TLB_CHECK)
 *   3. The entry of BUS_CHECK_INFO is 1.
 *   4. "External bus error" flag is set and the others are not set.
 */

static int
H
Hidetoshi Seto 已提交
630 631
recover_from_processor_error(int platform, slidx_table_t *slidx,
			     peidx_table_t *peidx, pal_bus_check_info_t *pbci,
632
			     struct ia64_sal_os_state *sos)
L
Linus Torvalds 已提交
633
{
H
Hidetoshi Seto 已提交
634 635
	pal_processor_state_info_t *psp =
		(pal_processor_state_info_t*)peidx_psp(peidx);
L
Linus Torvalds 已提交
636

H
Hidetoshi Seto 已提交
637
	/*
638 639
	 * Processor recovery status must key off of the PAL recovery
	 * status in the Processor State Parameter.
L
Linus Torvalds 已提交
640
	 */
641 642 643 644 645

	/*
	 * The machine check is corrected.
	 */
	if (psp->cm == 1)
646
		return mca_recovered("machine check is already corrected.");
647 648 649 650 651

	/*
	 * The error was not contained.  Software must be reset.
	 */
	if (psp->us || psp->ci == 0)
652
		return fatal_mca("error not contained");
L
Linus Torvalds 已提交
653 654

	/*
655 656 657 658 659 660
	 * The cache check and bus check bits have four possible states
	 *   cc bc
	 *    0  0	Weird record, not recovered
	 *    1  0	Cache error, not recovered
	 *    0  1	I/O error, attempt recovery
	 *    1  1	Memory error, attempt recovery
L
Linus Torvalds 已提交
661 662
	 */
	if (psp->bc == 0 || pbci == NULL)
663
		return fatal_mca("No bus check");
L
Linus Torvalds 已提交
664 665 666 667 668

	/*
	 * Sorry, we cannot handle so many.
	 */
	if (peidx_bus_check_num(peidx) > 1)
669
		return fatal_mca("Too many bus checks");
L
Linus Torvalds 已提交
670 671 672
	/*
	 * Well, here is only one bus error.
	 */
673
	if (pbci->ib)
674
		return fatal_mca("Internal Bus error");
675
	if (pbci->cc)
676
		return fatal_mca("Cache-cache error");
L
Linus Torvalds 已提交
677
	if (pbci->eb && pbci->bsi > 0)
678
		return fatal_mca("External bus check fatal status");
L
Linus Torvalds 已提交
679 680 681 682 683 684

	/*
	 * This is a local MCA and estimated as recoverble external bus error.
	 * (e.g. a load from poisoned memory)
	 * This means "there are some platform errors".
	 */
H
Hidetoshi Seto 已提交
685
	if (platform)
686
		return recover_from_platform_error(slidx, peidx, pbci, sos);
H
Hidetoshi Seto 已提交
687 688
	/*
	 * On account of strange SAL error record, we cannot recover.
L
Linus Torvalds 已提交
689
	 */
690
	return fatal_mca("Strange SAL record");
L
Linus Torvalds 已提交
691 692 693 694 695
}

/**
 * mca_try_to_recover - Try to recover from MCA
 * @rec:	pointer to a SAL error record
H
Hidetoshi Seto 已提交
696
 * @sos:	pointer to hand off struct between SAL and OS
L
Linus Torvalds 已提交
697 698 699 700 701 702
 *
 * Return value:
 *	1 on Success / 0 on Failure
 */

static int
H
Hidetoshi Seto 已提交
703
mca_try_to_recover(void *rec, struct ia64_sal_os_state *sos)
L
Linus Torvalds 已提交
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
{
	int platform_err;
	int n_proc_err;
	slidx_table_t slidx;
	peidx_table_t peidx;
	pal_bus_check_info_t pbci;

	/* Make index of SAL error record */
	platform_err = mca_make_slidx(rec, &slidx);

	/* Count processor error sections */
	n_proc_err = slidx_count(&slidx, proc_err);

	 /* Now, OS can recover when there is one processor error section */
	if (n_proc_err > 1)
719
		return fatal_mca("Too Many Errors");
720
	else if (n_proc_err == 0)
721 722
		/* Weird SAL record ... We can't do anything */
		return fatal_mca("Weird SAL record");
L
Linus Torvalds 已提交
723 724

	/* Make index of processor error section */
H
Hidetoshi Seto 已提交
725 726
	mca_make_peidx((sal_log_processor_info_t*)
		slidx_first_entry(&slidx.proc_err)->hdr, &peidx);
L
Linus Torvalds 已提交
727 728 729 730 731

	/* Extract Processor BUS_CHECK[0] */
	*((u64*)&pbci) = peidx_check_info(&peidx, bus_check, 0);

	/* Check whether MCA is global or not */
732
	if (is_mca_global(&peidx, &pbci, sos))
733
		return fatal_mca("global MCA");
L
Linus Torvalds 已提交
734 735
	
	/* Try to recover a processor error */
H
Hidetoshi Seto 已提交
736 737
	return recover_from_processor_error(platform_err, &slidx, &peidx,
					    &pbci, sos);
L
Linus Torvalds 已提交
738 739 740 741 742 743 744 745 746 747 748 749
}

/*
 * =============================================================================
 */

int __init mca_external_handler_init(void)
{
	if (init_record_index_pools())
		return -ENOMEM;

	/* register external mca handlers */
H
Hidetoshi Seto 已提交
750
	if (ia64_reg_MCA_extension(mca_try_to_recover)) {	
L
Linus Torvalds 已提交
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
		printk(KERN_ERR "ia64_reg_MCA_extension failed.\n");
		kfree(slidx_pool.buffer);
		return -EFAULT;
	}
	return 0;
}

void __exit mca_external_handler_exit(void)
{
	/* unregister external mca handlers */
	ia64_unreg_MCA_extension();
	kfree(slidx_pool.buffer);
}

module_init(mca_external_handler_init);
module_exit(mca_external_handler_exit);

module_param(sal_rec_max, int, 0644);
MODULE_PARM_DESC(sal_rec_max, "Max size of SAL error record");

MODULE_DESCRIPTION("ia64 platform dependent mca handler driver");
MODULE_LICENSE("GPL");