This commit was manufactured by cvs2svn to create tag
[linux-2.6.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/pci.h>
22 #include <linux/proc_fs.h>
23 #include <linux/bootmem.h>
24 #include <linux/mm.h>
25 #include <linux/rbtree.h>
26 #include <linux/spinlock.h>
27 #include <linux/seq_file.h>
28 #include <asm/paca.h>
29 #include <asm/processor.h>
30 #include <asm/naca.h>
31 #include <asm/io.h>
32 #include <asm/machdep.h>
33 #include <asm/pgtable.h>
34 #include <asm/rtas.h>
35 #include "pci.h"
36
37 #undef DEBUG
38
39 #define BUID_HI(buid) ((buid) >> 32)
40 #define BUID_LO(buid) ((buid) & 0xffffffff)
41 #define CONFIG_ADDR(busno, devfn) \
42                 (((((busno) & 0xff) << 8) | ((devfn) & 0xf8)) << 8)
43
44 /* RTAS tokens */
45 static int ibm_set_eeh_option;
46 static int ibm_set_slot_reset;
47 static int ibm_read_slot_reset_state;
48 static int ibm_read_slot_reset_state2;
49 static int ibm_slot_error_detail;
50
51 static int eeh_subsystem_enabled;
52
53 /* Buffer for reporting slot-error-detail rtas calls */
54 static unsigned char slot_errbuf[RTAS_ERROR_LOG_MAX];
55 static spinlock_t slot_errbuf_lock = SPIN_LOCK_UNLOCKED;
56 static int eeh_error_buf_size;
57
58 /* System monitoring statistics */
59 static DEFINE_PER_CPU(unsigned long, total_mmio_ffs);
60 static DEFINE_PER_CPU(unsigned long, false_positives);
61 static DEFINE_PER_CPU(unsigned long, ignored_failures);
62
63 /**
64  * The pci address cache subsystem.  This subsystem places
65  * PCI device address resources into a red-black tree, sorted
66  * according to the address range, so that given only an i/o
67  * address, the corresponding PCI device can be **quickly**
68  * found.
69  *
70  * Currently, the only customer of this code is the EEH subsystem;
71  * thus, this code has been somewhat tailored to suit EEH better.
72  * In particular, the cache does *not* hold the addresses of devices
73  * for which EEH is not enabled.
74  *
75  * (Implementation Note: The RB tree seems to be better/faster
76  * than any hash algo I could think of for this problem, even
77  * with the penalty of slow pointer chases for d-cache misses).
78  */
79 struct pci_io_addr_range
80 {
81         struct rb_node rb_node;
82         unsigned long addr_lo;
83         unsigned long addr_hi;
84         struct pci_dev *pcidev;
85         unsigned int flags;
86 };
87
88 static struct pci_io_addr_cache
89 {
90         struct rb_root rb_root;
91         spinlock_t piar_lock;
92 } pci_io_addr_cache_root;
93
94 static inline struct pci_dev *__pci_get_device_by_addr(unsigned long addr)
95 {
96         struct rb_node *n = pci_io_addr_cache_root.rb_root.rb_node;
97
98         while (n) {
99                 struct pci_io_addr_range *piar;
100                 piar = rb_entry(n, struct pci_io_addr_range, rb_node);
101
102                 if (addr < piar->addr_lo) {
103                         n = n->rb_left;
104                 } else {
105                         if (addr > piar->addr_hi) {
106                                 n = n->rb_right;
107                         } else {
108                                 pci_dev_get(piar->pcidev);
109                                 return piar->pcidev;
110                         }
111                 }
112         }
113
114         return NULL;
115 }
116
117 /**
118  * pci_get_device_by_addr - Get device, given only address
119  * @addr: mmio (PIO) phys address or i/o port number
120  *
121  * Given an mmio phys address, or a port number, find a pci device
122  * that implements this address.  Be sure to pci_dev_put the device
123  * when finished.  I/O port numbers are assumed to be offset
124  * from zero (that is, they do *not* have pci_io_addr added in).
125  * It is safe to call this function within an interrupt.
126  */
127 static struct pci_dev *pci_get_device_by_addr(unsigned long addr)
128 {
129         struct pci_dev *dev;
130         unsigned long flags;
131
132         spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
133         dev = __pci_get_device_by_addr(addr);
134         spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
135         return dev;
136 }
137
138 #ifdef DEBUG
139 /*
140  * Handy-dandy debug print routine, does nothing more
141  * than print out the contents of our addr cache.
142  */
143 static void pci_addr_cache_print(struct pci_io_addr_cache *cache)
144 {
145         struct rb_node *n;
146         int cnt = 0;
147
148         n = rb_first(&cache->rb_root);
149         while (n) {
150                 struct pci_io_addr_range *piar;
151                 piar = rb_entry(n, struct pci_io_addr_range, rb_node);
152                 printk(KERN_DEBUG "PCI: %s addr range %d [%lx-%lx]: %s %s\n",
153                        (piar->flags & IORESOURCE_IO) ? "i/o" : "mem", cnt,
154                        piar->addr_lo, piar->addr_hi, pci_name(piar->pcidev),
155                        pci_pretty_name(piar->pcidev));
156                 cnt++;
157                 n = rb_next(n);
158         }
159 }
160 #endif
161
162 /* Insert address range into the rb tree. */
163 static struct pci_io_addr_range *
164 pci_addr_cache_insert(struct pci_dev *dev, unsigned long alo,
165                       unsigned long ahi, unsigned int flags)
166 {
167         struct rb_node **p = &pci_io_addr_cache_root.rb_root.rb_node;
168         struct rb_node *parent = NULL;
169         struct pci_io_addr_range *piar;
170
171         /* Walk tree, find a place to insert into tree */
172         while (*p) {
173                 parent = *p;
174                 piar = rb_entry(parent, struct pci_io_addr_range, rb_node);
175                 if (alo < piar->addr_lo) {
176                         p = &parent->rb_left;
177                 } else if (ahi > piar->addr_hi) {
178                         p = &parent->rb_right;
179                 } else {
180                         if (dev != piar->pcidev ||
181                             alo != piar->addr_lo || ahi != piar->addr_hi) {
182                                 printk(KERN_WARNING "PIAR: overlapping address range\n");
183                         }
184                         return piar;
185                 }
186         }
187         piar = (struct pci_io_addr_range *)kmalloc(sizeof(struct pci_io_addr_range), GFP_ATOMIC);
188         if (!piar)
189                 return NULL;
190
191         piar->addr_lo = alo;
192         piar->addr_hi = ahi;
193         piar->pcidev = dev;
194         piar->flags = flags;
195
196         rb_link_node(&piar->rb_node, parent, p);
197         rb_insert_color(&piar->rb_node, &pci_io_addr_cache_root.rb_root);
198
199         return piar;
200 }
201
202 static void __pci_addr_cache_insert_device(struct pci_dev *dev)
203 {
204         struct device_node *dn;
205         int i;
206         int inserted = 0;
207
208         dn = pci_device_to_OF_node(dev);
209         if (!dn) {
210                 printk(KERN_WARNING "PCI: no pci dn found for dev=%s %s\n",
211                         pci_name(dev), pci_pretty_name(dev));
212                 return;
213         }
214
215         /* Skip any devices for which EEH is not enabled. */
216         if (!(dn->eeh_mode & EEH_MODE_SUPPORTED) ||
217             dn->eeh_mode & EEH_MODE_NOCHECK) {
218 #ifdef DEBUG
219                 printk(KERN_INFO "PCI: skip building address cache for=%s %s\n",
220                        pci_name(dev), pci_pretty_name(dev));
221 #endif
222                 return;
223         }
224
225         /* The cache holds a reference to the device... */
226         pci_dev_get(dev);
227
228         /* Walk resources on this device, poke them into the tree */
229         for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
230                 unsigned long start = pci_resource_start(dev,i);
231                 unsigned long end = pci_resource_end(dev,i);
232                 unsigned int flags = pci_resource_flags(dev,i);
233
234                 /* We are interested only bus addresses, not dma or other stuff */
235                 if (0 == (flags & (IORESOURCE_IO | IORESOURCE_MEM)))
236                         continue;
237                 if (start == 0 || ~start == 0 || end == 0 || ~end == 0)
238                          continue;
239                 pci_addr_cache_insert(dev, start, end, flags);
240                 inserted = 1;
241         }
242
243         /* If there was nothing to add, the cache has no reference... */
244         if (!inserted)
245                 pci_dev_put(dev);
246 }
247
248 /**
249  * pci_addr_cache_insert_device - Add a device to the address cache
250  * @dev: PCI device whose I/O addresses we are interested in.
251  *
252  * In order to support the fast lookup of devices based on addresses,
253  * we maintain a cache of devices that can be quickly searched.
254  * This routine adds a device to that cache.
255  */
256 void pci_addr_cache_insert_device(struct pci_dev *dev)
257 {
258         unsigned long flags;
259
260         spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
261         __pci_addr_cache_insert_device(dev);
262         spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
263 }
264
265 static inline void __pci_addr_cache_remove_device(struct pci_dev *dev)
266 {
267         struct rb_node *n;
268         int removed = 0;
269
270 restart:
271         n = rb_first(&pci_io_addr_cache_root.rb_root);
272         while (n) {
273                 struct pci_io_addr_range *piar;
274                 piar = rb_entry(n, struct pci_io_addr_range, rb_node);
275
276                 if (piar->pcidev == dev) {
277                         rb_erase(n, &pci_io_addr_cache_root.rb_root);
278                         removed = 1;
279                         kfree(piar);
280                         goto restart;
281                 }
282                 n = rb_next(n);
283         }
284
285         /* The cache no longer holds its reference to this device... */
286         if (removed)
287                 pci_dev_put(dev);
288 }
289
290 /**
291  * pci_addr_cache_remove_device - remove pci device from addr cache
292  * @dev: device to remove
293  *
294  * Remove a device from the addr-cache tree.
295  * This is potentially expensive, since it will walk
296  * the tree multiple times (once per resource).
297  * But so what; device removal doesn't need to be that fast.
298  */
299 void pci_addr_cache_remove_device(struct pci_dev *dev)
300 {
301         unsigned long flags;
302
303         spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
304         __pci_addr_cache_remove_device(dev);
305         spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
306 }
307
308 /**
309  * pci_addr_cache_build - Build a cache of I/O addresses
310  *
311  * Build a cache of pci i/o addresses.  This cache will be used to
312  * find the pci device that corresponds to a given address.
313  * This routine scans all pci busses to build the cache.
314  * Must be run late in boot process, after the pci controllers
315  * have been scaned for devices (after all device resources are known).
316  */
317 void __init pci_addr_cache_build(void)
318 {
319         struct pci_dev *dev = NULL;
320
321         spin_lock_init(&pci_io_addr_cache_root.piar_lock);
322
323         while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
324                 /* Ignore PCI bridges ( XXX why ??) */
325                 if ((dev->class >> 16) == PCI_BASE_CLASS_BRIDGE) {
326                         continue;
327                 }
328                 pci_addr_cache_insert_device(dev);
329         }
330
331 #ifdef DEBUG
332         /* Verify tree built up above, echo back the list of addrs. */
333         pci_addr_cache_print(&pci_io_addr_cache_root);
334 #endif
335 }
336
337 /**
338  * eeh_token_to_phys - convert EEH address token to phys address
339  * @token i/o token, should be address in the form 0xE....
340  */
341 static inline unsigned long eeh_token_to_phys(unsigned long token)
342 {
343         pte_t *ptep;
344         unsigned long pa;
345
346         ptep = find_linux_pte(ioremap_mm.pgd, token);
347         if (!ptep)
348                 return token;
349         pa = pte_pfn(*ptep) << PAGE_SHIFT;
350
351         return pa | (token & (PAGE_SIZE-1));
352 }
353
354 /**
355  * eeh_dn_check_failure - check if all 1's data is due to EEH slot freeze
356  * @dn device node
357  * @dev pci device, if known
358  *
359  * Check for an EEH failure for the given device node.  Call this
360  * routine if the result of a read was all 0xff's and you want to
361  * find out if this is due to an EEH slot freeze event.  This routine
362  * will query firmware for the EEH status.
363  *
364  * Returns 0 if there has not been an EEH error; otherwise returns
365  * an error code.
366  *
367  * It is safe to call this routine in an interrupt context.
368  */
369 int eeh_dn_check_failure(struct device_node *dn, struct pci_dev *dev)
370 {
371         int ret;
372         int rets[3];
373         unsigned long flags;
374
375         __get_cpu_var(total_mmio_ffs)++;
376
377         if (!eeh_subsystem_enabled)
378                 return 0;
379
380         if (!dn)
381                 return 0;
382
383         /* Access to IO BARs might get this far and still not want checking. */
384         if (!(dn->eeh_mode & EEH_MODE_SUPPORTED) ||
385             dn->eeh_mode & EEH_MODE_NOCHECK) {
386                 return 0;
387         }
388
389         if (!dn->eeh_config_addr) {
390                 return 0;
391         }
392
393         /*
394          * Now test for an EEH failure.  This is VERY expensive.
395          * Note that the eeh_config_addr may be a parent device
396          * in the case of a device behind a bridge, or it may be
397          * function zero of a multi-function device.
398          * In any case they must share a common PHB.
399          */
400         ret = read_slot_reset_state(dn, rets);
401
402         if (ret == 0 && rets[1] == 1 && (rets[0] == 2 || rets[0] == 4)) {
403                 int log_event;
404
405                 spin_lock_irqsave(&slot_errbuf_lock, flags);
406                 memset(slot_errbuf, 0, eeh_error_buf_size);
407
408                 log_event = rtas_call(ibm_slot_error_detail,
409                                       8, 1, NULL, dn->eeh_config_addr,
410                                       BUID_HI(dn->phb->buid),
411                                       BUID_LO(dn->phb->buid), NULL, 0,
412                                       virt_to_phys(slot_errbuf),
413                                       eeh_error_buf_size,
414                                       1 /* Temporary Error */);
415
416                 if (log_event == 0)
417                         log_error(slot_errbuf, ERR_TYPE_RTAS_LOG,
418                                   1 /* Fatal */);
419
420                 spin_unlock_irqrestore(&slot_errbuf_lock, flags);
421
422                 printk(KERN_INFO "EEH: MMIO failure (%d) on device: %s %s\n",
423                        rets[0], dn->name, dn->full_name);
424                 WARN_ON(1);
425
426                 /*
427                  * XXX We should create a separate sysctl for this.
428                  *
429                  * Since the panic_on_oops sysctl is used to halt
430                  * the system in light of potential corruption, we
431                  * can use it here.
432                  */
433                 if (panic_on_oops) {
434                         panic("EEH: MMIO failure (%d) on device: %s %s\n",
435                               rets[0], dn->name, dn->full_name);
436                 } else {
437                         __get_cpu_var(ignored_failures)++;
438                 }
439         } else {
440                 __get_cpu_var(false_positives)++;
441         }
442
443         return 0;
444 }
445
446 EXPORT_SYMBOL(eeh_dn_check_failure);
447
448 /**
449  * read_slot_reset_state - get the current state of a slot for a
450  * given device node. 
451  *
452  * @dn device node for the slot to check
453  * @rets array to return results in
454  */
455 static int read_slot_reset_state(struct device_node *dn, unsigned long rets[])
456 {
457         int token, outputs;
458         
459         if (ibm_read_slot_reset_state2 != RTAS_UNKNOWN_SERVICE) {
460                 token = ibm_read_slot_reset_state2;
461                 outputs = 4;
462         } else {
463                 token = ibm_read_slot_reset_state;
464                 outputs = 3;
465         }
466
467         return rtas_call(token, 3, outputs, rets, dn->eeh_config_addr,
468                          BUID_HI(dn->phb->buid), BUID_LO(dn->phb->buid));
469 }
470
471 /**
472  * eeh_check_failure - check if all 1's data is due to EEH slot freeze
473  * @token i/o token, should be address in the form 0xA....
474  * @val value, should be all 1's (XXX why do we need this arg??)
475  *
476  * Check for an eeh failure at the given token address.
477  * Check for an EEH failure at the given token address.  Call this
478  * routine if the result of a read was all 0xff's and you want to
479  * find out if this is due to an EEH slot freeze event.  This routine
480  * will query firmware for the EEH status.
481  *
482  * Note this routine is safe to call in an interrupt context.
483  */
484 unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
485 {
486         unsigned long addr;
487         struct pci_dev *dev;
488         struct device_node *dn;
489
490         /* Finding the phys addr + pci device; this is pretty quick. */
491         addr = eeh_token_to_phys((unsigned long __force) token);
492         dev = pci_get_device_by_addr(addr);
493         if (!dev)
494                 return val;
495
496         dn = pci_device_to_OF_node(dev);
497         eeh_dn_check_failure (dn, dev);
498
499         pci_dev_put(dev);
500         return val;
501 }
502
503 EXPORT_SYMBOL(eeh_check_failure);
504
505 struct eeh_early_enable_info {
506         unsigned int buid_hi;
507         unsigned int buid_lo;
508 };
509
510 /* Enable eeh for the given device node. */
511 static void *early_enable_eeh(struct device_node *dn, void *data)
512 {
513         struct eeh_early_enable_info *info = data;
514         int ret;
515         char *status = get_property(dn, "status", NULL);
516         u32 *class_code = (u32 *)get_property(dn, "class-code", NULL);
517         u32 *vendor_id = (u32 *)get_property(dn, "vendor-id", NULL);
518         u32 *device_id = (u32 *)get_property(dn, "device-id", NULL);
519         u32 *regs;
520         int enable;
521
522         dn->eeh_mode = 0;
523
524         if (status && strcmp(status, "ok") != 0)
525                 return NULL;    /* ignore devices with bad status */
526
527         /* Ignore bad nodes. */
528         if (!class_code || !vendor_id || !device_id)
529                 return NULL;
530
531         /* There is nothing to check on PCI to ISA bridges */
532         if (dn->type && !strcmp(dn->type, "isa")) {
533                 dn->eeh_mode |= EEH_MODE_NOCHECK;
534                 return NULL;
535         }
536
537         /*
538          * Now decide if we are going to "Disable" EEH checking
539          * for this device.  We still run with the EEH hardware active,
540          * but we won't be checking for ff's.  This means a driver
541          * could return bad data (very bad!), an interrupt handler could
542          * hang waiting on status bits that won't change, etc.
543          * But there are a few cases like display devices that make sense.
544          */
545         enable = 1;     /* i.e. we will do checking */
546         if ((*class_code >> 16) == PCI_BASE_CLASS_DISPLAY)
547                 enable = 0;
548
549         if (!enable)
550                 dn->eeh_mode |= EEH_MODE_NOCHECK;
551
552         /* Ok... see if this device supports EEH.  Some do, some don't,
553          * and the only way to find out is to check each and every one. */
554         regs = (u32 *)get_property(dn, "reg", NULL);
555         if (regs) {
556                 /* First register entry is addr (00BBSS00)  */
557                 /* Try to enable eeh */
558                 ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
559                                 regs[0], info->buid_hi, info->buid_lo,
560                                 EEH_ENABLE);
561                 if (ret == 0) {
562                         eeh_subsystem_enabled = 1;
563                         dn->eeh_mode |= EEH_MODE_SUPPORTED;
564                         dn->eeh_config_addr = regs[0];
565 #ifdef DEBUG
566                         printk(KERN_DEBUG "EEH: %s: eeh enabled\n", dn->full_name);
567 #endif
568                 } else {
569
570                         /* This device doesn't support EEH, but it may have an
571                          * EEH parent, in which case we mark it as supported. */
572                         if (dn->parent && (dn->parent->eeh_mode & EEH_MODE_SUPPORTED)) {
573                                 /* Parent supports EEH. */
574                                 dn->eeh_mode |= EEH_MODE_SUPPORTED;
575                                 dn->eeh_config_addr = dn->parent->eeh_config_addr;
576                                 return NULL;
577                         }
578                 }
579         } else {
580                 printk(KERN_WARNING "EEH: %s: unable to get reg property.\n",
581                        dn->full_name);
582         }
583
584         return NULL; 
585 }
586
587 /*
588  * Initialize EEH by trying to enable it for all of the adapters in the system.
589  * As a side effect we can determine here if eeh is supported at all.
590  * Note that we leave EEH on so failed config cycles won't cause a machine
591  * check.  If a user turns off EEH for a particular adapter they are really
592  * telling Linux to ignore errors.  Some hardware (e.g. POWER5) won't
593  * grant access to a slot if EEH isn't enabled, and so we always enable
594  * EEH for all slots/all devices.
595  *
596  * The eeh-force-off option disables EEH checking globally, for all slots.
597  * Even if force-off is set, the EEH hardware is still enabled, so that
598  * newer systems can boot.
599  */
600 void __init eeh_init(void)
601 {
602         struct device_node *phb, *np;
603         struct eeh_early_enable_info info;
604
605         init_pci_config_tokens();
606
607         np = of_find_node_by_path("/rtas");
608         if (np == NULL)
609                 return;
610
611         ibm_set_eeh_option = rtas_token("ibm,set-eeh-option");
612         ibm_set_slot_reset = rtas_token("ibm,set-slot-reset");
613         ibm_read_slot_reset_state = rtas_token("ibm,read-slot-reset-state");
614         ibm_read_slot_reset_state2 = rtas_token("ibm,read-slot-reset-state2");
615         ibm_slot_error_detail = rtas_token("ibm,slot-error-detail");
616
617         if (ibm_set_eeh_option == RTAS_UNKNOWN_SERVICE)
618                 return;
619
620         eeh_error_buf_size = rtas_token("rtas-error-log-max");
621         if (eeh_error_buf_size == RTAS_UNKNOWN_SERVICE) {
622                 eeh_error_buf_size = 1024;
623         }
624         if (eeh_error_buf_size > RTAS_ERROR_LOG_MAX) {
625                 printk(KERN_WARNING "EEH: rtas-error-log-max is bigger than allocated "
626                       "buffer ! (%d vs %d)", eeh_error_buf_size, RTAS_ERROR_LOG_MAX);
627                 eeh_error_buf_size = RTAS_ERROR_LOG_MAX;
628         }
629
630         /* Enable EEH for all adapters.  Note that eeh requires buid's */
631         for (phb = of_find_node_by_name(NULL, "pci"); phb;
632              phb = of_find_node_by_name(phb, "pci")) {
633                 unsigned long buid;
634
635                 buid = get_phb_buid(phb);
636                 if (buid == 0)
637                         continue;
638
639                 info.buid_lo = BUID_LO(buid);
640                 info.buid_hi = BUID_HI(buid);
641                 traverse_pci_devices(phb, early_enable_eeh, &info);
642         }
643
644         if (eeh_subsystem_enabled)
645                 printk(KERN_INFO "EEH: PCI Enhanced I/O Error Handling Enabled\n");
646         else
647                 printk(KERN_WARNING "EEH: No capable adapters found\n");
648 }
649
650 /**
651  * eeh_add_device_early - enable EEH for the indicated device_node
652  * @dn: device node for which to set up EEH
653  *
654  * This routine must be used to perform EEH initialization for PCI
655  * devices that were added after system boot (e.g. hotplug, dlpar).
656  * This routine must be called before any i/o is performed to the
657  * adapter (inluding any config-space i/o).
658  * Whether this actually enables EEH or not for this device depends
659  * on the CEC architecture, type of the device, on earlier boot
660  * command-line arguments & etc.
661  */
662 void eeh_add_device_early(struct device_node *dn)
663 {
664         struct pci_controller *phb;
665         struct eeh_early_enable_info info;
666
667         if (!dn || !eeh_subsystem_enabled)
668                 return;
669         phb = dn->phb;
670         if (NULL == phb || 0 == phb->buid) {
671                 printk(KERN_WARNING "EEH: Expected buid but found none\n");
672                 return;
673         }
674
675         info.buid_hi = BUID_HI(phb->buid);
676         info.buid_lo = BUID_LO(phb->buid);
677         early_enable_eeh(dn, &info);
678 }
679 EXPORT_SYMBOL(eeh_add_device_early);
680
681 /**
682  * eeh_add_device_late - perform EEH initialization for the indicated pci device
683  * @dev: pci device for which to set up EEH
684  *
685  * This routine must be used to complete EEH initialization for PCI
686  * devices that were added after system boot (e.g. hotplug, dlpar).
687  */
688 void eeh_add_device_late(struct pci_dev *dev)
689 {
690         if (!dev || !eeh_subsystem_enabled)
691                 return;
692
693 #ifdef DEBUG
694         printk(KERN_DEBUG "EEH: adding device %s %s\n", pci_name(dev),
695                pci_pretty_name(dev));
696 #endif
697
698         pci_addr_cache_insert_device (dev);
699 }
700 EXPORT_SYMBOL(eeh_add_device_late);
701
702 /**
703  * eeh_remove_device - undo EEH setup for the indicated pci device
704  * @dev: pci device to be removed
705  *
706  * This routine should be when a device is removed from a running
707  * system (e.g. by hotplug or dlpar).
708  */
709 void eeh_remove_device(struct pci_dev *dev)
710 {
711         if (!dev || !eeh_subsystem_enabled)
712                 return;
713
714         /* Unregister the device with the EEH/PCI address search system */
715 #ifdef DEBUG
716         printk(KERN_DEBUG "EEH: remove device %s %s\n", pci_name(dev),
717                pci_pretty_name(dev));
718 #endif
719         pci_addr_cache_remove_device(dev);
720 }
721 EXPORT_SYMBOL(eeh_remove_device);
722
723 static int proc_eeh_show(struct seq_file *m, void *v)
724 {
725         unsigned int cpu;
726         unsigned long ffs = 0, positives = 0, failures = 0;
727
728         for_each_cpu(cpu) {
729                 ffs += per_cpu(total_mmio_ffs, cpu);
730                 positives += per_cpu(false_positives, cpu);
731                 failures += per_cpu(ignored_failures, cpu);
732         }
733
734         if (0 == eeh_subsystem_enabled) {
735                 seq_printf(m, "EEH Subsystem is globally disabled\n");
736                 seq_printf(m, "eeh_total_mmio_ffs=%ld\n", ffs);
737         } else {
738                 seq_printf(m, "EEH Subsystem is enabled\n");
739                 seq_printf(m, "eeh_total_mmio_ffs=%ld\n"
740                            "eeh_false_positives=%ld\n"
741                            "eeh_ignored_failures=%ld\n",
742                            ffs, positives, failures);
743         }
744
745         return 0;
746 }
747
748 static int proc_eeh_open(struct inode *inode, struct file *file)
749 {
750         return single_open(file, proc_eeh_show, NULL);
751 }
752
753 static struct file_operations proc_eeh_operations = {
754         .open      = proc_eeh_open,
755         .read      = seq_read,
756         .llseek    = seq_lseek,
757         .release   = single_release,
758 };
759
760 static int __init eeh_init_proc(void)
761 {
762         struct proc_dir_entry *e;
763
764         if (systemcfg->platform & PLATFORM_PSERIES) {
765                 e = create_proc_entry("ppc64/eeh", 0, NULL);
766                 if (e)
767                         e->proc_fops = &proc_eeh_operations;
768         }
769
770         return 0;
771 }
772 __initcall(eeh_init_proc);