powerpc/eeh: Device bars restore based on PE
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / powerpc / platforms / pseries / eeh.c
CommitLineData
1da177e4 1/*
3c8c90ab
LV
2 * Copyright IBM Corporation 2001, 2005, 2006
3 * Copyright Dave Engebretsen & Todd Inglett 2001
4 * Copyright Linas Vepstas 2005, 2006
cb3bc9d0 5 * Copyright 2001-2012 IBM Corporation.
69376502 6 *
1da177e4
LT
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
69376502 11 *
1da177e4
LT
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
69376502 16 *
1da177e4
LT
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
3c8c90ab
LV
20 *
21 * Please address comments and feedback to Linas Vepstas <linas@austin.ibm.com>
1da177e4
LT
22 */
23
6dee3fb9 24#include <linux/delay.h>
cb3bc9d0 25#include <linux/sched.h>
1da177e4
LT
26#include <linux/init.h>
27#include <linux/list.h>
1da177e4
LT
28#include <linux/pci.h>
29#include <linux/proc_fs.h>
30#include <linux/rbtree.h>
31#include <linux/seq_file.h>
32#include <linux/spinlock.h>
66b15db6 33#include <linux/export.h>
acaa6176
SR
34#include <linux/of.h>
35
60063497 36#include <linux/atomic.h>
1da177e4 37#include <asm/eeh.h>
172ca926 38#include <asm/eeh_event.h>
1da177e4
LT
39#include <asm/io.h>
40#include <asm/machdep.h>
172ca926 41#include <asm/ppc-pci.h>
1da177e4 42#include <asm/rtas.h>
1da177e4 43
1da177e4
LT
44
45/** Overview:
46 * EEH, or "Extended Error Handling" is a PCI bridge technology for
47 * dealing with PCI bus errors that can't be dealt with within the
48 * usual PCI framework, except by check-stopping the CPU. Systems
49 * that are designed for high-availability/reliability cannot afford
50 * to crash due to a "mere" PCI error, thus the need for EEH.
51 * An EEH-capable bridge operates by converting a detected error
52 * into a "slot freeze", taking the PCI adapter off-line, making
53 * the slot behave, from the OS'es point of view, as if the slot
54 * were "empty": all reads return 0xff's and all writes are silently
55 * ignored. EEH slot isolation events can be triggered by parity
56 * errors on the address or data busses (e.g. during posted writes),
69376502
LV
57 * which in turn might be caused by low voltage on the bus, dust,
58 * vibration, humidity, radioactivity or plain-old failed hardware.
1da177e4
LT
59 *
60 * Note, however, that one of the leading causes of EEH slot
61 * freeze events are buggy device drivers, buggy device microcode,
62 * or buggy device hardware. This is because any attempt by the
63 * device to bus-master data to a memory address that is not
64 * assigned to the device will trigger a slot freeze. (The idea
65 * is to prevent devices-gone-wild from corrupting system memory).
66 * Buggy hardware/drivers will have a miserable time co-existing
67 * with EEH.
68 *
69 * Ideally, a PCI device driver, when suspecting that an isolation
25985edc 70 * event has occurred (e.g. by reading 0xff's), will then ask EEH
1da177e4
LT
71 * whether this is the case, and then take appropriate steps to
72 * reset the PCI slot, the PCI device, and then resume operations.
73 * However, until that day, the checking is done here, with the
74 * eeh_check_failure() routine embedded in the MMIO macros. If
75 * the slot is found to be isolated, an "EEH Event" is synthesized
76 * and sent out for processing.
77 */
78
5c1344e9 79/* If a device driver keeps reading an MMIO register in an interrupt
f36c5227
MM
80 * handler after a slot isolation event, it might be broken.
81 * This sets the threshold for how many read attempts we allow
82 * before printing an error message.
1da177e4 83 */
2fd30be8 84#define EEH_MAX_FAILS 2100000
1da177e4 85
17213c3b 86/* Time to wait for a PCI slot to report status, in milliseconds */
9c547768
LV
87#define PCI_BUS_RESET_WAIT_MSEC (60*1000)
88
aa1e6374
GS
89/* Platform dependent EEH operations */
90struct eeh_ops *eeh_ops = NULL;
91
1e28a7dd
DW
92int eeh_subsystem_enabled;
93EXPORT_SYMBOL(eeh_subsystem_enabled);
1da177e4 94
646a8499
GS
95/* Global EEH mutex */
96DEFINE_MUTEX(eeh_mutex);
97
fd761fd8 98/* Lock to avoid races due to multiple reports of an error */
3d372628 99static DEFINE_RAW_SPINLOCK(confirm_error_lock);
fd761fd8 100
17213c3b
LV
101/* Buffer for reporting pci register dumps. Its here in BSS, and
102 * not dynamically alloced, so that it ends up in RMO where RTAS
103 * can access it.
104 */
d99bb1db
LV
105#define EEH_PCI_REGS_LOG_LEN 4096
106static unsigned char pci_regs_buf[EEH_PCI_REGS_LOG_LEN];
107
e575f8db
GS
108/*
109 * The struct is used to maintain the EEH global statistic
110 * information. Besides, the EEH global statistics will be
111 * exported to user space through procfs
112 */
113struct eeh_stats {
114 u64 no_device; /* PCI device not found */
115 u64 no_dn; /* OF node not found */
116 u64 no_cfg_addr; /* Config address not found */
117 u64 ignored_check; /* EEH check skipped */
118 u64 total_mmio_ffs; /* Total EEH checks */
119 u64 false_positives; /* Unnecessary EEH checks */
120 u64 slot_resets; /* PE reset */
121};
122
123static struct eeh_stats eeh_stats;
1da177e4 124
7684b40c
LV
125#define IS_BRIDGE(class_code) (((class_code)<<16) == PCI_BASE_CLASS_BRIDGE)
126
d99bb1db 127/**
cce4b2d2 128 * eeh_gather_pci_data - Copy assorted PCI config space registers to buff
f631acd3 129 * @edev: device to report data for
d99bb1db
LV
130 * @buf: point to buffer in which to log
131 * @len: amount of room in buffer
132 *
133 * This routine captures assorted PCI configuration space data,
134 * and puts them into a buffer for RTAS error logging.
135 */
f631acd3 136static size_t eeh_gather_pci_data(struct eeh_dev *edev, char * buf, size_t len)
d99bb1db 137{
f631acd3
GS
138 struct device_node *dn = eeh_dev_to_of_node(edev);
139 struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
d99bb1db 140 u32 cfg;
fcf9892b 141 int cap, i;
d99bb1db
LV
142 int n = 0;
143
f631acd3
GS
144 n += scnprintf(buf+n, len-n, "%s\n", dn->full_name);
145 printk(KERN_WARNING "EEH: of node=%s\n", dn->full_name);
fcf9892b 146
3780444c 147 eeh_ops->read_config(dn, PCI_VENDOR_ID, 4, &cfg);
fcf9892b
LV
148 n += scnprintf(buf+n, len-n, "dev/vend:%08x\n", cfg);
149 printk(KERN_WARNING "EEH: PCI device/vendor: %08x\n", cfg);
150
3780444c 151 eeh_ops->read_config(dn, PCI_COMMAND, 4, &cfg);
d99bb1db 152 n += scnprintf(buf+n, len-n, "cmd/stat:%x\n", cfg);
fcf9892b
LV
153 printk(KERN_WARNING "EEH: PCI cmd/status register: %08x\n", cfg);
154
b37ceefe
LV
155 if (!dev) {
156 printk(KERN_WARNING "EEH: no PCI device for this of node\n");
157 return n;
158 }
159
0b9369f4
LV
160 /* Gather bridge-specific registers */
161 if (dev->class >> 16 == PCI_BASE_CLASS_BRIDGE) {
3780444c 162 eeh_ops->read_config(dn, PCI_SEC_STATUS, 2, &cfg);
0b9369f4
LV
163 n += scnprintf(buf+n, len-n, "sec stat:%x\n", cfg);
164 printk(KERN_WARNING "EEH: Bridge secondary status: %04x\n", cfg);
165
3780444c 166 eeh_ops->read_config(dn, PCI_BRIDGE_CONTROL, 2, &cfg);
0b9369f4
LV
167 n += scnprintf(buf+n, len-n, "brdg ctl:%x\n", cfg);
168 printk(KERN_WARNING "EEH: Bridge control: %04x\n", cfg);
169 }
170
fcf9892b 171 /* Dump out the PCI-X command and status regs */
b37ceefe 172 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
fcf9892b 173 if (cap) {
3780444c 174 eeh_ops->read_config(dn, cap, 4, &cfg);
fcf9892b
LV
175 n += scnprintf(buf+n, len-n, "pcix-cmd:%x\n", cfg);
176 printk(KERN_WARNING "EEH: PCI-X cmd: %08x\n", cfg);
177
3780444c 178 eeh_ops->read_config(dn, cap+4, 4, &cfg);
fcf9892b
LV
179 n += scnprintf(buf+n, len-n, "pcix-stat:%x\n", cfg);
180 printk(KERN_WARNING "EEH: PCI-X status: %08x\n", cfg);
181 }
182
183 /* If PCI-E capable, dump PCI-E cap 10, and the AER */
b37ceefe 184 cap = pci_find_capability(dev, PCI_CAP_ID_EXP);
fcf9892b
LV
185 if (cap) {
186 n += scnprintf(buf+n, len-n, "pci-e cap10:\n");
187 printk(KERN_WARNING
188 "EEH: PCI-E capabilities and status follow:\n");
189
190 for (i=0; i<=8; i++) {
3780444c 191 eeh_ops->read_config(dn, cap+4*i, 4, &cfg);
fcf9892b
LV
192 n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
193 printk(KERN_WARNING "EEH: PCI-E %02x: %08x\n", i, cfg);
194 }
195
b37ceefe 196 cap = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
fcf9892b
LV
197 if (cap) {
198 n += scnprintf(buf+n, len-n, "pci-e AER:\n");
199 printk(KERN_WARNING
200 "EEH: PCI-E AER capability register set follows:\n");
201
202 for (i=0; i<14; i++) {
3780444c 203 eeh_ops->read_config(dn, cap+4*i, 4, &cfg);
fcf9892b
LV
204 n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
205 printk(KERN_WARNING "EEH: PCI-E AER %02x: %08x\n", i, cfg);
206 }
207 }
208 }
0b9369f4
LV
209
210 /* Gather status on devices under the bridge */
211 if (dev->class >> 16 == PCI_BASE_CLASS_BRIDGE) {
f631acd3 212 struct device_node *child;
acaa6176 213
f631acd3
GS
214 for_each_child_of_node(dn, child) {
215 if (of_node_to_eeh_dev(child))
216 n += eeh_gather_pci_data(of_node_to_eeh_dev(child), buf+n, len-n);
0b9369f4
LV
217 }
218 }
219
d99bb1db
LV
220 return n;
221}
222
cb3bc9d0
GS
223/**
224 * eeh_slot_error_detail - Generate combined log including driver log and error log
f631acd3 225 * @edev: device to report error log for
cb3bc9d0
GS
226 * @severity: temporary or permanent error log
227 *
228 * This routine should be called to generate the combined log, which
229 * is comprised of driver log and error log. The driver log is figured
230 * out from the config space of the corresponding PCI device, while
231 * the error log is fetched through platform dependent function call.
232 */
f631acd3 233void eeh_slot_error_detail(struct eeh_dev *edev, int severity)
d99bb1db
LV
234{
235 size_t loglen = 0;
17213c3b 236 pci_regs_buf[0] = 0;
d99bb1db 237
f631acd3
GS
238 eeh_pci_enable(edev, EEH_OPT_THAW_MMIO);
239 eeh_ops->configure_bridge(eeh_dev_to_of_node(edev));
240 eeh_restore_bars(edev);
241 loglen = eeh_gather_pci_data(edev, pci_regs_buf, EEH_PCI_REGS_LOG_LEN);
d99bb1db 242
f631acd3 243 eeh_ops->get_log(eeh_dev_to_of_node(edev), severity, pci_regs_buf, loglen);
d99bb1db
LV
244}
245
1da177e4 246/**
cb3bc9d0
GS
247 * eeh_token_to_phys - Convert EEH address token to phys address
248 * @token: I/O token, should be address in the form 0xA....
249 *
250 * This routine should be called to convert virtual I/O address
251 * to physical one.
1da177e4
LT
252 */
253static inline unsigned long eeh_token_to_phys(unsigned long token)
254{
255 pte_t *ptep;
256 unsigned long pa;
257
20cee16c 258 ptep = find_linux_pte(init_mm.pgd, token);
1da177e4
LT
259 if (!ptep)
260 return token;
261 pa = pte_pfn(*ptep) << PAGE_SHIFT;
262
263 return pa | (token & (PAGE_SIZE-1));
264}
265
266/**
cb3bc9d0
GS
267 * eeh_dn_check_failure - Check if all 1's data is due to EEH slot freeze
268 * @dn: device node
269 * @dev: pci device, if known
1da177e4
LT
270 *
271 * Check for an EEH failure for the given device node. Call this
272 * routine if the result of a read was all 0xff's and you want to
273 * find out if this is due to an EEH slot freeze. This routine
274 * will query firmware for the EEH status.
275 *
276 * Returns 0 if there has not been an EEH error; otherwise returns
69376502 277 * a non-zero value and queues up a slot isolation event notification.
1da177e4
LT
278 *
279 * It is safe to call this routine in an interrupt context.
280 */
281int eeh_dn_check_failure(struct device_node *dn, struct pci_dev *dev)
282{
283 int ret;
1da177e4 284 unsigned long flags;
66523d9f 285 struct eeh_pe *pe;
f631acd3 286 struct eeh_dev *edev;
fd761fd8 287 int rc = 0;
f36c5227 288 const char *location;
1da177e4 289
e575f8db 290 eeh_stats.total_mmio_ffs++;
1da177e4
LT
291
292 if (!eeh_subsystem_enabled)
293 return 0;
294
66523d9f
GS
295 if (dn) {
296 edev = of_node_to_eeh_dev(dn);
297 } else if (dev) {
298 edev = pci_dev_to_eeh_dev(dev);
299 dn = pci_device_to_OF_node(dev);
300 } else {
e575f8db 301 eeh_stats.no_dn++;
1da177e4 302 return 0;
177bc936 303 }
66523d9f 304 pe = edev->pe;
1da177e4
LT
305
306 /* Access to IO BARs might get this far and still not want checking. */
66523d9f 307 if (!pe) {
e575f8db 308 eeh_stats.ignored_check++;
66523d9f
GS
309 pr_debug("EEH: Ignored check for %s %s\n",
310 eeh_pci_name(dev), dn->full_name);
1da177e4
LT
311 return 0;
312 }
313
66523d9f 314 if (!pe->addr && !pe->config_addr) {
e575f8db 315 eeh_stats.no_cfg_addr++;
1da177e4
LT
316 return 0;
317 }
318
fd761fd8
LV
319 /* If we already have a pending isolation event for this
320 * slot, we know it's bad already, we don't need to check.
321 * Do this checking under a lock; as multiple PCI devices
322 * in one slot might report errors simultaneously, and we
323 * only want one error recovery routine running.
1da177e4 324 */
3d372628 325 raw_spin_lock_irqsave(&confirm_error_lock, flags);
fd761fd8 326 rc = 1;
66523d9f
GS
327 if (pe->state & EEH_PE_ISOLATED) {
328 pe->check_count++;
329 if (pe->check_count % EEH_MAX_FAILS == 0) {
f36c5227 330 location = of_get_property(dn, "ibm,loc-code", NULL);
cb3bc9d0 331 printk(KERN_ERR "EEH: %d reads ignored for recovering device at "
f36c5227 332 "location=%s driver=%s pci addr=%s\n",
66523d9f 333 pe->check_count, location,
778a785f 334 eeh_driver_name(dev), eeh_pci_name(dev));
cb3bc9d0 335 printk(KERN_ERR "EEH: Might be infinite loop in %s driver\n",
778a785f 336 eeh_driver_name(dev));
5c1344e9 337 dump_stack();
1da177e4 338 }
fd761fd8 339 goto dn_unlock;
1da177e4
LT
340 }
341
342 /*
343 * Now test for an EEH failure. This is VERY expensive.
344 * Note that the eeh_config_addr may be a parent device
345 * in the case of a device behind a bridge, or it may be
346 * function zero of a multi-function device.
347 * In any case they must share a common PHB.
348 */
66523d9f 349 ret = eeh_ops->get_state(pe, NULL);
76e6faf7 350
39d16e29 351 /* Note that config-io to empty slots may fail;
cb3bc9d0 352 * they are empty when they don't have children.
eb594a47
GS
353 * We will punt with the following conditions: Failure to get
354 * PE's state, EEH not support and Permanently unavailable
355 * state, PE is in good state.
cb3bc9d0 356 */
eb594a47
GS
357 if ((ret < 0) ||
358 (ret == EEH_STATE_NOT_SUPPORT) ||
359 (ret & (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) ==
360 (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) {
e575f8db 361 eeh_stats.false_positives++;
66523d9f 362 pe->false_positives++;
fd761fd8
LV
363 rc = 0;
364 goto dn_unlock;
76e6faf7
LV
365 }
366
e575f8db 367 eeh_stats.slot_resets++;
fd761fd8
LV
368
369 /* Avoid repeated reports of this failure, including problems
370 * with other functions on this device, and functions under
cb3bc9d0
GS
371 * bridges.
372 */
66523d9f 373 eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
3d372628 374 raw_spin_unlock_irqrestore(&confirm_error_lock, flags);
1da177e4 375
66523d9f 376 eeh_send_failure_event(pe);
77bd7415 377
1da177e4
LT
378 /* Most EEH events are due to device driver bugs. Having
379 * a stack trace will help the device-driver authors figure
cb3bc9d0
GS
380 * out what happened. So print that out.
381 */
14fb1fa6 382 WARN(1, "EEH: failure detected\n");
fd761fd8
LV
383 return 1;
384
385dn_unlock:
3d372628 386 raw_spin_unlock_irqrestore(&confirm_error_lock, flags);
fd761fd8 387 return rc;
1da177e4
LT
388}
389
fd761fd8 390EXPORT_SYMBOL_GPL(eeh_dn_check_failure);
1da177e4
LT
391
392/**
cb3bc9d0
GS
393 * eeh_check_failure - Check if all 1's data is due to EEH slot freeze
394 * @token: I/O token, should be address in the form 0xA....
395 * @val: value, should be all 1's (XXX why do we need this arg??)
1da177e4 396 *
1da177e4
LT
397 * Check for an EEH failure at the given token address. Call this
398 * routine if the result of a read was all 0xff's and you want to
399 * find out if this is due to an EEH slot freeze event. This routine
400 * will query firmware for the EEH status.
401 *
402 * Note this routine is safe to call in an interrupt context.
403 */
404unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
405{
406 unsigned long addr;
407 struct pci_dev *dev;
408 struct device_node *dn;
409
410 /* Finding the phys addr + pci device; this is pretty quick. */
411 addr = eeh_token_to_phys((unsigned long __force) token);
def9d83d 412 dev = pci_addr_cache_get_device(addr);
177bc936 413 if (!dev) {
e575f8db 414 eeh_stats.no_device++;
1da177e4 415 return val;
177bc936 416 }
1da177e4
LT
417
418 dn = pci_device_to_OF_node(dev);
cb3bc9d0 419 eeh_dn_check_failure(dn, dev);
1da177e4
LT
420
421 pci_dev_put(dev);
422 return val;
423}
424
425EXPORT_SYMBOL(eeh_check_failure);
426
6dee3fb9 427
47b5c838 428/**
cce4b2d2 429 * eeh_pci_enable - Enable MMIO or DMA transfers for this slot
f631acd3 430 * @edev: pci device node
cb3bc9d0
GS
431 *
432 * This routine should be called to reenable frozen MMIO or DMA
433 * so that it would work correctly again. It's useful while doing
434 * recovery or log collection on the indicated device.
47b5c838 435 */
f631acd3 436int eeh_pci_enable(struct eeh_dev *edev, int function)
47b5c838 437{
47b5c838 438 int rc;
f631acd3 439 struct device_node *dn = eeh_dev_to_of_node(edev);
47b5c838 440
f631acd3 441 rc = eeh_ops->set_option(dn, function);
47b5c838 442 if (rc)
fa1be476 443 printk(KERN_WARNING "EEH: Unexpected state change %d, err=%d dn=%s\n",
f631acd3 444 function, rc, dn->full_name);
47b5c838 445
f631acd3 446 rc = eeh_ops->wait_state(dn, PCI_BUS_RESET_WAIT_MSEC);
eb594a47
GS
447 if (rc > 0 && (rc & EEH_STATE_MMIO_ENABLED) &&
448 (function == EEH_OPT_THAW_MMIO))
fa1be476
LV
449 return 0;
450
47b5c838
LV
451 return rc;
452}
453
00c2ae35
BK
454/**
455 * pcibios_set_pcie_slot_reset - Set PCI-E reset state
cb3bc9d0
GS
456 * @dev: pci device struct
457 * @state: reset state to enter
00c2ae35
BK
458 *
459 * Return value:
460 * 0 if success
cb3bc9d0 461 */
00c2ae35
BK
462int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
463{
464 struct device_node *dn = pci_device_to_OF_node(dev);
00c2ae35
BK
465
466 switch (state) {
467 case pcie_deassert_reset:
2652481f 468 eeh_ops->reset(dn, EEH_RESET_DEACTIVATE);
00c2ae35
BK
469 break;
470 case pcie_hot_reset:
2652481f 471 eeh_ops->reset(dn, EEH_RESET_HOT);
00c2ae35
BK
472 break;
473 case pcie_warm_reset:
2652481f 474 eeh_ops->reset(dn, EEH_RESET_FUNDAMENTAL);
00c2ae35
BK
475 break;
476 default:
477 return -EINVAL;
478 };
479
480 return 0;
481}
482
cb5b5624 483/**
cb3bc9d0
GS
484 * __eeh_set_pe_freset - Check the required reset for child devices
485 * @parent: parent device
486 * @freset: return value
487 *
488 * Each device might have its preferred reset type: fundamental or
489 * hot reset. The routine is used to collect the information from
490 * the child devices so that they could be reset accordingly.
6dee3fb9 491 */
cb3bc9d0
GS
492void __eeh_set_pe_freset(struct device_node *parent, unsigned int *freset)
493{
494 struct device_node *dn;
495
496 for_each_child_of_node(parent, dn) {
f631acd3
GS
497 if (of_node_to_eeh_dev(dn)) {
498 struct pci_dev *dev = of_node_to_eeh_dev(dn)->pdev;
cb3bc9d0
GS
499
500 if (dev && dev->driver)
501 *freset |= dev->needs_freset;
502
503 __eeh_set_pe_freset(dn, freset);
504 }
505 }
506}
507
508/**
509 * eeh_set_pe_freset - Check the required reset for the indicated device and its children
510 * @dn: parent device
511 * @freset: return value
512 *
513 * Each device might have its preferred reset type: fundamental or
514 * hot reset. The routine is used to collected the information for
515 * the indicated device and its children so that the bunch of the
516 * devices could be reset properly.
517 */
518void eeh_set_pe_freset(struct device_node *dn, unsigned int *freset)
519{
520 struct pci_dev *dev;
cce4b2d2 521 dn = eeh_find_device_pe(dn);
cb3bc9d0
GS
522
523 /* Back up one, since config addrs might be shared */
f631acd3 524 if (!pcibios_find_pci_bus(dn) && of_node_to_eeh_dev(dn->parent))
cb3bc9d0 525 dn = dn->parent;
6dee3fb9 526
f631acd3 527 dev = of_node_to_eeh_dev(dn)->pdev;
cb3bc9d0
GS
528 if (dev)
529 *freset |= dev->needs_freset;
530
531 __eeh_set_pe_freset(dn, freset);
532}
533
534/**
cce4b2d2 535 * eeh_reset_pe_once - Assert the pci #RST line for 1/4 second
f631acd3 536 * @edev: pci device node to be reset.
cb3bc9d0
GS
537 *
538 * Assert the PCI #RST line for 1/4 second.
539 */
f631acd3 540static void eeh_reset_pe_once(struct eeh_dev *edev)
6dee3fb9 541{
308fc4f8 542 unsigned int freset = 0;
f631acd3 543 struct device_node *dn = eeh_dev_to_of_node(edev);
6e19314c 544
308fc4f8
RL
545 /* Determine type of EEH reset required for
546 * Partitionable Endpoint, a hot-reset (1)
547 * or a fundamental reset (3).
548 * A fundamental reset required by any device under
549 * Partitionable Endpoint trumps hot-reset.
550 */
f631acd3 551 eeh_set_pe_freset(dn, &freset);
308fc4f8
RL
552
553 if (freset)
f631acd3 554 eeh_ops->reset(dn, EEH_RESET_FUNDAMENTAL);
6e19314c 555 else
f631acd3 556 eeh_ops->reset(dn, EEH_RESET_HOT);
6dee3fb9
LV
557
558 /* The PCI bus requires that the reset be held high for at least
cb3bc9d0
GS
559 * a 100 milliseconds. We wait a bit longer 'just in case'.
560 */
6dee3fb9 561#define PCI_BUS_RST_HOLD_TIME_MSEC 250
cb3bc9d0 562 msleep(PCI_BUS_RST_HOLD_TIME_MSEC);
d9564ad1
LV
563
564 /* We might get hit with another EEH freeze as soon as the
565 * pci slot reset line is dropped. Make sure we don't miss
cb3bc9d0
GS
566 * these, and clear the flag now.
567 */
f631acd3 568 eeh_clear_slot(dn, EEH_MODE_ISOLATED);
d9564ad1 569
f631acd3 570 eeh_ops->reset(dn, EEH_RESET_DEACTIVATE);
6dee3fb9
LV
571
572 /* After a PCI slot has been reset, the PCI Express spec requires
573 * a 1.5 second idle time for the bus to stabilize, before starting
cb3bc9d0
GS
574 * up traffic.
575 */
6dee3fb9 576#define PCI_BUS_SETTLE_TIME_MSEC 1800
cb3bc9d0 577 msleep(PCI_BUS_SETTLE_TIME_MSEC);
e1029263
LV
578}
579
cb3bc9d0 580/**
cce4b2d2 581 * eeh_reset_pe - Reset the indicated PE
f631acd3 582 * @edev: PCI device associated EEH device
cb3bc9d0
GS
583 *
584 * This routine should be called to reset indicated device, including
585 * PE. A PE might include multiple PCI devices and sometimes PCI bridges
586 * might be involved as well.
587 */
f631acd3 588int eeh_reset_pe(struct eeh_dev *edev)
e1029263
LV
589{
590 int i, rc;
f631acd3 591 struct device_node *dn = eeh_dev_to_of_node(edev);
e1029263 592
9c547768
LV
593 /* Take three shots at resetting the bus */
594 for (i=0; i<3; i++) {
f631acd3 595 eeh_reset_pe_once(edev);
6dee3fb9 596
f631acd3 597 rc = eeh_ops->wait_state(dn, PCI_BUS_RESET_WAIT_MSEC);
eb594a47 598 if (rc == (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE))
b6495c0c 599 return 0;
e1029263 600
e1029263 601 if (rc < 0) {
12588da7 602 printk(KERN_ERR "EEH: unrecoverable slot failure %s\n",
f631acd3 603 dn->full_name);
b6495c0c 604 return -1;
e1029263 605 }
12588da7 606 printk(KERN_ERR "EEH: bus reset %d failed on slot %s, rc=%d\n",
f631acd3 607 i+1, dn->full_name, rc);
6dee3fb9 608 }
b6495c0c 609
9c547768 610 return -1;
6dee3fb9
LV
611}
612
8b553f32 613/**
cb3bc9d0 614 * eeh_save_bars - Save device bars
f631acd3 615 * @edev: PCI device associated EEH device
8b553f32
LV
616 *
617 * Save the values of the device bars. Unlike the restore
618 * routine, this routine is *not* recursive. This is because
31116f0b 619 * PCI devices are added individually; but, for the restore,
8b553f32
LV
620 * an entire slot is reset at a time.
621 */
f631acd3 622static void eeh_save_bars(struct eeh_dev *edev)
8b553f32
LV
623{
624 int i;
f631acd3 625 struct device_node *dn;
8b553f32 626
f631acd3 627 if (!edev)
8b553f32 628 return;
f631acd3 629 dn = eeh_dev_to_of_node(edev);
8b553f32
LV
630
631 for (i = 0; i < 16; i++)
3780444c 632 eeh_ops->read_config(dn, i * 4, 4, &edev->config_space[i]);
8b553f32
LV
633}
634
cb3bc9d0 635/**
cce4b2d2 636 * eeh_early_enable - Early enable EEH on the indicated device
cb3bc9d0
GS
637 * @dn: device node
638 * @data: BUID
639 *
640 * Enable EEH functionality on the specified PCI device. The function
641 * is expected to be called before real PCI probing is done. However,
642 * the PHBs have been initialized at this point.
643 */
cce4b2d2 644static void *eeh_early_enable(struct device_node *dn, void *data)
1da177e4 645{
1da177e4 646 int ret;
e2eb6392
SR
647 const u32 *class_code = of_get_property(dn, "class-code", NULL);
648 const u32 *vendor_id = of_get_property(dn, "vendor-id", NULL);
649 const u32 *device_id = of_get_property(dn, "device-id", NULL);
954a46e2 650 const u32 *regs;
1da177e4 651 int enable;
f631acd3 652 struct eeh_dev *edev = of_node_to_eeh_dev(dn);
371a395d 653 struct eeh_pe pe;
1da177e4 654
f631acd3
GS
655 edev->class_code = 0;
656 edev->mode = 0;
657 edev->check_count = 0;
658 edev->freeze_count = 0;
659 edev->false_positives = 0;
1da177e4 660
c6d4d5a8
NL
661 if (!of_device_is_available(dn))
662 return NULL;
1da177e4
LT
663
664 /* Ignore bad nodes. */
665 if (!class_code || !vendor_id || !device_id)
666 return NULL;
667
668 /* There is nothing to check on PCI to ISA bridges */
669 if (dn->type && !strcmp(dn->type, "isa")) {
f631acd3 670 edev->mode |= EEH_MODE_NOCHECK;
1da177e4
LT
671 return NULL;
672 }
f631acd3 673 edev->class_code = *class_code;
1da177e4 674
1da177e4 675 /* Ok... see if this device supports EEH. Some do, some don't,
cb3bc9d0
GS
676 * and the only way to find out is to check each and every one.
677 */
e2eb6392 678 regs = of_get_property(dn, "reg", NULL);
1da177e4 679 if (regs) {
371a395d
GS
680 /* Initialize the fake PE */
681 memset(&pe, 0, sizeof(struct eeh_pe));
682 pe.phb = edev->phb;
683 pe.config_addr = regs[0];
684
1da177e4
LT
685 /* First register entry is addr (00BBSS00) */
686 /* Try to enable eeh */
371a395d 687 ret = eeh_ops->set_option(&pe, EEH_OPT_ENABLE);
172ca926 688
25c4a46f 689 enable = 0;
1da177e4 690 if (ret == 0) {
f631acd3 691 edev->config_addr = regs[0];
25e591f6
LV
692
693 /* If the newer, better, ibm,get-config-addr-info is supported,
cb3bc9d0
GS
694 * then use that instead.
695 */
371a395d
GS
696 edev->pe_config_addr = eeh_ops->get_pe_addr(&pe);
697 pe.addr = edev->pe_config_addr;
25c4a46f
LV
698
699 /* Some older systems (Power4) allow the
700 * ibm,set-eeh-option call to succeed even on nodes
701 * where EEH is not supported. Verify support
cb3bc9d0
GS
702 * explicitly.
703 */
371a395d 704 ret = eeh_ops->get_state(&pe, NULL);
eb594a47 705 if (ret > 0 && ret != EEH_STATE_NOT_SUPPORT)
25c4a46f
LV
706 enable = 1;
707 }
708
709 if (enable) {
710 eeh_subsystem_enabled = 1;
f631acd3 711 edev->mode |= EEH_MODE_SUPPORTED;
25c4a46f 712
9b84348c
GS
713 eeh_add_to_parent_pe(edev);
714
57b066ff 715 pr_debug("EEH: %s: eeh enabled, config=%x pe_config=%x\n",
f631acd3
GS
716 dn->full_name, edev->config_addr,
717 edev->pe_config_addr);
1da177e4
LT
718 } else {
719
720 /* This device doesn't support EEH, but it may have an
cb3bc9d0
GS
721 * EEH parent, in which case we mark it as supported.
722 */
f631acd3
GS
723 if (dn->parent && of_node_to_eeh_dev(dn->parent) &&
724 (of_node_to_eeh_dev(dn->parent)->mode & EEH_MODE_SUPPORTED)) {
1da177e4 725 /* Parent supports EEH. */
f631acd3
GS
726 edev->mode |= EEH_MODE_SUPPORTED;
727 edev->config_addr = of_node_to_eeh_dev(dn->parent)->config_addr;
9b84348c
GS
728 edev->pe_config_addr = of_node_to_eeh_dev(dn->parent)->pe_config_addr;
729
730 eeh_add_to_parent_pe(edev);
731
1da177e4
LT
732 return NULL;
733 }
734 }
735 } else {
736 printk(KERN_WARNING "EEH: %s: unable to get reg property.\n",
737 dn->full_name);
738 }
739
f631acd3 740 eeh_save_bars(edev);
69376502 741 return NULL;
1da177e4
LT
742}
743
aa1e6374
GS
744/**
745 * eeh_ops_register - Register platform dependent EEH operations
746 * @ops: platform dependent EEH operations
747 *
748 * Register the platform dependent EEH operation callback
749 * functions. The platform should call this function before
750 * any other EEH operations.
751 */
752int __init eeh_ops_register(struct eeh_ops *ops)
753{
754 if (!ops->name) {
755 pr_warning("%s: Invalid EEH ops name for %p\n",
756 __func__, ops);
757 return -EINVAL;
758 }
759
760 if (eeh_ops && eeh_ops != ops) {
761 pr_warning("%s: EEH ops of platform %s already existing (%s)\n",
762 __func__, eeh_ops->name, ops->name);
763 return -EEXIST;
764 }
765
766 eeh_ops = ops;
767
768 return 0;
769}
770
771/**
772 * eeh_ops_unregister - Unreigster platform dependent EEH operations
773 * @name: name of EEH platform operations
774 *
775 * Unregister the platform dependent EEH operation callback
776 * functions.
777 */
778int __exit eeh_ops_unregister(const char *name)
779{
780 if (!name || !strlen(name)) {
781 pr_warning("%s: Invalid EEH ops name\n",
782 __func__);
783 return -EINVAL;
784 }
785
786 if (eeh_ops && !strcmp(eeh_ops->name, name)) {
787 eeh_ops = NULL;
788 return 0;
789 }
790
791 return -EEXIST;
792}
793
cb3bc9d0
GS
794/**
795 * eeh_init - EEH initialization
796 *
1da177e4
LT
797 * Initialize EEH by trying to enable it for all of the adapters in the system.
798 * As a side effect we can determine here if eeh is supported at all.
799 * Note that we leave EEH on so failed config cycles won't cause a machine
800 * check. If a user turns off EEH for a particular adapter they are really
801 * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
802 * grant access to a slot if EEH isn't enabled, and so we always enable
803 * EEH for all slots/all devices.
804 *
805 * The eeh-force-off option disables EEH checking globally, for all slots.
806 * Even if force-off is set, the EEH hardware is still enabled, so that
807 * newer systems can boot.
808 */
35e5cfe2 809static int __init eeh_init(void)
1da177e4 810{
1a5c2e63
GS
811 struct pci_controller *hose, *tmp;
812 struct device_node *phb;
e2af155c
GS
813 int ret;
814
815 /* call platform initialization function */
816 if (!eeh_ops) {
817 pr_warning("%s: Platform EEH operation not found\n",
818 __func__);
35e5cfe2 819 return -EEXIST;
e2af155c
GS
820 } else if ((ret = eeh_ops->init())) {
821 pr_warning("%s: Failed to call platform init function (%d)\n",
822 __func__, ret);
35e5cfe2 823 return ret;
e2af155c 824 }
1da177e4 825
3d372628 826 raw_spin_lock_init(&confirm_error_lock);
df7242b1 827
1a5c2e63
GS
828 /* Enable EEH for all adapters */
829 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
830 phb = hose->dn;
c8c29b38 831 traverse_pci_devices(phb, eeh_early_enable, NULL);
1da177e4
LT
832 }
833
834 if (eeh_subsystem_enabled)
835 printk(KERN_INFO "EEH: PCI Enhanced I/O Error Handling Enabled\n");
836 else
837 printk(KERN_WARNING "EEH: No capable adapters found\n");
35e5cfe2
GS
838
839 return ret;
1da177e4
LT
840}
841
35e5cfe2
GS
842core_initcall_sync(eeh_init);
843
1da177e4 844/**
cb3bc9d0 845 * eeh_add_device_early - Enable EEH for the indicated device_node
1da177e4
LT
846 * @dn: device node for which to set up EEH
847 *
848 * This routine must be used to perform EEH initialization for PCI
849 * devices that were added after system boot (e.g. hotplug, dlpar).
850 * This routine must be called before any i/o is performed to the
851 * adapter (inluding any config-space i/o).
852 * Whether this actually enables EEH or not for this device depends
853 * on the CEC architecture, type of the device, on earlier boot
854 * command-line arguments & etc.
855 */
794e085e 856static void eeh_add_device_early(struct device_node *dn)
1da177e4
LT
857{
858 struct pci_controller *phb;
1da177e4 859
f631acd3 860 if (!dn || !of_node_to_eeh_dev(dn))
1da177e4 861 return;
f631acd3 862 phb = of_node_to_eeh_dev(dn)->phb;
f751f841
LV
863
864 /* USB Bus children of PCI devices will not have BUID's */
865 if (NULL == phb || 0 == phb->buid)
1da177e4 866 return;
1da177e4 867
c8c29b38 868 eeh_early_enable(dn, NULL);
1da177e4 869}
1da177e4 870
cb3bc9d0
GS
871/**
872 * eeh_add_device_tree_early - Enable EEH for the indicated device
873 * @dn: device node
874 *
875 * This routine must be used to perform EEH initialization for the
876 * indicated PCI device that was added after system boot (e.g.
877 * hotplug, dlpar).
878 */
e2a296ee
LV
879void eeh_add_device_tree_early(struct device_node *dn)
880{
881 struct device_node *sib;
acaa6176
SR
882
883 for_each_child_of_node(dn, sib)
e2a296ee
LV
884 eeh_add_device_tree_early(sib);
885 eeh_add_device_early(dn);
886}
887EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
888
1da177e4 889/**
cb3bc9d0 890 * eeh_add_device_late - Perform EEH initialization for the indicated pci device
1da177e4
LT
891 * @dev: pci device for which to set up EEH
892 *
893 * This routine must be used to complete EEH initialization for PCI
894 * devices that were added after system boot (e.g. hotplug, dlpar).
895 */
794e085e 896static void eeh_add_device_late(struct pci_dev *dev)
1da177e4 897{
56b0fca3 898 struct device_node *dn;
f631acd3 899 struct eeh_dev *edev;
56b0fca3 900
1da177e4
LT
901 if (!dev || !eeh_subsystem_enabled)
902 return;
903
57b066ff 904 pr_debug("EEH: Adding device %s\n", pci_name(dev));
1da177e4 905
56b0fca3 906 dn = pci_device_to_OF_node(dev);
2ef822c5 907 edev = of_node_to_eeh_dev(dn);
f631acd3 908 if (edev->pdev == dev) {
57b066ff
BH
909 pr_debug("EEH: Already referenced !\n");
910 return;
911 }
f631acd3 912 WARN_ON(edev->pdev);
57b066ff 913
cb3bc9d0 914 pci_dev_get(dev);
f631acd3
GS
915 edev->pdev = dev;
916 dev->dev.archdata.edev = edev;
56b0fca3 917
e1d04c97
LV
918 pci_addr_cache_insert_device(dev);
919 eeh_sysfs_add_device(dev);
1da177e4 920}
794e085e 921
cb3bc9d0
GS
922/**
923 * eeh_add_device_tree_late - Perform EEH initialization for the indicated PCI bus
924 * @bus: PCI bus
925 *
926 * This routine must be used to perform EEH initialization for PCI
927 * devices which are attached to the indicated PCI bus. The PCI bus
928 * is added after system boot through hotplug or dlpar.
929 */
794e085e
NF
930void eeh_add_device_tree_late(struct pci_bus *bus)
931{
932 struct pci_dev *dev;
933
934 list_for_each_entry(dev, &bus->devices, bus_list) {
935 eeh_add_device_late(dev);
936 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
937 struct pci_bus *subbus = dev->subordinate;
938 if (subbus)
939 eeh_add_device_tree_late(subbus);
940 }
941 }
942}
943EXPORT_SYMBOL_GPL(eeh_add_device_tree_late);
1da177e4
LT
944
945/**
cb3bc9d0 946 * eeh_remove_device - Undo EEH setup for the indicated pci device
1da177e4
LT
947 * @dev: pci device to be removed
948 *
794e085e
NF
949 * This routine should be called when a device is removed from
950 * a running system (e.g. by hotplug or dlpar). It unregisters
951 * the PCI device from the EEH subsystem. I/O errors affecting
952 * this device will no longer be detected after this call; thus,
953 * i/o errors affecting this slot may leave this device unusable.
1da177e4 954 */
794e085e 955static void eeh_remove_device(struct pci_dev *dev)
1da177e4 956{
f631acd3
GS
957 struct eeh_dev *edev;
958
1da177e4
LT
959 if (!dev || !eeh_subsystem_enabled)
960 return;
f631acd3 961 edev = pci_dev_to_eeh_dev(dev);
1da177e4
LT
962
963 /* Unregister the device with the EEH/PCI address search system */
57b066ff 964 pr_debug("EEH: Removing device %s\n", pci_name(dev));
56b0fca3 965
f631acd3 966 if (!edev || !edev->pdev) {
57b066ff
BH
967 pr_debug("EEH: Not referenced !\n");
968 return;
b055a9e1 969 }
f631acd3
GS
970 edev->pdev = NULL;
971 dev->dev.archdata.edev = NULL;
cb3bc9d0 972 pci_dev_put(dev);
57b066ff 973
82e8882f 974 eeh_rmv_from_parent_pe(edev);
57b066ff
BH
975 pci_addr_cache_remove_device(dev);
976 eeh_sysfs_remove_device(dev);
1da177e4 977}
1da177e4 978
cb3bc9d0
GS
979/**
980 * eeh_remove_bus_device - Undo EEH setup for the indicated PCI device
981 * @dev: PCI device
982 *
983 * This routine must be called when a device is removed from the
984 * running system through hotplug or dlpar. The corresponding
985 * PCI address cache will be removed.
986 */
e2a296ee
LV
987void eeh_remove_bus_device(struct pci_dev *dev)
988{
794e085e
NF
989 struct pci_bus *bus = dev->subordinate;
990 struct pci_dev *child, *tmp;
991
e2a296ee 992 eeh_remove_device(dev);
794e085e
NF
993
994 if (bus && dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
995 list_for_each_entry_safe(child, tmp, &bus->devices, bus_list)
996 eeh_remove_bus_device(child);
e2a296ee
LV
997 }
998}
999EXPORT_SYMBOL_GPL(eeh_remove_bus_device);
1000
1da177e4
LT
1001static int proc_eeh_show(struct seq_file *m, void *v)
1002{
1da177e4
LT
1003 if (0 == eeh_subsystem_enabled) {
1004 seq_printf(m, "EEH Subsystem is globally disabled\n");
e575f8db 1005 seq_printf(m, "eeh_total_mmio_ffs=%llu\n", eeh_stats.total_mmio_ffs);
1da177e4
LT
1006 } else {
1007 seq_printf(m, "EEH Subsystem is enabled\n");
177bc936 1008 seq_printf(m,
e575f8db
GS
1009 "no device=%llu\n"
1010 "no device node=%llu\n"
1011 "no config address=%llu\n"
1012 "check not wanted=%llu\n"
1013 "eeh_total_mmio_ffs=%llu\n"
1014 "eeh_false_positives=%llu\n"
1015 "eeh_slot_resets=%llu\n",
1016 eeh_stats.no_device,
1017 eeh_stats.no_dn,
1018 eeh_stats.no_cfg_addr,
1019 eeh_stats.ignored_check,
1020 eeh_stats.total_mmio_ffs,
1021 eeh_stats.false_positives,
1022 eeh_stats.slot_resets);
1da177e4
LT
1023 }
1024
1025 return 0;
1026}
1027
1028static int proc_eeh_open(struct inode *inode, struct file *file)
1029{
1030 return single_open(file, proc_eeh_show, NULL);
1031}
1032
5dfe4c96 1033static const struct file_operations proc_eeh_operations = {
1da177e4
LT
1034 .open = proc_eeh_open,
1035 .read = seq_read,
1036 .llseek = seq_lseek,
1037 .release = single_release,
1038};
1039
1040static int __init eeh_init_proc(void)
1041{
66747138 1042 if (machine_is(pseries))
8feaa434 1043 proc_create("powerpc/eeh", 0, NULL, &proc_eeh_operations);
1da177e4
LT
1044 return 0;
1045}
1046__initcall(eeh_init_proc);