VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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_slot_error_detail;
49
50 static int eeh_subsystem_enabled;
51 #define EEH_MAX_OPTS 4096
52 static char *eeh_opts;
53 static int eeh_opts_last;
54
55 /* Buffer for reporting slot-error-detail rtas calls */
56 static unsigned char slot_errbuf[RTAS_ERROR_LOG_MAX];
57 static spinlock_t slot_errbuf_lock = SPIN_LOCK_UNLOCKED;
58 static int eeh_error_buf_size;
59
60 /* System monitoring statistics */
61 static DEFINE_PER_CPU(unsigned long, total_mmio_ffs);
62 static DEFINE_PER_CPU(unsigned long, false_positives);
63 static DEFINE_PER_CPU(unsigned long, ignored_failures);
64
65 static int eeh_check_opts_config(struct device_node *dn, int class_code,
66                                  int vendor_id, int device_id,
67                                  int default_state);
68
69 /**
70  * The pci address cache subsystem.  This subsystem places
71  * PCI device address resources into a red-black tree, sorted
72  * according to the address range, so that given only an i/o
73  * address, the corresponding PCI device can be **quickly**
74  * found.
75  *
76  * Currently, the only customer of this code is the EEH subsystem;
77  * thus, this code has been somewhat tailored to suit EEH better.
78  * In particular, the cache does *not* hold the addresses of devices
79  * for which EEH is not enabled.
80  *
81  * (Implementation Note: The RB tree seems to be better/faster
82  * than any hash algo I could think of for this problem, even
83  * with the penalty of slow pointer chases for d-cache misses).
84  */
85 struct pci_io_addr_range
86 {
87         struct rb_node rb_node;
88         unsigned long addr_lo;
89         unsigned long addr_hi;
90         struct pci_dev *pcidev;
91         unsigned int flags;
92 };
93
94 static struct pci_io_addr_cache
95 {
96         struct rb_root rb_root;
97         spinlock_t piar_lock;
98 } pci_io_addr_cache_root;
99
100 static inline struct pci_dev *__pci_get_device_by_addr(unsigned long addr)
101 {
102         struct rb_node *n = pci_io_addr_cache_root.rb_root.rb_node;
103
104         while (n) {
105                 struct pci_io_addr_range *piar;
106                 piar = rb_entry(n, struct pci_io_addr_range, rb_node);
107
108                 if (addr < piar->addr_lo) {
109                         n = n->rb_left;
110                 } else {
111                         if (addr > piar->addr_hi) {
112                                 n = n->rb_right;
113                         } else {
114                                 pci_dev_get(piar->pcidev);
115                                 return piar->pcidev;
116                         }
117                 }
118         }
119
120         return NULL;
121 }
122
123 /**
124  * pci_get_device_by_addr - Get device, given only address
125  * @addr: mmio (PIO) phys address or i/o port number
126  *
127  * Given an mmio phys address, or a port number, find a pci device
128  * that implements this address.  Be sure to pci_dev_put the device
129  * when finished.  I/O port numbers are assumed to be offset
130  * from zero (that is, they do *not* have pci_io_addr added in).
131  * It is safe to call this function within an interrupt.
132  */
133 static struct pci_dev *pci_get_device_by_addr(unsigned long addr)
134 {
135         struct pci_dev *dev;
136         unsigned long flags;
137
138         spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
139         dev = __pci_get_device_by_addr(addr);
140         spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
141         return dev;
142 }
143
144 #ifdef DEBUG
145 /*
146  * Handy-dandy debug print routine, does nothing more
147  * than print out the contents of our addr cache.
148  */
149 static void pci_addr_cache_print(struct pci_io_addr_cache *cache)
150 {
151         struct rb_node *n;
152         int cnt = 0;
153
154         n = rb_first(&cache->rb_root);
155         while (n) {
156                 struct pci_io_addr_range *piar;
157                 piar = rb_entry(n, struct pci_io_addr_range, rb_node);
158                 printk(KERN_DEBUG "PCI: %s addr range %d [%lx-%lx]: %s %s\n",
159                        (piar->flags & IORESOURCE_IO) ? "i/o" : "mem", cnt,
160                        piar->addr_lo, piar->addr_hi, pci_name(piar->pcidev),
161                        pci_pretty_name(piar->pcidev));
162                 cnt++;
163                 n = rb_next(n);
164         }
165 }
166 #endif
167
168 /* Insert address range into the rb tree. */
169 static struct pci_io_addr_range *
170 pci_addr_cache_insert(struct pci_dev *dev, unsigned long alo,
171                       unsigned long ahi, unsigned int flags)
172 {
173         struct rb_node **p = &pci_io_addr_cache_root.rb_root.rb_node;
174         struct rb_node *parent = NULL;
175         struct pci_io_addr_range *piar;
176
177         /* Walk tree, find a place to insert into tree */
178         while (*p) {
179                 parent = *p;
180                 piar = rb_entry(parent, struct pci_io_addr_range, rb_node);
181                 if (alo < piar->addr_lo) {
182                         p = &parent->rb_left;
183                 } else if (ahi > piar->addr_hi) {
184                         p = &parent->rb_right;
185                 } else {
186                         if (dev != piar->pcidev ||
187                             alo != piar->addr_lo || ahi != piar->addr_hi) {
188                                 printk(KERN_WARNING "PIAR: overlapping address range\n");
189                         }
190                         return piar;
191                 }
192         }
193         piar = (struct pci_io_addr_range *)kmalloc(sizeof(struct pci_io_addr_range), GFP_ATOMIC);
194         if (!piar)
195                 return NULL;
196
197         piar->addr_lo = alo;
198         piar->addr_hi = ahi;
199         piar->pcidev = dev;
200         piar->flags = flags;
201
202         rb_link_node(&piar->rb_node, parent, p);
203         rb_insert_color(&piar->rb_node, &pci_io_addr_cache_root.rb_root);
204
205         return piar;
206 }
207
208 static void __pci_addr_cache_insert_device(struct pci_dev *dev)
209 {
210         struct device_node *dn;
211         int i;
212
213         dn = pci_device_to_OF_node(dev);
214         if (!dn) {
215                 printk(KERN_WARNING "PCI: no pci dn found for dev=%s %s\n",
216                         pci_name(dev), pci_pretty_name(dev));
217                 return;
218         }
219
220         /* Skip any devices for which EEH is not enabled. */
221         if (!(dn->eeh_mode & EEH_MODE_SUPPORTED) ||
222             dn->eeh_mode & EEH_MODE_NOCHECK) {
223 #ifdef DEBUG
224                 printk(KERN_INFO "PCI: skip building address cache for=%s %s\n",
225                        pci_name(dev), pci_pretty_name(dev));
226 #endif
227                 return;
228         }
229
230         /* The cache holds a reference to the device... */
231         pci_dev_get(dev);
232
233         /* Walk resources on this device, poke them into the tree */
234         for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
235                 unsigned long start = pci_resource_start(dev,i);
236                 unsigned long end = pci_resource_end(dev,i);
237                 unsigned int flags = pci_resource_flags(dev,i);
238
239                 /* We are interested only bus addresses, not dma or other stuff */
240                 if (0 == (flags & (IORESOURCE_IO | IORESOURCE_MEM)))
241                         continue;
242                 if (start == 0 || ~start == 0 || end == 0 || ~end == 0)
243                          continue;
244                 pci_addr_cache_insert(dev, start, end, flags);
245         }
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
269 restart:
270         n = rb_first(&pci_io_addr_cache_root.rb_root);
271         while (n) {
272                 struct pci_io_addr_range *piar;
273                 piar = rb_entry(n, struct pci_io_addr_range, rb_node);
274
275                 if (piar->pcidev == dev) {
276                         rb_erase(n, &pci_io_addr_cache_root.rb_root);
277                         kfree(piar);
278                         goto restart;
279                 }
280                 n = rb_next(n);
281         }
282
283         /* The cache no longer holds its reference to this device... */
284         pci_dev_put(dev);
285 }
286
287 /**
288  * pci_addr_cache_remove_device - remove pci device from addr cache
289  * @dev: device to remove
290  *
291  * Remove a device from the addr-cache tree.
292  * This is potentially expensive, since it will walk
293  * the tree multiple times (once per resource).
294  * But so what; device removal doesn't need to be that fast.
295  */
296 void pci_addr_cache_remove_device(struct pci_dev *dev)
297 {
298         unsigned long flags;
299
300         spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
301         __pci_addr_cache_remove_device(dev);
302         spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
303 }
304
305 /**
306  * pci_addr_cache_build - Build a cache of I/O addresses
307  *
308  * Build a cache of pci i/o addresses.  This cache will be used to
309  * find the pci device that corresponds to a given address.
310  * This routine scans all pci busses to build the cache.
311  * Must be run late in boot process, after the pci controllers
312  * have been scaned for devices (after all device resources are known).
313  */
314 void __init pci_addr_cache_build(void)
315 {
316         struct pci_dev *dev = NULL;
317
318         spin_lock_init(&pci_io_addr_cache_root.piar_lock);
319
320         while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
321                 /* Ignore PCI bridges ( XXX why ??) */
322                 if ((dev->class >> 16) == PCI_BASE_CLASS_BRIDGE) {
323                         continue;
324                 }
325                 pci_addr_cache_insert_device(dev);
326         }
327
328 #ifdef DEBUG
329         /* Verify tree built up above, echo back the list of addrs. */
330         pci_addr_cache_print(&pci_io_addr_cache_root);
331 #endif
332 }
333
334 /**
335  * eeh_token_to_phys - convert EEH address token to phys address
336  * @token i/o token, should be address in the form 0xA....
337  *
338  * Converts EEH address tokens into physical addresses.  Note that
339  * ths routine does *not* convert I/O BAR addresses (which start
340  * with 0xE...) to phys addresses!
341  */
342 static unsigned long eeh_token_to_phys(unsigned long token)
343 {
344         pte_t *ptep;
345         unsigned long pa, vaddr;
346
347         if (REGION_ID(token) == EEH_REGION_ID)
348                 vaddr = IO_TOKEN_TO_ADDR(token);
349         else
350                 return token;
351
352         ptep = find_linux_pte(ioremap_mm.pgd, vaddr);
353         pa = pte_pfn(*ptep) << PAGE_SHIFT;
354
355         return pa | (vaddr & (PAGE_SIZE-1));
356 }
357
358 /**
359  * eeh_check_failure - check if all 1's data is due to EEH slot freeze
360  * @token i/o token, should be address in the form 0xA....
361  * @val value, should be all 1's (XXX why do we need this arg??)
362  *
363  * Check for an eeh failure at the given token address.
364  * The given value has been read and it should be 1's (0xff, 0xffff or
365  * 0xffffffff).
366  *
367  * Probe to determine if an error actually occurred.  If not return val.
368  * Otherwise panic.
369  *
370  * Note this routine might be called in an interrupt context ...
371  */
372 unsigned long eeh_check_failure(void *token, unsigned long val)
373 {
374         unsigned long addr;
375         struct pci_dev *dev;
376         struct device_node *dn;
377         int ret;
378         int rets[2];
379         unsigned long flags;
380
381         __get_cpu_var(total_mmio_ffs)++;
382
383         if (!eeh_subsystem_enabled)
384                 return val;
385
386         /* Finding the phys addr + pci device; this is pretty quick. */
387         addr = eeh_token_to_phys((unsigned long)token);
388         dev = pci_get_device_by_addr(addr);
389         if (!dev)
390                 return val;
391
392         dn = pci_device_to_OF_node(dev);
393         if (!dn) {
394                 pci_dev_put(dev);
395                 return val;
396         }
397
398         /* Access to IO BARs might get this far and still not want checking. */
399         if (!(dn->eeh_mode & EEH_MODE_SUPPORTED) ||
400             dn->eeh_mode & EEH_MODE_NOCHECK) {
401                 pci_dev_put(dev);
402                 return val;
403         }
404
405         if (!dn->eeh_config_addr) {
406                 pci_dev_put(dev);
407                 return val;
408         }
409
410         /*
411          * Now test for an EEH failure.  This is VERY expensive.
412          * Note that the eeh_config_addr may be a parent device
413          * in the case of a device behind a bridge, or it may be
414          * function zero of a multi-function device.
415          * In any case they must share a common PHB.
416          */
417         ret = rtas_call(ibm_read_slot_reset_state, 3, 3, rets,
418                         dn->eeh_config_addr, BUID_HI(dn->phb->buid),
419                         BUID_LO(dn->phb->buid));
420
421         if (ret == 0 && rets[1] == 1 && rets[0] >= 2) {
422                 int log_event;
423
424                 spin_lock_irqsave(&slot_errbuf_lock, flags);
425                 memset(slot_errbuf, 0, eeh_error_buf_size);
426
427                 log_event = rtas_call(ibm_slot_error_detail,
428                                       8, 1, NULL, dn->eeh_config_addr,
429                                       BUID_HI(dn->phb->buid),
430                                       BUID_LO(dn->phb->buid), NULL, 0,
431                                       virt_to_phys(slot_errbuf),
432                                       eeh_error_buf_size,
433                                       2 /* Permanent Error */);
434
435                 if (log_event == 0)
436                         log_error(slot_errbuf, ERR_TYPE_RTAS_LOG,
437                                   1 /* Fatal */);
438
439                 spin_unlock_irqrestore(&slot_errbuf_lock, flags);
440
441                 /*
442                  * XXX We should create a separate sysctl for this.
443                  *
444                  * Since the panic_on_oops sysctl is used to halt
445                  * the system in light of potential corruption, we
446                  * can use it here.
447                  */
448                 if (panic_on_oops) {
449                         panic("EEH: MMIO failure (%d) on device:%s %s\n",
450                               rets[0], pci_name(dev), pci_pretty_name(dev));
451                 } else {
452                         __get_cpu_var(ignored_failures)++;
453                         printk(KERN_INFO "EEH: MMIO failure (%d) on device:%s %s\n",
454                                rets[0], pci_name(dev), pci_pretty_name(dev));
455                 }
456         } else {
457                 __get_cpu_var(false_positives)++;
458         }
459
460         pci_dev_put(dev);
461         return val;
462 }
463 EXPORT_SYMBOL(eeh_check_failure);
464
465 struct eeh_early_enable_info {
466         unsigned int buid_hi;
467         unsigned int buid_lo;
468         int force_off;
469 };
470
471 /* Enable eeh for the given device node. */
472 static void *early_enable_eeh(struct device_node *dn, void *data)
473 {
474         struct eeh_early_enable_info *info = data;
475         int ret;
476         char *status = get_property(dn, "status", NULL);
477         u32 *class_code = (u32 *)get_property(dn, "class-code", NULL);
478         u32 *vendor_id = (u32 *)get_property(dn, "vendor-id", NULL);
479         u32 *device_id = (u32 *)get_property(dn, "device-id", NULL);
480         u32 *regs;
481         int enable;
482
483         dn->eeh_mode = 0;
484
485         if (status && strcmp(status, "ok") != 0)
486                 return NULL;    /* ignore devices with bad status */
487
488         /* Ignore bad nodes. */
489         if (!class_code || !vendor_id || !device_id)
490                 return NULL;
491
492         /* There is nothing to check on PCI to ISA bridges */
493         if (dn->type && !strcmp(dn->type, "isa")) {
494                 dn->eeh_mode |= EEH_MODE_NOCHECK;
495                 return NULL;
496         }
497
498         /*
499          * Now decide if we are going to "Disable" EEH checking
500          * for this device.  We still run with the EEH hardware active,
501          * but we won't be checking for ff's.  This means a driver
502          * could return bad data (very bad!), an interrupt handler could
503          * hang waiting on status bits that won't change, etc.
504          * But there are a few cases like display devices that make sense.
505          */
506         enable = 1;     /* i.e. we will do checking */
507         if ((*class_code >> 16) == PCI_BASE_CLASS_DISPLAY)
508                 enable = 0;
509
510         if (!eeh_check_opts_config(dn, *class_code, *vendor_id, *device_id,
511                                    enable)) {
512                 if (enable) {
513                         printk(KERN_WARNING "EEH: %s user requested to run "
514                                "without EEH checking.\n", dn->full_name);
515                         enable = 0;
516                 }
517         }
518
519         if (!enable || info->force_off) {
520                 dn->eeh_mode |= EEH_MODE_NOCHECK;
521         }
522
523         /* Ok... see if this device supports EEH.  Some do, some don't,
524          * and the only way to find out is to check each and every one. */
525         regs = (u32 *)get_property(dn, "reg", NULL);
526         if (regs) {
527                 /* First register entry is addr (00BBSS00)  */
528                 /* Try to enable eeh */
529                 ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
530                                 regs[0], info->buid_hi, info->buid_lo,
531                                 EEH_ENABLE);
532                 if (ret == 0) {
533                         eeh_subsystem_enabled = 1;
534                         dn->eeh_mode |= EEH_MODE_SUPPORTED;
535                         dn->eeh_config_addr = regs[0];
536 #ifdef DEBUG
537                         printk(KERN_DEBUG "EEH: %s: eeh enabled\n", dn->full_name);
538 #endif
539                 } else {
540
541                         /* This device doesn't support EEH, but it may have an
542                          * EEH parent, in which case we mark it as supported. */
543                         if (dn->parent && (dn->parent->eeh_mode & EEH_MODE_SUPPORTED)) {
544                                 /* Parent supports EEH. */
545                                 dn->eeh_mode |= EEH_MODE_SUPPORTED;
546                                 dn->eeh_config_addr = dn->parent->eeh_config_addr;
547                                 return NULL;
548                         }
549                 }
550         } else {
551                 printk(KERN_WARNING "EEH: %s: unable to get reg property.\n",
552                        dn->full_name);
553         }
554
555         return NULL; 
556 }
557
558 /*
559  * Initialize EEH by trying to enable it for all of the adapters in the system.
560  * As a side effect we can determine here if eeh is supported at all.
561  * Note that we leave EEH on so failed config cycles won't cause a machine
562  * check.  If a user turns off EEH for a particular adapter they are really
563  * telling Linux to ignore errors.  Some hardware (e.g. POWER5) won't
564  * grant access to a slot if EEH isn't enabled, and so we always enable
565  * EEH for all slots/all devices.
566  *
567  * The eeh-force-off option disables EEH checking globally, for all slots.
568  * Even if force-off is set, the EEH hardware is still enabled, so that
569  * newer systems can boot.
570  */
571 void __init eeh_init(void)
572 {
573         struct device_node *phb, *np;
574         struct eeh_early_enable_info info;
575         char *eeh_force_off = strstr(saved_command_line, "eeh-force-off");
576
577         init_pci_config_tokens();
578
579         np = of_find_node_by_path("/rtas");
580         if (np == NULL) {
581                 printk(KERN_WARNING "EEH: RTAS not found !\n");
582                 return;
583         }
584
585         ibm_set_eeh_option = rtas_token("ibm,set-eeh-option");
586         ibm_set_slot_reset = rtas_token("ibm,set-slot-reset");
587         ibm_read_slot_reset_state = rtas_token("ibm,read-slot-reset-state");
588         ibm_slot_error_detail = rtas_token("ibm,slot-error-detail");
589
590         if (ibm_set_eeh_option == RTAS_UNKNOWN_SERVICE)
591                 return;
592
593         eeh_error_buf_size = rtas_token("rtas-error-log-max");
594         if (eeh_error_buf_size == RTAS_UNKNOWN_SERVICE) {
595                 eeh_error_buf_size = 1024;
596         }
597         if (eeh_error_buf_size > RTAS_ERROR_LOG_MAX) {
598                 printk(KERN_WARNING "EEH: rtas-error-log-max is bigger than allocated "
599                       "buffer ! (%d vs %d)", eeh_error_buf_size, RTAS_ERROR_LOG_MAX);
600                 eeh_error_buf_size = RTAS_ERROR_LOG_MAX;
601         }
602
603         info.force_off = 0;
604         if (eeh_force_off) {
605                 printk(KERN_WARNING "EEH: WARNING: PCI Enhanced I/O Error "
606                        "Handling is user disabled\n");
607                 info.force_off = 1;
608         }
609
610         /* Enable EEH for all adapters.  Note that eeh requires buid's */
611         for (phb = of_find_node_by_name(NULL, "pci"); phb;
612              phb = of_find_node_by_name(phb, "pci")) {
613                 unsigned long buid;
614
615                 buid = get_phb_buid(phb);
616                 if (buid == 0)
617                         continue;
618
619                 info.buid_lo = BUID_LO(buid);
620                 info.buid_hi = BUID_HI(buid);
621                 traverse_pci_devices(phb, early_enable_eeh, &info);
622         }
623
624         if (eeh_subsystem_enabled) {
625                 printk(KERN_INFO "EEH: PCI Enhanced I/O Error Handling Enabled\n");
626         } else {
627                 printk(KERN_WARNING "EEH: disabled PCI Enhanced I/O Error Handling\n");
628         }
629 }
630
631 /**
632  * eeh_add_device_early - enable EEH for the indicated device_node
633  * @dn: device node for which to set up EEH
634  *
635  * This routine must be used to perform EEH initialization for PCI
636  * devices that were added after system boot (e.g. hotplug, dlpar).
637  * This routine must be called before any i/o is performed to the
638  * adapter (inluding any config-space i/o).
639  * Whether this actually enables EEH or not for this device depends
640  * on the CEC architecture, type of the device, on earlier boot
641  * command-line arguments & etc.
642  */
643 void eeh_add_device_early(struct device_node *dn)
644 {
645         struct pci_controller *phb;
646         struct eeh_early_enable_info info;
647
648         if (!dn || !eeh_subsystem_enabled)
649                 return;
650         phb = dn->phb;
651         if (NULL == phb || 0 == phb->buid) {
652                 printk(KERN_WARNING "EEH: Expected buid but found none\n");
653                 return;
654         }
655
656         info.buid_hi = BUID_HI(phb->buid);
657         info.buid_lo = BUID_LO(phb->buid);
658         early_enable_eeh(dn, &info);
659 }
660 EXPORT_SYMBOL(eeh_add_device_early);
661
662 /**
663  * eeh_add_device_late - perform EEH initialization for the indicated pci device
664  * @dev: pci device for which to set up EEH
665  *
666  * This routine must be used to complete EEH initialization for PCI
667  * devices that were added after system boot (e.g. hotplug, dlpar).
668  */
669 void eeh_add_device_late(struct pci_dev *dev)
670 {
671         if (!dev || !eeh_subsystem_enabled)
672                 return;
673
674 #ifdef DEBUG
675         printk(KERN_DEBUG "EEH: adding device %s %s\n", pci_name(dev),
676                pci_pretty_name(dev));
677 #endif
678
679         pci_addr_cache_insert_device (dev);
680 }
681 EXPORT_SYMBOL(eeh_add_device_late);
682
683 /**
684  * eeh_remove_device - undo EEH setup for the indicated pci device
685  * @dev: pci device to be removed
686  *
687  * This routine should be when a device is removed from a running
688  * system (e.g. by hotplug or dlpar).
689  */
690 void eeh_remove_device(struct pci_dev *dev)
691 {
692         if (!dev || !eeh_subsystem_enabled)
693                 return;
694
695         /* Unregister the device with the EEH/PCI address search system */
696 #ifdef DEBUG
697         printk(KERN_DEBUG "EEH: remove device %s %s\n", pci_name(dev),
698                pci_pretty_name(dev));
699 #endif
700         pci_addr_cache_remove_device(dev);
701 }
702 EXPORT_SYMBOL(eeh_remove_device);
703
704 /*
705  * If EEH is implemented, find the PCI device using given phys addr
706  * and check to see if eeh failure checking is disabled.
707  * Remap the addr (trivially) to the EEH region if EEH checking enabled.
708  * For addresses not known to PCI the vaddr is simply returned unchanged.
709  */
710 void *eeh_ioremap(unsigned long addr, void *vaddr)
711 {
712         struct pci_dev *dev;
713         struct device_node *dn;
714
715         if (!eeh_subsystem_enabled)
716                 return vaddr;
717
718         dev = pci_get_device_by_addr(addr);
719         if (!dev)
720                 return vaddr;
721
722         dn = pci_device_to_OF_node(dev);
723         if (!dn) {
724                 pci_dev_put(dev);
725                 return vaddr;
726         }
727
728         if (dn->eeh_mode & EEH_MODE_NOCHECK) {
729                 pci_dev_put(dev);
730                 return vaddr;
731         }
732
733         pci_dev_put(dev);
734         return (void *)IO_ADDR_TO_TOKEN(vaddr);
735 }
736
737 static int proc_eeh_show(struct seq_file *m, void *v)
738 {
739         unsigned int cpu;
740         unsigned long ffs = 0, positives = 0, failures = 0;
741
742         for_each_cpu(cpu) {
743                 ffs += per_cpu(total_mmio_ffs, cpu);
744                 positives += per_cpu(false_positives, cpu);
745                 failures += per_cpu(ignored_failures, cpu);
746         }
747
748         if (0 == eeh_subsystem_enabled) {
749                 seq_printf(m, "EEH Subsystem is globally disabled\n");
750                 seq_printf(m, "eeh_total_mmio_ffs=%ld\n", ffs);
751         } else {
752                 seq_printf(m, "EEH Subsystem is enabled\n");
753                 seq_printf(m, "eeh_total_mmio_ffs=%ld\n"
754                            "eeh_false_positives=%ld\n"
755                            "eeh_ignored_failures=%ld\n",
756                            ffs, positives, failures);
757         }
758
759         return 0;
760 }
761
762 static int proc_eeh_open(struct inode *inode, struct file *file)
763 {
764         return single_open(file, proc_eeh_show, NULL);
765 }
766
767 static struct file_operations proc_eeh_operations = {
768         .open      = proc_eeh_open,
769         .read      = seq_read,
770         .llseek    = seq_lseek,
771         .release   = single_release,
772 };
773
774 static int __init eeh_init_proc(void)
775 {
776         struct proc_dir_entry *e;
777
778         if (systemcfg->platform & PLATFORM_PSERIES) {
779                 e = create_proc_entry("ppc64/eeh", 0, NULL);
780                 if (e)
781                         e->proc_fops = &proc_eeh_operations;
782         }
783
784         return 0;
785 }
786 __initcall(eeh_init_proc);
787
788 /*
789  * Test if "dev" should be configured on or off.
790  * This processes the options literally from left to right.
791  * This lets the user specify stupid combinations of options,
792  * but at least the result should be very predictable.
793  */
794 static int eeh_check_opts_config(struct device_node *dn,
795                                  int class_code, int vendor_id, int device_id,
796                                  int default_state)
797 {
798         char devname[32], classname[32];
799         char *strs[8], *s;
800         int nstrs, i;
801         int ret = default_state;
802
803         /* Build list of strings to match */
804         nstrs = 0;
805         s = (char *)get_property(dn, "ibm,loc-code", NULL);
806         if (s)
807                 strs[nstrs++] = s;
808         sprintf(devname, "dev%04x:%04x", vendor_id, device_id);
809         strs[nstrs++] = devname;
810         sprintf(classname, "class%04x", class_code);
811         strs[nstrs++] = classname;
812         strs[nstrs++] = "";     /* yes, this matches the empty string */
813
814         /*
815          * Now see if any string matches the eeh_opts list.
816          * The eeh_opts list entries start with + or -.
817          */
818         for (s = eeh_opts; s && (s < (eeh_opts + eeh_opts_last));
819              s += strlen(s)+1) {
820                 for (i = 0; i < nstrs; i++) {
821                         if (strcasecmp(strs[i], s+1) == 0) {
822                                 ret = (strs[i][0] == '+') ? 1 : 0;
823                         }
824                 }
825         }
826         return ret;
827 }
828
829 /*
830  * Handle kernel eeh-on & eeh-off cmd line options for eeh.
831  *
832  * We support:
833  *      eeh-off=loc1,loc2,loc3...
834  *
835  * and this option can be repeated so
836  *      eeh-off=loc1,loc2 eeh-off=loc3
837  * is the same as eeh-off=loc1,loc2,loc3
838  *
839  * loc is an IBM location code that can be found in a manual or
840  * via openfirmware (or the Hardware Management Console).
841  *
842  * We also support these additional "loc" values:
843  *
844  *      dev#:#    vendor:device id in hex (e.g. dev1022:2000)
845  *      class#    class id in hex (e.g. class0200)
846  *
847  * If no location code is specified all devices are assumed
848  * so eeh-off means eeh by default is off.
849  */
850
851 /*
852  * This is implemented as a null separated list of strings.
853  * Each string looks like this:  "+X" or "-X"
854  * where X is a loc code, vendor:device, class (as shown above)
855  * or empty which is used to indicate all.
856  *
857  * We interpret this option string list so that it will literally
858  * behave left-to-right even if some combinations don't make sense.
859  */
860 static int __init eeh_parm(char *str, int state)
861 {
862         char *s, *cur, *curend;
863
864         if (!eeh_opts) {
865                 eeh_opts = alloc_bootmem(EEH_MAX_OPTS);
866                 eeh_opts[eeh_opts_last++] = '+'; /* default */
867                 eeh_opts[eeh_opts_last++] = '\0';
868         }
869         if (*str == '\0') {
870                 eeh_opts[eeh_opts_last++] = state ? '+' : '-';
871                 eeh_opts[eeh_opts_last++] = '\0';
872                 return 1;
873         }
874         if (*str == '=')
875                 str++;
876         for (s = str; s && *s != '\0'; s = curend) {
877                 cur = s;
878                 /* ignore empties.  Don't treat as "all-on" or "all-off" */
879                 while (*cur == ',')
880                         cur++;
881                 curend = strchr(cur, ',');
882                 if (!curend)
883                         curend = cur + strlen(cur);
884                 if (*cur) {
885                         int curlen = curend-cur;
886                         if (eeh_opts_last + curlen > EEH_MAX_OPTS-2) {
887                                 printk(KERN_WARNING "EEH: sorry...too many "
888                                        "eeh cmd line options\n");
889                                 return 1;
890                         }
891                         eeh_opts[eeh_opts_last++] = state ? '+' : '-';
892                         strncpy(eeh_opts+eeh_opts_last, cur, curlen);
893                         eeh_opts_last += curlen;
894                         eeh_opts[eeh_opts_last++] = '\0';
895                 }
896         }
897
898         return 1;
899 }
900
901 static int __init eehoff_parm(char *str)
902 {
903         return eeh_parm(str, 0);
904 }
905
906 static int __init eehon_parm(char *str)
907 {
908         return eeh_parm(str, 1);
909 }
910
911 __setup("eeh-off", eehoff_parm);
912 __setup("eeh-on", eehon_parm);