blob: a33ba833109c5e2bfc0628af4a9179d00997be0f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * eeh.c
3 * Copyright (C) 2001 Dave Engebretsen & Todd Inglett IBM Corporation
Linas Vepstas69376502005-11-03 18:47:50 -06004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
Linas Vepstas69376502005-11-03 18:47:50 -06009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Linas Vepstas69376502005-11-03 18:47:50 -060014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
Linas Vepstas6dee3fb2005-11-03 18:50:10 -060020#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/init.h>
22#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/pci.h>
24#include <linux/proc_fs.h>
25#include <linux/rbtree.h>
26#include <linux/seq_file.h>
27#include <linux/spinlock.h>
Linas Vepstas69376502005-11-03 18:47:50 -060028#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/eeh.h>
Linas Vepstas172ca922005-11-03 18:50:04 -060030#include <asm/eeh_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/io.h>
32#include <asm/machdep.h>
Stephen Rothwelld3878992005-09-28 02:50:25 +100033#include <asm/ppc-pci.h>
Linas Vepstas172ca922005-11-03 18:50:04 -060034#include <asm/rtas.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#undef DEBUG
37
38/** Overview:
39 * EEH, or "Extended Error Handling" is a PCI bridge technology for
40 * dealing with PCI bus errors that can't be dealt with within the
41 * usual PCI framework, except by check-stopping the CPU. Systems
42 * that are designed for high-availability/reliability cannot afford
43 * to crash due to a "mere" PCI error, thus the need for EEH.
44 * An EEH-capable bridge operates by converting a detected error
45 * into a "slot freeze", taking the PCI adapter off-line, making
46 * the slot behave, from the OS'es point of view, as if the slot
47 * were "empty": all reads return 0xff's and all writes are silently
48 * ignored. EEH slot isolation events can be triggered by parity
49 * errors on the address or data busses (e.g. during posted writes),
Linas Vepstas69376502005-11-03 18:47:50 -060050 * which in turn might be caused by low voltage on the bus, dust,
51 * vibration, humidity, radioactivity or plain-old failed hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 *
53 * Note, however, that one of the leading causes of EEH slot
54 * freeze events are buggy device drivers, buggy device microcode,
55 * or buggy device hardware. This is because any attempt by the
56 * device to bus-master data to a memory address that is not
57 * assigned to the device will trigger a slot freeze. (The idea
58 * is to prevent devices-gone-wild from corrupting system memory).
59 * Buggy hardware/drivers will have a miserable time co-existing
60 * with EEH.
61 *
62 * Ideally, a PCI device driver, when suspecting that an isolation
63 * event has occured (e.g. by reading 0xff's), will then ask EEH
64 * whether this is the case, and then take appropriate steps to
65 * reset the PCI slot, the PCI device, and then resume operations.
66 * However, until that day, the checking is done here, with the
67 * eeh_check_failure() routine embedded in the MMIO macros. If
68 * the slot is found to be isolated, an "EEH Event" is synthesized
69 * and sent out for processing.
70 */
71
Linas Vepstas5c1344e2005-11-03 18:49:31 -060072/* If a device driver keeps reading an MMIO register in an interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * handler after a slot isolation event has occurred, we assume it
74 * is broken and panic. This sets the threshold for how many read
75 * attempts we allow before panicking.
76 */
Linas Vepstas5c1344e2005-11-03 18:49:31 -060077#define EEH_MAX_FAILS 100000
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Linas Vepstas8b553f32005-11-03 18:50:17 -060079/* Misc forward declaraions */
80static void eeh_save_bars(struct pci_dev * pdev, struct pci_dn *pdn);
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/* RTAS tokens */
83static int ibm_set_eeh_option;
84static int ibm_set_slot_reset;
85static int ibm_read_slot_reset_state;
86static int ibm_read_slot_reset_state2;
87static int ibm_slot_error_detail;
88
89static int eeh_subsystem_enabled;
90
Linas Vepstasfd761fd2005-11-03 18:49:23 -060091/* Lock to avoid races due to multiple reports of an error */
92static DEFINE_SPINLOCK(confirm_error_lock);
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/* Buffer for reporting slot-error-detail rtas calls */
95static unsigned char slot_errbuf[RTAS_ERROR_LOG_MAX];
96static DEFINE_SPINLOCK(slot_errbuf_lock);
97static int eeh_error_buf_size;
98
99/* System monitoring statistics */
Linas Vepstas177bc932005-11-03 18:48:52 -0600100static DEFINE_PER_CPU(unsigned long, no_device);
101static DEFINE_PER_CPU(unsigned long, no_dn);
102static DEFINE_PER_CPU(unsigned long, no_cfg_addr);
103static DEFINE_PER_CPU(unsigned long, ignored_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104static DEFINE_PER_CPU(unsigned long, total_mmio_ffs);
105static DEFINE_PER_CPU(unsigned long, false_positives);
106static DEFINE_PER_CPU(unsigned long, ignored_failures);
107static DEFINE_PER_CPU(unsigned long, slot_resets);
108
109/**
110 * The pci address cache subsystem. This subsystem places
111 * PCI device address resources into a red-black tree, sorted
112 * according to the address range, so that given only an i/o
113 * address, the corresponding PCI device can be **quickly**
114 * found. It is safe to perform an address lookup in an interrupt
115 * context; this ability is an important feature.
116 *
117 * Currently, the only customer of this code is the EEH subsystem;
118 * thus, this code has been somewhat tailored to suit EEH better.
119 * In particular, the cache does *not* hold the addresses of devices
120 * for which EEH is not enabled.
121 *
122 * (Implementation Note: The RB tree seems to be better/faster
123 * than any hash algo I could think of for this problem, even
124 * with the penalty of slow pointer chases for d-cache misses).
125 */
126struct pci_io_addr_range
127{
128 struct rb_node rb_node;
129 unsigned long addr_lo;
130 unsigned long addr_hi;
131 struct pci_dev *pcidev;
132 unsigned int flags;
133};
134
135static struct pci_io_addr_cache
136{
137 struct rb_root rb_root;
138 spinlock_t piar_lock;
139} pci_io_addr_cache_root;
140
141static inline struct pci_dev *__pci_get_device_by_addr(unsigned long addr)
142{
143 struct rb_node *n = pci_io_addr_cache_root.rb_root.rb_node;
144
145 while (n) {
146 struct pci_io_addr_range *piar;
147 piar = rb_entry(n, struct pci_io_addr_range, rb_node);
148
149 if (addr < piar->addr_lo) {
150 n = n->rb_left;
151 } else {
152 if (addr > piar->addr_hi) {
153 n = n->rb_right;
154 } else {
155 pci_dev_get(piar->pcidev);
156 return piar->pcidev;
157 }
158 }
159 }
160
161 return NULL;
162}
163
164/**
165 * pci_get_device_by_addr - Get device, given only address
166 * @addr: mmio (PIO) phys address or i/o port number
167 *
168 * Given an mmio phys address, or a port number, find a pci device
169 * that implements this address. Be sure to pci_dev_put the device
170 * when finished. I/O port numbers are assumed to be offset
171 * from zero (that is, they do *not* have pci_io_addr added in).
172 * It is safe to call this function within an interrupt.
173 */
174static struct pci_dev *pci_get_device_by_addr(unsigned long addr)
175{
176 struct pci_dev *dev;
177 unsigned long flags;
178
179 spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
180 dev = __pci_get_device_by_addr(addr);
181 spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
182 return dev;
183}
184
185#ifdef DEBUG
186/*
187 * Handy-dandy debug print routine, does nothing more
188 * than print out the contents of our addr cache.
189 */
190static void pci_addr_cache_print(struct pci_io_addr_cache *cache)
191{
192 struct rb_node *n;
193 int cnt = 0;
194
195 n = rb_first(&cache->rb_root);
196 while (n) {
197 struct pci_io_addr_range *piar;
198 piar = rb_entry(n, struct pci_io_addr_range, rb_node);
Adrian Bunk982245f2005-07-17 04:22:20 +0200199 printk(KERN_DEBUG "PCI: %s addr range %d [%lx-%lx]: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 (piar->flags & IORESOURCE_IO) ? "i/o" : "mem", cnt,
Adrian Bunk982245f2005-07-17 04:22:20 +0200201 piar->addr_lo, piar->addr_hi, pci_name(piar->pcidev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 cnt++;
203 n = rb_next(n);
204 }
205}
206#endif
207
208/* Insert address range into the rb tree. */
209static struct pci_io_addr_range *
210pci_addr_cache_insert(struct pci_dev *dev, unsigned long alo,
211 unsigned long ahi, unsigned int flags)
212{
213 struct rb_node **p = &pci_io_addr_cache_root.rb_root.rb_node;
214 struct rb_node *parent = NULL;
215 struct pci_io_addr_range *piar;
216
217 /* Walk tree, find a place to insert into tree */
218 while (*p) {
219 parent = *p;
220 piar = rb_entry(parent, struct pci_io_addr_range, rb_node);
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600221 if (ahi < piar->addr_lo) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 p = &parent->rb_left;
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600223 } else if (alo > piar->addr_hi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 p = &parent->rb_right;
225 } else {
226 if (dev != piar->pcidev ||
227 alo != piar->addr_lo || ahi != piar->addr_hi) {
228 printk(KERN_WARNING "PIAR: overlapping address range\n");
229 }
230 return piar;
231 }
232 }
233 piar = (struct pci_io_addr_range *)kmalloc(sizeof(struct pci_io_addr_range), GFP_ATOMIC);
234 if (!piar)
235 return NULL;
236
237 piar->addr_lo = alo;
238 piar->addr_hi = ahi;
239 piar->pcidev = dev;
240 piar->flags = flags;
241
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600242#ifdef DEBUG
243 printk(KERN_DEBUG "PIAR: insert range=[%lx:%lx] dev=%s\n",
244 alo, ahi, pci_name (dev));
245#endif
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 rb_link_node(&piar->rb_node, parent, p);
248 rb_insert_color(&piar->rb_node, &pci_io_addr_cache_root.rb_root);
249
250 return piar;
251}
252
253static void __pci_addr_cache_insert_device(struct pci_dev *dev)
254{
255 struct device_node *dn;
Paul Mackerras16353172005-09-06 13:17:54 +1000256 struct pci_dn *pdn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 int i;
258 int inserted = 0;
259
260 dn = pci_device_to_OF_node(dev);
261 if (!dn) {
Linas Vepstas69376502005-11-03 18:47:50 -0600262 printk(KERN_WARNING "PCI: no pci dn found for dev=%s\n", pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return;
264 }
265
266 /* Skip any devices for which EEH is not enabled. */
Linas Vepstas69376502005-11-03 18:47:50 -0600267 pdn = PCI_DN(dn);
Paul Mackerras16353172005-09-06 13:17:54 +1000268 if (!(pdn->eeh_mode & EEH_MODE_SUPPORTED) ||
269 pdn->eeh_mode & EEH_MODE_NOCHECK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270#ifdef DEBUG
Linas Vepstas69376502005-11-03 18:47:50 -0600271 printk(KERN_INFO "PCI: skip building address cache for=%s - %s\n",
272 pci_name(dev), pdn->node->full_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273#endif
274 return;
275 }
276
277 /* The cache holds a reference to the device... */
278 pci_dev_get(dev);
279
280 /* Walk resources on this device, poke them into the tree */
281 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
282 unsigned long start = pci_resource_start(dev,i);
283 unsigned long end = pci_resource_end(dev,i);
284 unsigned int flags = pci_resource_flags(dev,i);
285
286 /* We are interested only bus addresses, not dma or other stuff */
287 if (0 == (flags & (IORESOURCE_IO | IORESOURCE_MEM)))
288 continue;
289 if (start == 0 || ~start == 0 || end == 0 || ~end == 0)
290 continue;
291 pci_addr_cache_insert(dev, start, end, flags);
292 inserted = 1;
293 }
294
295 /* If there was nothing to add, the cache has no reference... */
296 if (!inserted)
297 pci_dev_put(dev);
298}
299
300/**
301 * pci_addr_cache_insert_device - Add a device to the address cache
302 * @dev: PCI device whose I/O addresses we are interested in.
303 *
304 * In order to support the fast lookup of devices based on addresses,
305 * we maintain a cache of devices that can be quickly searched.
306 * This routine adds a device to that cache.
307 */
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600308static void pci_addr_cache_insert_device(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 unsigned long flags;
311
312 spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
313 __pci_addr_cache_insert_device(dev);
314 spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
315}
316
317static inline void __pci_addr_cache_remove_device(struct pci_dev *dev)
318{
319 struct rb_node *n;
320 int removed = 0;
321
322restart:
323 n = rb_first(&pci_io_addr_cache_root.rb_root);
324 while (n) {
325 struct pci_io_addr_range *piar;
326 piar = rb_entry(n, struct pci_io_addr_range, rb_node);
327
328 if (piar->pcidev == dev) {
329 rb_erase(n, &pci_io_addr_cache_root.rb_root);
330 removed = 1;
331 kfree(piar);
332 goto restart;
333 }
334 n = rb_next(n);
335 }
336
337 /* The cache no longer holds its reference to this device... */
338 if (removed)
339 pci_dev_put(dev);
340}
341
342/**
343 * pci_addr_cache_remove_device - remove pci device from addr cache
344 * @dev: device to remove
345 *
346 * Remove a device from the addr-cache tree.
347 * This is potentially expensive, since it will walk
348 * the tree multiple times (once per resource).
349 * But so what; device removal doesn't need to be that fast.
350 */
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600351static void pci_addr_cache_remove_device(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 unsigned long flags;
354
355 spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
356 __pci_addr_cache_remove_device(dev);
357 spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
358}
359
360/**
361 * pci_addr_cache_build - Build a cache of I/O addresses
362 *
363 * Build a cache of pci i/o addresses. This cache will be used to
364 * find the pci device that corresponds to a given address.
365 * This routine scans all pci busses to build the cache.
366 * Must be run late in boot process, after the pci controllers
367 * have been scaned for devices (after all device resources are known).
368 */
369void __init pci_addr_cache_build(void)
370{
Linas Vepstas8b553f32005-11-03 18:50:17 -0600371 struct device_node *dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 struct pci_dev *dev = NULL;
373
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600374 if (!eeh_subsystem_enabled)
375 return;
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 spin_lock_init(&pci_io_addr_cache_root.piar_lock);
378
379 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
380 /* Ignore PCI bridges ( XXX why ??) */
381 if ((dev->class >> 16) == PCI_BASE_CLASS_BRIDGE) {
382 continue;
383 }
384 pci_addr_cache_insert_device(dev);
Linas Vepstas8b553f32005-11-03 18:50:17 -0600385
386 /* Save the BAR's; firmware doesn't restore these after EEH reset */
387 dn = pci_device_to_OF_node(dev);
388 eeh_save_bars(dev, PCI_DN(dn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390
391#ifdef DEBUG
392 /* Verify tree built up above, echo back the list of addrs. */
393 pci_addr_cache_print(&pci_io_addr_cache_root);
394#endif
395}
396
397/* --------------------------------------------------------------- */
398/* Above lies the PCI Address Cache. Below lies the EEH event infrastructure */
399
Linas Vepstasdf7242b2005-11-03 18:49:01 -0600400void eeh_slot_error_detail (struct pci_dn *pdn, int severity)
401{
402 unsigned long flags;
403 int rc;
404
405 /* Log the error with the rtas logger */
406 spin_lock_irqsave(&slot_errbuf_lock, flags);
407 memset(slot_errbuf, 0, eeh_error_buf_size);
408
409 rc = rtas_call(ibm_slot_error_detail,
410 8, 1, NULL, pdn->eeh_config_addr,
411 BUID_HI(pdn->phb->buid),
412 BUID_LO(pdn->phb->buid), NULL, 0,
413 virt_to_phys(slot_errbuf),
414 eeh_error_buf_size,
415 severity);
416
417 if (rc == 0)
418 log_error(slot_errbuf, ERR_TYPE_RTAS_LOG, 0);
419 spin_unlock_irqrestore(&slot_errbuf_lock, flags);
420}
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * read_slot_reset_state - Read the reset state of a device node's slot
424 * @dn: device node to read
425 * @rets: array to return results in
426 */
Linas Vepstas69376502005-11-03 18:47:50 -0600427static int read_slot_reset_state(struct pci_dn *pdn, int rets[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 int token, outputs;
430
431 if (ibm_read_slot_reset_state2 != RTAS_UNKNOWN_SERVICE) {
432 token = ibm_read_slot_reset_state2;
433 outputs = 4;
434 } else {
435 token = ibm_read_slot_reset_state;
Linas Vepstas69376502005-11-03 18:47:50 -0600436 rets[2] = 0; /* fake PE Unavailable info */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 outputs = 3;
438 }
439
Paul Mackerras16353172005-09-06 13:17:54 +1000440 return rtas_call(token, 3, outputs, rets, pdn->eeh_config_addr,
441 BUID_HI(pdn->phb->buid), BUID_LO(pdn->phb->buid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 * eeh_token_to_phys - convert EEH address token to phys address
Linas Vepstas69376502005-11-03 18:47:50 -0600446 * @token i/o token, should be address in the form 0xA....
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 */
448static inline unsigned long eeh_token_to_phys(unsigned long token)
449{
450 pte_t *ptep;
451 unsigned long pa;
452
David Gibson20cee162005-06-21 17:15:31 -0700453 ptep = find_linux_pte(init_mm.pgd, token);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (!ptep)
455 return token;
456 pa = pte_pfn(*ptep) << PAGE_SHIFT;
457
458 return pa | (token & (PAGE_SIZE-1));
459}
460
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600461/**
462 * Return the "partitionable endpoint" (pe) under which this device lies
463 */
464static struct device_node * find_device_pe(struct device_node *dn)
465{
466 while ((dn->parent) && PCI_DN(dn->parent) &&
467 (PCI_DN(dn->parent)->eeh_mode & EEH_MODE_SUPPORTED)) {
468 dn = dn->parent;
469 }
470 return dn;
471}
472
473/** Mark all devices that are peers of this device as failed.
474 * Mark the device driver too, so that it can see the failure
475 * immediately; this is critical, since some drivers poll
476 * status registers in interrupts ... If a driver is polling,
477 * and the slot is frozen, then the driver can deadlock in
478 * an interrupt context, which is bad.
479 */
480
481static inline void __eeh_mark_slot (struct device_node *dn)
482{
483 while (dn) {
484 PCI_DN(dn)->eeh_mode |= EEH_MODE_ISOLATED;
485
486 if (dn->child)
487 __eeh_mark_slot (dn->child);
488 dn = dn->sibling;
489 }
490}
491
492static inline void __eeh_clear_slot (struct device_node *dn)
493{
494 while (dn) {
495 PCI_DN(dn)->eeh_mode &= ~EEH_MODE_ISOLATED;
496 if (dn->child)
497 __eeh_clear_slot (dn->child);
498 dn = dn->sibling;
499 }
500}
501
502static inline void eeh_clear_slot (struct device_node *dn)
503{
504 unsigned long flags;
505 spin_lock_irqsave(&confirm_error_lock, flags);
506 __eeh_clear_slot (dn);
507 spin_unlock_irqrestore(&confirm_error_lock, flags);
508}
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510/**
511 * eeh_dn_check_failure - check if all 1's data is due to EEH slot freeze
512 * @dn device node
513 * @dev pci device, if known
514 *
515 * Check for an EEH failure for the given device node. Call this
516 * routine if the result of a read was all 0xff's and you want to
517 * find out if this is due to an EEH slot freeze. This routine
518 * will query firmware for the EEH status.
519 *
520 * Returns 0 if there has not been an EEH error; otherwise returns
Linas Vepstas69376502005-11-03 18:47:50 -0600521 * a non-zero value and queues up a slot isolation event notification.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 *
523 * It is safe to call this routine in an interrupt context.
524 */
525int eeh_dn_check_failure(struct device_node *dn, struct pci_dev *dev)
526{
527 int ret;
528 int rets[3];
529 unsigned long flags;
Paul Mackerras16353172005-09-06 13:17:54 +1000530 struct pci_dn *pdn;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600531 struct device_node *pe_dn;
532 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 __get_cpu_var(total_mmio_ffs)++;
535
536 if (!eeh_subsystem_enabled)
537 return 0;
538
Linas Vepstas177bc932005-11-03 18:48:52 -0600539 if (!dn) {
540 __get_cpu_var(no_dn)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return 0;
Linas Vepstas177bc932005-11-03 18:48:52 -0600542 }
Linas Vepstas69376502005-11-03 18:47:50 -0600543 pdn = PCI_DN(dn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 /* Access to IO BARs might get this far and still not want checking. */
Linas Vepstasf8632c82005-11-03 18:49:45 -0600546 if (!(pdn->eeh_mode & EEH_MODE_SUPPORTED) ||
Paul Mackerras16353172005-09-06 13:17:54 +1000547 pdn->eeh_mode & EEH_MODE_NOCHECK) {
Linas Vepstas177bc932005-11-03 18:48:52 -0600548 __get_cpu_var(ignored_check)++;
549#ifdef DEBUG
Linas Vepstasf8632c82005-11-03 18:49:45 -0600550 printk ("EEH:ignored check (%x) for %s %s\n",
551 pdn->eeh_mode, pci_name (dev), dn->full_name);
Linas Vepstas177bc932005-11-03 18:48:52 -0600552#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return 0;
554 }
555
Paul Mackerras16353172005-09-06 13:17:54 +1000556 if (!pdn->eeh_config_addr) {
Linas Vepstas177bc932005-11-03 18:48:52 -0600557 __get_cpu_var(no_cfg_addr)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return 0;
559 }
560
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600561 /* If we already have a pending isolation event for this
562 * slot, we know it's bad already, we don't need to check.
563 * Do this checking under a lock; as multiple PCI devices
564 * in one slot might report errors simultaneously, and we
565 * only want one error recovery routine running.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 */
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600567 spin_lock_irqsave(&confirm_error_lock, flags);
568 rc = 1;
Paul Mackerras16353172005-09-06 13:17:54 +1000569 if (pdn->eeh_mode & EEH_MODE_ISOLATED) {
Linas Vepstas5c1344e2005-11-03 18:49:31 -0600570 pdn->eeh_check_count ++;
571 if (pdn->eeh_check_count >= EEH_MAX_FAILS) {
572 printk (KERN_ERR "EEH: Device driver ignored %d bad reads, panicing\n",
573 pdn->eeh_check_count);
574 dump_stack();
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 /* re-read the slot reset state */
Linas Vepstas69376502005-11-03 18:47:50 -0600577 if (read_slot_reset_state(pdn, rets) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 rets[0] = -1; /* reset state unknown */
Linas Vepstas5c1344e2005-11-03 18:49:31 -0600579
580 /* If we are here, then we hit an infinite loop. Stop. */
581 panic("EEH: MMIO halt (%d) on device:%s\n", rets[0], pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600583 goto dn_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585
586 /*
587 * Now test for an EEH failure. This is VERY expensive.
588 * Note that the eeh_config_addr may be a parent device
589 * in the case of a device behind a bridge, or it may be
590 * function zero of a multi-function device.
591 * In any case they must share a common PHB.
592 */
Linas Vepstas69376502005-11-03 18:47:50 -0600593 ret = read_slot_reset_state(pdn, rets);
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600594
595 /* If the call to firmware failed, punt */
596 if (ret != 0) {
597 printk(KERN_WARNING "EEH: read_slot_reset_state() failed; rc=%d dn=%s\n",
598 ret, dn->full_name);
599 __get_cpu_var(false_positives)++;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600600 rc = 0;
601 goto dn_unlock;
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600602 }
603
604 /* If EEH is not supported on this device, punt. */
605 if (rets[1] != 1) {
606 printk(KERN_WARNING "EEH: event on unsupported device, rc=%d dn=%s\n",
607 ret, dn->full_name);
608 __get_cpu_var(false_positives)++;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600609 rc = 0;
610 goto dn_unlock;
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600611 }
612
613 /* If not the kind of error we know about, punt. */
614 if (rets[0] != 2 && rets[0] != 4 && rets[0] != 5) {
615 __get_cpu_var(false_positives)++;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600616 rc = 0;
617 goto dn_unlock;
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600618 }
619
620 /* Note that config-io to empty slots may fail;
621 * we recognize empty because they don't have children. */
622 if ((rets[0] == 5) && (dn->child == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 __get_cpu_var(false_positives)++;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600624 rc = 0;
625 goto dn_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600628 __get_cpu_var(slot_resets)++;
629
630 /* Avoid repeated reports of this failure, including problems
631 * with other functions on this device, and functions under
632 * bridges. */
633 pe_dn = find_device_pe (dn);
634 __eeh_mark_slot (pe_dn);
635 spin_unlock_irqrestore(&confirm_error_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Linas Vepstas172ca922005-11-03 18:50:04 -0600637 eeh_send_failure_event (dn, dev, rets[0], rets[2]);
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 /* Most EEH events are due to device driver bugs. Having
640 * a stack trace will help the device-driver authors figure
641 * out what happened. So print that out. */
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600642 if (rets[0] != 5) dump_stack();
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600643 return 1;
644
645dn_unlock:
646 spin_unlock_irqrestore(&confirm_error_lock, flags);
647 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600650EXPORT_SYMBOL_GPL(eeh_dn_check_failure);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652/**
653 * eeh_check_failure - check if all 1's data is due to EEH slot freeze
654 * @token i/o token, should be address in the form 0xA....
655 * @val value, should be all 1's (XXX why do we need this arg??)
656 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 * Check for an EEH failure at the given token address. Call this
658 * routine if the result of a read was all 0xff's and you want to
659 * find out if this is due to an EEH slot freeze event. This routine
660 * will query firmware for the EEH status.
661 *
662 * Note this routine is safe to call in an interrupt context.
663 */
664unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
665{
666 unsigned long addr;
667 struct pci_dev *dev;
668 struct device_node *dn;
669
670 /* Finding the phys addr + pci device; this is pretty quick. */
671 addr = eeh_token_to_phys((unsigned long __force) token);
672 dev = pci_get_device_by_addr(addr);
Linas Vepstas177bc932005-11-03 18:48:52 -0600673 if (!dev) {
674 __get_cpu_var(no_device)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return val;
Linas Vepstas177bc932005-11-03 18:48:52 -0600676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 dn = pci_device_to_OF_node(dev);
679 eeh_dn_check_failure (dn, dev);
680
681 pci_dev_put(dev);
682 return val;
683}
684
685EXPORT_SYMBOL(eeh_check_failure);
686
Linas Vepstas172ca922005-11-03 18:50:04 -0600687/* ------------------------------------------------------------- */
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600688/* The code below deals with error recovery */
689
690/** Return negative value if a permanent error, else return
691 * a number of milliseconds to wait until the PCI slot is
692 * ready to be used.
693 */
694static int
695eeh_slot_availability(struct pci_dn *pdn)
696{
697 int rc;
698 int rets[3];
699
700 rc = read_slot_reset_state(pdn, rets);
701
702 if (rc) return rc;
703
704 if (rets[1] == 0) return -1; /* EEH is not supported */
705 if (rets[0] == 0) return 0; /* Oll Korrect */
706 if (rets[0] == 5) {
707 if (rets[2] == 0) return -1; /* permanently unavailable */
708 return rets[2]; /* number of millisecs to wait */
709 }
710 return -1;
711}
712
713/** rtas_pci_slot_reset raises/lowers the pci #RST line
714 * state: 1/0 to raise/lower the #RST
715 *
716 * Clear the EEH-frozen condition on a slot. This routine
717 * asserts the PCI #RST line if the 'state' argument is '1',
718 * and drops the #RST line if 'state is '0'. This routine is
719 * safe to call in an interrupt context.
720 *
721 */
722
723static void
724rtas_pci_slot_reset(struct pci_dn *pdn, int state)
725{
726 int rc;
727
728 BUG_ON (pdn==NULL);
729
730 if (!pdn->phb) {
731 printk (KERN_WARNING "EEH: in slot reset, device node %s has no phb\n",
732 pdn->node->full_name);
733 return;
734 }
735
736 rc = rtas_call(ibm_set_slot_reset,4,1, NULL,
737 pdn->eeh_config_addr,
738 BUID_HI(pdn->phb->buid),
739 BUID_LO(pdn->phb->buid),
740 state);
741 if (rc) {
742 printk (KERN_WARNING "EEH: Unable to reset the failed slot, (%d) #RST=%d dn=%s\n",
743 rc, state, pdn->node->full_name);
744 return;
745 }
746
747 if (state == 0)
748 eeh_clear_slot (pdn->node->parent->child);
749}
750
751/** rtas_set_slot_reset -- assert the pci #RST line for 1/4 second
752 * dn -- device node to be reset.
753 */
754
755void
756rtas_set_slot_reset(struct pci_dn *pdn)
757{
758 int i, rc;
759
760 rtas_pci_slot_reset (pdn, 1);
761
762 /* The PCI bus requires that the reset be held high for at least
763 * a 100 milliseconds. We wait a bit longer 'just in case'. */
764
765#define PCI_BUS_RST_HOLD_TIME_MSEC 250
766 msleep (PCI_BUS_RST_HOLD_TIME_MSEC);
767 rtas_pci_slot_reset (pdn, 0);
768
769 /* After a PCI slot has been reset, the PCI Express spec requires
770 * a 1.5 second idle time for the bus to stabilize, before starting
771 * up traffic. */
772#define PCI_BUS_SETTLE_TIME_MSEC 1800
773 msleep (PCI_BUS_SETTLE_TIME_MSEC);
774
775 /* Now double check with the firmware to make sure the device is
776 * ready to be used; if not, wait for recovery. */
777 for (i=0; i<10; i++) {
778 rc = eeh_slot_availability (pdn);
779 if (rc <= 0) break;
780
781 msleep (rc+100);
782 }
783}
784
Linas Vepstas8b553f32005-11-03 18:50:17 -0600785/* ------------------------------------------------------- */
786/** Save and restore of PCI BARs
787 *
788 * Although firmware will set up BARs during boot, it doesn't
789 * set up device BAR's after a device reset, although it will,
790 * if requested, set up bridge configuration. Thus, we need to
791 * configure the PCI devices ourselves.
792 */
793
794/**
795 * __restore_bars - Restore the Base Address Registers
796 * Loads the PCI configuration space base address registers,
797 * the expansion ROM base address, the latency timer, and etc.
798 * from the saved values in the device node.
799 */
800static inline void __restore_bars (struct pci_dn *pdn)
801{
802 int i;
803
804 if (NULL==pdn->phb) return;
805 for (i=4; i<10; i++) {
806 rtas_write_config(pdn, i*4, 4, pdn->config_space[i]);
807 }
808
809 /* 12 == Expansion ROM Address */
810 rtas_write_config(pdn, 12*4, 4, pdn->config_space[12]);
811
812#define BYTE_SWAP(OFF) (8*((OFF)/4)+3-(OFF))
813#define SAVED_BYTE(OFF) (((u8 *)(pdn->config_space))[BYTE_SWAP(OFF)])
814
815 rtas_write_config (pdn, PCI_CACHE_LINE_SIZE, 1,
816 SAVED_BYTE(PCI_CACHE_LINE_SIZE));
817
818 rtas_write_config (pdn, PCI_LATENCY_TIMER, 1,
819 SAVED_BYTE(PCI_LATENCY_TIMER));
820
821 /* max latency, min grant, interrupt pin and line */
822 rtas_write_config(pdn, 15*4, 4, pdn->config_space[15]);
823}
824
825/**
826 * eeh_restore_bars - restore the PCI config space info
827 *
828 * This routine performs a recursive walk to the children
829 * of this device as well.
830 */
831void eeh_restore_bars(struct pci_dn *pdn)
832{
833 struct device_node *dn;
834 if (!pdn)
835 return;
836
837 if (! pdn->eeh_is_bridge)
838 __restore_bars (pdn);
839
840 dn = pdn->node->child;
841 while (dn) {
842 eeh_restore_bars (PCI_DN(dn));
843 dn = dn->sibling;
844 }
845}
846
847/**
848 * eeh_save_bars - save device bars
849 *
850 * Save the values of the device bars. Unlike the restore
851 * routine, this routine is *not* recursive. This is because
852 * PCI devices are added individuallly; but, for the restore,
853 * an entire slot is reset at a time.
854 */
855static void eeh_save_bars(struct pci_dev * pdev, struct pci_dn *pdn)
856{
857 int i;
858
859 if (!pdev || !pdn )
860 return;
861
862 for (i = 0; i < 16; i++)
863 pci_read_config_dword(pdev, i * 4, &pdn->config_space[i]);
864
865 if (pdev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
866 pdn->eeh_is_bridge = 1;
867}
868
869void
870rtas_configure_bridge(struct pci_dn *pdn)
871{
872 int token = rtas_token ("ibm,configure-bridge");
873 int rc;
874
875 if (token == RTAS_UNKNOWN_SERVICE)
876 return;
877 rc = rtas_call(token,3,1, NULL,
878 pdn->eeh_config_addr,
879 BUID_HI(pdn->phb->buid),
880 BUID_LO(pdn->phb->buid));
881 if (rc) {
882 printk (KERN_WARNING "EEH: Unable to configure device bridge (%d) for %s\n",
883 rc, pdn->node->full_name);
884 }
885}
886
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600887/* ------------------------------------------------------------- */
Linas Vepstas172ca922005-11-03 18:50:04 -0600888/* The code below deals with enabling EEH for devices during the
889 * early boot sequence. EEH must be enabled before any PCI probing
890 * can be done.
891 */
892
893#define EEH_ENABLE 1
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895struct eeh_early_enable_info {
896 unsigned int buid_hi;
897 unsigned int buid_lo;
898};
899
900/* Enable eeh for the given device node. */
901static void *early_enable_eeh(struct device_node *dn, void *data)
902{
903 struct eeh_early_enable_info *info = data;
904 int ret;
905 char *status = get_property(dn, "status", NULL);
906 u32 *class_code = (u32 *)get_property(dn, "class-code", NULL);
907 u32 *vendor_id = (u32 *)get_property(dn, "vendor-id", NULL);
908 u32 *device_id = (u32 *)get_property(dn, "device-id", NULL);
909 u32 *regs;
910 int enable;
Linas Vepstas69376502005-11-03 18:47:50 -0600911 struct pci_dn *pdn = PCI_DN(dn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Paul Mackerras16353172005-09-06 13:17:54 +1000913 pdn->eeh_mode = 0;
Linas Vepstas5c1344e2005-11-03 18:49:31 -0600914 pdn->eeh_check_count = 0;
915 pdn->eeh_freeze_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 if (status && strcmp(status, "ok") != 0)
918 return NULL; /* ignore devices with bad status */
919
920 /* Ignore bad nodes. */
921 if (!class_code || !vendor_id || !device_id)
922 return NULL;
923
924 /* There is nothing to check on PCI to ISA bridges */
925 if (dn->type && !strcmp(dn->type, "isa")) {
Paul Mackerras16353172005-09-06 13:17:54 +1000926 pdn->eeh_mode |= EEH_MODE_NOCHECK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return NULL;
928 }
929
930 /*
931 * Now decide if we are going to "Disable" EEH checking
932 * for this device. We still run with the EEH hardware active,
933 * but we won't be checking for ff's. This means a driver
934 * could return bad data (very bad!), an interrupt handler could
935 * hang waiting on status bits that won't change, etc.
936 * But there are a few cases like display devices that make sense.
937 */
938 enable = 1; /* i.e. we will do checking */
939 if ((*class_code >> 16) == PCI_BASE_CLASS_DISPLAY)
940 enable = 0;
941
942 if (!enable)
Paul Mackerras16353172005-09-06 13:17:54 +1000943 pdn->eeh_mode |= EEH_MODE_NOCHECK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 /* Ok... see if this device supports EEH. Some do, some don't,
946 * and the only way to find out is to check each and every one. */
947 regs = (u32 *)get_property(dn, "reg", NULL);
948 if (regs) {
949 /* First register entry is addr (00BBSS00) */
950 /* Try to enable eeh */
951 ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
Linas Vepstas172ca922005-11-03 18:50:04 -0600952 regs[0], info->buid_hi, info->buid_lo,
953 EEH_ENABLE);
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (ret == 0) {
956 eeh_subsystem_enabled = 1;
Paul Mackerras16353172005-09-06 13:17:54 +1000957 pdn->eeh_mode |= EEH_MODE_SUPPORTED;
958 pdn->eeh_config_addr = regs[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959#ifdef DEBUG
960 printk(KERN_DEBUG "EEH: %s: eeh enabled\n", dn->full_name);
961#endif
962 } else {
963
964 /* This device doesn't support EEH, but it may have an
965 * EEH parent, in which case we mark it as supported. */
Linas Vepstas69376502005-11-03 18:47:50 -0600966 if (dn->parent && PCI_DN(dn->parent)
Paul Mackerras16353172005-09-06 13:17:54 +1000967 && (PCI_DN(dn->parent)->eeh_mode & EEH_MODE_SUPPORTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 /* Parent supports EEH. */
Paul Mackerras16353172005-09-06 13:17:54 +1000969 pdn->eeh_mode |= EEH_MODE_SUPPORTED;
970 pdn->eeh_config_addr = PCI_DN(dn->parent)->eeh_config_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return NULL;
972 }
973 }
974 } else {
975 printk(KERN_WARNING "EEH: %s: unable to get reg property.\n",
976 dn->full_name);
977 }
978
Linas Vepstas69376502005-11-03 18:47:50 -0600979 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
982/*
983 * Initialize EEH by trying to enable it for all of the adapters in the system.
984 * As a side effect we can determine here if eeh is supported at all.
985 * Note that we leave EEH on so failed config cycles won't cause a machine
986 * check. If a user turns off EEH for a particular adapter they are really
987 * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
988 * grant access to a slot if EEH isn't enabled, and so we always enable
989 * EEH for all slots/all devices.
990 *
991 * The eeh-force-off option disables EEH checking globally, for all slots.
992 * Even if force-off is set, the EEH hardware is still enabled, so that
993 * newer systems can boot.
994 */
995void __init eeh_init(void)
996{
997 struct device_node *phb, *np;
998 struct eeh_early_enable_info info;
999
Linas Vepstasfd761fd2005-11-03 18:49:23 -06001000 spin_lock_init(&confirm_error_lock);
Linas Vepstasdf7242b2005-11-03 18:49:01 -06001001 spin_lock_init(&slot_errbuf_lock);
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 np = of_find_node_by_path("/rtas");
1004 if (np == NULL)
1005 return;
1006
1007 ibm_set_eeh_option = rtas_token("ibm,set-eeh-option");
1008 ibm_set_slot_reset = rtas_token("ibm,set-slot-reset");
1009 ibm_read_slot_reset_state2 = rtas_token("ibm,read-slot-reset-state2");
1010 ibm_read_slot_reset_state = rtas_token("ibm,read-slot-reset-state");
1011 ibm_slot_error_detail = rtas_token("ibm,slot-error-detail");
1012
1013 if (ibm_set_eeh_option == RTAS_UNKNOWN_SERVICE)
1014 return;
1015
1016 eeh_error_buf_size = rtas_token("rtas-error-log-max");
1017 if (eeh_error_buf_size == RTAS_UNKNOWN_SERVICE) {
1018 eeh_error_buf_size = 1024;
1019 }
1020 if (eeh_error_buf_size > RTAS_ERROR_LOG_MAX) {
1021 printk(KERN_WARNING "EEH: rtas-error-log-max is bigger than allocated "
1022 "buffer ! (%d vs %d)", eeh_error_buf_size, RTAS_ERROR_LOG_MAX);
1023 eeh_error_buf_size = RTAS_ERROR_LOG_MAX;
1024 }
1025
1026 /* Enable EEH for all adapters. Note that eeh requires buid's */
1027 for (phb = of_find_node_by_name(NULL, "pci"); phb;
1028 phb = of_find_node_by_name(phb, "pci")) {
1029 unsigned long buid;
1030
1031 buid = get_phb_buid(phb);
Linas Vepstas69376502005-11-03 18:47:50 -06001032 if (buid == 0 || PCI_DN(phb) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 continue;
1034
1035 info.buid_lo = BUID_LO(buid);
1036 info.buid_hi = BUID_HI(buid);
1037 traverse_pci_devices(phb, early_enable_eeh, &info);
1038 }
1039
1040 if (eeh_subsystem_enabled)
1041 printk(KERN_INFO "EEH: PCI Enhanced I/O Error Handling Enabled\n");
1042 else
1043 printk(KERN_WARNING "EEH: No capable adapters found\n");
1044}
1045
1046/**
1047 * eeh_add_device_early - enable EEH for the indicated device_node
1048 * @dn: device node for which to set up EEH
1049 *
1050 * This routine must be used to perform EEH initialization for PCI
1051 * devices that were added after system boot (e.g. hotplug, dlpar).
1052 * This routine must be called before any i/o is performed to the
1053 * adapter (inluding any config-space i/o).
1054 * Whether this actually enables EEH or not for this device depends
1055 * on the CEC architecture, type of the device, on earlier boot
1056 * command-line arguments & etc.
1057 */
1058void eeh_add_device_early(struct device_node *dn)
1059{
1060 struct pci_controller *phb;
1061 struct eeh_early_enable_info info;
1062
Linas Vepstas69376502005-11-03 18:47:50 -06001063 if (!dn || !PCI_DN(dn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return;
Paul Mackerras16353172005-09-06 13:17:54 +10001065 phb = PCI_DN(dn)->phb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (NULL == phb || 0 == phb->buid) {
Linas Vepstas69376502005-11-03 18:47:50 -06001067 printk(KERN_WARNING "EEH: Expected buid but found none for %s\n",
1068 dn->full_name);
1069 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 return;
1071 }
1072
1073 info.buid_hi = BUID_HI(phb->buid);
1074 info.buid_lo = BUID_LO(phb->buid);
1075 early_enable_eeh(dn, &info);
1076}
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001077EXPORT_SYMBOL_GPL(eeh_add_device_early);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079/**
1080 * eeh_add_device_late - perform EEH initialization for the indicated pci device
1081 * @dev: pci device for which to set up EEH
1082 *
1083 * This routine must be used to complete EEH initialization for PCI
1084 * devices that were added after system boot (e.g. hotplug, dlpar).
1085 */
1086void eeh_add_device_late(struct pci_dev *dev)
1087{
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001088 struct device_node *dn;
Linas Vepstas8b553f32005-11-03 18:50:17 -06001089 struct pci_dn *pdn;
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (!dev || !eeh_subsystem_enabled)
1092 return;
1093
1094#ifdef DEBUG
Adrian Bunk982245f2005-07-17 04:22:20 +02001095 printk(KERN_DEBUG "EEH: adding device %s\n", pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096#endif
1097
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001098 pci_dev_get (dev);
1099 dn = pci_device_to_OF_node(dev);
Linas Vepstas8b553f32005-11-03 18:50:17 -06001100 pdn = PCI_DN(dn);
1101 pdn->pcidev = dev;
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 pci_addr_cache_insert_device (dev);
Linas Vepstas8b553f32005-11-03 18:50:17 -06001104 eeh_save_bars(dev, pdn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001106EXPORT_SYMBOL_GPL(eeh_add_device_late);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108/**
1109 * eeh_remove_device - undo EEH setup for the indicated pci device
1110 * @dev: pci device to be removed
1111 *
1112 * This routine should be when a device is removed from a running
1113 * system (e.g. by hotplug or dlpar).
1114 */
1115void eeh_remove_device(struct pci_dev *dev)
1116{
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001117 struct device_node *dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (!dev || !eeh_subsystem_enabled)
1119 return;
1120
1121 /* Unregister the device with the EEH/PCI address search system */
1122#ifdef DEBUG
Adrian Bunk982245f2005-07-17 04:22:20 +02001123 printk(KERN_DEBUG "EEH: remove device %s\n", pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124#endif
1125 pci_addr_cache_remove_device(dev);
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001126
1127 dn = pci_device_to_OF_node(dev);
1128 PCI_DN(dn)->pcidev = NULL;
1129 pci_dev_put (dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001131EXPORT_SYMBOL_GPL(eeh_remove_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133static int proc_eeh_show(struct seq_file *m, void *v)
1134{
1135 unsigned int cpu;
1136 unsigned long ffs = 0, positives = 0, failures = 0;
1137 unsigned long resets = 0;
Linas Vepstas177bc932005-11-03 18:48:52 -06001138 unsigned long no_dev = 0, no_dn = 0, no_cfg = 0, no_check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 for_each_cpu(cpu) {
1141 ffs += per_cpu(total_mmio_ffs, cpu);
1142 positives += per_cpu(false_positives, cpu);
1143 failures += per_cpu(ignored_failures, cpu);
1144 resets += per_cpu(slot_resets, cpu);
Linas Vepstas177bc932005-11-03 18:48:52 -06001145 no_dev += per_cpu(no_device, cpu);
1146 no_dn += per_cpu(no_dn, cpu);
1147 no_cfg += per_cpu(no_cfg_addr, cpu);
1148 no_check += per_cpu(ignored_check, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
1150
1151 if (0 == eeh_subsystem_enabled) {
1152 seq_printf(m, "EEH Subsystem is globally disabled\n");
1153 seq_printf(m, "eeh_total_mmio_ffs=%ld\n", ffs);
1154 } else {
1155 seq_printf(m, "EEH Subsystem is enabled\n");
Linas Vepstas177bc932005-11-03 18:48:52 -06001156 seq_printf(m,
1157 "no device=%ld\n"
1158 "no device node=%ld\n"
1159 "no config address=%ld\n"
1160 "check not wanted=%ld\n"
1161 "eeh_total_mmio_ffs=%ld\n"
1162 "eeh_false_positives=%ld\n"
1163 "eeh_ignored_failures=%ld\n"
1164 "eeh_slot_resets=%ld\n",
1165 no_dev, no_dn, no_cfg, no_check,
1166 ffs, positives, failures, resets);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168
1169 return 0;
1170}
1171
1172static int proc_eeh_open(struct inode *inode, struct file *file)
1173{
1174 return single_open(file, proc_eeh_show, NULL);
1175}
1176
1177static struct file_operations proc_eeh_operations = {
1178 .open = proc_eeh_open,
1179 .read = seq_read,
1180 .llseek = seq_lseek,
1181 .release = single_release,
1182};
1183
1184static int __init eeh_init_proc(void)
1185{
1186 struct proc_dir_entry *e;
1187
Paul Mackerras799d6042005-11-10 13:37:51 +11001188 if (platform_is_pseries()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 e = create_proc_entry("ppc64/eeh", 0, NULL);
1190 if (e)
1191 e->proc_fops = &proc_eeh_operations;
1192 }
1193
1194 return 0;
1195}
1196__initcall(eeh_init_proc);