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