powerpc/eeh: Trace EEH state based on PE
[firefly-linux-kernel-4.4.55.git] / arch / powerpc / platforms / pseries / eeh.c
1 /*
2  * Copyright IBM Corporation 2001, 2005, 2006
3  * Copyright Dave Engebretsen & Todd Inglett 2001
4  * Copyright Linas Vepstas 2005, 2006
5  * Copyright 2001-2012 IBM Corporation.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20  *
21  * Please address comments and feedback to Linas Vepstas <linas@austin.ibm.com>
22  */
23
24 #include <linux/delay.h>
25 #include <linux/sched.h>
26 #include <linux/init.h>
27 #include <linux/list.h>
28 #include <linux/pci.h>
29 #include <linux/proc_fs.h>
30 #include <linux/rbtree.h>
31 #include <linux/seq_file.h>
32 #include <linux/spinlock.h>
33 #include <linux/export.h>
34 #include <linux/of.h>
35
36 #include <linux/atomic.h>
37 #include <asm/eeh.h>
38 #include <asm/eeh_event.h>
39 #include <asm/io.h>
40 #include <asm/machdep.h>
41 #include <asm/ppc-pci.h>
42 #include <asm/rtas.h>
43
44
45 /** Overview:
46  *  EEH, or "Extended Error Handling" is a PCI bridge technology for
47  *  dealing with PCI bus errors that can't be dealt with within the
48  *  usual PCI framework, except by check-stopping the CPU.  Systems
49  *  that are designed for high-availability/reliability cannot afford
50  *  to crash due to a "mere" PCI error, thus the need for EEH.
51  *  An EEH-capable bridge operates by converting a detected error
52  *  into a "slot freeze", taking the PCI adapter off-line, making
53  *  the slot behave, from the OS'es point of view, as if the slot
54  *  were "empty": all reads return 0xff's and all writes are silently
55  *  ignored.  EEH slot isolation events can be triggered by parity
56  *  errors on the address or data busses (e.g. during posted writes),
57  *  which in turn might be caused by low voltage on the bus, dust,
58  *  vibration, humidity, radioactivity or plain-old failed hardware.
59  *
60  *  Note, however, that one of the leading causes of EEH slot
61  *  freeze events are buggy device drivers, buggy device microcode,
62  *  or buggy device hardware.  This is because any attempt by the
63  *  device to bus-master data to a memory address that is not
64  *  assigned to the device will trigger a slot freeze.   (The idea
65  *  is to prevent devices-gone-wild from corrupting system memory).
66  *  Buggy hardware/drivers will have a miserable time co-existing
67  *  with EEH.
68  *
69  *  Ideally, a PCI device driver, when suspecting that an isolation
70  *  event has occurred (e.g. by reading 0xff's), will then ask EEH
71  *  whether this is the case, and then take appropriate steps to
72  *  reset the PCI slot, the PCI device, and then resume operations.
73  *  However, until that day,  the checking is done here, with the
74  *  eeh_check_failure() routine embedded in the MMIO macros.  If
75  *  the slot is found to be isolated, an "EEH Event" is synthesized
76  *  and sent out for processing.
77  */
78
79 /* If a device driver keeps reading an MMIO register in an interrupt
80  * handler after a slot isolation event, it might be broken.
81  * This sets the threshold for how many read attempts we allow
82  * before printing an error message.
83  */
84 #define EEH_MAX_FAILS   2100000
85
86 /* Time to wait for a PCI slot to report status, in milliseconds */
87 #define PCI_BUS_RESET_WAIT_MSEC (60*1000)
88
89 /* Platform dependent EEH operations */
90 struct eeh_ops *eeh_ops = NULL;
91
92 int eeh_subsystem_enabled;
93 EXPORT_SYMBOL(eeh_subsystem_enabled);
94
95 /* Global EEH mutex */
96 DEFINE_MUTEX(eeh_mutex);
97
98 /* Lock to avoid races due to multiple reports of an error */
99 static DEFINE_RAW_SPINLOCK(confirm_error_lock);
100
101 /* Buffer for reporting pci register dumps. Its here in BSS, and
102  * not dynamically alloced, so that it ends up in RMO where RTAS
103  * can access it.
104  */
105 #define EEH_PCI_REGS_LOG_LEN 4096
106 static unsigned char pci_regs_buf[EEH_PCI_REGS_LOG_LEN];
107
108 /*
109  * The struct is used to maintain the EEH global statistic
110  * information. Besides, the EEH global statistics will be
111  * exported to user space through procfs
112  */
113 struct eeh_stats {
114         u64 no_device;          /* PCI device not found         */
115         u64 no_dn;              /* OF node not found            */
116         u64 no_cfg_addr;        /* Config address not found     */
117         u64 ignored_check;      /* EEH check skipped            */
118         u64 total_mmio_ffs;     /* Total EEH checks             */
119         u64 false_positives;    /* Unnecessary EEH checks       */
120         u64 slot_resets;        /* PE reset                     */
121 };
122
123 static struct eeh_stats eeh_stats;
124
125 #define IS_BRIDGE(class_code) (((class_code)<<16) == PCI_BASE_CLASS_BRIDGE)
126
127 /**
128  * eeh_gather_pci_data - Copy assorted PCI config space registers to buff
129  * @edev: device to report data for
130  * @buf: point to buffer in which to log
131  * @len: amount of room in buffer
132  *
133  * This routine captures assorted PCI configuration space data,
134  * and puts them into a buffer for RTAS error logging.
135  */
136 static size_t eeh_gather_pci_data(struct eeh_dev *edev, char * buf, size_t len)
137 {
138         struct device_node *dn = eeh_dev_to_of_node(edev);
139         struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
140         u32 cfg;
141         int cap, i;
142         int n = 0;
143
144         n += scnprintf(buf+n, len-n, "%s\n", dn->full_name);
145         printk(KERN_WARNING "EEH: of node=%s\n", dn->full_name);
146
147         eeh_ops->read_config(dn, PCI_VENDOR_ID, 4, &cfg);
148         n += scnprintf(buf+n, len-n, "dev/vend:%08x\n", cfg);
149         printk(KERN_WARNING "EEH: PCI device/vendor: %08x\n", cfg);
150
151         eeh_ops->read_config(dn, PCI_COMMAND, 4, &cfg);
152         n += scnprintf(buf+n, len-n, "cmd/stat:%x\n", cfg);
153         printk(KERN_WARNING "EEH: PCI cmd/status register: %08x\n", cfg);
154
155         if (!dev) {
156                 printk(KERN_WARNING "EEH: no PCI device for this of node\n");
157                 return n;
158         }
159
160         /* Gather bridge-specific registers */
161         if (dev->class >> 16 == PCI_BASE_CLASS_BRIDGE) {
162                 eeh_ops->read_config(dn, PCI_SEC_STATUS, 2, &cfg);
163                 n += scnprintf(buf+n, len-n, "sec stat:%x\n", cfg);
164                 printk(KERN_WARNING "EEH: Bridge secondary status: %04x\n", cfg);
165
166                 eeh_ops->read_config(dn, PCI_BRIDGE_CONTROL, 2, &cfg);
167                 n += scnprintf(buf+n, len-n, "brdg ctl:%x\n", cfg);
168                 printk(KERN_WARNING "EEH: Bridge control: %04x\n", cfg);
169         }
170
171         /* Dump out the PCI-X command and status regs */
172         cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
173         if (cap) {
174                 eeh_ops->read_config(dn, cap, 4, &cfg);
175                 n += scnprintf(buf+n, len-n, "pcix-cmd:%x\n", cfg);
176                 printk(KERN_WARNING "EEH: PCI-X cmd: %08x\n", cfg);
177
178                 eeh_ops->read_config(dn, cap+4, 4, &cfg);
179                 n += scnprintf(buf+n, len-n, "pcix-stat:%x\n", cfg);
180                 printk(KERN_WARNING "EEH: PCI-X status: %08x\n", cfg);
181         }
182
183         /* If PCI-E capable, dump PCI-E cap 10, and the AER */
184         cap = pci_find_capability(dev, PCI_CAP_ID_EXP);
185         if (cap) {
186                 n += scnprintf(buf+n, len-n, "pci-e cap10:\n");
187                 printk(KERN_WARNING
188                        "EEH: PCI-E capabilities and status follow:\n");
189
190                 for (i=0; i<=8; i++) {
191                         eeh_ops->read_config(dn, cap+4*i, 4, &cfg);
192                         n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
193                         printk(KERN_WARNING "EEH: PCI-E %02x: %08x\n", i, cfg);
194                 }
195
196                 cap = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
197                 if (cap) {
198                         n += scnprintf(buf+n, len-n, "pci-e AER:\n");
199                         printk(KERN_WARNING
200                                "EEH: PCI-E AER capability register set follows:\n");
201
202                         for (i=0; i<14; i++) {
203                                 eeh_ops->read_config(dn, cap+4*i, 4, &cfg);
204                                 n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
205                                 printk(KERN_WARNING "EEH: PCI-E AER %02x: %08x\n", i, cfg);
206                         }
207                 }
208         }
209
210         /* Gather status on devices under the bridge */
211         if (dev->class >> 16 == PCI_BASE_CLASS_BRIDGE) {
212                 struct device_node *child;
213
214                 for_each_child_of_node(dn, child) {
215                         if (of_node_to_eeh_dev(child))
216                                 n += eeh_gather_pci_data(of_node_to_eeh_dev(child), buf+n, len-n);
217                 }
218         }
219
220         return n;
221 }
222
223 /**
224  * eeh_slot_error_detail - Generate combined log including driver log and error log
225  * @edev: device to report error log for
226  * @severity: temporary or permanent error log
227  *
228  * This routine should be called to generate the combined log, which
229  * is comprised of driver log and error log. The driver log is figured
230  * out from the config space of the corresponding PCI device, while
231  * the error log is fetched through platform dependent function call.
232  */
233 void eeh_slot_error_detail(struct eeh_dev *edev, int severity)
234 {
235         size_t loglen = 0;
236         pci_regs_buf[0] = 0;
237
238         eeh_pci_enable(edev, EEH_OPT_THAW_MMIO);
239         eeh_ops->configure_bridge(eeh_dev_to_of_node(edev));
240         eeh_restore_bars(edev);
241         loglen = eeh_gather_pci_data(edev, pci_regs_buf, EEH_PCI_REGS_LOG_LEN);
242
243         eeh_ops->get_log(eeh_dev_to_of_node(edev), severity, pci_regs_buf, loglen);
244 }
245
246 /**
247  * eeh_token_to_phys - Convert EEH address token to phys address
248  * @token: I/O token, should be address in the form 0xA....
249  *
250  * This routine should be called to convert virtual I/O address
251  * to physical one.
252  */
253 static inline unsigned long eeh_token_to_phys(unsigned long token)
254 {
255         pte_t *ptep;
256         unsigned long pa;
257
258         ptep = find_linux_pte(init_mm.pgd, token);
259         if (!ptep)
260                 return token;
261         pa = pte_pfn(*ptep) << PAGE_SHIFT;
262
263         return pa | (token & (PAGE_SIZE-1));
264 }
265
266 /**
267  * eeh_find_device_pe - Retrieve the PE for the given device
268  * @dn: device node
269  *
270  * Return the PE under which this device lies
271  */
272 struct device_node *eeh_find_device_pe(struct device_node *dn)
273 {
274         while (dn->parent && of_node_to_eeh_dev(dn->parent) &&
275                (of_node_to_eeh_dev(dn->parent)->mode & EEH_MODE_SUPPORTED)) {
276                 dn = dn->parent;
277         }
278         return dn;
279 }
280
281 /**
282  * eeh_dn_check_failure - Check if all 1's data is due to EEH slot freeze
283  * @dn: device node
284  * @dev: pci device, if known
285  *
286  * Check for an EEH failure for the given device node.  Call this
287  * routine if the result of a read was all 0xff's and you want to
288  * find out if this is due to an EEH slot freeze.  This routine
289  * will query firmware for the EEH status.
290  *
291  * Returns 0 if there has not been an EEH error; otherwise returns
292  * a non-zero value and queues up a slot isolation event notification.
293  *
294  * It is safe to call this routine in an interrupt context.
295  */
296 int eeh_dn_check_failure(struct device_node *dn, struct pci_dev *dev)
297 {
298         int ret;
299         unsigned long flags;
300         struct eeh_dev *edev;
301         int rc = 0;
302         const char *location;
303
304         eeh_stats.total_mmio_ffs++;
305
306         if (!eeh_subsystem_enabled)
307                 return 0;
308
309         if (!dn) {
310                 eeh_stats.no_dn++;
311                 return 0;
312         }
313         dn = eeh_find_device_pe(dn);
314         edev = of_node_to_eeh_dev(dn);
315
316         /* Access to IO BARs might get this far and still not want checking. */
317         if (!(edev->mode & EEH_MODE_SUPPORTED) ||
318             edev->mode & EEH_MODE_NOCHECK) {
319                 eeh_stats.ignored_check++;
320                 pr_debug("EEH: Ignored check (%x) for %s %s\n",
321                         edev->mode, eeh_pci_name(dev), dn->full_name);
322                 return 0;
323         }
324
325         if (!edev->config_addr && !edev->pe_config_addr) {
326                 eeh_stats.no_cfg_addr++;
327                 return 0;
328         }
329
330         /* If we already have a pending isolation event for this
331          * slot, we know it's bad already, we don't need to check.
332          * Do this checking under a lock; as multiple PCI devices
333          * in one slot might report errors simultaneously, and we
334          * only want one error recovery routine running.
335          */
336         raw_spin_lock_irqsave(&confirm_error_lock, flags);
337         rc = 1;
338         if (edev->mode & EEH_MODE_ISOLATED) {
339                 edev->check_count++;
340                 if (edev->check_count % EEH_MAX_FAILS == 0) {
341                         location = of_get_property(dn, "ibm,loc-code", NULL);
342                         printk(KERN_ERR "EEH: %d reads ignored for recovering device at "
343                                 "location=%s driver=%s pci addr=%s\n",
344                                 edev->check_count, location,
345                                 eeh_driver_name(dev), eeh_pci_name(dev));
346                         printk(KERN_ERR "EEH: Might be infinite loop in %s driver\n",
347                                 eeh_driver_name(dev));
348                         dump_stack();
349                 }
350                 goto dn_unlock;
351         }
352
353         /*
354          * Now test for an EEH failure.  This is VERY expensive.
355          * Note that the eeh_config_addr may be a parent device
356          * in the case of a device behind a bridge, or it may be
357          * function zero of a multi-function device.
358          * In any case they must share a common PHB.
359          */
360         ret = eeh_ops->get_state(dn, NULL);
361
362         /* Note that config-io to empty slots may fail;
363          * they are empty when they don't have children.
364          * We will punt with the following conditions: Failure to get
365          * PE's state, EEH not support and Permanently unavailable
366          * state, PE is in good state.
367          */
368         if ((ret < 0) ||
369             (ret == EEH_STATE_NOT_SUPPORT) ||
370             (ret & (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) ==
371             (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) {
372                 eeh_stats.false_positives++;
373                 edev->false_positives ++;
374                 rc = 0;
375                 goto dn_unlock;
376         }
377
378         eeh_stats.slot_resets++;
379  
380         /* Avoid repeated reports of this failure, including problems
381          * with other functions on this device, and functions under
382          * bridges.
383          */
384         eeh_mark_slot(dn, EEH_MODE_ISOLATED);
385         raw_spin_unlock_irqrestore(&confirm_error_lock, flags);
386
387         eeh_send_failure_event(edev);
388
389         /* Most EEH events are due to device driver bugs.  Having
390          * a stack trace will help the device-driver authors figure
391          * out what happened.  So print that out.
392          */
393         WARN(1, "EEH: failure detected\n");
394         return 1;
395
396 dn_unlock:
397         raw_spin_unlock_irqrestore(&confirm_error_lock, flags);
398         return rc;
399 }
400
401 EXPORT_SYMBOL_GPL(eeh_dn_check_failure);
402
403 /**
404  * eeh_check_failure - Check if all 1's data is due to EEH slot freeze
405  * @token: I/O token, should be address in the form 0xA....
406  * @val: value, should be all 1's (XXX why do we need this arg??)
407  *
408  * Check for an EEH failure at the given token address.  Call this
409  * routine if the result of a read was all 0xff's and you want to
410  * find out if this is due to an EEH slot freeze event.  This routine
411  * will query firmware for the EEH status.
412  *
413  * Note this routine is safe to call in an interrupt context.
414  */
415 unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
416 {
417         unsigned long addr;
418         struct pci_dev *dev;
419         struct device_node *dn;
420
421         /* Finding the phys addr + pci device; this is pretty quick. */
422         addr = eeh_token_to_phys((unsigned long __force) token);
423         dev = pci_addr_cache_get_device(addr);
424         if (!dev) {
425                 eeh_stats.no_device++;
426                 return val;
427         }
428
429         dn = pci_device_to_OF_node(dev);
430         eeh_dn_check_failure(dn, dev);
431
432         pci_dev_put(dev);
433         return val;
434 }
435
436 EXPORT_SYMBOL(eeh_check_failure);
437
438
439 /**
440  * eeh_pci_enable - Enable MMIO or DMA transfers for this slot
441  * @edev: pci device node
442  *
443  * This routine should be called to reenable frozen MMIO or DMA
444  * so that it would work correctly again. It's useful while doing
445  * recovery or log collection on the indicated device.
446  */
447 int eeh_pci_enable(struct eeh_dev *edev, int function)
448 {
449         int rc;
450         struct device_node *dn = eeh_dev_to_of_node(edev);
451
452         rc = eeh_ops->set_option(dn, function);
453         if (rc)
454                 printk(KERN_WARNING "EEH: Unexpected state change %d, err=%d dn=%s\n",
455                         function, rc, dn->full_name);
456
457         rc = eeh_ops->wait_state(dn, PCI_BUS_RESET_WAIT_MSEC);
458         if (rc > 0 && (rc & EEH_STATE_MMIO_ENABLED) &&
459            (function == EEH_OPT_THAW_MMIO))
460                 return 0;
461
462         return rc;
463 }
464
465 /**
466  * pcibios_set_pcie_slot_reset - Set PCI-E reset state
467  * @dev: pci device struct
468  * @state: reset state to enter
469  *
470  * Return value:
471  *      0 if success
472  */
473 int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
474 {
475         struct device_node *dn = pci_device_to_OF_node(dev);
476
477         switch (state) {
478         case pcie_deassert_reset:
479                 eeh_ops->reset(dn, EEH_RESET_DEACTIVATE);
480                 break;
481         case pcie_hot_reset:
482                 eeh_ops->reset(dn, EEH_RESET_HOT);
483                 break;
484         case pcie_warm_reset:
485                 eeh_ops->reset(dn, EEH_RESET_FUNDAMENTAL);
486                 break;
487         default:
488                 return -EINVAL;
489         };
490
491         return 0;
492 }
493
494 /**
495  * __eeh_set_pe_freset - Check the required reset for child devices
496  * @parent: parent device
497  * @freset: return value
498  *
499  * Each device might have its preferred reset type: fundamental or
500  * hot reset. The routine is used to collect the information from
501  * the child devices so that they could be reset accordingly.
502  */
503 void __eeh_set_pe_freset(struct device_node *parent, unsigned int *freset)
504 {
505         struct device_node *dn;
506
507         for_each_child_of_node(parent, dn) {
508                 if (of_node_to_eeh_dev(dn)) {
509                         struct pci_dev *dev = of_node_to_eeh_dev(dn)->pdev;
510
511                         if (dev && dev->driver)
512                                 *freset |= dev->needs_freset;
513
514                         __eeh_set_pe_freset(dn, freset);
515                 }
516         }
517 }
518
519 /**
520  * eeh_set_pe_freset - Check the required reset for the indicated device and its children
521  * @dn: parent device
522  * @freset: return value
523  *
524  * Each device might have its preferred reset type: fundamental or
525  * hot reset. The routine is used to collected the information for
526  * the indicated device and its children so that the bunch of the
527  * devices could be reset properly.
528  */
529 void eeh_set_pe_freset(struct device_node *dn, unsigned int *freset)
530 {
531         struct pci_dev *dev;
532         dn = eeh_find_device_pe(dn);
533
534         /* Back up one, since config addrs might be shared */
535         if (!pcibios_find_pci_bus(dn) && of_node_to_eeh_dev(dn->parent))
536                 dn = dn->parent;
537
538         dev = of_node_to_eeh_dev(dn)->pdev;
539         if (dev)
540                 *freset |= dev->needs_freset;
541
542         __eeh_set_pe_freset(dn, freset);
543 }
544
545 /**
546  * eeh_reset_pe_once - Assert the pci #RST line for 1/4 second
547  * @edev: pci device node to be reset.
548  *
549  * Assert the PCI #RST line for 1/4 second.
550  */
551 static void eeh_reset_pe_once(struct eeh_dev *edev)
552 {
553         unsigned int freset = 0;
554         struct device_node *dn = eeh_dev_to_of_node(edev);
555
556         /* Determine type of EEH reset required for
557          * Partitionable Endpoint, a hot-reset (1)
558          * or a fundamental reset (3).
559          * A fundamental reset required by any device under
560          * Partitionable Endpoint trumps hot-reset.
561          */
562         eeh_set_pe_freset(dn, &freset);
563
564         if (freset)
565                 eeh_ops->reset(dn, EEH_RESET_FUNDAMENTAL);
566         else
567                 eeh_ops->reset(dn, EEH_RESET_HOT);
568
569         /* The PCI bus requires that the reset be held high for at least
570          * a 100 milliseconds. We wait a bit longer 'just in case'.
571          */
572 #define PCI_BUS_RST_HOLD_TIME_MSEC 250
573         msleep(PCI_BUS_RST_HOLD_TIME_MSEC);
574         
575         /* We might get hit with another EEH freeze as soon as the 
576          * pci slot reset line is dropped. Make sure we don't miss
577          * these, and clear the flag now.
578          */
579         eeh_clear_slot(dn, EEH_MODE_ISOLATED);
580
581         eeh_ops->reset(dn, EEH_RESET_DEACTIVATE);
582
583         /* After a PCI slot has been reset, the PCI Express spec requires
584          * a 1.5 second idle time for the bus to stabilize, before starting
585          * up traffic.
586          */
587 #define PCI_BUS_SETTLE_TIME_MSEC 1800
588         msleep(PCI_BUS_SETTLE_TIME_MSEC);
589 }
590
591 /**
592  * eeh_reset_pe - Reset the indicated PE
593  * @edev: PCI device associated EEH device
594  *
595  * This routine should be called to reset indicated device, including
596  * PE. A PE might include multiple PCI devices and sometimes PCI bridges
597  * might be involved as well.
598  */
599 int eeh_reset_pe(struct eeh_dev *edev)
600 {
601         int i, rc;
602         struct device_node *dn = eeh_dev_to_of_node(edev);
603
604         /* Take three shots at resetting the bus */
605         for (i=0; i<3; i++) {
606                 eeh_reset_pe_once(edev);
607
608                 rc = eeh_ops->wait_state(dn, PCI_BUS_RESET_WAIT_MSEC);
609                 if (rc == (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE))
610                         return 0;
611
612                 if (rc < 0) {
613                         printk(KERN_ERR "EEH: unrecoverable slot failure %s\n",
614                                dn->full_name);
615                         return -1;
616                 }
617                 printk(KERN_ERR "EEH: bus reset %d failed on slot %s, rc=%d\n",
618                        i+1, dn->full_name, rc);
619         }
620
621         return -1;
622 }
623
624 /** Save and restore of PCI BARs
625  *
626  * Although firmware will set up BARs during boot, it doesn't
627  * set up device BAR's after a device reset, although it will,
628  * if requested, set up bridge configuration. Thus, we need to
629  * configure the PCI devices ourselves.  
630  */
631
632 /**
633  * eeh_restore_one_device_bars - Restore the Base Address Registers for one device
634  * @edev: PCI device associated EEH device
635  *
636  * Loads the PCI configuration space base address registers,
637  * the expansion ROM base address, the latency timer, and etc.
638  * from the saved values in the device node.
639  */
640 static inline void eeh_restore_one_device_bars(struct eeh_dev *edev)
641 {
642         int i;
643         u32 cmd;
644         struct device_node *dn = eeh_dev_to_of_node(edev);
645
646         if (!edev->phb)
647                 return;
648
649         for (i=4; i<10; i++) {
650                 eeh_ops->write_config(dn, i*4, 4, edev->config_space[i]);
651         }
652
653         /* 12 == Expansion ROM Address */
654         eeh_ops->write_config(dn, 12*4, 4, edev->config_space[12]);
655
656 #define BYTE_SWAP(OFF) (8*((OFF)/4)+3-(OFF))
657 #define SAVED_BYTE(OFF) (((u8 *)(edev->config_space))[BYTE_SWAP(OFF)])
658
659         eeh_ops->write_config(dn, PCI_CACHE_LINE_SIZE, 1,
660                     SAVED_BYTE(PCI_CACHE_LINE_SIZE));
661
662         eeh_ops->write_config(dn, PCI_LATENCY_TIMER, 1,
663                     SAVED_BYTE(PCI_LATENCY_TIMER));
664
665         /* max latency, min grant, interrupt pin and line */
666         eeh_ops->write_config(dn, 15*4, 4, edev->config_space[15]);
667
668         /* Restore PERR & SERR bits, some devices require it,
669          * don't touch the other command bits
670          */
671         eeh_ops->read_config(dn, PCI_COMMAND, 4, &cmd);
672         if (edev->config_space[1] & PCI_COMMAND_PARITY)
673                 cmd |= PCI_COMMAND_PARITY;
674         else
675                 cmd &= ~PCI_COMMAND_PARITY;
676         if (edev->config_space[1] & PCI_COMMAND_SERR)
677                 cmd |= PCI_COMMAND_SERR;
678         else
679                 cmd &= ~PCI_COMMAND_SERR;
680         eeh_ops->write_config(dn, PCI_COMMAND, 4, cmd);
681 }
682
683 /**
684  * eeh_restore_bars - Restore the PCI config space info
685  * @edev: EEH device
686  *
687  * This routine performs a recursive walk to the children
688  * of this device as well.
689  */
690 void eeh_restore_bars(struct eeh_dev *edev)
691 {
692         struct device_node *dn;
693         if (!edev)
694                 return;
695         
696         if ((edev->mode & EEH_MODE_SUPPORTED) && !IS_BRIDGE(edev->class_code))
697                 eeh_restore_one_device_bars(edev);
698
699         for_each_child_of_node(eeh_dev_to_of_node(edev), dn)
700                 eeh_restore_bars(of_node_to_eeh_dev(dn));
701 }
702
703 /**
704  * eeh_save_bars - Save device bars
705  * @edev: PCI device associated EEH device
706  *
707  * Save the values of the device bars. Unlike the restore
708  * routine, this routine is *not* recursive. This is because
709  * PCI devices are added individually; but, for the restore,
710  * an entire slot is reset at a time.
711  */
712 static void eeh_save_bars(struct eeh_dev *edev)
713 {
714         int i;
715         struct device_node *dn;
716
717         if (!edev)
718                 return;
719         dn = eeh_dev_to_of_node(edev);
720         
721         for (i = 0; i < 16; i++)
722                 eeh_ops->read_config(dn, i * 4, 4, &edev->config_space[i]);
723 }
724
725 /**
726  * eeh_early_enable - Early enable EEH on the indicated device
727  * @dn: device node
728  * @data: BUID
729  *
730  * Enable EEH functionality on the specified PCI device. The function
731  * is expected to be called before real PCI probing is done. However,
732  * the PHBs have been initialized at this point.
733  */
734 static void *eeh_early_enable(struct device_node *dn, void *data)
735 {
736         int ret;
737         const u32 *class_code = of_get_property(dn, "class-code", NULL);
738         const u32 *vendor_id = of_get_property(dn, "vendor-id", NULL);
739         const u32 *device_id = of_get_property(dn, "device-id", NULL);
740         const u32 *regs;
741         int enable;
742         struct eeh_dev *edev = of_node_to_eeh_dev(dn);
743
744         edev->class_code = 0;
745         edev->mode = 0;
746         edev->check_count = 0;
747         edev->freeze_count = 0;
748         edev->false_positives = 0;
749
750         if (!of_device_is_available(dn))
751                 return NULL;
752
753         /* Ignore bad nodes. */
754         if (!class_code || !vendor_id || !device_id)
755                 return NULL;
756
757         /* There is nothing to check on PCI to ISA bridges */
758         if (dn->type && !strcmp(dn->type, "isa")) {
759                 edev->mode |= EEH_MODE_NOCHECK;
760                 return NULL;
761         }
762         edev->class_code = *class_code;
763
764         /* Ok... see if this device supports EEH.  Some do, some don't,
765          * and the only way to find out is to check each and every one.
766          */
767         regs = of_get_property(dn, "reg", NULL);
768         if (regs) {
769                 /* First register entry is addr (00BBSS00)  */
770                 /* Try to enable eeh */
771                 ret = eeh_ops->set_option(dn, EEH_OPT_ENABLE);
772
773                 enable = 0;
774                 if (ret == 0) {
775                         edev->config_addr = regs[0];
776
777                         /* If the newer, better, ibm,get-config-addr-info is supported, 
778                          * then use that instead.
779                          */
780                         edev->pe_config_addr = eeh_ops->get_pe_addr(dn);
781
782                         /* Some older systems (Power4) allow the
783                          * ibm,set-eeh-option call to succeed even on nodes
784                          * where EEH is not supported. Verify support
785                          * explicitly.
786                          */
787                         ret = eeh_ops->get_state(dn, NULL);
788                         if (ret > 0 && ret != EEH_STATE_NOT_SUPPORT)
789                                 enable = 1;
790                 }
791
792                 if (enable) {
793                         eeh_subsystem_enabled = 1;
794                         edev->mode |= EEH_MODE_SUPPORTED;
795
796                         eeh_add_to_parent_pe(edev);
797
798                         pr_debug("EEH: %s: eeh enabled, config=%x pe_config=%x\n",
799                                  dn->full_name, edev->config_addr,
800                                  edev->pe_config_addr);
801                 } else {
802
803                         /* This device doesn't support EEH, but it may have an
804                          * EEH parent, in which case we mark it as supported.
805                          */
806                         if (dn->parent && of_node_to_eeh_dev(dn->parent) &&
807                             (of_node_to_eeh_dev(dn->parent)->mode & EEH_MODE_SUPPORTED)) {
808                                 /* Parent supports EEH. */
809                                 edev->mode |= EEH_MODE_SUPPORTED;
810                                 edev->config_addr = of_node_to_eeh_dev(dn->parent)->config_addr;
811                                 edev->pe_config_addr = of_node_to_eeh_dev(dn->parent)->pe_config_addr;
812
813                                 eeh_add_to_parent_pe(edev);
814
815                                 return NULL;
816                         }
817                 }
818         } else {
819                 printk(KERN_WARNING "EEH: %s: unable to get reg property.\n",
820                        dn->full_name);
821         }
822
823         eeh_save_bars(edev);
824         return NULL;
825 }
826
827 /**
828  * eeh_ops_register - Register platform dependent EEH operations
829  * @ops: platform dependent EEH operations
830  *
831  * Register the platform dependent EEH operation callback
832  * functions. The platform should call this function before
833  * any other EEH operations.
834  */
835 int __init eeh_ops_register(struct eeh_ops *ops)
836 {
837         if (!ops->name) {
838                 pr_warning("%s: Invalid EEH ops name for %p\n",
839                         __func__, ops);
840                 return -EINVAL;
841         }
842
843         if (eeh_ops && eeh_ops != ops) {
844                 pr_warning("%s: EEH ops of platform %s already existing (%s)\n",
845                         __func__, eeh_ops->name, ops->name);
846                 return -EEXIST;
847         }
848
849         eeh_ops = ops;
850
851         return 0;
852 }
853
854 /**
855  * eeh_ops_unregister - Unreigster platform dependent EEH operations
856  * @name: name of EEH platform operations
857  *
858  * Unregister the platform dependent EEH operation callback
859  * functions.
860  */
861 int __exit eeh_ops_unregister(const char *name)
862 {
863         if (!name || !strlen(name)) {
864                 pr_warning("%s: Invalid EEH ops name\n",
865                         __func__);
866                 return -EINVAL;
867         }
868
869         if (eeh_ops && !strcmp(eeh_ops->name, name)) {
870                 eeh_ops = NULL;
871                 return 0;
872         }
873
874         return -EEXIST;
875 }
876
877 /**
878  * eeh_init - EEH initialization
879  *
880  * Initialize EEH by trying to enable it for all of the adapters in the system.
881  * As a side effect we can determine here if eeh is supported at all.
882  * Note that we leave EEH on so failed config cycles won't cause a machine
883  * check.  If a user turns off EEH for a particular adapter they are really
884  * telling Linux to ignore errors.  Some hardware (e.g. POWER5) won't
885  * grant access to a slot if EEH isn't enabled, and so we always enable
886  * EEH for all slots/all devices.
887  *
888  * The eeh-force-off option disables EEH checking globally, for all slots.
889  * Even if force-off is set, the EEH hardware is still enabled, so that
890  * newer systems can boot.
891  */
892 static int __init eeh_init(void)
893 {
894         struct pci_controller *hose, *tmp;
895         struct device_node *phb;
896         int ret;
897
898         /* call platform initialization function */
899         if (!eeh_ops) {
900                 pr_warning("%s: Platform EEH operation not found\n",
901                         __func__);
902                 return -EEXIST;
903         } else if ((ret = eeh_ops->init())) {
904                 pr_warning("%s: Failed to call platform init function (%d)\n",
905                         __func__, ret);
906                 return ret;
907         }
908
909         raw_spin_lock_init(&confirm_error_lock);
910
911         /* Enable EEH for all adapters */
912         list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
913                 phb = hose->dn;
914                 traverse_pci_devices(phb, eeh_early_enable, NULL);
915         }
916
917         if (eeh_subsystem_enabled)
918                 printk(KERN_INFO "EEH: PCI Enhanced I/O Error Handling Enabled\n");
919         else
920                 printk(KERN_WARNING "EEH: No capable adapters found\n");
921
922         return ret;
923 }
924
925 core_initcall_sync(eeh_init);
926
927 /**
928  * eeh_add_device_early - Enable EEH for the indicated device_node
929  * @dn: device node for which to set up EEH
930  *
931  * This routine must be used to perform EEH initialization for PCI
932  * devices that were added after system boot (e.g. hotplug, dlpar).
933  * This routine must be called before any i/o is performed to the
934  * adapter (inluding any config-space i/o).
935  * Whether this actually enables EEH or not for this device depends
936  * on the CEC architecture, type of the device, on earlier boot
937  * command-line arguments & etc.
938  */
939 static void eeh_add_device_early(struct device_node *dn)
940 {
941         struct pci_controller *phb;
942
943         if (!dn || !of_node_to_eeh_dev(dn))
944                 return;
945         phb = of_node_to_eeh_dev(dn)->phb;
946
947         /* USB Bus children of PCI devices will not have BUID's */
948         if (NULL == phb || 0 == phb->buid)
949                 return;
950
951         eeh_early_enable(dn, NULL);
952 }
953
954 /**
955  * eeh_add_device_tree_early - Enable EEH for the indicated device
956  * @dn: device node
957  *
958  * This routine must be used to perform EEH initialization for the
959  * indicated PCI device that was added after system boot (e.g.
960  * hotplug, dlpar).
961  */
962 void eeh_add_device_tree_early(struct device_node *dn)
963 {
964         struct device_node *sib;
965
966         for_each_child_of_node(dn, sib)
967                 eeh_add_device_tree_early(sib);
968         eeh_add_device_early(dn);
969 }
970 EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
971
972 /**
973  * eeh_add_device_late - Perform EEH initialization for the indicated pci device
974  * @dev: pci device for which to set up EEH
975  *
976  * This routine must be used to complete EEH initialization for PCI
977  * devices that were added after system boot (e.g. hotplug, dlpar).
978  */
979 static void eeh_add_device_late(struct pci_dev *dev)
980 {
981         struct device_node *dn;
982         struct eeh_dev *edev;
983
984         if (!dev || !eeh_subsystem_enabled)
985                 return;
986
987         pr_debug("EEH: Adding device %s\n", pci_name(dev));
988
989         dn = pci_device_to_OF_node(dev);
990         edev = of_node_to_eeh_dev(dn);
991         if (edev->pdev == dev) {
992                 pr_debug("EEH: Already referenced !\n");
993                 return;
994         }
995         WARN_ON(edev->pdev);
996
997         pci_dev_get(dev);
998         edev->pdev = dev;
999         dev->dev.archdata.edev = edev;
1000
1001         pci_addr_cache_insert_device(dev);
1002         eeh_sysfs_add_device(dev);
1003 }
1004
1005 /**
1006  * eeh_add_device_tree_late - Perform EEH initialization for the indicated PCI bus
1007  * @bus: PCI bus
1008  *
1009  * This routine must be used to perform EEH initialization for PCI
1010  * devices which are attached to the indicated PCI bus. The PCI bus
1011  * is added after system boot through hotplug or dlpar.
1012  */
1013 void eeh_add_device_tree_late(struct pci_bus *bus)
1014 {
1015         struct pci_dev *dev;
1016
1017         list_for_each_entry(dev, &bus->devices, bus_list) {
1018                 eeh_add_device_late(dev);
1019                 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
1020                         struct pci_bus *subbus = dev->subordinate;
1021                         if (subbus)
1022                                 eeh_add_device_tree_late(subbus);
1023                 }
1024         }
1025 }
1026 EXPORT_SYMBOL_GPL(eeh_add_device_tree_late);
1027
1028 /**
1029  * eeh_remove_device - Undo EEH setup for the indicated pci device
1030  * @dev: pci device to be removed
1031  *
1032  * This routine should be called when a device is removed from
1033  * a running system (e.g. by hotplug or dlpar).  It unregisters
1034  * the PCI device from the EEH subsystem.  I/O errors affecting
1035  * this device will no longer be detected after this call; thus,
1036  * i/o errors affecting this slot may leave this device unusable.
1037  */
1038 static void eeh_remove_device(struct pci_dev *dev)
1039 {
1040         struct eeh_dev *edev;
1041
1042         if (!dev || !eeh_subsystem_enabled)
1043                 return;
1044         edev = pci_dev_to_eeh_dev(dev);
1045
1046         /* Unregister the device with the EEH/PCI address search system */
1047         pr_debug("EEH: Removing device %s\n", pci_name(dev));
1048
1049         if (!edev || !edev->pdev) {
1050                 pr_debug("EEH: Not referenced !\n");
1051                 return;
1052         }
1053         edev->pdev = NULL;
1054         dev->dev.archdata.edev = NULL;
1055         pci_dev_put(dev);
1056
1057         eeh_rmv_from_parent_pe(edev);
1058         pci_addr_cache_remove_device(dev);
1059         eeh_sysfs_remove_device(dev);
1060 }
1061
1062 /**
1063  * eeh_remove_bus_device - Undo EEH setup for the indicated PCI device
1064  * @dev: PCI device
1065  *
1066  * This routine must be called when a device is removed from the
1067  * running system through hotplug or dlpar. The corresponding
1068  * PCI address cache will be removed.
1069  */
1070 void eeh_remove_bus_device(struct pci_dev *dev)
1071 {
1072         struct pci_bus *bus = dev->subordinate;
1073         struct pci_dev *child, *tmp;
1074
1075         eeh_remove_device(dev);
1076
1077         if (bus && dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
1078                 list_for_each_entry_safe(child, tmp, &bus->devices, bus_list)
1079                          eeh_remove_bus_device(child);
1080         }
1081 }
1082 EXPORT_SYMBOL_GPL(eeh_remove_bus_device);
1083
1084 static int proc_eeh_show(struct seq_file *m, void *v)
1085 {
1086         if (0 == eeh_subsystem_enabled) {
1087                 seq_printf(m, "EEH Subsystem is globally disabled\n");
1088                 seq_printf(m, "eeh_total_mmio_ffs=%llu\n", eeh_stats.total_mmio_ffs);
1089         } else {
1090                 seq_printf(m, "EEH Subsystem is enabled\n");
1091                 seq_printf(m,
1092                                 "no device=%llu\n"
1093                                 "no device node=%llu\n"
1094                                 "no config address=%llu\n"
1095                                 "check not wanted=%llu\n"
1096                                 "eeh_total_mmio_ffs=%llu\n"
1097                                 "eeh_false_positives=%llu\n"
1098                                 "eeh_slot_resets=%llu\n",
1099                                 eeh_stats.no_device,
1100                                 eeh_stats.no_dn,
1101                                 eeh_stats.no_cfg_addr,
1102                                 eeh_stats.ignored_check,
1103                                 eeh_stats.total_mmio_ffs,
1104                                 eeh_stats.false_positives,
1105                                 eeh_stats.slot_resets);
1106         }
1107
1108         return 0;
1109 }
1110
1111 static int proc_eeh_open(struct inode *inode, struct file *file)
1112 {
1113         return single_open(file, proc_eeh_show, NULL);
1114 }
1115
1116 static const struct file_operations proc_eeh_operations = {
1117         .open      = proc_eeh_open,
1118         .read      = seq_read,
1119         .llseek    = seq_lseek,
1120         .release   = single_release,
1121 };
1122
1123 static int __init eeh_init_proc(void)
1124 {
1125         if (machine_is(pseries))
1126                 proc_create("powerpc/eeh", 0, NULL, &proc_eeh_operations);
1127         return 0;
1128 }
1129 __initcall(eeh_init_proc);