e7522f6da69d2bd03dc87318f3fa93328385921e
[pandora-kernel.git] / arch / ppc64 / kernel / eeh.c
1 /*
2  * eeh.c
3  * Copyright (C) 2001 Dave Engebretsen & Todd Inglett IBM Corporation
4  *
5  * 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.
9  *
10  * 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.
14  *
15  * 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
20 #include <linux/init.h>
21 #include <linux/list.h>
22 #include <linux/notifier.h>
23 #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>
28 #include <asm/atomic.h>
29 #include <asm/eeh.h>
30 #include <asm/io.h>
31 #include <asm/machdep.h>
32 #include <asm/rtas.h>
33 #include <asm/atomic.h>
34 #include <asm/systemcfg.h>
35 #include <asm/ppc-pci.h>
36
37 #undef DEBUG
38
39 /** Overview:
40  *  EEH, or "Extended Error Handling" is a PCI bridge technology for
41  *  dealing with PCI bus errors that can't be dealt with within the
42  *  usual PCI framework, except by check-stopping the CPU.  Systems
43  *  that are designed for high-availability/reliability cannot afford
44  *  to crash due to a "mere" PCI error, thus the need for EEH.
45  *  An EEH-capable bridge operates by converting a detected error
46  *  into a "slot freeze", taking the PCI adapter off-line, making
47  *  the slot behave, from the OS'es point of view, as if the slot
48  *  were "empty": all reads return 0xff's and all writes are silently
49  *  ignored.  EEH slot isolation events can be triggered by parity
50  *  errors on the address or data busses (e.g. during posted writes),
51  *  which in turn might be caused by low voltage on the bus, dust,
52  *  vibration, humidity, radioactivity or plain-old failed hardware.
53  *
54  *  Note, however, that one of the leading causes of EEH slot
55  *  freeze events are buggy device drivers, buggy device microcode,
56  *  or buggy device hardware.  This is because any attempt by the
57  *  device to bus-master data to a memory address that is not
58  *  assigned to the device will trigger a slot freeze.   (The idea
59  *  is to prevent devices-gone-wild from corrupting system memory).
60  *  Buggy hardware/drivers will have a miserable time co-existing
61  *  with EEH.
62  *
63  *  Ideally, a PCI device driver, when suspecting that an isolation
64  *  event has occured (e.g. by reading 0xff's), will then ask EEH
65  *  whether this is the case, and then take appropriate steps to
66  *  reset the PCI slot, the PCI device, and then resume operations.
67  *  However, until that day,  the checking is done here, with the
68  *  eeh_check_failure() routine embedded in the MMIO macros.  If
69  *  the slot is found to be isolated, an "EEH Event" is synthesized
70  *  and sent out for processing.
71  */
72
73 /* EEH event workqueue setup. */
74 static DEFINE_SPINLOCK(eeh_eventlist_lock);
75 LIST_HEAD(eeh_eventlist);
76 static void eeh_event_handler(void *);
77 DECLARE_WORK(eeh_event_wq, eeh_event_handler, NULL);
78
79 static struct notifier_block *eeh_notifier_chain;
80
81 /*
82  * If a device driver keeps reading an MMIO register in an interrupt
83  * handler after a slot isolation event has occurred, we assume it
84  * is broken and panic.  This sets the threshold for how many read
85  * attempts we allow before panicking.
86  */
87 #define EEH_MAX_FAILS   1000
88 static atomic_t eeh_fail_count;
89
90 /* RTAS tokens */
91 static int ibm_set_eeh_option;
92 static int ibm_set_slot_reset;
93 static int ibm_read_slot_reset_state;
94 static int ibm_read_slot_reset_state2;
95 static int ibm_slot_error_detail;
96
97 static int eeh_subsystem_enabled;
98
99 /* Lock to avoid races due to multiple reports of an error */
100 static DEFINE_SPINLOCK(confirm_error_lock);
101
102 /* Buffer for reporting slot-error-detail rtas calls */
103 static unsigned char slot_errbuf[RTAS_ERROR_LOG_MAX];
104 static DEFINE_SPINLOCK(slot_errbuf_lock);
105 static int eeh_error_buf_size;
106
107 /* System monitoring statistics */
108 static DEFINE_PER_CPU(unsigned long, no_device);
109 static DEFINE_PER_CPU(unsigned long, no_dn);
110 static DEFINE_PER_CPU(unsigned long, no_cfg_addr);
111 static DEFINE_PER_CPU(unsigned long, ignored_check);
112 static DEFINE_PER_CPU(unsigned long, total_mmio_ffs);
113 static DEFINE_PER_CPU(unsigned long, false_positives);
114 static DEFINE_PER_CPU(unsigned long, ignored_failures);
115 static DEFINE_PER_CPU(unsigned long, slot_resets);
116
117 /**
118  * The pci address cache subsystem.  This subsystem places
119  * PCI device address resources into a red-black tree, sorted
120  * according to the address range, so that given only an i/o
121  * address, the corresponding PCI device can be **quickly**
122  * found. It is safe to perform an address lookup in an interrupt
123  * context; this ability is an important feature.
124  *
125  * Currently, the only customer of this code is the EEH subsystem;
126  * thus, this code has been somewhat tailored to suit EEH better.
127  * In particular, the cache does *not* hold the addresses of devices
128  * for which EEH is not enabled.
129  *
130  * (Implementation Note: The RB tree seems to be better/faster
131  * than any hash algo I could think of for this problem, even
132  * with the penalty of slow pointer chases for d-cache misses).
133  */
134 struct pci_io_addr_range
135 {
136         struct rb_node rb_node;
137         unsigned long addr_lo;
138         unsigned long addr_hi;
139         struct pci_dev *pcidev;
140         unsigned int flags;
141 };
142
143 static struct pci_io_addr_cache
144 {
145         struct rb_root rb_root;
146         spinlock_t piar_lock;
147 } pci_io_addr_cache_root;
148
149 static inline struct pci_dev *__pci_get_device_by_addr(unsigned long addr)
150 {
151         struct rb_node *n = pci_io_addr_cache_root.rb_root.rb_node;
152
153         while (n) {
154                 struct pci_io_addr_range *piar;
155                 piar = rb_entry(n, struct pci_io_addr_range, rb_node);
156
157                 if (addr < piar->addr_lo) {
158                         n = n->rb_left;
159                 } else {
160                         if (addr > piar->addr_hi) {
161                                 n = n->rb_right;
162                         } else {
163                                 pci_dev_get(piar->pcidev);
164                                 return piar->pcidev;
165                         }
166                 }
167         }
168
169         return NULL;
170 }
171
172 /**
173  * pci_get_device_by_addr - Get device, given only address
174  * @addr: mmio (PIO) phys address or i/o port number
175  *
176  * Given an mmio phys address, or a port number, find a pci device
177  * that implements this address.  Be sure to pci_dev_put the device
178  * when finished.  I/O port numbers are assumed to be offset
179  * from zero (that is, they do *not* have pci_io_addr added in).
180  * It is safe to call this function within an interrupt.
181  */
182 static struct pci_dev *pci_get_device_by_addr(unsigned long addr)
183 {
184         struct pci_dev *dev;
185         unsigned long flags;
186
187         spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
188         dev = __pci_get_device_by_addr(addr);
189         spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
190         return dev;
191 }
192
193 #ifdef DEBUG
194 /*
195  * Handy-dandy debug print routine, does nothing more
196  * than print out the contents of our addr cache.
197  */
198 static void pci_addr_cache_print(struct pci_io_addr_cache *cache)
199 {
200         struct rb_node *n;
201         int cnt = 0;
202
203         n = rb_first(&cache->rb_root);
204         while (n) {
205                 struct pci_io_addr_range *piar;
206                 piar = rb_entry(n, struct pci_io_addr_range, rb_node);
207                 printk(KERN_DEBUG "PCI: %s addr range %d [%lx-%lx]: %s\n",
208                        (piar->flags & IORESOURCE_IO) ? "i/o" : "mem", cnt,
209                        piar->addr_lo, piar->addr_hi, pci_name(piar->pcidev));
210                 cnt++;
211                 n = rb_next(n);
212         }
213 }
214 #endif
215
216 /* Insert address range into the rb tree. */
217 static struct pci_io_addr_range *
218 pci_addr_cache_insert(struct pci_dev *dev, unsigned long alo,
219                       unsigned long ahi, unsigned int flags)
220 {
221         struct rb_node **p = &pci_io_addr_cache_root.rb_root.rb_node;
222         struct rb_node *parent = NULL;
223         struct pci_io_addr_range *piar;
224
225         /* Walk tree, find a place to insert into tree */
226         while (*p) {
227                 parent = *p;
228                 piar = rb_entry(parent, struct pci_io_addr_range, rb_node);
229                 if (ahi < piar->addr_lo) {
230                         p = &parent->rb_left;
231                 } else if (alo > piar->addr_hi) {
232                         p = &parent->rb_right;
233                 } else {
234                         if (dev != piar->pcidev ||
235                             alo != piar->addr_lo || ahi != piar->addr_hi) {
236                                 printk(KERN_WARNING "PIAR: overlapping address range\n");
237                         }
238                         return piar;
239                 }
240         }
241         piar = (struct pci_io_addr_range *)kmalloc(sizeof(struct pci_io_addr_range), GFP_ATOMIC);
242         if (!piar)
243                 return NULL;
244
245         piar->addr_lo = alo;
246         piar->addr_hi = ahi;
247         piar->pcidev = dev;
248         piar->flags = flags;
249
250 #ifdef DEBUG
251         printk(KERN_DEBUG "PIAR: insert range=[%lx:%lx] dev=%s\n",
252                           alo, ahi, pci_name (dev));
253 #endif
254
255         rb_link_node(&piar->rb_node, parent, p);
256         rb_insert_color(&piar->rb_node, &pci_io_addr_cache_root.rb_root);
257
258         return piar;
259 }
260
261 static void __pci_addr_cache_insert_device(struct pci_dev *dev)
262 {
263         struct device_node *dn;
264         struct pci_dn *pdn;
265         int i;
266         int inserted = 0;
267
268         dn = pci_device_to_OF_node(dev);
269         if (!dn) {
270                 printk(KERN_WARNING "PCI: no pci dn found for dev=%s\n", pci_name(dev));
271                 return;
272         }
273
274         /* Skip any devices for which EEH is not enabled. */
275         pdn = PCI_DN(dn);
276         if (!(pdn->eeh_mode & EEH_MODE_SUPPORTED) ||
277             pdn->eeh_mode & EEH_MODE_NOCHECK) {
278 #ifdef DEBUG
279                 printk(KERN_INFO "PCI: skip building address cache for=%s - %s\n",
280                        pci_name(dev), pdn->node->full_name);
281 #endif
282                 return;
283         }
284
285         /* The cache holds a reference to the device... */
286         pci_dev_get(dev);
287
288         /* Walk resources on this device, poke them into the tree */
289         for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
290                 unsigned long start = pci_resource_start(dev,i);
291                 unsigned long end = pci_resource_end(dev,i);
292                 unsigned int flags = pci_resource_flags(dev,i);
293
294                 /* We are interested only bus addresses, not dma or other stuff */
295                 if (0 == (flags & (IORESOURCE_IO | IORESOURCE_MEM)))
296                         continue;
297                 if (start == 0 || ~start == 0 || end == 0 || ~end == 0)
298                          continue;
299                 pci_addr_cache_insert(dev, start, end, flags);
300                 inserted = 1;
301         }
302
303         /* If there was nothing to add, the cache has no reference... */
304         if (!inserted)
305                 pci_dev_put(dev);
306 }
307
308 /**
309  * pci_addr_cache_insert_device - Add a device to the address cache
310  * @dev: PCI device whose I/O addresses we are interested in.
311  *
312  * In order to support the fast lookup of devices based on addresses,
313  * we maintain a cache of devices that can be quickly searched.
314  * This routine adds a device to that cache.
315  */
316 static void pci_addr_cache_insert_device(struct pci_dev *dev)
317 {
318         unsigned long flags;
319
320         spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
321         __pci_addr_cache_insert_device(dev);
322         spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
323 }
324
325 static inline void __pci_addr_cache_remove_device(struct pci_dev *dev)
326 {
327         struct rb_node *n;
328         int removed = 0;
329
330 restart:
331         n = rb_first(&pci_io_addr_cache_root.rb_root);
332         while (n) {
333                 struct pci_io_addr_range *piar;
334                 piar = rb_entry(n, struct pci_io_addr_range, rb_node);
335
336                 if (piar->pcidev == dev) {
337                         rb_erase(n, &pci_io_addr_cache_root.rb_root);
338                         removed = 1;
339                         kfree(piar);
340                         goto restart;
341                 }
342                 n = rb_next(n);
343         }
344
345         /* The cache no longer holds its reference to this device... */
346         if (removed)
347                 pci_dev_put(dev);
348 }
349
350 /**
351  * pci_addr_cache_remove_device - remove pci device from addr cache
352  * @dev: device to remove
353  *
354  * Remove a device from the addr-cache tree.
355  * This is potentially expensive, since it will walk
356  * the tree multiple times (once per resource).
357  * But so what; device removal doesn't need to be that fast.
358  */
359 static void pci_addr_cache_remove_device(struct pci_dev *dev)
360 {
361         unsigned long flags;
362
363         spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
364         __pci_addr_cache_remove_device(dev);
365         spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
366 }
367
368 /**
369  * pci_addr_cache_build - Build a cache of I/O addresses
370  *
371  * Build a cache of pci i/o addresses.  This cache will be used to
372  * find the pci device that corresponds to a given address.
373  * This routine scans all pci busses to build the cache.
374  * Must be run late in boot process, after the pci controllers
375  * have been scaned for devices (after all device resources are known).
376  */
377 void __init pci_addr_cache_build(void)
378 {
379         struct pci_dev *dev = NULL;
380
381         if (!eeh_subsystem_enabled)
382                 return;
383
384         spin_lock_init(&pci_io_addr_cache_root.piar_lock);
385
386         while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
387                 /* Ignore PCI bridges ( XXX why ??) */
388                 if ((dev->class >> 16) == PCI_BASE_CLASS_BRIDGE) {
389                         continue;
390                 }
391                 pci_addr_cache_insert_device(dev);
392         }
393
394 #ifdef DEBUG
395         /* Verify tree built up above, echo back the list of addrs. */
396         pci_addr_cache_print(&pci_io_addr_cache_root);
397 #endif
398 }
399
400 /* --------------------------------------------------------------- */
401 /* Above lies the PCI Address Cache. Below lies the EEH event infrastructure */
402
403 void eeh_slot_error_detail (struct pci_dn *pdn, int severity)
404 {
405         unsigned long flags;
406         int rc;
407
408         /* Log the error with the rtas logger */
409         spin_lock_irqsave(&slot_errbuf_lock, flags);
410         memset(slot_errbuf, 0, eeh_error_buf_size);
411
412         rc = rtas_call(ibm_slot_error_detail,
413                        8, 1, NULL, pdn->eeh_config_addr,
414                        BUID_HI(pdn->phb->buid),
415                        BUID_LO(pdn->phb->buid), NULL, 0,
416                        virt_to_phys(slot_errbuf),
417                        eeh_error_buf_size,
418                        severity);
419
420         if (rc == 0)
421                 log_error(slot_errbuf, ERR_TYPE_RTAS_LOG, 0);
422         spin_unlock_irqrestore(&slot_errbuf_lock, flags);
423 }
424
425 /**
426  * eeh_register_notifier - Register to find out about EEH events.
427  * @nb: notifier block to callback on events
428  */
429 int eeh_register_notifier(struct notifier_block *nb)
430 {
431         return notifier_chain_register(&eeh_notifier_chain, nb);
432 }
433
434 /**
435  * eeh_unregister_notifier - Unregister to an EEH event notifier.
436  * @nb: notifier block to callback on events
437  */
438 int eeh_unregister_notifier(struct notifier_block *nb)
439 {
440         return notifier_chain_unregister(&eeh_notifier_chain, nb);
441 }
442
443 /**
444  * read_slot_reset_state - Read the reset state of a device node's slot
445  * @dn: device node to read
446  * @rets: array to return results in
447  */
448 static int read_slot_reset_state(struct pci_dn *pdn, int rets[])
449 {
450         int token, outputs;
451
452         if (ibm_read_slot_reset_state2 != RTAS_UNKNOWN_SERVICE) {
453                 token = ibm_read_slot_reset_state2;
454                 outputs = 4;
455         } else {
456                 token = ibm_read_slot_reset_state;
457                 rets[2] = 0; /* fake PE Unavailable info */
458                 outputs = 3;
459         }
460
461         return rtas_call(token, 3, outputs, rets, pdn->eeh_config_addr,
462                          BUID_HI(pdn->phb->buid), BUID_LO(pdn->phb->buid));
463 }
464
465 /**
466  * eeh_panic - call panic() for an eeh event that cannot be handled.
467  * The philosophy of this routine is that it is better to panic and
468  * halt the OS than it is to risk possible data corruption by
469  * oblivious device drivers that don't know better.
470  *
471  * @dev pci device that had an eeh event
472  * @reset_state current reset state of the device slot
473  */
474 static void eeh_panic(struct pci_dev *dev, int reset_state)
475 {
476         /*
477          * XXX We should create a separate sysctl for this.
478          *
479          * Since the panic_on_oops sysctl is used to halt the system
480          * in light of potential corruption, we can use it here.
481          */
482         if (panic_on_oops) {
483                 struct device_node *dn = pci_device_to_OF_node(dev);
484                 eeh_slot_error_detail (PCI_DN(dn), 2 /* Permanent Error */);
485                 panic("EEH: MMIO failure (%d) on device:%s\n", reset_state,
486                       pci_name(dev));
487         }
488         else {
489                 __get_cpu_var(ignored_failures)++;
490                 printk(KERN_INFO "EEH: Ignored MMIO failure (%d) on device:%s\n",
491                        reset_state, pci_name(dev));
492         }
493 }
494
495 /**
496  * eeh_event_handler - dispatch EEH events.  The detection of a frozen
497  * slot can occur inside an interrupt, where it can be hard to do
498  * anything about it.  The goal of this routine is to pull these
499  * detection events out of the context of the interrupt handler, and
500  * re-dispatch them for processing at a later time in a normal context.
501  *
502  * @dummy - unused
503  */
504 static void eeh_event_handler(void *dummy)
505 {
506         unsigned long flags;
507         struct eeh_event        *event;
508
509         while (1) {
510                 spin_lock_irqsave(&eeh_eventlist_lock, flags);
511                 event = NULL;
512                 if (!list_empty(&eeh_eventlist)) {
513                         event = list_entry(eeh_eventlist.next, struct eeh_event, list);
514                         list_del(&event->list);
515                 }
516                 spin_unlock_irqrestore(&eeh_eventlist_lock, flags);
517                 if (event == NULL)
518                         break;
519
520                 printk(KERN_INFO "EEH: MMIO failure (%d), notifiying device "
521                        "%s\n", event->reset_state,
522                        pci_name(event->dev));
523
524                 atomic_set(&eeh_fail_count, 0);
525                 notifier_call_chain (&eeh_notifier_chain,
526                                      EEH_NOTIFY_FREEZE, event);
527
528                 pci_dev_put(event->dev);
529                 kfree(event);
530         }
531 }
532
533 /**
534  * eeh_token_to_phys - convert EEH address token to phys address
535  * @token i/o token, should be address in the form 0xA....
536  */
537 static inline unsigned long eeh_token_to_phys(unsigned long token)
538 {
539         pte_t *ptep;
540         unsigned long pa;
541
542         ptep = find_linux_pte(init_mm.pgd, token);
543         if (!ptep)
544                 return token;
545         pa = pte_pfn(*ptep) << PAGE_SHIFT;
546
547         return pa | (token & (PAGE_SIZE-1));
548 }
549
550 /** 
551  * Return the "partitionable endpoint" (pe) under which this device lies
552  */
553 static struct device_node * find_device_pe(struct device_node *dn)
554 {
555         while ((dn->parent) && PCI_DN(dn->parent) &&
556               (PCI_DN(dn->parent)->eeh_mode & EEH_MODE_SUPPORTED)) {
557                 dn = dn->parent;
558         }
559         return dn;
560 }
561
562 /** Mark all devices that are peers of this device as failed.
563  *  Mark the device driver too, so that it can see the failure
564  *  immediately; this is critical, since some drivers poll
565  *  status registers in interrupts ... If a driver is polling,
566  *  and the slot is frozen, then the driver can deadlock in
567  *  an interrupt context, which is bad.
568  */
569
570 static inline void __eeh_mark_slot (struct device_node *dn)
571 {
572         while (dn) {
573                 PCI_DN(dn)->eeh_mode |= EEH_MODE_ISOLATED;
574
575                 if (dn->child)
576                         __eeh_mark_slot (dn->child);
577                 dn = dn->sibling;
578         }
579 }
580
581 static inline void __eeh_clear_slot (struct device_node *dn)
582 {
583         while (dn) {
584                 PCI_DN(dn)->eeh_mode &= ~EEH_MODE_ISOLATED;
585                 if (dn->child)
586                         __eeh_clear_slot (dn->child);
587                 dn = dn->sibling;
588         }
589 }
590
591 static inline void eeh_clear_slot (struct device_node *dn)
592 {
593         unsigned long flags;
594         spin_lock_irqsave(&confirm_error_lock, flags);
595         __eeh_clear_slot (dn);
596         spin_unlock_irqrestore(&confirm_error_lock, flags);
597 }
598
599 /**
600  * eeh_dn_check_failure - check if all 1's data is due to EEH slot freeze
601  * @dn device node
602  * @dev pci device, if known
603  *
604  * Check for an EEH failure for the given device node.  Call this
605  * routine if the result of a read was all 0xff's and you want to
606  * find out if this is due to an EEH slot freeze.  This routine
607  * will query firmware for the EEH status.
608  *
609  * Returns 0 if there has not been an EEH error; otherwise returns
610  * a non-zero value and queues up a slot isolation event notification.
611  *
612  * It is safe to call this routine in an interrupt context.
613  */
614 int eeh_dn_check_failure(struct device_node *dn, struct pci_dev *dev)
615 {
616         int ret;
617         int rets[3];
618         unsigned long flags;
619         int reset_state;
620         struct eeh_event  *event;
621         struct pci_dn *pdn;
622         struct device_node *pe_dn;
623         int rc = 0;
624
625         __get_cpu_var(total_mmio_ffs)++;
626
627         if (!eeh_subsystem_enabled)
628                 return 0;
629
630         if (!dn) {
631                 __get_cpu_var(no_dn)++;
632                 return 0;
633         }
634         pdn = PCI_DN(dn);
635
636         /* Access to IO BARs might get this far and still not want checking. */
637         if (!pdn->eeh_capable || !(pdn->eeh_mode & EEH_MODE_SUPPORTED) ||
638             pdn->eeh_mode & EEH_MODE_NOCHECK) {
639                 __get_cpu_var(ignored_check)++;
640 #ifdef DEBUG
641                 printk ("EEH:ignored check for %s %s\n", pci_name (dev), dn->full_name);
642 #endif
643                 return 0;
644         }
645
646         if (!pdn->eeh_config_addr) {
647                 __get_cpu_var(no_cfg_addr)++;
648                 return 0;
649         }
650
651         /* If we already have a pending isolation event for this
652          * slot, we know it's bad already, we don't need to check.
653          * Do this checking under a lock; as multiple PCI devices
654          * in one slot might report errors simultaneously, and we
655          * only want one error recovery routine running.
656          */
657         spin_lock_irqsave(&confirm_error_lock, flags);
658         rc = 1;
659         if (pdn->eeh_mode & EEH_MODE_ISOLATED) {
660                 atomic_inc(&eeh_fail_count);
661                 if (atomic_read(&eeh_fail_count) >= EEH_MAX_FAILS) {
662                         /* re-read the slot reset state */
663                         if (read_slot_reset_state(pdn, rets) != 0)
664                                 rets[0] = -1;   /* reset state unknown */
665                         eeh_panic(dev, rets[0]);
666                 }
667                 goto dn_unlock;
668         }
669
670         /*
671          * Now test for an EEH failure.  This is VERY expensive.
672          * Note that the eeh_config_addr may be a parent device
673          * in the case of a device behind a bridge, or it may be
674          * function zero of a multi-function device.
675          * In any case they must share a common PHB.
676          */
677         ret = read_slot_reset_state(pdn, rets);
678
679         /* If the call to firmware failed, punt */
680         if (ret != 0) {
681                 printk(KERN_WARNING "EEH: read_slot_reset_state() failed; rc=%d dn=%s\n",
682                        ret, dn->full_name);
683                 __get_cpu_var(false_positives)++;
684                 rc = 0;
685                 goto dn_unlock;
686         }
687
688         /* If EEH is not supported on this device, punt. */
689         if (rets[1] != 1) {
690                 printk(KERN_WARNING "EEH: event on unsupported device, rc=%d dn=%s\n",
691                        ret, dn->full_name);
692                 __get_cpu_var(false_positives)++;
693                 rc = 0;
694                 goto dn_unlock;
695         }
696
697         /* If not the kind of error we know about, punt. */
698         if (rets[0] != 2 && rets[0] != 4 && rets[0] != 5) {
699                 __get_cpu_var(false_positives)++;
700                 rc = 0;
701                 goto dn_unlock;
702         }
703
704         /* Note that config-io to empty slots may fail;
705          * we recognize empty because they don't have children. */
706         if ((rets[0] == 5) && (dn->child == NULL)) {
707                 __get_cpu_var(false_positives)++;
708                 rc = 0;
709                 goto dn_unlock;
710         }
711
712         __get_cpu_var(slot_resets)++;
713  
714         /* Avoid repeated reports of this failure, including problems
715          * with other functions on this device, and functions under
716          * bridges. */
717         pe_dn = find_device_pe (dn);
718         __eeh_mark_slot (pe_dn);
719         spin_unlock_irqrestore(&confirm_error_lock, flags);
720
721         reset_state = rets[0];
722
723         eeh_slot_error_detail (pdn, 1 /* Temporary Error */);
724
725         printk(KERN_INFO "EEH: MMIO failure (%d) on device: %s %s\n",
726                rets[0], dn->name, dn->full_name);
727         event = kmalloc(sizeof(*event), GFP_ATOMIC);
728         if (event == NULL) {
729                 eeh_panic(dev, reset_state);
730                 return 1;
731         }
732
733         event->dev = dev;
734         event->dn = dn;
735         event->reset_state = reset_state;
736
737         /* We may or may not be called in an interrupt context */
738         spin_lock_irqsave(&eeh_eventlist_lock, flags);
739         list_add(&event->list, &eeh_eventlist);
740         spin_unlock_irqrestore(&eeh_eventlist_lock, flags);
741
742         /* Most EEH events are due to device driver bugs.  Having
743          * a stack trace will help the device-driver authors figure
744          * out what happened.  So print that out. */
745         if (rets[0] != 5) dump_stack();
746         schedule_work(&eeh_event_wq);
747
748         return 1;
749
750 dn_unlock:
751         spin_unlock_irqrestore(&confirm_error_lock, flags);
752         return rc;
753 }
754
755 EXPORT_SYMBOL_GPL(eeh_dn_check_failure);
756
757 /**
758  * eeh_check_failure - check if all 1's data is due to EEH slot freeze
759  * @token i/o token, should be address in the form 0xA....
760  * @val value, should be all 1's (XXX why do we need this arg??)
761  *
762  * Check for an EEH failure at the given token address.  Call this
763  * routine if the result of a read was all 0xff's and you want to
764  * find out if this is due to an EEH slot freeze event.  This routine
765  * will query firmware for the EEH status.
766  *
767  * Note this routine is safe to call in an interrupt context.
768  */
769 unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
770 {
771         unsigned long addr;
772         struct pci_dev *dev;
773         struct device_node *dn;
774
775         /* Finding the phys addr + pci device; this is pretty quick. */
776         addr = eeh_token_to_phys((unsigned long __force) token);
777         dev = pci_get_device_by_addr(addr);
778         if (!dev) {
779                 __get_cpu_var(no_device)++;
780                 return val;
781         }
782
783         dn = pci_device_to_OF_node(dev);
784         eeh_dn_check_failure (dn, dev);
785
786         pci_dev_put(dev);
787         return val;
788 }
789
790 EXPORT_SYMBOL(eeh_check_failure);
791
792 struct eeh_early_enable_info {
793         unsigned int buid_hi;
794         unsigned int buid_lo;
795 };
796
797 /* Enable eeh for the given device node. */
798 static void *early_enable_eeh(struct device_node *dn, void *data)
799 {
800         struct eeh_early_enable_info *info = data;
801         int ret;
802         char *status = get_property(dn, "status", NULL);
803         u32 *class_code = (u32 *)get_property(dn, "class-code", NULL);
804         u32 *vendor_id = (u32 *)get_property(dn, "vendor-id", NULL);
805         u32 *device_id = (u32 *)get_property(dn, "device-id", NULL);
806         u32 *regs;
807         int enable;
808         struct pci_dn *pdn = PCI_DN(dn);
809
810         pdn->eeh_mode = 0;
811
812         if (status && strcmp(status, "ok") != 0)
813                 return NULL;    /* ignore devices with bad status */
814
815         /* Ignore bad nodes. */
816         if (!class_code || !vendor_id || !device_id)
817                 return NULL;
818
819         /* There is nothing to check on PCI to ISA bridges */
820         if (dn->type && !strcmp(dn->type, "isa")) {
821                 pdn->eeh_mode |= EEH_MODE_NOCHECK;
822                 return NULL;
823         }
824
825         /*
826          * Now decide if we are going to "Disable" EEH checking
827          * for this device.  We still run with the EEH hardware active,
828          * but we won't be checking for ff's.  This means a driver
829          * could return bad data (very bad!), an interrupt handler could
830          * hang waiting on status bits that won't change, etc.
831          * But there are a few cases like display devices that make sense.
832          */
833         enable = 1;     /* i.e. we will do checking */
834         if ((*class_code >> 16) == PCI_BASE_CLASS_DISPLAY)
835                 enable = 0;
836
837         if (!enable)
838                 pdn->eeh_mode |= EEH_MODE_NOCHECK;
839
840         /* Ok... see if this device supports EEH.  Some do, some don't,
841          * and the only way to find out is to check each and every one. */
842         regs = (u32 *)get_property(dn, "reg", NULL);
843         if (regs) {
844                 /* First register entry is addr (00BBSS00)  */
845                 /* Try to enable eeh */
846                 ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
847                                 regs[0], info->buid_hi, info->buid_lo,
848                                 EEH_ENABLE);
849                 if (ret == 0) {
850                         eeh_subsystem_enabled = 1;
851                         pdn->eeh_mode |= EEH_MODE_SUPPORTED;
852                         pdn->eeh_config_addr = regs[0];
853 #ifdef DEBUG
854                         printk(KERN_DEBUG "EEH: %s: eeh enabled\n", dn->full_name);
855 #endif
856                 } else {
857
858                         /* This device doesn't support EEH, but it may have an
859                          * EEH parent, in which case we mark it as supported. */
860                         if (dn->parent && PCI_DN(dn->parent)
861                             && (PCI_DN(dn->parent)->eeh_mode & EEH_MODE_SUPPORTED)) {
862                                 /* Parent supports EEH. */
863                                 pdn->eeh_mode |= EEH_MODE_SUPPORTED;
864                                 pdn->eeh_config_addr = PCI_DN(dn->parent)->eeh_config_addr;
865                                 return NULL;
866                         }
867                 }
868         } else {
869                 printk(KERN_WARNING "EEH: %s: unable to get reg property.\n",
870                        dn->full_name);
871         }
872
873         return NULL;
874 }
875
876 /*
877  * Initialize EEH by trying to enable it for all of the adapters in the system.
878  * As a side effect we can determine here if eeh is supported at all.
879  * Note that we leave EEH on so failed config cycles won't cause a machine
880  * check.  If a user turns off EEH for a particular adapter they are really
881  * telling Linux to ignore errors.  Some hardware (e.g. POWER5) won't
882  * grant access to a slot if EEH isn't enabled, and so we always enable
883  * EEH for all slots/all devices.
884  *
885  * The eeh-force-off option disables EEH checking globally, for all slots.
886  * Even if force-off is set, the EEH hardware is still enabled, so that
887  * newer systems can boot.
888  */
889 void __init eeh_init(void)
890 {
891         struct device_node *phb, *np;
892         struct eeh_early_enable_info info;
893
894         spin_lock_init(&confirm_error_lock);
895         spin_lock_init(&slot_errbuf_lock);
896
897         np = of_find_node_by_path("/rtas");
898         if (np == NULL)
899                 return;
900
901         ibm_set_eeh_option = rtas_token("ibm,set-eeh-option");
902         ibm_set_slot_reset = rtas_token("ibm,set-slot-reset");
903         ibm_read_slot_reset_state2 = rtas_token("ibm,read-slot-reset-state2");
904         ibm_read_slot_reset_state = rtas_token("ibm,read-slot-reset-state");
905         ibm_slot_error_detail = rtas_token("ibm,slot-error-detail");
906
907         if (ibm_set_eeh_option == RTAS_UNKNOWN_SERVICE)
908                 return;
909
910         eeh_error_buf_size = rtas_token("rtas-error-log-max");
911         if (eeh_error_buf_size == RTAS_UNKNOWN_SERVICE) {
912                 eeh_error_buf_size = 1024;
913         }
914         if (eeh_error_buf_size > RTAS_ERROR_LOG_MAX) {
915                 printk(KERN_WARNING "EEH: rtas-error-log-max is bigger than allocated "
916                       "buffer ! (%d vs %d)", eeh_error_buf_size, RTAS_ERROR_LOG_MAX);
917                 eeh_error_buf_size = RTAS_ERROR_LOG_MAX;
918         }
919
920         /* Enable EEH for all adapters.  Note that eeh requires buid's */
921         for (phb = of_find_node_by_name(NULL, "pci"); phb;
922              phb = of_find_node_by_name(phb, "pci")) {
923                 unsigned long buid;
924
925                 buid = get_phb_buid(phb);
926                 if (buid == 0 || PCI_DN(phb) == NULL)
927                         continue;
928
929                 info.buid_lo = BUID_LO(buid);
930                 info.buid_hi = BUID_HI(buid);
931                 traverse_pci_devices(phb, early_enable_eeh, &info);
932         }
933
934         if (eeh_subsystem_enabled)
935                 printk(KERN_INFO "EEH: PCI Enhanced I/O Error Handling Enabled\n");
936         else
937                 printk(KERN_WARNING "EEH: No capable adapters found\n");
938 }
939
940 /**
941  * eeh_add_device_early - enable EEH for the indicated device_node
942  * @dn: device node for which to set up EEH
943  *
944  * This routine must be used to perform EEH initialization for PCI
945  * devices that were added after system boot (e.g. hotplug, dlpar).
946  * This routine must be called before any i/o is performed to the
947  * adapter (inluding any config-space i/o).
948  * Whether this actually enables EEH or not for this device depends
949  * on the CEC architecture, type of the device, on earlier boot
950  * command-line arguments & etc.
951  */
952 void eeh_add_device_early(struct device_node *dn)
953 {
954         struct pci_controller *phb;
955         struct eeh_early_enable_info info;
956
957         if (!dn || !PCI_DN(dn))
958                 return;
959         phb = PCI_DN(dn)->phb;
960         if (NULL == phb || 0 == phb->buid) {
961                 printk(KERN_WARNING "EEH: Expected buid but found none for %s\n",
962                        dn->full_name);
963                 dump_stack();
964                 return;
965         }
966
967         info.buid_hi = BUID_HI(phb->buid);
968         info.buid_lo = BUID_LO(phb->buid);
969         early_enable_eeh(dn, &info);
970 }
971 EXPORT_SYMBOL_GPL(eeh_add_device_early);
972
973 /**
974  * eeh_add_device_late - perform EEH initialization for the indicated pci device
975  * @dev: pci device for which to set up EEH
976  *
977  * This routine must be used to complete EEH initialization for PCI
978  * devices that were added after system boot (e.g. hotplug, dlpar).
979  */
980 void eeh_add_device_late(struct pci_dev *dev)
981 {
982         struct device_node *dn;
983
984         if (!dev || !eeh_subsystem_enabled)
985                 return;
986
987 #ifdef DEBUG
988         printk(KERN_DEBUG "EEH: adding device %s\n", pci_name(dev));
989 #endif
990
991         pci_dev_get (dev);
992         dn = pci_device_to_OF_node(dev);
993         PCI_DN(dn)->pcidev = dev;
994
995         pci_addr_cache_insert_device (dev);
996 }
997 EXPORT_SYMBOL_GPL(eeh_add_device_late);
998
999 /**
1000  * eeh_remove_device - undo EEH setup for the indicated pci device
1001  * @dev: pci device to be removed
1002  *
1003  * This routine should be when a device is removed from a running
1004  * system (e.g. by hotplug or dlpar).
1005  */
1006 void eeh_remove_device(struct pci_dev *dev)
1007 {
1008         struct device_node *dn;
1009         if (!dev || !eeh_subsystem_enabled)
1010                 return;
1011
1012         /* Unregister the device with the EEH/PCI address search system */
1013 #ifdef DEBUG
1014         printk(KERN_DEBUG "EEH: remove device %s\n", pci_name(dev));
1015 #endif
1016         pci_addr_cache_remove_device(dev);
1017
1018         dn = pci_device_to_OF_node(dev);
1019         PCI_DN(dn)->pcidev = NULL;
1020         pci_dev_put (dev);
1021 }
1022 EXPORT_SYMBOL_GPL(eeh_remove_device);
1023
1024 static int proc_eeh_show(struct seq_file *m, void *v)
1025 {
1026         unsigned int cpu;
1027         unsigned long ffs = 0, positives = 0, failures = 0;
1028         unsigned long resets = 0;
1029         unsigned long no_dev = 0, no_dn = 0, no_cfg = 0, no_check = 0;
1030
1031         for_each_cpu(cpu) {
1032                 ffs += per_cpu(total_mmio_ffs, cpu);
1033                 positives += per_cpu(false_positives, cpu);
1034                 failures += per_cpu(ignored_failures, cpu);
1035                 resets += per_cpu(slot_resets, cpu);
1036                 no_dev += per_cpu(no_device, cpu);
1037                 no_dn += per_cpu(no_dn, cpu);
1038                 no_cfg += per_cpu(no_cfg_addr, cpu);
1039                 no_check += per_cpu(ignored_check, cpu);
1040         }
1041
1042         if (0 == eeh_subsystem_enabled) {
1043                 seq_printf(m, "EEH Subsystem is globally disabled\n");
1044                 seq_printf(m, "eeh_total_mmio_ffs=%ld\n", ffs);
1045         } else {
1046                 seq_printf(m, "EEH Subsystem is enabled\n");
1047                 seq_printf(m,
1048                                 "no device=%ld\n"
1049                                 "no device node=%ld\n"
1050                                 "no config address=%ld\n"
1051                                 "check not wanted=%ld\n"
1052                                 "eeh_total_mmio_ffs=%ld\n"
1053                                 "eeh_false_positives=%ld\n"
1054                                 "eeh_ignored_failures=%ld\n"
1055                                 "eeh_slot_resets=%ld\n",
1056                                 no_dev, no_dn, no_cfg, no_check,
1057                                 ffs, positives, failures, resets);
1058         }
1059
1060         return 0;
1061 }
1062
1063 static int proc_eeh_open(struct inode *inode, struct file *file)
1064 {
1065         return single_open(file, proc_eeh_show, NULL);
1066 }
1067
1068 static struct file_operations proc_eeh_operations = {
1069         .open      = proc_eeh_open,
1070         .read      = seq_read,
1071         .llseek    = seq_lseek,
1072         .release   = single_release,
1073 };
1074
1075 static int __init eeh_init_proc(void)
1076 {
1077         struct proc_dir_entry *e;
1078
1079         if (systemcfg->platform & PLATFORM_PSERIES) {
1080                 e = create_proc_entry("ppc64/eeh", 0, NULL);
1081                 if (e)
1082                         e->proc_fops = &proc_eeh_operations;
1083         }
1084
1085         return 0;
1086 }
1087 __initcall(eeh_init_proc);