eeh_pseries.c 19.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 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
/*
 * The file intends to implement the platform dependent EEH operations on pseries.
 * Actually, the pseries platform is built based on RTAS heavily. That means the
 * pseries platform dependent EEH operations will be built on RTAS calls. The functions
 * are devired from arch/powerpc/platforms/pseries/eeh.c and necessary cleanup has
 * been done.
 *
 * Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2011.
 * Copyright IBM Corporation 2001, 2005, 2006
 * Copyright Dave Engebretsen & Todd Inglett 2001
 * Copyright Linas Vepstas 2005, 2006
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <linux/atomic.h>
#include <linux/delay.h>
#include <linux/export.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/of.h>
#include <linux/pci.h>
#include <linux/proc_fs.h>
#include <linux/rbtree.h>
#include <linux/sched.h>
#include <linux/seq_file.h>
#include <linux/spinlock.h>

#include <asm/eeh.h>
#include <asm/eeh_event.h>
#include <asm/io.h>
#include <asm/machdep.h>
#include <asm/ppc-pci.h>
#include <asm/rtas.h>

48 49 50 51 52 53 54 55 56 57
/* RTAS tokens */
static int ibm_set_eeh_option;
static int ibm_set_slot_reset;
static int ibm_read_slot_reset_state;
static int ibm_read_slot_reset_state2;
static int ibm_slot_error_detail;
static int ibm_get_config_addr_info;
static int ibm_get_config_addr_info2;
static int ibm_configure_pe;

58 59 60 61 62 63 64 65 66
/*
 * Buffer for reporting slot-error-detail rtas calls. Its here
 * in BSS, and not dynamically alloced, so that it ends up in
 * RMO where RTAS can access it.
 */
static unsigned char slot_errbuf[RTAS_ERROR_LOG_MAX];
static DEFINE_SPINLOCK(slot_errbuf_lock);
static int eeh_error_buf_size;

67 68 69 70 71 72 73
/**
 * pseries_eeh_init - EEH platform dependent initialization
 *
 * EEH platform dependent initialization on pseries.
 */
static int pseries_eeh_init(void)
{
74 75 76 77 78 79 80 81 82
	/* figure out EEH RTAS function call tokens */
	ibm_set_eeh_option		= rtas_token("ibm,set-eeh-option");
	ibm_set_slot_reset		= rtas_token("ibm,set-slot-reset");
	ibm_read_slot_reset_state2	= rtas_token("ibm,read-slot-reset-state2");
	ibm_read_slot_reset_state	= rtas_token("ibm,read-slot-reset-state");
	ibm_slot_error_detail		= rtas_token("ibm,slot-error-detail");
	ibm_get_config_addr_info2	= rtas_token("ibm,get-config-addr-info2");
	ibm_get_config_addr_info	= rtas_token("ibm,get-config-addr-info");
	ibm_configure_pe		= rtas_token("ibm,configure-pe");
83 84 85 86 87 88 89 90

	/*
	 * ibm,configure-pe and ibm,configure-bridge have the same semantics,
	 * however ibm,configure-pe can be faster.  If we can't find
	 * ibm,configure-pe then fall back to using ibm,configure-bridge.
	 */
	if (ibm_configure_pe == RTAS_UNKNOWN_SERVICE)
		ibm_configure_pe 	= rtas_token("ibm,configure-bridge");
91

92 93 94 95 96
	/*
	 * Necessary sanity check. We needn't check "get-config-addr-info"
	 * and its variant since the old firmware probably support address
	 * of domain/bus/slot/function for EEH RTAS operations.
	 */
97 98 99 100 101
	if (ibm_set_eeh_option == RTAS_UNKNOWN_SERVICE		||
	    ibm_set_slot_reset == RTAS_UNKNOWN_SERVICE		||
	    (ibm_read_slot_reset_state2 == RTAS_UNKNOWN_SERVICE &&
	     ibm_read_slot_reset_state == RTAS_UNKNOWN_SERVICE)	||
	    ibm_slot_error_detail == RTAS_UNKNOWN_SERVICE	||
102
	    ibm_configure_pe == RTAS_UNKNOWN_SERVICE) {
103
		pr_info("EEH functionality not supported\n");
104 105 106
		return -EINVAL;
	}

107 108 109 110
	/* Initialize error log lock and size */
	spin_lock_init(&slot_errbuf_lock);
	eeh_error_buf_size = rtas_token("rtas-error-log-max");
	if (eeh_error_buf_size == RTAS_UNKNOWN_SERVICE) {
111
		pr_info("%s: unknown EEH error log size\n",
112 113 114
			__func__);
		eeh_error_buf_size = 1024;
	} else if (eeh_error_buf_size > RTAS_ERROR_LOG_MAX) {
115
		pr_info("%s: EEH error log size %d exceeds the maximal %d\n",
116 117 118 119
			__func__, eeh_error_buf_size, RTAS_ERROR_LOG_MAX);
		eeh_error_buf_size = RTAS_ERROR_LOG_MAX;
	}

G
Gavin Shan 已提交
120
	/* Set EEH probe mode */
121
	eeh_add_flag(EEH_PROBE_MODE_DEVTREE | EEH_ENABLE_IO_FOR_LOG);
G
Gavin Shan 已提交
122

123 124 125
	return 0;
}

G
Gavin Shan 已提交
126
static int pseries_eeh_cap_start(struct pci_dn *pdn)
127 128 129 130 131 132 133 134 135 136 137 138 139 140
{
	u32 status;

	if (!pdn)
		return 0;

	rtas_read_config(pdn, PCI_STATUS, 2, &status);
	if (!(status & PCI_STATUS_CAP_LIST))
		return 0;

	return PCI_CAPABILITY_LIST;
}


G
Gavin Shan 已提交
141
static int pseries_eeh_find_cap(struct pci_dn *pdn, int cap)
142
{
G
Gavin Shan 已提交
143
	int pos = pseries_eeh_cap_start(pdn);
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
	int cnt = 48;	/* Maximal number of capabilities */
	u32 id;

	if (!pos)
		return 0;

        while (cnt--) {
		rtas_read_config(pdn, pos, 1, &pos);
		if (pos < 0x40)
			break;
		pos &= ~3;
		rtas_read_config(pdn, pos + PCI_CAP_LIST_ID, 1, &id);
		if (id == 0xff)
			break;
		if (id == cap)
			return pos;
		pos += PCI_CAP_LIST_NEXT;
	}

	return 0;
}

G
Gavin Shan 已提交
166
static int pseries_eeh_find_ecap(struct pci_dn *pdn, int cap)
167
{
G
Gavin Shan 已提交
168
	struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
	u32 header;
	int pos = 256;
	int ttl = (4096 - 256) / 8;

	if (!edev || !edev->pcie_cap)
		return 0;
	if (rtas_read_config(pdn, pos, 4, &header) != PCIBIOS_SUCCESSFUL)
		return 0;
	else if (!header)
		return 0;

	while (ttl-- > 0) {
		if (PCI_EXT_CAP_ID(header) == cap && pos)
			return pos;

		pos = PCI_EXT_CAP_NEXT(header);
		if (pos < 256)
			break;

		if (rtas_read_config(pdn, pos, 4, &header) != PCIBIOS_SUCCESSFUL)
			break;
	}

	return 0;
}

G
Gavin Shan 已提交
195
/**
G
Gavin Shan 已提交
196 197 198
 * pseries_eeh_probe - EEH probe on the given device
 * @pdn: PCI device node
 * @data: Unused
G
Gavin Shan 已提交
199 200 201 202 203
 *
 * When EEH module is installed during system boot, all PCI devices
 * are checked one by one to see if it supports EEH. The function
 * is introduced for the purpose.
 */
G
Gavin Shan 已提交
204
static void *pseries_eeh_probe(struct pci_dn *pdn, void *data)
G
Gavin Shan 已提交
205 206 207
{
	struct eeh_dev *edev;
	struct eeh_pe pe;
208
	u32 pcie_flags;
G
Gavin Shan 已提交
209 210 211 212
	int enable = 0;
	int ret;

	/* Retrieve OF node and eeh device */
G
Gavin Shan 已提交
213 214
	edev = pdn_to_eeh_dev(pdn);
	if (!edev || edev->pe)
G
Gavin Shan 已提交
215 216
		return NULL;

G
Gavin Shan 已提交
217 218
	/* Check class/vendor/device IDs */
	if (!pdn->vendor_id || !pdn->device_id || !pdn->class_code)
G
Gavin Shan 已提交
219 220
		return NULL;

G
Gavin Shan 已提交
221 222 223
	/* Skip for PCI-ISA bridge */
        if ((pdn->class_code >> 8) == PCI_CLASS_BRIDGE_ISA)
		return NULL;
224

225 226 227 228 229
	/*
	 * Update class code and mode of eeh device. We need
	 * correctly reflects that current device is root port
	 * or PCIe switch downstream port.
	 */
G
Gavin Shan 已提交
230 231 232 233
	edev->class_code = pdn->class_code;
	edev->pcix_cap = pseries_eeh_find_cap(pdn, PCI_CAP_ID_PCIX);
	edev->pcie_cap = pseries_eeh_find_cap(pdn, PCI_CAP_ID_EXP);
	edev->aer_cap = pseries_eeh_find_ecap(pdn, PCI_EXT_CAP_ID_ERR);
234
	edev->mode &= 0xFFFFFF00;
235 236 237 238 239 240 241 242 243 244 245 246
	if ((edev->class_code >> 8) == PCI_CLASS_BRIDGE_PCI) {
		edev->mode |= EEH_DEV_BRIDGE;
		if (edev->pcie_cap) {
			rtas_read_config(pdn, edev->pcie_cap + PCI_EXP_FLAGS,
					 2, &pcie_flags);
			pcie_flags = (pcie_flags & PCI_EXP_FLAGS_TYPE) >> 4;
			if (pcie_flags == PCI_EXP_TYPE_ROOT_PORT)
				edev->mode |= EEH_DEV_ROOT_PORT;
			else if (pcie_flags == PCI_EXP_TYPE_DOWNSTREAM)
				edev->mode |= EEH_DEV_DS_PORT;
		}
	}
G
Gavin Shan 已提交
247 248 249 250

	/* Initialize the fake PE */
	memset(&pe, 0, sizeof(struct eeh_pe));
	pe.phb = edev->phb;
G
Gavin Shan 已提交
251
	pe.config_addr = (pdn->busno << 16) | (pdn->devfn << 8);
G
Gavin Shan 已提交
252 253 254 255 256

	/* Enable EEH on the device */
	ret = eeh_ops->set_option(&pe, EEH_OPT_ENABLE);
	if (!ret) {
		/* Retrieve PE address */
G
Gavin Shan 已提交
257
		edev->config_addr = (pdn->busno << 16) | (pdn->devfn << 8);
G
Gavin Shan 已提交
258 259 260 261 262 263 264 265 266 267 268 269
		edev->pe_config_addr = eeh_ops->get_pe_addr(&pe);
		pe.addr = edev->pe_config_addr;

		/* Some older systems (Power4) allow the ibm,set-eeh-option
		 * call to succeed even on nodes where EEH is not supported.
		 * Verify support explicitly.
		 */
		ret = eeh_ops->get_state(&pe, NULL);
		if (ret > 0 && ret != EEH_STATE_NOT_SUPPORT)
			enable = 1;

		if (enable) {
270
			eeh_add_flag(EEH_ENABLED);
G
Gavin Shan 已提交
271 272
			eeh_add_to_parent_pe(edev);

G
Gavin Shan 已提交
273 274 275 276 277 278
			pr_debug("%s: EEH enabled on %02x:%02x.%01x PHB#%d-PE#%x\n",
				__func__, pdn->busno, PCI_SLOT(pdn->devfn),
				PCI_FUNC(pdn->devfn), pe.phb->global_number,
				pe.addr);
		} else if (pdn->parent && pdn_to_eeh_dev(pdn->parent) &&
			   (pdn_to_eeh_dev(pdn->parent))->pe) {
G
Gavin Shan 已提交
279 280 281
			/* This device doesn't support EEH, but it may have an
			 * EEH parent, in which case we mark it as supported.
			 */
G
Gavin Shan 已提交
282 283
			edev->config_addr = pdn_to_eeh_dev(pdn->parent)->config_addr;
			edev->pe_config_addr = pdn_to_eeh_dev(pdn->parent)->pe_config_addr;
G
Gavin Shan 已提交
284 285 286 287 288 289 290 291 292 293
			eeh_add_to_parent_pe(edev);
		}
	}

	/* Save memory bars */
	eeh_save_bars(edev);

	return NULL;
}

294 295
/**
 * pseries_eeh_set_option - Initialize EEH or MMIO/DMA reenable
296
 * @pe: EEH PE
297 298 299 300 301 302
 * @option: operation to be issued
 *
 * The function is used to control the EEH functionality globally.
 * Currently, following options are support according to PAPR:
 * Enable EEH, Disable EEH, Enable MMIO and Enable DMA
 */
303
static int pseries_eeh_set_option(struct eeh_pe *pe, int option)
304
{
305 306 307 308 309 310 311 312 313 314 315 316 317 318
	int ret = 0;
	int config_addr;

	/*
	 * When we're enabling or disabling EEH functioality on
	 * the particular PE, the PE config address is possibly
	 * unavailable. Therefore, we have to figure it out from
	 * the FDT node.
	 */
	switch (option) {
	case EEH_OPT_DISABLE:
	case EEH_OPT_ENABLE:
	case EEH_OPT_THAW_MMIO:
	case EEH_OPT_THAW_DMA:
319 320 321
		config_addr = pe->config_addr;
		if (pe->addr)
			config_addr = pe->addr;
322
		break;
323 324 325
	case EEH_OPT_FREEZE_PE:
		/* Not support */
		return 0;
326 327 328 329 330 331 332
	default:
		pr_err("%s: Invalid option %d\n",
			__func__, option);
		return -EINVAL;
	}

	ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
333 334
			config_addr, BUID_HI(pe->phb->buid),
			BUID_LO(pe->phb->buid), option);
335 336

	return ret;
337 338 339 340
}

/**
 * pseries_eeh_get_pe_addr - Retrieve PE address
341
 * @pe: EEH PE
342 343 344 345 346 347 348 349 350 351
 *
 * Retrieve the assocated PE address. Actually, there're 2 RTAS
 * function calls dedicated for the purpose. We need implement
 * it through the new function and then the old one. Besides,
 * you should make sure the config address is figured out from
 * FDT node before calling the function.
 *
 * It's notable that zero'ed return value means invalid PE config
 * address.
 */
352
static int pseries_eeh_get_pe_addr(struct eeh_pe *pe)
353
{
354 355 356 357 358 359 360 361 362 363
	int ret = 0;
	int rets[3];

	if (ibm_get_config_addr_info2 != RTAS_UNKNOWN_SERVICE) {
		/*
		 * First of all, we need to make sure there has one PE
		 * associated with the device. Otherwise, PE address is
		 * meaningless.
		 */
		ret = rtas_call(ibm_get_config_addr_info2, 4, 2, rets,
364 365
				pe->config_addr, BUID_HI(pe->phb->buid),
				BUID_LO(pe->phb->buid), 1);
366 367 368 369 370
		if (ret || (rets[0] == 0))
			return 0;

		/* Retrieve the associated PE config address */
		ret = rtas_call(ibm_get_config_addr_info2, 4, 2, rets,
371 372
				pe->config_addr, BUID_HI(pe->phb->buid),
				BUID_LO(pe->phb->buid), 0);
373
		if (ret) {
374
			pr_warn("%s: Failed to get address for PHB#%d-PE#%x\n",
375
				__func__, pe->phb->global_number, pe->config_addr);
376 377 378 379 380 381 382 383
			return 0;
		}

		return rets[0];
	}

	if (ibm_get_config_addr_info != RTAS_UNKNOWN_SERVICE) {
		ret = rtas_call(ibm_get_config_addr_info, 4, 2, rets,
384 385
				pe->config_addr, BUID_HI(pe->phb->buid),
				BUID_LO(pe->phb->buid), 0);
386
		if (ret) {
387
			pr_warn("%s: Failed to get address for PHB#%d-PE#%x\n",
388
				__func__, pe->phb->global_number, pe->config_addr);
389 390 391 392 393 394 395
			return 0;
		}

		return rets[0];
	}

	return ret;
396 397 398 399
}

/**
 * pseries_eeh_get_state - Retrieve PE state
400
 * @pe: EEH PE
401 402 403 404 405 406 407 408 409 410
 * @state: return value
 *
 * Retrieve the state of the specified PE. On RTAS compliant
 * pseries platform, there already has one dedicated RTAS function
 * for the purpose. It's notable that the associated PE config address
 * might be ready when calling the function. Therefore, endeavour to
 * use the PE config address if possible. Further more, there're 2
 * RTAS calls for the purpose, we need to try the new one and back
 * to the old one if the new one couldn't work properly.
 */
411
static int pseries_eeh_get_state(struct eeh_pe *pe, int *state)
412
{
413 414 415 416 417 418
	int config_addr;
	int ret;
	int rets[4];
	int result;

	/* Figure out PE config address if possible */
419 420 421
	config_addr = pe->config_addr;
	if (pe->addr)
		config_addr = pe->addr;
422 423 424

	if (ibm_read_slot_reset_state2 != RTAS_UNKNOWN_SERVICE) {
		ret = rtas_call(ibm_read_slot_reset_state2, 3, 4, rets,
425 426
				config_addr, BUID_HI(pe->phb->buid),
				BUID_LO(pe->phb->buid));
427 428 429 430
	} else if (ibm_read_slot_reset_state != RTAS_UNKNOWN_SERVICE) {
		/* Fake PE unavailable info */
		rets[2] = 0;
		ret = rtas_call(ibm_read_slot_reset_state, 3, 3, rets,
431 432
				config_addr, BUID_HI(pe->phb->buid),
				BUID_LO(pe->phb->buid));
433 434 435 436 437 438 439 440
	} else {
		return EEH_STATE_NOT_SUPPORT;
	}

	if (ret)
		return ret;

	/* Parse the result out */
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
	if (!rets[1])
		return EEH_STATE_NOT_SUPPORT;

	switch(rets[0]) {
	case 0:
		result = EEH_STATE_MMIO_ACTIVE |
			 EEH_STATE_DMA_ACTIVE;
		break;
	case 1:
		result = EEH_STATE_RESET_ACTIVE |
			 EEH_STATE_MMIO_ACTIVE  |
			 EEH_STATE_DMA_ACTIVE;
		break;
	case 2:
		result = 0;
		break;
	case 4:
		result = EEH_STATE_MMIO_ENABLED;
		break;
	case 5:
		if (rets[2]) {
			if (state) *state = rets[2];
			result = EEH_STATE_UNAVAILABLE;
		} else {
465 466
			result = EEH_STATE_NOT_SUPPORT;
		}
467 468
		break;
	default:
469 470 471 472
		result = EEH_STATE_NOT_SUPPORT;
	}

	return result;
473 474 475 476
}

/**
 * pseries_eeh_reset - Reset the specified PE
477
 * @pe: EEH PE
478 479 480 481
 * @option: reset option
 *
 * Reset the specified PE
 */
482
static int pseries_eeh_reset(struct eeh_pe *pe, int option)
483
{
484 485 486 487
	int config_addr;
	int ret;

	/* Figure out PE address */
488 489 490
	config_addr = pe->config_addr;
	if (pe->addr)
		config_addr = pe->addr;
491 492 493

	/* Reset PE through RTAS call */
	ret = rtas_call(ibm_set_slot_reset, 4, 1, NULL,
494 495
			config_addr, BUID_HI(pe->phb->buid),
			BUID_LO(pe->phb->buid), option);
496 497 498 499

	/* If fundamental-reset not supported, try hot-reset */
	if (option == EEH_RESET_FUNDAMENTAL &&
	    ret == -8) {
500
		option = EEH_RESET_HOT;
501
		ret = rtas_call(ibm_set_slot_reset, 4, 1, NULL,
502
				config_addr, BUID_HI(pe->phb->buid),
503
				BUID_LO(pe->phb->buid), option);
504 505
	}

506 507 508 509 510 511 512
	/* We need reset hold or settlement delay */
	if (option == EEH_RESET_FUNDAMENTAL ||
	    option == EEH_RESET_HOT)
		msleep(EEH_PE_RST_HOLD_TIME);
	else
		msleep(EEH_PE_RST_SETTLE_TIME);

513
	return ret;
514 515 516 517
}

/**
 * pseries_eeh_wait_state - Wait for PE state
518
 * @pe: EEH PE
519
 * @max_wait: maximal period in millisecond
520 521 522 523
 *
 * Wait for the state of associated PE. It might take some time
 * to retrieve the PE's state.
 */
524
static int pseries_eeh_wait_state(struct eeh_pe *pe, int max_wait)
525
{
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
	int ret;
	int mwait;

	/*
	 * According to PAPR, the state of PE might be temporarily
	 * unavailable. Under the circumstance, we have to wait
	 * for indicated time determined by firmware. The maximal
	 * wait time is 5 minutes, which is acquired from the original
	 * EEH implementation. Also, the original implementation
	 * also defined the minimal wait time as 1 second.
	 */
#define EEH_STATE_MIN_WAIT_TIME	(1000)
#define EEH_STATE_MAX_WAIT_TIME	(300 * 1000)

	while (1) {
541
		ret = pseries_eeh_get_state(pe, &mwait);
542 543 544 545 546 547 548 549 550 551

		/*
		 * If the PE's state is temporarily unavailable,
		 * we have to wait for the specified time. Otherwise,
		 * the PE's state will be returned immediately.
		 */
		if (ret != EEH_STATE_UNAVAILABLE)
			return ret;

		if (max_wait <= 0) {
552
			pr_warn("%s: Timeout when getting PE's state (%d)\n",
553 554 555 556 557
				__func__, max_wait);
			return EEH_STATE_NOT_SUPPORT;
		}

		if (mwait <= 0) {
558
			pr_warn("%s: Firmware returned bad wait value %d\n",
559 560 561
				__func__, mwait);
			mwait = EEH_STATE_MIN_WAIT_TIME;
		} else if (mwait > EEH_STATE_MAX_WAIT_TIME) {
562
			pr_warn("%s: Firmware returned too long wait value %d\n",
563 564 565 566 567 568 569 570 571
				__func__, mwait);
			mwait = EEH_STATE_MAX_WAIT_TIME;
		}

		max_wait -= mwait;
		msleep(mwait);
	}

	return EEH_STATE_NOT_SUPPORT;
572 573 574 575
}

/**
 * pseries_eeh_get_log - Retrieve error log
576
 * @pe: EEH PE
577 578 579 580 581 582 583 584
 * @severity: temporary or permanent error log
 * @drv_log: driver log to be combined with retrieved error log
 * @len: length of driver log
 *
 * Retrieve the temporary or permanent error from the PE.
 * Actually, the error will be retrieved through the dedicated
 * RTAS call.
 */
585
static int pseries_eeh_get_log(struct eeh_pe *pe, int severity, char *drv_log, unsigned long len)
586
{
587 588 589 590 591 592 593 594
	int config_addr;
	unsigned long flags;
	int ret;

	spin_lock_irqsave(&slot_errbuf_lock, flags);
	memset(slot_errbuf, 0, eeh_error_buf_size);

	/* Figure out the PE address */
595 596 597
	config_addr = pe->config_addr;
	if (pe->addr)
		config_addr = pe->addr;
598 599

	ret = rtas_call(ibm_slot_error_detail, 8, 1, NULL, config_addr,
600
			BUID_HI(pe->phb->buid), BUID_LO(pe->phb->buid),
601 602 603 604 605 606 607 608
			virt_to_phys(drv_log), len,
			virt_to_phys(slot_errbuf), eeh_error_buf_size,
			severity);
	if (!ret)
		log_error(slot_errbuf, ERR_TYPE_RTAS_LOG, 0);
	spin_unlock_irqrestore(&slot_errbuf_lock, flags);

	return ret;
609 610 611 612
}

/**
 * pseries_eeh_configure_bridge - Configure PCI bridges in the indicated PE
613
 * @pe: EEH PE
614 615 616 617 618
 *
 * The function will be called to reconfigure the bridges included
 * in the specified PE so that the mulfunctional PE would be recovered
 * again.
 */
619
static int pseries_eeh_configure_bridge(struct eeh_pe *pe)
620
{
621 622
	int config_addr;
	int ret;
623 624
	/* Waiting 0.2s maximum before skipping configuration */
	int max_wait = 200;
625 626

	/* Figure out the PE address */
627 628 629
	config_addr = pe->config_addr;
	if (pe->addr)
		config_addr = pe->addr;
630

631
	while (max_wait > 0) {
632 633 634
		ret = rtas_call(ibm_configure_pe, 3, 1, NULL,
				config_addr, BUID_HI(pe->phb->buid),
				BUID_LO(pe->phb->buid));
635

636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
		if (!ret)
			return ret;

		/*
		 * If RTAS returns a delay value that's above 100ms, cut it
		 * down to 100ms in case firmware made a mistake.  For more
		 * on how these delay values work see rtas_busy_delay_time
		 */
		if (ret > RTAS_EXTENDED_DELAY_MIN+2 &&
		    ret <= RTAS_EXTENDED_DELAY_MAX)
			ret = RTAS_EXTENDED_DELAY_MIN+2;

		max_wait -= rtas_busy_delay_time(ret);

		if (max_wait < 0)
			break;

		rtas_busy_delay(ret);
	}
655

656 657
	pr_warn("%s: Unable to configure bridge PHB#%d-PE#%x (%d)\n",
		__func__, pe->phb->global_number, pe->addr, ret);
658
	return ret;
659 660
}

661 662
/**
 * pseries_eeh_read_config - Read PCI config space
663
 * @pdn: PCI device node
664 665 666 667 668 669
 * @where: PCI address
 * @size: size to read
 * @val: return value
 *
 * Read config space from the speicifed device
 */
670
static int pseries_eeh_read_config(struct pci_dn *pdn, int where, int size, u32 *val)
671 672 673 674 675 676
{
	return rtas_read_config(pdn, where, size, val);
}

/**
 * pseries_eeh_write_config - Write PCI config space
677
 * @pdn: PCI device node
678 679 680 681 682 683
 * @where: PCI address
 * @size: size to write
 * @val: value to be written
 *
 * Write config space to the specified device
 */
684
static int pseries_eeh_write_config(struct pci_dn *pdn, int where, int size, u32 val)
685 686 687 688
{
	return rtas_write_config(pdn, where, size, val);
}

689 690 691
static struct eeh_ops pseries_eeh_ops = {
	.name			= "pseries",
	.init			= pseries_eeh_init,
G
Gavin Shan 已提交
692
	.probe			= pseries_eeh_probe,
693 694 695 696 697 698
	.set_option		= pseries_eeh_set_option,
	.get_pe_addr		= pseries_eeh_get_pe_addr,
	.get_state		= pseries_eeh_get_state,
	.reset			= pseries_eeh_reset,
	.wait_state		= pseries_eeh_wait_state,
	.get_log		= pseries_eeh_get_log,
699
	.configure_bridge       = pseries_eeh_configure_bridge,
700
	.err_inject		= NULL,
701
	.read_config		= pseries_eeh_read_config,
702 703 704
	.write_config		= pseries_eeh_write_config,
	.next_error		= NULL,
	.restore_config		= NULL
705 706 707 708 709 710 711 712
};

/**
 * eeh_pseries_init - Register platform dependent EEH operations
 *
 * EEH initialization on pseries platform. This function should be
 * called before any EEH related functions.
 */
713
static int __init eeh_pseries_init(void)
714
{
715
	int ret;
716 717 718 719 720 721 722 723 724

	ret = eeh_ops_register(&pseries_eeh_ops);
	if (!ret)
		pr_info("EEH: pSeries platform initialized\n");
	else
		pr_info("EEH: pSeries platform initialization failure (%d)\n",
			ret);

	return ret;
725
}
726
machine_early_initcall(pseries, eeh_pseries_init);