eeh.c 46.9 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
2
3
4
 * Copyright IBM Corporation 2001, 2005, 2006
 * Copyright Dave Engebretsen & Todd Inglett 2001
 * Copyright Linas Vepstas 2005, 2006
5
 * Copyright 2001-2012 IBM Corporation.
6
 *
Linus Torvalds's avatar
Linus Torvalds committed
7
8
9
10
 * 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.
11
 *
Linus Torvalds's avatar
Linus Torvalds committed
12
13
14
15
 * 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.
16
 *
Linus Torvalds's avatar
Linus Torvalds committed
17
18
19
 * 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
20
21
 *
 * Please address comments and feedback to Linas Vepstas <linas@austin.ibm.com>
Linus Torvalds's avatar
Linus Torvalds committed
22
23
 */

24
#include <linux/delay.h>
Gavin Shan's avatar
Gavin Shan committed
25
#include <linux/debugfs.h>
26
#include <linux/sched.h>
Linus Torvalds's avatar
Linus Torvalds committed
27
28
29
#include <linux/init.h>
#include <linux/list.h>
#include <linux/pci.h>
30
#include <linux/iommu.h>
Linus Torvalds's avatar
Linus Torvalds committed
31
32
#include <linux/proc_fs.h>
#include <linux/rbtree.h>
33
#include <linux/reboot.h>
Linus Torvalds's avatar
Linus Torvalds committed
34
35
#include <linux/seq_file.h>
#include <linux/spinlock.h>
36
#include <linux/export.h>
37
38
#include <linux/of.h>

Arun Sharma's avatar
Arun Sharma committed
39
#include <linux/atomic.h>
40
#include <asm/debug.h>
Linus Torvalds's avatar
Linus Torvalds committed
41
#include <asm/eeh.h>
42
#include <asm/eeh_event.h>
Linus Torvalds's avatar
Linus Torvalds committed
43
#include <asm/io.h>
44
#include <asm/iommu.h>
Linus Torvalds's avatar
Linus Torvalds committed
45
#include <asm/machdep.h>
46
#include <asm/ppc-pci.h>
Linus Torvalds's avatar
Linus Torvalds committed
47
48
49
50
#include <asm/rtas.h>


/** Overview:
51
 *  EEH, or "Enhanced Error Handling" is a PCI bridge technology for
Linus Torvalds's avatar
Linus Torvalds committed
52
53
54
55
56
57
58
59
60
61
 *  dealing with PCI bus errors that can't be dealt with within the
 *  usual PCI framework, except by check-stopping the CPU.  Systems
 *  that are designed for high-availability/reliability cannot afford
 *  to crash due to a "mere" PCI error, thus the need for EEH.
 *  An EEH-capable bridge operates by converting a detected error
 *  into a "slot freeze", taking the PCI adapter off-line, making
 *  the slot behave, from the OS'es point of view, as if the slot
 *  were "empty": all reads return 0xff's and all writes are silently
 *  ignored.  EEH slot isolation events can be triggered by parity
 *  errors on the address or data busses (e.g. during posted writes),
62
63
 *  which in turn might be caused by low voltage on the bus, dust,
 *  vibration, humidity, radioactivity or plain-old failed hardware.
Linus Torvalds's avatar
Linus Torvalds committed
64
65
66
67
68
69
70
71
72
73
74
 *
 *  Note, however, that one of the leading causes of EEH slot
 *  freeze events are buggy device drivers, buggy device microcode,
 *  or buggy device hardware.  This is because any attempt by the
 *  device to bus-master data to a memory address that is not
 *  assigned to the device will trigger a slot freeze.   (The idea
 *  is to prevent devices-gone-wild from corrupting system memory).
 *  Buggy hardware/drivers will have a miserable time co-existing
 *  with EEH.
 *
 *  Ideally, a PCI device driver, when suspecting that an isolation
Lucas De Marchi's avatar
Lucas De Marchi committed
75
 *  event has occurred (e.g. by reading 0xff's), will then ask EEH
Linus Torvalds's avatar
Linus Torvalds committed
76
77
78
79
80
81
82
83
 *  whether this is the case, and then take appropriate steps to
 *  reset the PCI slot, the PCI device, and then resume operations.
 *  However, until that day,  the checking is done here, with the
 *  eeh_check_failure() routine embedded in the MMIO macros.  If
 *  the slot is found to be isolated, an "EEH Event" is synthesized
 *  and sent out for processing.
 */

84
/* If a device driver keeps reading an MMIO register in an interrupt
85
86
87
 * handler after a slot isolation event, it might be broken.
 * This sets the threshold for how many read attempts we allow
 * before printing an error message.
Linus Torvalds's avatar
Linus Torvalds committed
88
 */
89
#define EEH_MAX_FAILS	2100000
Linus Torvalds's avatar
Linus Torvalds committed
90

91
/* Time to wait for a PCI slot to report status, in milliseconds */
92
#define PCI_BUS_RESET_WAIT_MSEC (5*60*1000)
93

Gavin Shan's avatar
Gavin Shan committed
94
/*
95
96
97
98
99
100
101
102
 * EEH probe mode support, which is part of the flags,
 * is to support multiple platforms for EEH. Some platforms
 * like pSeries do PCI emunation based on device tree.
 * However, other platforms like powernv probe PCI devices
 * from hardware. The flag is used to distinguish that.
 * In addition, struct eeh_ops::probe would be invoked for
 * particular OF node or PCI device so that the corresponding
 * PE would be created there.
Gavin Shan's avatar
Gavin Shan committed
103
 */
104
105
106
int eeh_subsystem_flags;
EXPORT_SYMBOL(eeh_subsystem_flags);

107
108
109
110
111
112
113
/*
 * EEH allowed maximal frozen times. If one particular PE's
 * frozen count in last hour exceeds this limit, the PE will
 * be forced to be offline permanently.
 */
int eeh_max_freezes = 5;

114
115
/* Platform dependent EEH operations */
struct eeh_ops *eeh_ops = NULL;
Gavin Shan's avatar
Gavin Shan committed
116

117
/* Lock to avoid races due to multiple reports of an error */
118
DEFINE_RAW_SPINLOCK(confirm_error_lock);
119
EXPORT_SYMBOL_GPL(confirm_error_lock);
120

121
122
123
/* Lock to protect passed flags */
static DEFINE_MUTEX(eeh_dev_mutex);

124
125
126
127
/* Buffer for reporting pci register dumps. Its here in BSS, and
 * not dynamically alloced, so that it ends up in RMO where RTAS
 * can access it.
 */
128
#define EEH_PCI_REGS_LOG_LEN 8192
129
130
static unsigned char pci_regs_buf[EEH_PCI_REGS_LOG_LEN];

131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * The struct is used to maintain the EEH global statistic
 * information. Besides, the EEH global statistics will be
 * exported to user space through procfs
 */
struct eeh_stats {
	u64 no_device;		/* PCI device not found		*/
	u64 no_dn;		/* OF node not found		*/
	u64 no_cfg_addr;	/* Config address not found	*/
	u64 ignored_check;	/* EEH check skipped		*/
	u64 total_mmio_ffs;	/* Total EEH checks		*/
	u64 false_positives;	/* Unnecessary EEH checks	*/
	u64 slot_resets;	/* PE reset			*/
};

static struct eeh_stats eeh_stats;
Linus Torvalds's avatar
Linus Torvalds committed
147

Gavin Shan's avatar
Gavin Shan committed
148
149
150
static int __init eeh_setup(char *str)
{
	if (!strcmp(str, "off"))
151
		eeh_add_flag(EEH_FORCE_DISABLED);
152
153
	else if (!strcmp(str, "early_log"))
		eeh_add_flag(EEH_EARLY_DUMP_LOG);
Gavin Shan's avatar
Gavin Shan committed
154
155
156
157
158

	return 1;
}
__setup("eeh=", eeh_setup);

159
160
161
162
/*
 * This routine captures assorted PCI configuration space data
 * for the indicated PCI device, and puts them into a buffer
 * for RTAS error logging.
163
 */
164
static size_t eeh_dump_dev_log(struct eeh_dev *edev, char *buf, size_t len)
165
{
166
	struct pci_dn *pdn = eeh_dev_to_pdn(edev);
167
	u32 cfg;
168
	int cap, i;
169
170
	int n = 0, l = 0;
	char buffer[128];
171

172
	n += scnprintf(buf+n, len-n, "%04x:%02x:%02x.%01x\n",
173
174
		       edev->phb->global_number, pdn->busno,
		       PCI_SLOT(pdn->devfn), PCI_FUNC(pdn->devfn));
175
	pr_warn("EEH: of node=%04x:%02x:%02x.%01x\n",
176
177
		edev->phb->global_number, pdn->busno,
		PCI_SLOT(pdn->devfn), PCI_FUNC(pdn->devfn));
178

179
	eeh_ops->read_config(pdn, PCI_VENDOR_ID, 4, &cfg);
180
	n += scnprintf(buf+n, len-n, "dev/vend:%08x\n", cfg);
181
	pr_warn("EEH: PCI device/vendor: %08x\n", cfg);
182

183
	eeh_ops->read_config(pdn, PCI_COMMAND, 4, &cfg);
184
	n += scnprintf(buf+n, len-n, "cmd/stat:%x\n", cfg);
185
	pr_warn("EEH: PCI cmd/status register: %08x\n", cfg);
186

187
	/* Gather bridge-specific registers */
188
	if (edev->mode & EEH_DEV_BRIDGE) {
189
		eeh_ops->read_config(pdn, PCI_SEC_STATUS, 2, &cfg);
190
		n += scnprintf(buf+n, len-n, "sec stat:%x\n", cfg);
191
		pr_warn("EEH: Bridge secondary status: %04x\n", cfg);
192

193
		eeh_ops->read_config(pdn, PCI_BRIDGE_CONTROL, 2, &cfg);
194
		n += scnprintf(buf+n, len-n, "brdg ctl:%x\n", cfg);
195
		pr_warn("EEH: Bridge control: %04x\n", cfg);
196
197
	}

198
	/* Dump out the PCI-X command and status regs */
199
	cap = edev->pcix_cap;
200
	if (cap) {
201
		eeh_ops->read_config(pdn, cap, 4, &cfg);
202
		n += scnprintf(buf+n, len-n, "pcix-cmd:%x\n", cfg);
203
		pr_warn("EEH: PCI-X cmd: %08x\n", cfg);
204

205
		eeh_ops->read_config(pdn, cap+4, 4, &cfg);
206
		n += scnprintf(buf+n, len-n, "pcix-stat:%x\n", cfg);
207
		pr_warn("EEH: PCI-X status: %08x\n", cfg);
208
209
	}

210
211
212
	/* If PCI-E capable, dump PCI-E cap 10 */
	cap = edev->pcie_cap;
	if (cap) {
213
		n += scnprintf(buf+n, len-n, "pci-e cap10:\n");
214
		pr_warn("EEH: PCI-E capabilities and status follow:\n");
215
216

		for (i=0; i<=8; i++) {
217
			eeh_ops->read_config(pdn, cap+4*i, 4, &cfg);
218
			n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
219
220
221
222
223
224
225
226
227
228
229
230
231

			if ((i % 4) == 0) {
				if (i != 0)
					pr_warn("%s\n", buffer);

				l = scnprintf(buffer, sizeof(buffer),
					      "EEH: PCI-E %02x: %08x ",
					      4*i, cfg);
			} else {
				l += scnprintf(buffer+l, sizeof(buffer)-l,
					       "%08x ", cfg);
			}

232
		}
233
234

		pr_warn("%s\n", buffer);
235
	}
236

237
238
239
240
241
242
	/* If AER capable, dump it */
	cap = edev->aer_cap;
	if (cap) {
		n += scnprintf(buf+n, len-n, "pci-e AER:\n");
		pr_warn("EEH: PCI-E AER capability register set follows:\n");

243
		for (i=0; i<=13; i++) {
244
			eeh_ops->read_config(pdn, cap+4*i, 4, &cfg);
245
			n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
246
247
248
249
250
251
252
253
254
255
256
257

			if ((i % 4) == 0) {
				if (i != 0)
					pr_warn("%s\n", buffer);

				l = scnprintf(buffer, sizeof(buffer),
					      "EEH: PCI-E AER %02x: %08x ",
					      4*i, cfg);
			} else {
				l += scnprintf(buffer+l, sizeof(buffer)-l,
					       "%08x ", cfg);
			}
258
		}
259
260

		pr_warn("%s\n", buffer);
261
	}
262

263
264
265
	return n;
}

266
267
268
269
270
271
272
273
274
275
276
277
278
static void *eeh_dump_pe_log(void *data, void *flag)
{
	struct eeh_pe *pe = data;
	struct eeh_dev *edev, *tmp;
	size_t *plen = flag;

	eeh_pe_for_each_dev(pe, edev, tmp)
		*plen += eeh_dump_dev_log(edev, pci_regs_buf + *plen,
					  EEH_PCI_REGS_LOG_LEN - *plen);

	return NULL;
}

279
280
/**
 * eeh_slot_error_detail - Generate combined log including driver log and error log
281
 * @pe: EEH PE
282
283
284
285
286
287
288
 * @severity: temporary or permanent error log
 *
 * This routine should be called to generate the combined log, which
 * is comprised of driver log and error log. The driver log is figured
 * out from the config space of the corresponding PCI device, while
 * the error log is fetched through platform dependent function call.
 */
289
void eeh_slot_error_detail(struct eeh_pe *pe, int severity)
290
291
292
{
	size_t loglen = 0;

293
294
295
296
297
	/*
	 * When the PHB is fenced or dead, it's pointless to collect
	 * the data from PCI config space because it should return
	 * 0xFF's. For ER, we still retrieve the data from the PCI
	 * config space.
298
299
300
	 *
	 * For pHyp, we have to enable IO for log retrieval. Otherwise,
	 * 0xFF's is always returned from PCI config space.
301
302
303
304
305
306
307
	 *
	 * When the @severity is EEH_LOG_PERM, the PE is going to be
	 * removed. Prior to that, the drivers for devices included in
	 * the PE will be closed. The drivers rely on working IO path
	 * to bring the devices to quiet state. Otherwise, PCI traffic
	 * from those devices after they are removed is like to cause
	 * another unexpected EEH error.
308
	 */
309
	if (!(pe->type & EEH_PE_PHB)) {
310
311
		if (eeh_has_flag(EEH_ENABLE_IO_FOR_LOG) ||
		    severity == EEH_LOG_PERM)
312
			eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
313
314
315
316
317
318
319
320
321
322
323
324
325

		/*
		 * The config space of some PCI devices can't be accessed
		 * when their PEs are in frozen state. Otherwise, fenced
		 * PHB might be seen. Those PEs are identified with flag
		 * EEH_PE_CFG_RESTRICTED, indicating EEH_PE_CFG_BLOCKED
		 * is set automatically when the PE is put to EEH_PE_ISOLATED.
		 *
		 * Restoring BARs possibly triggers PCI config access in
		 * (OPAL) firmware and then causes fenced PHB. If the
		 * PCI config is blocked with flag EEH_PE_CFG_BLOCKED, it's
		 * pointless to restore BARs and dump config space.
		 */
326
		eeh_ops->configure_bridge(pe);
327
328
		if (!(pe->state & EEH_PE_CFG_BLOCKED)) {
			eeh_pe_restore_bars(pe);
329

330
331
332
			pci_regs_buf[0] = 0;
			eeh_pe_traverse(pe, eeh_dump_pe_log, &loglen);
		}
333
	}
334
335

	eeh_ops->get_log(pe, severity, pci_regs_buf, loglen);
336
337
}

Linus Torvalds's avatar
Linus Torvalds committed
338
/**
339
340
341
342
343
 * eeh_token_to_phys - Convert EEH address token to phys address
 * @token: I/O token, should be address in the form 0xA....
 *
 * This routine should be called to convert virtual I/O address
 * to physical one.
Linus Torvalds's avatar
Linus Torvalds committed
344
345
346
347
348
 */
static inline unsigned long eeh_token_to_phys(unsigned long token)
{
	pte_t *ptep;
	unsigned long pa;
349
	int hugepage_shift;
Linus Torvalds's avatar
Linus Torvalds committed
350

351
	/*
352
353
354
	 * We won't find hugepages here(this is iomem). Hence we are not
	 * worried about _PAGE_SPLITTING/collapse. Also we will not hit
	 * page table free, because of init_mm.
355
	 */
356
357
	ptep = __find_linux_pte_or_hugepte(init_mm.pgd, token,
					   NULL, &hugepage_shift);
Linus Torvalds's avatar
Linus Torvalds committed
358
359
	if (!ptep)
		return token;
360
	WARN_ON(hugepage_shift);
Linus Torvalds's avatar
Linus Torvalds committed
361
362
363
364
365
	pa = pte_pfn(*ptep) << PAGE_SHIFT;

	return pa | (token & (PAGE_SIZE-1));
}

366
367
368
369
370
371
372
373
374
375
376
/*
 * On PowerNV platform, we might already have fenced PHB there.
 * For that case, it's meaningless to recover frozen PE. Intead,
 * We have to handle fenced PHB firstly.
 */
static int eeh_phb_check_failure(struct eeh_pe *pe)
{
	struct eeh_pe *phb_pe;
	unsigned long flags;
	int ret;

377
	if (!eeh_has_flag(EEH_PROBE_MODE_DEV))
378
379
380
381
382
		return -EPERM;

	/* Find the PHB PE */
	phb_pe = eeh_phb_pe_get(pe->phb);
	if (!phb_pe) {
383
		pr_warn("%s Can't find PE for PHB#%x\n",
384
			__func__, pe->phb->global_number);
385
386
387
388
389
		return -EEXIST;
	}

	/* If the PHB has been in problematic state */
	eeh_serialize_lock(&flags);
390
	if (phb_pe->state & EEH_PE_ISOLATED) {
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
		ret = 0;
		goto out;
	}

	/* Check PHB state */
	ret = eeh_ops->get_state(phb_pe, NULL);
	if ((ret < 0) ||
	    (ret == EEH_STATE_NOT_SUPPORT) ||
	    (ret & (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) ==
	    (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) {
		ret = 0;
		goto out;
	}

	/* Isolate the PHB and send event */
	eeh_pe_state_mark(phb_pe, EEH_PE_ISOLATED);
	eeh_serialize_unlock(flags);

409
410
	pr_err("EEH: PHB#%x failure detected, location: %s\n",
		phb_pe->phb->global_number, eeh_pe_loc_get(phb_pe));
411
	dump_stack();
412
	eeh_send_failure_event(phb_pe);
413
414
415
416
417
418
419

	return 1;
out:
	eeh_serialize_unlock(flags);
	return ret;
}

Linus Torvalds's avatar
Linus Torvalds committed
420
/**
421
422
 * eeh_dev_check_failure - Check if all 1's data is due to EEH slot freeze
 * @edev: eeh device
Linus Torvalds's avatar
Linus Torvalds committed
423
424
425
426
427
428
429
 *
 * Check for an EEH failure for the given device node.  Call this
 * routine if the result of a read was all 0xff's and you want to
 * find out if this is due to an EEH slot freeze.  This routine
 * will query firmware for the EEH status.
 *
 * Returns 0 if there has not been an EEH error; otherwise returns
430
 * a non-zero value and queues up a slot isolation event notification.
Linus Torvalds's avatar
Linus Torvalds committed
431
432
433
 *
 * It is safe to call this routine in an interrupt context.
 */
434
int eeh_dev_check_failure(struct eeh_dev *edev)
Linus Torvalds's avatar
Linus Torvalds committed
435
436
{
	int ret;
437
	int active_flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
Linus Torvalds's avatar
Linus Torvalds committed
438
	unsigned long flags;
439
	struct pci_dn *pdn;
440
	struct pci_dev *dev;
441
	struct eeh_pe *pe, *parent_pe, *phb_pe;
442
	int rc = 0;
443
	const char *location = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
444

445
	eeh_stats.total_mmio_ffs++;
Linus Torvalds's avatar
Linus Torvalds committed
446

447
	if (!eeh_enabled())
Linus Torvalds's avatar
Linus Torvalds committed
448
449
		return 0;

450
	if (!edev) {
451
		eeh_stats.no_dn++;
Linus Torvalds's avatar
Linus Torvalds committed
452
		return 0;
453
	}
454
	dev = eeh_dev_to_pci_dev(edev);
455
	pe = eeh_dev_to_pe(edev);
Linus Torvalds's avatar
Linus Torvalds committed
456
457

	/* Access to IO BARs might get this far and still not want checking. */
458
	if (!pe) {
459
		eeh_stats.ignored_check++;
460
461
		pr_debug("EEH: Ignored check for %s\n",
			eeh_pci_name(dev));
Linus Torvalds's avatar
Linus Torvalds committed
462
463
464
		return 0;
	}

465
	if (!pe->addr && !pe->config_addr) {
466
		eeh_stats.no_cfg_addr++;
Linus Torvalds's avatar
Linus Torvalds committed
467
468
469
		return 0;
	}

470
471
472
473
474
475
476
477
	/*
	 * On PowerNV platform, we might already have fenced PHB
	 * there and we need take care of that firstly.
	 */
	ret = eeh_phb_check_failure(pe);
	if (ret > 0)
		return ret;

478
479
480
481
482
483
484
485
	/*
	 * If the PE isn't owned by us, we shouldn't check the
	 * state. Instead, let the owner handle it if the PE has
	 * been frozen.
	 */
	if (eeh_pe_passed(pe))
		return 0;

486
487
488
489
490
	/* If we already have a pending isolation event for this
	 * slot, we know it's bad already, we don't need to check.
	 * Do this checking under a lock; as multiple PCI devices
	 * in one slot might report errors simultaneously, and we
	 * only want one error recovery routine running.
Linus Torvalds's avatar
Linus Torvalds committed
491
	 */
492
	eeh_serialize_lock(&flags);
493
	rc = 1;
494
495
496
	if (pe->state & EEH_PE_ISOLATED) {
		pe->check_count++;
		if (pe->check_count % EEH_MAX_FAILS == 0) {
497
498
499
			pdn = eeh_dev_to_pdn(edev);
			if (pdn->node)
				location = of_get_property(pdn->node, "ibm,loc-code", NULL);
500
			printk(KERN_ERR "EEH: %d reads ignored for recovering device at "
501
				"location=%s driver=%s pci addr=%s\n",
502
503
				pe->check_count,
				location ? location : "unknown",
504
				eeh_driver_name(dev), eeh_pci_name(dev));
505
			printk(KERN_ERR "EEH: Might be infinite loop in %s driver\n",
506
				eeh_driver_name(dev));
507
			dump_stack();
Linus Torvalds's avatar
Linus Torvalds committed
508
		}
509
		goto dn_unlock;
Linus Torvalds's avatar
Linus Torvalds committed
510
511
512
513
514
515
516
517
518
	}

	/*
	 * Now test for an EEH failure.  This is VERY expensive.
	 * Note that the eeh_config_addr may be a parent device
	 * in the case of a device behind a bridge, or it may be
	 * function zero of a multi-function device.
	 * In any case they must share a common PHB.
	 */
519
	ret = eeh_ops->get_state(pe, NULL);
520

521
	/* Note that config-io to empty slots may fail;
522
	 * they are empty when they don't have children.
523
524
525
	 * We will punt with the following conditions: Failure to get
	 * PE's state, EEH not support and Permanently unavailable
	 * state, PE is in good state.
526
	 */
527
528
	if ((ret < 0) ||
	    (ret == EEH_STATE_NOT_SUPPORT) ||
529
	    ((ret & active_flags) == active_flags)) {
530
		eeh_stats.false_positives++;
531
		pe->false_positives++;
532
533
		rc = 0;
		goto dn_unlock;
534
535
	}

536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
	/*
	 * It should be corner case that the parent PE has been
	 * put into frozen state as well. We should take care
	 * that at first.
	 */
	parent_pe = pe->parent;
	while (parent_pe) {
		/* Hit the ceiling ? */
		if (parent_pe->type & EEH_PE_PHB)
			break;

		/* Frozen parent PE ? */
		ret = eeh_ops->get_state(parent_pe, NULL);
		if (ret > 0 &&
		    (ret & active_flags) != active_flags)
			pe = parent_pe;

		/* Next parent level */
		parent_pe = parent_pe->parent;
	}

557
	eeh_stats.slot_resets++;
Gavin Shan's avatar
Gavin Shan committed
558

559
560
	/* Avoid repeated reports of this failure, including problems
	 * with other functions on this device, and functions under
561
562
	 * bridges.
	 */
563
	eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
564
	eeh_serialize_unlock(flags);
Linus Torvalds's avatar
Linus Torvalds committed
565
566
567

	/* Most EEH events are due to device driver bugs.  Having
	 * a stack trace will help the device-driver authors figure
568
569
	 * out what happened.  So print that out.
	 */
570
571
572
573
574
	phb_pe = eeh_phb_pe_get(pe->phb);
	pr_err("EEH: Frozen PHB#%x-PE#%x detected\n",
	       pe->phb->global_number, pe->addr);
	pr_err("EEH: PE location: %s, PHB location: %s\n",
	       eeh_pe_loc_get(pe), eeh_pe_loc_get(phb_pe));
575
576
	dump_stack();

577
578
	eeh_send_failure_event(pe);

579
580
581
	return 1;

dn_unlock:
582
	eeh_serialize_unlock(flags);
583
	return rc;
Linus Torvalds's avatar
Linus Torvalds committed
584
585
}

586
EXPORT_SYMBOL_GPL(eeh_dev_check_failure);
Linus Torvalds's avatar
Linus Torvalds committed
587
588

/**
589
 * eeh_check_failure - Check if all 1's data is due to EEH slot freeze
590
 * @token: I/O address
Linus Torvalds's avatar
Linus Torvalds committed
591
 *
592
 * Check for an EEH failure at the given I/O address. Call this
Linus Torvalds's avatar
Linus Torvalds committed
593
 * routine if the result of a read was all 0xff's and you want to
594
 * find out if this is due to an EEH slot freeze event. This routine
Linus Torvalds's avatar
Linus Torvalds committed
595
596
597
598
 * will query firmware for the EEH status.
 *
 * Note this routine is safe to call in an interrupt context.
 */
599
int eeh_check_failure(const volatile void __iomem *token)
Linus Torvalds's avatar
Linus Torvalds committed
600
601
{
	unsigned long addr;
602
	struct eeh_dev *edev;
Linus Torvalds's avatar
Linus Torvalds committed
603
604
605

	/* Finding the phys addr + pci device; this is pretty quick. */
	addr = eeh_token_to_phys((unsigned long __force) token);
606
	edev = eeh_addr_cache_get_dev(addr);
607
	if (!edev) {
608
		eeh_stats.no_device++;
609
		return 0;
610
	}
Linus Torvalds's avatar
Linus Torvalds committed
611

612
	return eeh_dev_check_failure(edev);
Linus Torvalds's avatar
Linus Torvalds committed
613
614
615
}
EXPORT_SYMBOL(eeh_check_failure);

616

617
/**
618
 * eeh_pci_enable - Enable MMIO or DMA transfers for this slot
619
 * @pe: EEH PE
620
621
622
623
 *
 * This routine should be called to reenable frozen MMIO or DMA
 * so that it would work correctly again. It's useful while doing
 * recovery or log collection on the indicated device.
624
 */
625
int eeh_pci_enable(struct eeh_pe *pe, int function)
626
{
627
	int active_flag, rc;
628
629
630
631

	/*
	 * pHyp doesn't allow to enable IO or DMA on unfrozen PE.
	 * Also, it's pointless to enable them on unfrozen PE. So
632
	 * we have to check before enabling IO or DMA.
633
	 */
634
635
	switch (function) {
	case EEH_OPT_THAW_MMIO:
636
		active_flag = EEH_STATE_MMIO_ACTIVE | EEH_STATE_MMIO_ENABLED;
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
		break;
	case EEH_OPT_THAW_DMA:
		active_flag = EEH_STATE_DMA_ACTIVE;
		break;
	case EEH_OPT_DISABLE:
	case EEH_OPT_ENABLE:
	case EEH_OPT_FREEZE_PE:
		active_flag = 0;
		break;
	default:
		pr_warn("%s: Invalid function %d\n",
			__func__, function);
		return -EINVAL;
	}

	/*
	 * Check if IO or DMA has been enabled before
	 * enabling them.
	 */
	if (active_flag) {
657
658
659
660
		rc = eeh_ops->get_state(pe, NULL);
		if (rc < 0)
			return rc;

661
662
663
664
665
666
		/* Needn't enable it at all */
		if (rc == EEH_STATE_NOT_SUPPORT)
			return 0;

		/* It's already enabled */
		if (rc & active_flag)
667
668
			return 0;
	}
669

670
671

	/* Issue the request */
672
	rc = eeh_ops->set_option(pe, function);
673
	if (rc)
674
		pr_warn("%s: Unexpected state change %d on "
675
			"PHB#%x-PE#%x, err=%d\n",
676
677
			__func__, function, pe->phb->global_number,
			pe->addr, rc);
678

679
680
681
	/* Check if the request is finished successfully */
	if (active_flag) {
		rc = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
682
		if (rc < 0)
683
			return rc;
684

685
686
		if (rc & active_flag)
			return 0;
687

688
689
		return -EIO;
	}
690

691
692
693
	return rc;
}

694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
static void *eeh_disable_and_save_dev_state(void *data, void *userdata)
{
	struct eeh_dev *edev = data;
	struct pci_dev *pdev = eeh_dev_to_pci_dev(edev);
	struct pci_dev *dev = userdata;

	/*
	 * The caller should have disabled and saved the
	 * state for the specified device
	 */
	if (!pdev || pdev == dev)
		return NULL;

	/* Ensure we have D0 power state */
	pci_set_power_state(pdev, PCI_D0);

	/* Save device state */
	pci_save_state(pdev);

	/*
	 * Disable device to avoid any DMA traffic and
	 * interrupt from the device
	 */
	pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);

	return NULL;
}

static void *eeh_restore_dev_state(void *data, void *userdata)
{
	struct eeh_dev *edev = data;
725
	struct pci_dn *pdn = eeh_dev_to_pdn(edev);
726
727
728
729
730
731
732
	struct pci_dev *pdev = eeh_dev_to_pci_dev(edev);
	struct pci_dev *dev = userdata;

	if (!pdev)
		return NULL;

	/* Apply customization from firmware */
733
734
	if (pdn && eeh_ops->restore_config)
		eeh_ops->restore_config(pdn);
735
736
737

	/* The caller should restore state for the specified device */
	if (pdev != dev)
738
		pci_restore_state(pdev);
739
740
741
742

	return NULL;
}

743
/**
744
 * pcibios_set_pcie_reset_state - Set PCI-E reset state
745
746
 * @dev: pci device struct
 * @state: reset state to enter
747
748
749
 *
 * Return value:
 * 	0 if success
750
 */
751
752
int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
{
Gavin Shan's avatar
Gavin Shan committed
753
	struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
754
	struct eeh_pe *pe = eeh_dev_to_pe(edev);
Gavin Shan's avatar
Gavin Shan committed
755
756
757
758
759
760

	if (!pe) {
		pr_err("%s: No PE found on PCI device %s\n",
			__func__, pci_name(dev));
		return -EINVAL;
	}
761
762
763

	switch (state) {
	case pcie_deassert_reset:
Gavin Shan's avatar
Gavin Shan committed
764
		eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
765
		eeh_unfreeze_pe(pe, false);
766
767
		if (!(pe->type & EEH_PE_VF))
			eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED);
768
		eeh_pe_dev_traverse(pe, eeh_restore_dev_state, dev);
769
		eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
770
771
		break;
	case pcie_hot_reset:
772
		eeh_pe_state_mark_with_cfg(pe, EEH_PE_ISOLATED);
773
774
		eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
		eeh_pe_dev_traverse(pe, eeh_disable_and_save_dev_state, dev);
775
776
		if (!(pe->type & EEH_PE_VF))
			eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
Gavin Shan's avatar
Gavin Shan committed
777
		eeh_ops->reset(pe, EEH_RESET_HOT);
778
779
		break;
	case pcie_warm_reset:
780
		eeh_pe_state_mark_with_cfg(pe, EEH_PE_ISOLATED);
781
782
		eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
		eeh_pe_dev_traverse(pe, eeh_disable_and_save_dev_state, dev);
783
784
		if (!(pe->type & EEH_PE_VF))
			eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
Gavin Shan's avatar
Gavin Shan committed
785
		eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
786
787
		break;
	default:
788
		eeh_pe_state_clear(pe, EEH_PE_ISOLATED | EEH_PE_CFG_BLOCKED);
789
790
791
792
793
794
		return -EINVAL;
	};

	return 0;
}

795
/**
Gavin Shan's avatar
Gavin Shan committed
796
797
798
 * eeh_set_pe_freset - Check the required reset for the indicated device
 * @data: EEH device
 * @flag: return value
799
800
801
802
803
804
 *
 * Each device might have its preferred reset type: fundamental or
 * hot reset. The routine is used to collected the information for
 * the indicated device and its children so that the bunch of the
 * devices could be reset properly.
 */
Gavin Shan's avatar
Gavin Shan committed
805
static void *eeh_set_dev_freset(void *data, void *flag)
806
807
{
	struct pci_dev *dev;
Gavin Shan's avatar
Gavin Shan committed
808
809
	unsigned int *freset = (unsigned int *)flag;
	struct eeh_dev *edev = (struct eeh_dev *)data;
810

Gavin Shan's avatar
Gavin Shan committed
811
	dev = eeh_dev_to_pci_dev(edev);
812
813
814
	if (dev)
		*freset |= dev->needs_freset;

Gavin Shan's avatar
Gavin Shan committed
815
	return NULL;
816
817
818
}

/**
819
 * eeh_pe_reset_full - Complete a full reset process on the indicated PE
Gavin Shan's avatar
Gavin Shan committed
820
 * @pe: EEH PE
821
 *
822
823
824
825
826
827
828
 * This function executes a full reset procedure on a PE, including setting
 * the appropriate flags, performing a fundamental or hot reset, and then
 * deactivating the reset status.  It is designed to be used within the EEH
 * subsystem, as opposed to eeh_pe_reset which is exported to drivers and
 * only performs a single operation at a time.
 *
 * This function will attempt to reset a PE three times before failing.
829
 */
830
int eeh_pe_reset_full(struct eeh_pe *pe)
831
{
832
833
834
	int active_flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
	int reset_state = (EEH_PE_RESET | EEH_PE_CFG_BLOCKED);
	int type = EEH_RESET_HOT;
835
	unsigned int freset = 0;
836
	int i, state, ret;
837

838
839
840
841
	/*
	 * Determine the type of reset to perform - hot or fundamental.
	 * Hot reset is the default operation, unless any device under the
	 * PE requires a fundamental reset.
Gavin Shan's avatar
Gavin Shan committed
842
	 */
Gavin Shan's avatar
Gavin Shan committed
843
	eeh_pe_dev_traverse(pe, eeh_set_dev_freset, &freset);
844
845

	if (freset)
846
		type = EEH_RESET_FUNDAMENTAL;
847

848
849
	/* Mark the PE as in reset state and block config space accesses */
	eeh_pe_state_mark(pe, reset_state);
850

851
	/* Make three attempts at resetting the bus */
852
	for (i = 0; i < 3; i++) {
853
854
855
		ret = eeh_pe_reset(pe, type);
		if (ret)
			break;
856

857
858
859
860
861
		ret = eeh_pe_reset(pe, EEH_RESET_DEACTIVATE);
		if (ret)
			break;

		/* Wait until the PE is in a functioning state */
862
		state = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
863
864
		if ((state & active_flags) == active_flags)
			break;
865

866
		if (state < 0) {
867
			pr_warn("%s: Unrecoverable slot failure on PHB#%x-PE#%x",
Gavin Shan's avatar
Gavin Shan committed
868
				__func__, pe->phb->global_number, pe->addr);
869
			ret = -ENOTRECOVERABLE;
870
			break;
871
		}
872

873
		/* Set error in case this is our last attempt */
874
875
876
		ret = -EIO;
		pr_warn("%s: Failure %d resetting PHB#%x-PE#%x\n (%d)\n",
			__func__, state, pe->phb->global_number, pe->addr, (i + 1));
877
	}
878

879
	eeh_pe_state_clear(pe, reset_state);
880
	return ret;
881
882
}

883
/**
884
 * eeh_save_bars - Save device bars
885
 * @edev: PCI device associated EEH device
886
887
888
 *
 * Save the values of the device bars. Unlike the restore
 * routine, this routine is *not* recursive. This is because
889
 * PCI devices are added individually; but, for the restore,
890
891
 * an entire slot is reset at a time.
 */
Gavin Shan's avatar
Gavin Shan committed
892
void eeh_save_bars(struct eeh_dev *edev)
893
{
894
	struct pci_dn *pdn;
895
896
	int i;

897
898
	pdn = eeh_dev_to_pdn(edev);
	if (!pdn)
899
		return;
Gavin Shan's avatar
Gavin Shan committed
900

901
	for (i = 0; i < 16; i++)
902
		eeh_ops->read_config(pdn, i * 4, 4, &edev->config_space[i]);
903
904
905
906
907
908
909
910
911

	/*
	 * For PCI bridges including root port, we need enable bus
	 * master explicitly. Otherwise, it can't fetch IODA table
	 * entries correctly. So we cache the bit in advance so that
	 * we can restore it after reset, either PHB range or PE range.
	 */
	if (edev->mode & EEH_DEV_BRIDGE)
		edev->config_space[1] |= PCI_COMMAND_MASTER;
912
913
}

914
915
916
917
918
919
920
921
922
923
924
/**
 * eeh_ops_register - Register platform dependent EEH operations
 * @ops: platform dependent EEH operations
 *
 * Register the platform dependent EEH operation callback
 * functions. The platform should call this function before
 * any other EEH operations.
 */
int __init eeh_ops_register(struct eeh_ops *ops)
{
	if (!ops->name) {
925
		pr_warn("%s: Invalid EEH ops name for %p\n",
926
927
928
929
930
			__func__, ops);
		return -EINVAL;
	}

	if (eeh_ops && eeh_ops != ops) {
931
		pr_warn("%s: EEH ops of platform %s already existing (%s)\n",
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
			__func__, eeh_ops->name, ops->name);
		return -EEXIST;
	}

	eeh_ops = ops;

	return 0;
}

/**
 * eeh_ops_unregister - Unreigster platform dependent EEH operations
 * @name: name of EEH platform operations
 *
 * Unregister the platform dependent EEH operation callback
 * functions.
 */
int __exit eeh_ops_unregister(const char *name)
{
	if (!name || !strlen(name)) {
951
		pr_warn("%s: Invalid EEH ops name\n",
952
953
954
955
956
957
958
959
960
961
962
963
			__func__);
		return -EINVAL;
	}

	if (eeh_ops && !strcmp(eeh_ops->name, name)) {
		eeh_ops = NULL;
		return 0;
	}

	return -EEXIST;
}

964
965
966
static int eeh_reboot_notifier(struct notifier_block *nb,
			       unsigned long action, void *unused)
{
967
	eeh_clear_flag(EEH_ENABLED);
968
969
970
971
972
973
974
	return NOTIFY_DONE;
}

static struct notifier_block eeh_reboot_nb = {
	.notifier_call = eeh_reboot_notifier,
};

975
976
977
/**
 * eeh_init - EEH initialization
 *
Linus Torvalds's avatar
Linus Torvalds committed
978
979
980
981
982
983
984
985
986
987
988
989
 * Initialize EEH by trying to enable it for all of the adapters in the system.
 * As a side effect we can determine here if eeh is supported at all.
 * Note that we leave EEH on so failed config cycles won't cause a machine
 * check.  If a user turns off EEH for a particular adapter they are really
 * telling Linux to ignore errors.  Some hardware (e.g. POWER5) won't
 * grant access to a slot if EEH isn't enabled, and so we always enable
 * EEH for all slots/all devices.
 *
 * The eeh-force-off option disables EEH checking globally, for all slots.
 * Even if force-off is set, the EEH hardware is still enabled, so that
 * newer systems can boot.
 */
Gavin Shan's avatar
Gavin Shan committed
990
int eeh_init(void)
Linus Torvalds's avatar
Linus Torvalds committed
991
{
992
	struct pci_controller *hose, *tmp;
Gavin Shan's avatar
Gavin Shan committed
993
	struct pci_dn *pdn;
994
995
996
997
998
999
1000
1001
1002
1003
1004
	static int cnt = 0;
	int ret = 0;

	/*
	 * We have to delay the initialization on PowerNV after
	 * the PCI hierarchy tree has been built because the PEs
	 * are figured out based on PCI devices instead of device
	 * tree nodes
	 */
	if (machine_is(powernv) && cnt++ <= 0)
		return ret;
1005

1006
1007
1008
1009
1010
1011
1012
1013
	/* Register reboot notifier */
	ret = register_reboot_notifier(&eeh_reboot_nb);
	if (ret) {
		pr_warn("%s: Failed to register notifier (%d)\n",
			__func__, ret);
		return ret;
	}

1014
1015
	/* call platform initialization function */
	if (!eeh_ops) {
1016
		pr_warn("%s: Platform EEH operation not found\n",
1017
			__func__);
1018
		return -EEXIST;
1019
	} else if ((ret = eeh_ops->init()))
1020
		return ret;
Linus Torvalds's avatar
Linus Torvalds committed
1021

1022
1023
1024
1025
1026
	/* Initialize EEH event */
	ret = eeh_event_init();
	if (ret)
		return ret;

1027
	/* Enable EEH for all adapters */
Gavin Shan's avatar
Gavin Shan committed
1028
1029
1030
	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
		pdn = hose->pci_data;
		traverse_pci_dn(pdn, eeh_ops->probe, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
1031
1032
	}

1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
	/*
	 * Call platform post-initialization. Actually, It's good chance
	 * to inform platform that EEH is ready to supply service if the
	 * I/O cache stuff has been built up.
	 */
	if (eeh_ops->post_init) {
		ret = eeh_ops->post_init();
		if (ret)
			return ret;
	}

1044
	if (eeh_enabled())
Gavin Shan's avatar
Gavin Shan committed
1045
		pr_info("EEH: PCI Enhanced I/O Error Handling Enabled\n");
Linus Torvalds's avatar
Linus Torvalds committed
1046
	else
1047
		pr_info("EEH: No capable adapters found\n");
1048
1049

	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
1050
1051
}

1052
1053
core_initcall_sync(eeh_init);