blob: 1fec99d53311bb6f99875e1875172428547dabda [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/init.h>
21#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pci.h>
23#include <linux/proc_fs.h>
24#include <linux/rbtree.h>
25#include <linux/seq_file.h>
26#include <linux/spinlock.h>
Linas Vepstas69376502005-11-03 18:47:50 -060027#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/eeh.h>
Linas Vepstas172ca922005-11-03 18:50:04 -060029#include <asm/eeh_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/io.h>
31#include <asm/machdep.h>
Stephen Rothwelld3878992005-09-28 02:50:25 +100032#include <asm/ppc-pci.h>
Linas Vepstas172ca922005-11-03 18:50:04 -060033#include <asm/rtas.h>
34#include <asm/systemcfg.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
79/* RTAS tokens */
80static int ibm_set_eeh_option;
81static int ibm_set_slot_reset;
82static int ibm_read_slot_reset_state;
83static int ibm_read_slot_reset_state2;
84static int ibm_slot_error_detail;
85
86static int eeh_subsystem_enabled;
87
Linas Vepstasfd761fd2005-11-03 18:49:23 -060088/* Lock to avoid races due to multiple reports of an error */
89static DEFINE_SPINLOCK(confirm_error_lock);
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/* Buffer for reporting slot-error-detail rtas calls */
92static unsigned char slot_errbuf[RTAS_ERROR_LOG_MAX];
93static DEFINE_SPINLOCK(slot_errbuf_lock);
94static int eeh_error_buf_size;
95
96/* System monitoring statistics */
Linas Vepstas177bc932005-11-03 18:48:52 -060097static DEFINE_PER_CPU(unsigned long, no_device);
98static DEFINE_PER_CPU(unsigned long, no_dn);
99static DEFINE_PER_CPU(unsigned long, no_cfg_addr);
100static DEFINE_PER_CPU(unsigned long, ignored_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static DEFINE_PER_CPU(unsigned long, total_mmio_ffs);
102static DEFINE_PER_CPU(unsigned long, false_positives);
103static DEFINE_PER_CPU(unsigned long, ignored_failures);
104static DEFINE_PER_CPU(unsigned long, slot_resets);
105
106/**
107 * The pci address cache subsystem. This subsystem places
108 * PCI device address resources into a red-black tree, sorted
109 * according to the address range, so that given only an i/o
110 * address, the corresponding PCI device can be **quickly**
111 * found. It is safe to perform an address lookup in an interrupt
112 * context; this ability is an important feature.
113 *
114 * Currently, the only customer of this code is the EEH subsystem;
115 * thus, this code has been somewhat tailored to suit EEH better.
116 * In particular, the cache does *not* hold the addresses of devices
117 * for which EEH is not enabled.
118 *
119 * (Implementation Note: The RB tree seems to be better/faster
120 * than any hash algo I could think of for this problem, even
121 * with the penalty of slow pointer chases for d-cache misses).
122 */
123struct pci_io_addr_range
124{
125 struct rb_node rb_node;
126 unsigned long addr_lo;
127 unsigned long addr_hi;
128 struct pci_dev *pcidev;
129 unsigned int flags;
130};
131
132static struct pci_io_addr_cache
133{
134 struct rb_root rb_root;
135 spinlock_t piar_lock;
136} pci_io_addr_cache_root;
137
138static inline struct pci_dev *__pci_get_device_by_addr(unsigned long addr)
139{
140 struct rb_node *n = pci_io_addr_cache_root.rb_root.rb_node;
141
142 while (n) {
143 struct pci_io_addr_range *piar;
144 piar = rb_entry(n, struct pci_io_addr_range, rb_node);
145
146 if (addr < piar->addr_lo) {
147 n = n->rb_left;
148 } else {
149 if (addr > piar->addr_hi) {
150 n = n->rb_right;
151 } else {
152 pci_dev_get(piar->pcidev);
153 return piar->pcidev;
154 }
155 }
156 }
157
158 return NULL;
159}
160
161/**
162 * pci_get_device_by_addr - Get device, given only address
163 * @addr: mmio (PIO) phys address or i/o port number
164 *
165 * Given an mmio phys address, or a port number, find a pci device
166 * that implements this address. Be sure to pci_dev_put the device
167 * when finished. I/O port numbers are assumed to be offset
168 * from zero (that is, they do *not* have pci_io_addr added in).
169 * It is safe to call this function within an interrupt.
170 */
171static struct pci_dev *pci_get_device_by_addr(unsigned long addr)
172{
173 struct pci_dev *dev;
174 unsigned long flags;
175
176 spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
177 dev = __pci_get_device_by_addr(addr);
178 spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
179 return dev;
180}
181
182#ifdef DEBUG
183/*
184 * Handy-dandy debug print routine, does nothing more
185 * than print out the contents of our addr cache.
186 */
187static void pci_addr_cache_print(struct pci_io_addr_cache *cache)
188{
189 struct rb_node *n;
190 int cnt = 0;
191
192 n = rb_first(&cache->rb_root);
193 while (n) {
194 struct pci_io_addr_range *piar;
195 piar = rb_entry(n, struct pci_io_addr_range, rb_node);
Adrian Bunk982245f2005-07-17 04:22:20 +0200196 printk(KERN_DEBUG "PCI: %s addr range %d [%lx-%lx]: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 (piar->flags & IORESOURCE_IO) ? "i/o" : "mem", cnt,
Adrian Bunk982245f2005-07-17 04:22:20 +0200198 piar->addr_lo, piar->addr_hi, pci_name(piar->pcidev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 cnt++;
200 n = rb_next(n);
201 }
202}
203#endif
204
205/* Insert address range into the rb tree. */
206static struct pci_io_addr_range *
207pci_addr_cache_insert(struct pci_dev *dev, unsigned long alo,
208 unsigned long ahi, unsigned int flags)
209{
210 struct rb_node **p = &pci_io_addr_cache_root.rb_root.rb_node;
211 struct rb_node *parent = NULL;
212 struct pci_io_addr_range *piar;
213
214 /* Walk tree, find a place to insert into tree */
215 while (*p) {
216 parent = *p;
217 piar = rb_entry(parent, struct pci_io_addr_range, rb_node);
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600218 if (ahi < piar->addr_lo) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 p = &parent->rb_left;
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600220 } else if (alo > piar->addr_hi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 p = &parent->rb_right;
222 } else {
223 if (dev != piar->pcidev ||
224 alo != piar->addr_lo || ahi != piar->addr_hi) {
225 printk(KERN_WARNING "PIAR: overlapping address range\n");
226 }
227 return piar;
228 }
229 }
230 piar = (struct pci_io_addr_range *)kmalloc(sizeof(struct pci_io_addr_range), GFP_ATOMIC);
231 if (!piar)
232 return NULL;
233
234 piar->addr_lo = alo;
235 piar->addr_hi = ahi;
236 piar->pcidev = dev;
237 piar->flags = flags;
238
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600239#ifdef DEBUG
240 printk(KERN_DEBUG "PIAR: insert range=[%lx:%lx] dev=%s\n",
241 alo, ahi, pci_name (dev));
242#endif
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 rb_link_node(&piar->rb_node, parent, p);
245 rb_insert_color(&piar->rb_node, &pci_io_addr_cache_root.rb_root);
246
247 return piar;
248}
249
250static void __pci_addr_cache_insert_device(struct pci_dev *dev)
251{
252 struct device_node *dn;
Paul Mackerras16353172005-09-06 13:17:54 +1000253 struct pci_dn *pdn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 int i;
255 int inserted = 0;
256
257 dn = pci_device_to_OF_node(dev);
258 if (!dn) {
Linas Vepstas69376502005-11-03 18:47:50 -0600259 printk(KERN_WARNING "PCI: no pci dn found for dev=%s\n", pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return;
261 }
262
263 /* Skip any devices for which EEH is not enabled. */
Linas Vepstas69376502005-11-03 18:47:50 -0600264 pdn = PCI_DN(dn);
Paul Mackerras16353172005-09-06 13:17:54 +1000265 if (!(pdn->eeh_mode & EEH_MODE_SUPPORTED) ||
266 pdn->eeh_mode & EEH_MODE_NOCHECK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267#ifdef DEBUG
Linas Vepstas69376502005-11-03 18:47:50 -0600268 printk(KERN_INFO "PCI: skip building address cache for=%s - %s\n",
269 pci_name(dev), pdn->node->full_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270#endif
271 return;
272 }
273
274 /* The cache holds a reference to the device... */
275 pci_dev_get(dev);
276
277 /* Walk resources on this device, poke them into the tree */
278 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
279 unsigned long start = pci_resource_start(dev,i);
280 unsigned long end = pci_resource_end(dev,i);
281 unsigned int flags = pci_resource_flags(dev,i);
282
283 /* We are interested only bus addresses, not dma or other stuff */
284 if (0 == (flags & (IORESOURCE_IO | IORESOURCE_MEM)))
285 continue;
286 if (start == 0 || ~start == 0 || end == 0 || ~end == 0)
287 continue;
288 pci_addr_cache_insert(dev, start, end, flags);
289 inserted = 1;
290 }
291
292 /* If there was nothing to add, the cache has no reference... */
293 if (!inserted)
294 pci_dev_put(dev);
295}
296
297/**
298 * pci_addr_cache_insert_device - Add a device to the address cache
299 * @dev: PCI device whose I/O addresses we are interested in.
300 *
301 * In order to support the fast lookup of devices based on addresses,
302 * we maintain a cache of devices that can be quickly searched.
303 * This routine adds a device to that cache.
304 */
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600305static void pci_addr_cache_insert_device(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 unsigned long flags;
308
309 spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
310 __pci_addr_cache_insert_device(dev);
311 spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
312}
313
314static inline void __pci_addr_cache_remove_device(struct pci_dev *dev)
315{
316 struct rb_node *n;
317 int removed = 0;
318
319restart:
320 n = rb_first(&pci_io_addr_cache_root.rb_root);
321 while (n) {
322 struct pci_io_addr_range *piar;
323 piar = rb_entry(n, struct pci_io_addr_range, rb_node);
324
325 if (piar->pcidev == dev) {
326 rb_erase(n, &pci_io_addr_cache_root.rb_root);
327 removed = 1;
328 kfree(piar);
329 goto restart;
330 }
331 n = rb_next(n);
332 }
333
334 /* The cache no longer holds its reference to this device... */
335 if (removed)
336 pci_dev_put(dev);
337}
338
339/**
340 * pci_addr_cache_remove_device - remove pci device from addr cache
341 * @dev: device to remove
342 *
343 * Remove a device from the addr-cache tree.
344 * This is potentially expensive, since it will walk
345 * the tree multiple times (once per resource).
346 * But so what; device removal doesn't need to be that fast.
347 */
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600348static void pci_addr_cache_remove_device(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 unsigned long flags;
351
352 spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
353 __pci_addr_cache_remove_device(dev);
354 spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
355}
356
357/**
358 * pci_addr_cache_build - Build a cache of I/O addresses
359 *
360 * Build a cache of pci i/o addresses. This cache will be used to
361 * find the pci device that corresponds to a given address.
362 * This routine scans all pci busses to build the cache.
363 * Must be run late in boot process, after the pci controllers
364 * have been scaned for devices (after all device resources are known).
365 */
366void __init pci_addr_cache_build(void)
367{
368 struct pci_dev *dev = NULL;
369
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600370 if (!eeh_subsystem_enabled)
371 return;
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 spin_lock_init(&pci_io_addr_cache_root.piar_lock);
374
375 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
376 /* Ignore PCI bridges ( XXX why ??) */
377 if ((dev->class >> 16) == PCI_BASE_CLASS_BRIDGE) {
378 continue;
379 }
380 pci_addr_cache_insert_device(dev);
381 }
382
383#ifdef DEBUG
384 /* Verify tree built up above, echo back the list of addrs. */
385 pci_addr_cache_print(&pci_io_addr_cache_root);
386#endif
387}
388
389/* --------------------------------------------------------------- */
390/* Above lies the PCI Address Cache. Below lies the EEH event infrastructure */
391
Linas Vepstasdf7242b2005-11-03 18:49:01 -0600392void eeh_slot_error_detail (struct pci_dn *pdn, int severity)
393{
394 unsigned long flags;
395 int rc;
396
397 /* Log the error with the rtas logger */
398 spin_lock_irqsave(&slot_errbuf_lock, flags);
399 memset(slot_errbuf, 0, eeh_error_buf_size);
400
401 rc = rtas_call(ibm_slot_error_detail,
402 8, 1, NULL, pdn->eeh_config_addr,
403 BUID_HI(pdn->phb->buid),
404 BUID_LO(pdn->phb->buid), NULL, 0,
405 virt_to_phys(slot_errbuf),
406 eeh_error_buf_size,
407 severity);
408
409 if (rc == 0)
410 log_error(slot_errbuf, ERR_TYPE_RTAS_LOG, 0);
411 spin_unlock_irqrestore(&slot_errbuf_lock, flags);
412}
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 * read_slot_reset_state - Read the reset state of a device node's slot
416 * @dn: device node to read
417 * @rets: array to return results in
418 */
Linas Vepstas69376502005-11-03 18:47:50 -0600419static int read_slot_reset_state(struct pci_dn *pdn, int rets[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 int token, outputs;
422
423 if (ibm_read_slot_reset_state2 != RTAS_UNKNOWN_SERVICE) {
424 token = ibm_read_slot_reset_state2;
425 outputs = 4;
426 } else {
427 token = ibm_read_slot_reset_state;
Linas Vepstas69376502005-11-03 18:47:50 -0600428 rets[2] = 0; /* fake PE Unavailable info */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 outputs = 3;
430 }
431
Paul Mackerras16353172005-09-06 13:17:54 +1000432 return rtas_call(token, 3, outputs, rets, pdn->eeh_config_addr,
433 BUID_HI(pdn->phb->buid), BUID_LO(pdn->phb->buid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 * eeh_token_to_phys - convert EEH address token to phys address
Linas Vepstas69376502005-11-03 18:47:50 -0600438 * @token i/o token, should be address in the form 0xA....
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 */
440static inline unsigned long eeh_token_to_phys(unsigned long token)
441{
442 pte_t *ptep;
443 unsigned long pa;
444
David Gibson20cee162005-06-21 17:15:31 -0700445 ptep = find_linux_pte(init_mm.pgd, token);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (!ptep)
447 return token;
448 pa = pte_pfn(*ptep) << PAGE_SHIFT;
449
450 return pa | (token & (PAGE_SIZE-1));
451}
452
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600453/**
454 * Return the "partitionable endpoint" (pe) under which this device lies
455 */
456static struct device_node * find_device_pe(struct device_node *dn)
457{
458 while ((dn->parent) && PCI_DN(dn->parent) &&
459 (PCI_DN(dn->parent)->eeh_mode & EEH_MODE_SUPPORTED)) {
460 dn = dn->parent;
461 }
462 return dn;
463}
464
465/** Mark all devices that are peers of this device as failed.
466 * Mark the device driver too, so that it can see the failure
467 * immediately; this is critical, since some drivers poll
468 * status registers in interrupts ... If a driver is polling,
469 * and the slot is frozen, then the driver can deadlock in
470 * an interrupt context, which is bad.
471 */
472
473static inline void __eeh_mark_slot (struct device_node *dn)
474{
475 while (dn) {
476 PCI_DN(dn)->eeh_mode |= EEH_MODE_ISOLATED;
477
478 if (dn->child)
479 __eeh_mark_slot (dn->child);
480 dn = dn->sibling;
481 }
482}
483
484static inline void __eeh_clear_slot (struct device_node *dn)
485{
486 while (dn) {
487 PCI_DN(dn)->eeh_mode &= ~EEH_MODE_ISOLATED;
488 if (dn->child)
489 __eeh_clear_slot (dn->child);
490 dn = dn->sibling;
491 }
492}
493
494static inline void eeh_clear_slot (struct device_node *dn)
495{
496 unsigned long flags;
497 spin_lock_irqsave(&confirm_error_lock, flags);
498 __eeh_clear_slot (dn);
499 spin_unlock_irqrestore(&confirm_error_lock, flags);
500}
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502/**
503 * eeh_dn_check_failure - check if all 1's data is due to EEH slot freeze
504 * @dn device node
505 * @dev pci device, if known
506 *
507 * Check for an EEH failure for the given device node. Call this
508 * routine if the result of a read was all 0xff's and you want to
509 * find out if this is due to an EEH slot freeze. This routine
510 * will query firmware for the EEH status.
511 *
512 * Returns 0 if there has not been an EEH error; otherwise returns
Linas Vepstas69376502005-11-03 18:47:50 -0600513 * a non-zero value and queues up a slot isolation event notification.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 *
515 * It is safe to call this routine in an interrupt context.
516 */
517int eeh_dn_check_failure(struct device_node *dn, struct pci_dev *dev)
518{
519 int ret;
520 int rets[3];
521 unsigned long flags;
Paul Mackerras16353172005-09-06 13:17:54 +1000522 struct pci_dn *pdn;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600523 struct device_node *pe_dn;
524 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 __get_cpu_var(total_mmio_ffs)++;
527
528 if (!eeh_subsystem_enabled)
529 return 0;
530
Linas Vepstas177bc932005-11-03 18:48:52 -0600531 if (!dn) {
532 __get_cpu_var(no_dn)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return 0;
Linas Vepstas177bc932005-11-03 18:48:52 -0600534 }
Linas Vepstas69376502005-11-03 18:47:50 -0600535 pdn = PCI_DN(dn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 /* Access to IO BARs might get this far and still not want checking. */
Linas Vepstasf8632c82005-11-03 18:49:45 -0600538 if (!(pdn->eeh_mode & EEH_MODE_SUPPORTED) ||
Paul Mackerras16353172005-09-06 13:17:54 +1000539 pdn->eeh_mode & EEH_MODE_NOCHECK) {
Linas Vepstas177bc932005-11-03 18:48:52 -0600540 __get_cpu_var(ignored_check)++;
541#ifdef DEBUG
Linas Vepstasf8632c82005-11-03 18:49:45 -0600542 printk ("EEH:ignored check (%x) for %s %s\n",
543 pdn->eeh_mode, pci_name (dev), dn->full_name);
Linas Vepstas177bc932005-11-03 18:48:52 -0600544#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return 0;
546 }
547
Paul Mackerras16353172005-09-06 13:17:54 +1000548 if (!pdn->eeh_config_addr) {
Linas Vepstas177bc932005-11-03 18:48:52 -0600549 __get_cpu_var(no_cfg_addr)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return 0;
551 }
552
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600553 /* If we already have a pending isolation event for this
554 * slot, we know it's bad already, we don't need to check.
555 * Do this checking under a lock; as multiple PCI devices
556 * in one slot might report errors simultaneously, and we
557 * only want one error recovery routine running.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 */
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600559 spin_lock_irqsave(&confirm_error_lock, flags);
560 rc = 1;
Paul Mackerras16353172005-09-06 13:17:54 +1000561 if (pdn->eeh_mode & EEH_MODE_ISOLATED) {
Linas Vepstas5c1344e2005-11-03 18:49:31 -0600562 pdn->eeh_check_count ++;
563 if (pdn->eeh_check_count >= EEH_MAX_FAILS) {
564 printk (KERN_ERR "EEH: Device driver ignored %d bad reads, panicing\n",
565 pdn->eeh_check_count);
566 dump_stack();
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* re-read the slot reset state */
Linas Vepstas69376502005-11-03 18:47:50 -0600569 if (read_slot_reset_state(pdn, rets) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 rets[0] = -1; /* reset state unknown */
Linas Vepstas5c1344e2005-11-03 18:49:31 -0600571
572 /* If we are here, then we hit an infinite loop. Stop. */
573 panic("EEH: MMIO halt (%d) on device:%s\n", rets[0], pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600575 goto dn_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577
578 /*
579 * Now test for an EEH failure. This is VERY expensive.
580 * Note that the eeh_config_addr may be a parent device
581 * in the case of a device behind a bridge, or it may be
582 * function zero of a multi-function device.
583 * In any case they must share a common PHB.
584 */
Linas Vepstas69376502005-11-03 18:47:50 -0600585 ret = read_slot_reset_state(pdn, rets);
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600586
587 /* If the call to firmware failed, punt */
588 if (ret != 0) {
589 printk(KERN_WARNING "EEH: read_slot_reset_state() failed; rc=%d dn=%s\n",
590 ret, dn->full_name);
591 __get_cpu_var(false_positives)++;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600592 rc = 0;
593 goto dn_unlock;
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600594 }
595
596 /* If EEH is not supported on this device, punt. */
597 if (rets[1] != 1) {
598 printk(KERN_WARNING "EEH: event on unsupported device, rc=%d dn=%s\n",
599 ret, dn->full_name);
600 __get_cpu_var(false_positives)++;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600601 rc = 0;
602 goto dn_unlock;
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600603 }
604
605 /* If not the kind of error we know about, punt. */
606 if (rets[0] != 2 && rets[0] != 4 && rets[0] != 5) {
607 __get_cpu_var(false_positives)++;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600608 rc = 0;
609 goto dn_unlock;
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600610 }
611
612 /* Note that config-io to empty slots may fail;
613 * we recognize empty because they don't have children. */
614 if ((rets[0] == 5) && (dn->child == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 __get_cpu_var(false_positives)++;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600616 rc = 0;
617 goto dn_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600620 __get_cpu_var(slot_resets)++;
621
622 /* Avoid repeated reports of this failure, including problems
623 * with other functions on this device, and functions under
624 * bridges. */
625 pe_dn = find_device_pe (dn);
626 __eeh_mark_slot (pe_dn);
627 spin_unlock_irqrestore(&confirm_error_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Linas Vepstas172ca922005-11-03 18:50:04 -0600629 eeh_send_failure_event (dn, dev, rets[0], rets[2]);
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /* Most EEH events are due to device driver bugs. Having
632 * a stack trace will help the device-driver authors figure
633 * out what happened. So print that out. */
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600634 if (rets[0] != 5) dump_stack();
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600635 return 1;
636
637dn_unlock:
638 spin_unlock_irqrestore(&confirm_error_lock, flags);
639 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600642EXPORT_SYMBOL_GPL(eeh_dn_check_failure);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644/**
645 * eeh_check_failure - check if all 1's data is due to EEH slot freeze
646 * @token i/o token, should be address in the form 0xA....
647 * @val value, should be all 1's (XXX why do we need this arg??)
648 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 * Check for an EEH failure at the given token address. Call this
650 * routine if the result of a read was all 0xff's and you want to
651 * find out if this is due to an EEH slot freeze event. This routine
652 * will query firmware for the EEH status.
653 *
654 * Note this routine is safe to call in an interrupt context.
655 */
656unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
657{
658 unsigned long addr;
659 struct pci_dev *dev;
660 struct device_node *dn;
661
662 /* Finding the phys addr + pci device; this is pretty quick. */
663 addr = eeh_token_to_phys((unsigned long __force) token);
664 dev = pci_get_device_by_addr(addr);
Linas Vepstas177bc932005-11-03 18:48:52 -0600665 if (!dev) {
666 __get_cpu_var(no_device)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return val;
Linas Vepstas177bc932005-11-03 18:48:52 -0600668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 dn = pci_device_to_OF_node(dev);
671 eeh_dn_check_failure (dn, dev);
672
673 pci_dev_put(dev);
674 return val;
675}
676
677EXPORT_SYMBOL(eeh_check_failure);
678
Linas Vepstas172ca922005-11-03 18:50:04 -0600679/* ------------------------------------------------------------- */
680/* The code below deals with enabling EEH for devices during the
681 * early boot sequence. EEH must be enabled before any PCI probing
682 * can be done.
683 */
684
685#define EEH_ENABLE 1
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687struct eeh_early_enable_info {
688 unsigned int buid_hi;
689 unsigned int buid_lo;
690};
691
692/* Enable eeh for the given device node. */
693static void *early_enable_eeh(struct device_node *dn, void *data)
694{
695 struct eeh_early_enable_info *info = data;
696 int ret;
697 char *status = get_property(dn, "status", NULL);
698 u32 *class_code = (u32 *)get_property(dn, "class-code", NULL);
699 u32 *vendor_id = (u32 *)get_property(dn, "vendor-id", NULL);
700 u32 *device_id = (u32 *)get_property(dn, "device-id", NULL);
701 u32 *regs;
702 int enable;
Linas Vepstas69376502005-11-03 18:47:50 -0600703 struct pci_dn *pdn = PCI_DN(dn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Paul Mackerras16353172005-09-06 13:17:54 +1000705 pdn->eeh_mode = 0;
Linas Vepstas5c1344e2005-11-03 18:49:31 -0600706 pdn->eeh_check_count = 0;
707 pdn->eeh_freeze_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 if (status && strcmp(status, "ok") != 0)
710 return NULL; /* ignore devices with bad status */
711
712 /* Ignore bad nodes. */
713 if (!class_code || !vendor_id || !device_id)
714 return NULL;
715
716 /* There is nothing to check on PCI to ISA bridges */
717 if (dn->type && !strcmp(dn->type, "isa")) {
Paul Mackerras16353172005-09-06 13:17:54 +1000718 pdn->eeh_mode |= EEH_MODE_NOCHECK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return NULL;
720 }
721
722 /*
723 * Now decide if we are going to "Disable" EEH checking
724 * for this device. We still run with the EEH hardware active,
725 * but we won't be checking for ff's. This means a driver
726 * could return bad data (very bad!), an interrupt handler could
727 * hang waiting on status bits that won't change, etc.
728 * But there are a few cases like display devices that make sense.
729 */
730 enable = 1; /* i.e. we will do checking */
731 if ((*class_code >> 16) == PCI_BASE_CLASS_DISPLAY)
732 enable = 0;
733
734 if (!enable)
Paul Mackerras16353172005-09-06 13:17:54 +1000735 pdn->eeh_mode |= EEH_MODE_NOCHECK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 /* Ok... see if this device supports EEH. Some do, some don't,
738 * and the only way to find out is to check each and every one. */
739 regs = (u32 *)get_property(dn, "reg", NULL);
740 if (regs) {
741 /* First register entry is addr (00BBSS00) */
742 /* Try to enable eeh */
743 ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
Linas Vepstas172ca922005-11-03 18:50:04 -0600744 regs[0], info->buid_hi, info->buid_lo,
745 EEH_ENABLE);
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (ret == 0) {
748 eeh_subsystem_enabled = 1;
Paul Mackerras16353172005-09-06 13:17:54 +1000749 pdn->eeh_mode |= EEH_MODE_SUPPORTED;
750 pdn->eeh_config_addr = regs[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751#ifdef DEBUG
752 printk(KERN_DEBUG "EEH: %s: eeh enabled\n", dn->full_name);
753#endif
754 } else {
755
756 /* This device doesn't support EEH, but it may have an
757 * EEH parent, in which case we mark it as supported. */
Linas Vepstas69376502005-11-03 18:47:50 -0600758 if (dn->parent && PCI_DN(dn->parent)
Paul Mackerras16353172005-09-06 13:17:54 +1000759 && (PCI_DN(dn->parent)->eeh_mode & EEH_MODE_SUPPORTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 /* Parent supports EEH. */
Paul Mackerras16353172005-09-06 13:17:54 +1000761 pdn->eeh_mode |= EEH_MODE_SUPPORTED;
762 pdn->eeh_config_addr = PCI_DN(dn->parent)->eeh_config_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return NULL;
764 }
765 }
766 } else {
767 printk(KERN_WARNING "EEH: %s: unable to get reg property.\n",
768 dn->full_name);
769 }
770
Linas Vepstas69376502005-11-03 18:47:50 -0600771 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
774/*
775 * Initialize EEH by trying to enable it for all of the adapters in the system.
776 * As a side effect we can determine here if eeh is supported at all.
777 * Note that we leave EEH on so failed config cycles won't cause a machine
778 * check. If a user turns off EEH for a particular adapter they are really
779 * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
780 * grant access to a slot if EEH isn't enabled, and so we always enable
781 * EEH for all slots/all devices.
782 *
783 * The eeh-force-off option disables EEH checking globally, for all slots.
784 * Even if force-off is set, the EEH hardware is still enabled, so that
785 * newer systems can boot.
786 */
787void __init eeh_init(void)
788{
789 struct device_node *phb, *np;
790 struct eeh_early_enable_info info;
791
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600792 spin_lock_init(&confirm_error_lock);
Linas Vepstasdf7242b2005-11-03 18:49:01 -0600793 spin_lock_init(&slot_errbuf_lock);
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 np = of_find_node_by_path("/rtas");
796 if (np == NULL)
797 return;
798
799 ibm_set_eeh_option = rtas_token("ibm,set-eeh-option");
800 ibm_set_slot_reset = rtas_token("ibm,set-slot-reset");
801 ibm_read_slot_reset_state2 = rtas_token("ibm,read-slot-reset-state2");
802 ibm_read_slot_reset_state = rtas_token("ibm,read-slot-reset-state");
803 ibm_slot_error_detail = rtas_token("ibm,slot-error-detail");
804
805 if (ibm_set_eeh_option == RTAS_UNKNOWN_SERVICE)
806 return;
807
808 eeh_error_buf_size = rtas_token("rtas-error-log-max");
809 if (eeh_error_buf_size == RTAS_UNKNOWN_SERVICE) {
810 eeh_error_buf_size = 1024;
811 }
812 if (eeh_error_buf_size > RTAS_ERROR_LOG_MAX) {
813 printk(KERN_WARNING "EEH: rtas-error-log-max is bigger than allocated "
814 "buffer ! (%d vs %d)", eeh_error_buf_size, RTAS_ERROR_LOG_MAX);
815 eeh_error_buf_size = RTAS_ERROR_LOG_MAX;
816 }
817
818 /* Enable EEH for all adapters. Note that eeh requires buid's */
819 for (phb = of_find_node_by_name(NULL, "pci"); phb;
820 phb = of_find_node_by_name(phb, "pci")) {
821 unsigned long buid;
822
823 buid = get_phb_buid(phb);
Linas Vepstas69376502005-11-03 18:47:50 -0600824 if (buid == 0 || PCI_DN(phb) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 continue;
826
827 info.buid_lo = BUID_LO(buid);
828 info.buid_hi = BUID_HI(buid);
829 traverse_pci_devices(phb, early_enable_eeh, &info);
830 }
831
832 if (eeh_subsystem_enabled)
833 printk(KERN_INFO "EEH: PCI Enhanced I/O Error Handling Enabled\n");
834 else
835 printk(KERN_WARNING "EEH: No capable adapters found\n");
836}
837
838/**
839 * eeh_add_device_early - enable EEH for the indicated device_node
840 * @dn: device node for which to set up EEH
841 *
842 * This routine must be used to perform EEH initialization for PCI
843 * devices that were added after system boot (e.g. hotplug, dlpar).
844 * This routine must be called before any i/o is performed to the
845 * adapter (inluding any config-space i/o).
846 * Whether this actually enables EEH or not for this device depends
847 * on the CEC architecture, type of the device, on earlier boot
848 * command-line arguments & etc.
849 */
850void eeh_add_device_early(struct device_node *dn)
851{
852 struct pci_controller *phb;
853 struct eeh_early_enable_info info;
854
Linas Vepstas69376502005-11-03 18:47:50 -0600855 if (!dn || !PCI_DN(dn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return;
Paul Mackerras16353172005-09-06 13:17:54 +1000857 phb = PCI_DN(dn)->phb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (NULL == phb || 0 == phb->buid) {
Linas Vepstas69376502005-11-03 18:47:50 -0600859 printk(KERN_WARNING "EEH: Expected buid but found none for %s\n",
860 dn->full_name);
861 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return;
863 }
864
865 info.buid_hi = BUID_HI(phb->buid);
866 info.buid_lo = BUID_LO(phb->buid);
867 early_enable_eeh(dn, &info);
868}
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600869EXPORT_SYMBOL_GPL(eeh_add_device_early);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871/**
872 * eeh_add_device_late - perform EEH initialization for the indicated pci device
873 * @dev: pci device for which to set up EEH
874 *
875 * This routine must be used to complete EEH initialization for PCI
876 * devices that were added after system boot (e.g. hotplug, dlpar).
877 */
878void eeh_add_device_late(struct pci_dev *dev)
879{
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600880 struct device_node *dn;
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (!dev || !eeh_subsystem_enabled)
883 return;
884
885#ifdef DEBUG
Adrian Bunk982245f2005-07-17 04:22:20 +0200886 printk(KERN_DEBUG "EEH: adding device %s\n", pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887#endif
888
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600889 pci_dev_get (dev);
890 dn = pci_device_to_OF_node(dev);
891 PCI_DN(dn)->pcidev = dev;
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 pci_addr_cache_insert_device (dev);
894}
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600895EXPORT_SYMBOL_GPL(eeh_add_device_late);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897/**
898 * eeh_remove_device - undo EEH setup for the indicated pci device
899 * @dev: pci device to be removed
900 *
901 * This routine should be when a device is removed from a running
902 * system (e.g. by hotplug or dlpar).
903 */
904void eeh_remove_device(struct pci_dev *dev)
905{
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600906 struct device_node *dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (!dev || !eeh_subsystem_enabled)
908 return;
909
910 /* Unregister the device with the EEH/PCI address search system */
911#ifdef DEBUG
Adrian Bunk982245f2005-07-17 04:22:20 +0200912 printk(KERN_DEBUG "EEH: remove device %s\n", pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913#endif
914 pci_addr_cache_remove_device(dev);
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600915
916 dn = pci_device_to_OF_node(dev);
917 PCI_DN(dn)->pcidev = NULL;
918 pci_dev_put (dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600920EXPORT_SYMBOL_GPL(eeh_remove_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922static int proc_eeh_show(struct seq_file *m, void *v)
923{
924 unsigned int cpu;
925 unsigned long ffs = 0, positives = 0, failures = 0;
926 unsigned long resets = 0;
Linas Vepstas177bc932005-11-03 18:48:52 -0600927 unsigned long no_dev = 0, no_dn = 0, no_cfg = 0, no_check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 for_each_cpu(cpu) {
930 ffs += per_cpu(total_mmio_ffs, cpu);
931 positives += per_cpu(false_positives, cpu);
932 failures += per_cpu(ignored_failures, cpu);
933 resets += per_cpu(slot_resets, cpu);
Linas Vepstas177bc932005-11-03 18:48:52 -0600934 no_dev += per_cpu(no_device, cpu);
935 no_dn += per_cpu(no_dn, cpu);
936 no_cfg += per_cpu(no_cfg_addr, cpu);
937 no_check += per_cpu(ignored_check, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939
940 if (0 == eeh_subsystem_enabled) {
941 seq_printf(m, "EEH Subsystem is globally disabled\n");
942 seq_printf(m, "eeh_total_mmio_ffs=%ld\n", ffs);
943 } else {
944 seq_printf(m, "EEH Subsystem is enabled\n");
Linas Vepstas177bc932005-11-03 18:48:52 -0600945 seq_printf(m,
946 "no device=%ld\n"
947 "no device node=%ld\n"
948 "no config address=%ld\n"
949 "check not wanted=%ld\n"
950 "eeh_total_mmio_ffs=%ld\n"
951 "eeh_false_positives=%ld\n"
952 "eeh_ignored_failures=%ld\n"
953 "eeh_slot_resets=%ld\n",
954 no_dev, no_dn, no_cfg, no_check,
955 ffs, positives, failures, resets);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
957
958 return 0;
959}
960
961static int proc_eeh_open(struct inode *inode, struct file *file)
962{
963 return single_open(file, proc_eeh_show, NULL);
964}
965
966static struct file_operations proc_eeh_operations = {
967 .open = proc_eeh_open,
968 .read = seq_read,
969 .llseek = seq_lseek,
970 .release = single_release,
971};
972
973static int __init eeh_init_proc(void)
974{
975 struct proc_dir_entry *e;
976
977 if (systemcfg->platform & PLATFORM_PSERIES) {
978 e = create_proc_entry("ppc64/eeh", 0, NULL);
979 if (e)
980 e->proc_fops = &proc_eeh_operations;
981 }
982
983 return 0;
984}
985__initcall(eeh_init_proc);