powerpc/eeh: Make EEH operations 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_dn_check_failure - Check if all 1's data is due to EEH slot freeze
268  * @dn: device node
269  * @dev: pci device, if known
270  *
271  * Check for an EEH failure for the given device node.  Call this
272  * routine if the result of a read was all 0xff's and you want to
273  * find out if this is due to an EEH slot freeze.  This routine
274  * will query firmware for the EEH status.
275  *
276  * Returns 0 if there has not been an EEH error; otherwise returns
277  * a non-zero value and queues up a slot isolation event notification.
278  *
279  * It is safe to call this routine in an interrupt context.
280  */
281 int eeh_dn_check_failure(struct device_node *dn, struct pci_dev *dev)
282 {
283         int ret;
284         unsigned long flags;
285         struct eeh_pe *pe;
286         struct eeh_dev *edev;
287         int rc = 0;
288         const char *location;
289
290         eeh_stats.total_mmio_ffs++;
291
292         if (!eeh_subsystem_enabled)
293                 return 0;
294
295         if (dn) {
296                 edev = of_node_to_eeh_dev(dn);
297         } else if (dev) {
298                 edev = pci_dev_to_eeh_dev(dev);
299                 dn = pci_device_to_OF_node(dev);
300         } else {
301                 eeh_stats.no_dn++;
302                 return 0;
303         }
304         pe = edev->pe;
305
306         /* Access to IO BARs might get this far and still not want checking. */
307         if (!pe) {
308                 eeh_stats.ignored_check++;
309                 pr_debug("EEH: Ignored check for %s %s\n",
310                         eeh_pci_name(dev), dn->full_name);
311                 return 0;
312         }
313
314         if (!pe->addr && !pe->config_addr) {
315                 eeh_stats.no_cfg_addr++;
316                 return 0;
317         }
318
319         /* If we already have a pending isolation event for this
320          * slot, we know it's bad already, we don't need to check.
321          * Do this checking under a lock; as multiple PCI devices
322          * in one slot might report errors simultaneously, and we
323          * only want one error recovery routine running.
324          */
325         raw_spin_lock_irqsave(&confirm_error_lock, flags);
326         rc = 1;
327         if (pe->state & EEH_PE_ISOLATED) {
328                 pe->check_count++;
329                 if (pe->check_count % EEH_MAX_FAILS == 0) {
330                         location = of_get_property(dn, "ibm,loc-code", NULL);
331                         printk(KERN_ERR "EEH: %d reads ignored for recovering device at "
332                                 "location=%s driver=%s pci addr=%s\n",
333                                 pe->check_count, location,
334                                 eeh_driver_name(dev), eeh_pci_name(dev));
335                         printk(KERN_ERR "EEH: Might be infinite loop in %s driver\n",
336                                 eeh_driver_name(dev));
337                         dump_stack();
338                 }
339                 goto dn_unlock;
340         }
341
342         /*
343          * Now test for an EEH failure.  This is VERY expensive.
344          * Note that the eeh_config_addr may be a parent device
345          * in the case of a device behind a bridge, or it may be
346          * function zero of a multi-function device.
347          * In any case they must share a common PHB.
348          */
349         ret = eeh_ops->get_state(pe, NULL);
350
351         /* Note that config-io to empty slots may fail;
352          * they are empty when they don't have children.
353          * We will punt with the following conditions: Failure to get
354          * PE's state, EEH not support and Permanently unavailable
355          * state, PE is in good state.
356          */
357         if ((ret < 0) ||
358             (ret == EEH_STATE_NOT_SUPPORT) ||
359             (ret & (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) ==
360             (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) {
361                 eeh_stats.false_positives++;
362                 pe->false_positives++;
363                 rc = 0;
364                 goto dn_unlock;
365         }
366
367         eeh_stats.slot_resets++;
368  
369         /* Avoid repeated reports of this failure, including problems
370          * with other functions on this device, and functions under
371          * bridges.
372          */
373         eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
374         raw_spin_unlock_irqrestore(&confirm_error_lock, flags);
375
376         eeh_send_failure_event(pe);
377
378         /* Most EEH events are due to device driver bugs.  Having
379          * a stack trace will help the device-driver authors figure
380          * out what happened.  So print that out.
381          */
382         WARN(1, "EEH: failure detected\n");
383         return 1;
384
385 dn_unlock:
386         raw_spin_unlock_irqrestore(&confirm_error_lock, flags);
387         return rc;
388 }
389
390 EXPORT_SYMBOL_GPL(eeh_dn_check_failure);
391
392 /**
393  * eeh_check_failure - Check if all 1's data is due to EEH slot freeze
394  * @token: I/O token, should be address in the form 0xA....
395  * @val: value, should be all 1's (XXX why do we need this arg??)
396  *
397  * Check for an EEH failure at the given token address.  Call this
398  * routine if the result of a read was all 0xff's and you want to
399  * find out if this is due to an EEH slot freeze event.  This routine
400  * will query firmware for the EEH status.
401  *
402  * Note this routine is safe to call in an interrupt context.
403  */
404 unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
405 {
406         unsigned long addr;
407         struct pci_dev *dev;
408         struct device_node *dn;
409
410         /* Finding the phys addr + pci device; this is pretty quick. */
411         addr = eeh_token_to_phys((unsigned long __force) token);
412         dev = pci_addr_cache_get_device(addr);
413         if (!dev) {
414                 eeh_stats.no_device++;
415                 return val;
416         }
417
418         dn = pci_device_to_OF_node(dev);
419         eeh_dn_check_failure(dn, dev);
420
421         pci_dev_put(dev);
422         return val;
423 }
424
425 EXPORT_SYMBOL(eeh_check_failure);
426
427
428 /**
429  * eeh_pci_enable - Enable MMIO or DMA transfers for this slot
430  * @edev: pci device node
431  *
432  * This routine should be called to reenable frozen MMIO or DMA
433  * so that it would work correctly again. It's useful while doing
434  * recovery or log collection on the indicated device.
435  */
436 int eeh_pci_enable(struct eeh_dev *edev, int function)
437 {
438         int rc;
439         struct device_node *dn = eeh_dev_to_of_node(edev);
440
441         rc = eeh_ops->set_option(dn, function);
442         if (rc)
443                 printk(KERN_WARNING "EEH: Unexpected state change %d, err=%d dn=%s\n",
444                         function, rc, dn->full_name);
445
446         rc = eeh_ops->wait_state(dn, PCI_BUS_RESET_WAIT_MSEC);
447         if (rc > 0 && (rc & EEH_STATE_MMIO_ENABLED) &&
448            (function == EEH_OPT_THAW_MMIO))
449                 return 0;
450
451         return rc;
452 }
453
454 /**
455  * pcibios_set_pcie_slot_reset - Set PCI-E reset state
456  * @dev: pci device struct
457  * @state: reset state to enter
458  *
459  * Return value:
460  *      0 if success
461  */
462 int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
463 {
464         struct device_node *dn = pci_device_to_OF_node(dev);
465
466         switch (state) {
467         case pcie_deassert_reset:
468                 eeh_ops->reset(dn, EEH_RESET_DEACTIVATE);
469                 break;
470         case pcie_hot_reset:
471                 eeh_ops->reset(dn, EEH_RESET_HOT);
472                 break;
473         case pcie_warm_reset:
474                 eeh_ops->reset(dn, EEH_RESET_FUNDAMENTAL);
475                 break;
476         default:
477                 return -EINVAL;
478         };
479
480         return 0;
481 }
482
483 /**
484  * __eeh_set_pe_freset - Check the required reset for child devices
485  * @parent: parent device
486  * @freset: return value
487  *
488  * Each device might have its preferred reset type: fundamental or
489  * hot reset. The routine is used to collect the information from
490  * the child devices so that they could be reset accordingly.
491  */
492 void __eeh_set_pe_freset(struct device_node *parent, unsigned int *freset)
493 {
494         struct device_node *dn;
495
496         for_each_child_of_node(parent, dn) {
497                 if (of_node_to_eeh_dev(dn)) {
498                         struct pci_dev *dev = of_node_to_eeh_dev(dn)->pdev;
499
500                         if (dev && dev->driver)
501                                 *freset |= dev->needs_freset;
502
503                         __eeh_set_pe_freset(dn, freset);
504                 }
505         }
506 }
507
508 /**
509  * eeh_set_pe_freset - Check the required reset for the indicated device and its children
510  * @dn: parent device
511  * @freset: return value
512  *
513  * Each device might have its preferred reset type: fundamental or
514  * hot reset. The routine is used to collected the information for
515  * the indicated device and its children so that the bunch of the
516  * devices could be reset properly.
517  */
518 void eeh_set_pe_freset(struct device_node *dn, unsigned int *freset)
519 {
520         struct pci_dev *dev;
521         dn = eeh_find_device_pe(dn);
522
523         /* Back up one, since config addrs might be shared */
524         if (!pcibios_find_pci_bus(dn) && of_node_to_eeh_dev(dn->parent))
525                 dn = dn->parent;
526
527         dev = of_node_to_eeh_dev(dn)->pdev;
528         if (dev)
529                 *freset |= dev->needs_freset;
530
531         __eeh_set_pe_freset(dn, freset);
532 }
533
534 /**
535  * eeh_reset_pe_once - Assert the pci #RST line for 1/4 second
536  * @edev: pci device node to be reset.
537  *
538  * Assert the PCI #RST line for 1/4 second.
539  */
540 static void eeh_reset_pe_once(struct eeh_dev *edev)
541 {
542         unsigned int freset = 0;
543         struct device_node *dn = eeh_dev_to_of_node(edev);
544
545         /* Determine type of EEH reset required for
546          * Partitionable Endpoint, a hot-reset (1)
547          * or a fundamental reset (3).
548          * A fundamental reset required by any device under
549          * Partitionable Endpoint trumps hot-reset.
550          */
551         eeh_set_pe_freset(dn, &freset);
552
553         if (freset)
554                 eeh_ops->reset(dn, EEH_RESET_FUNDAMENTAL);
555         else
556                 eeh_ops->reset(dn, EEH_RESET_HOT);
557
558         /* The PCI bus requires that the reset be held high for at least
559          * a 100 milliseconds. We wait a bit longer 'just in case'.
560          */
561 #define PCI_BUS_RST_HOLD_TIME_MSEC 250
562         msleep(PCI_BUS_RST_HOLD_TIME_MSEC);
563         
564         /* We might get hit with another EEH freeze as soon as the 
565          * pci slot reset line is dropped. Make sure we don't miss
566          * these, and clear the flag now.
567          */
568         eeh_clear_slot(dn, EEH_MODE_ISOLATED);
569
570         eeh_ops->reset(dn, EEH_RESET_DEACTIVATE);
571
572         /* After a PCI slot has been reset, the PCI Express spec requires
573          * a 1.5 second idle time for the bus to stabilize, before starting
574          * up traffic.
575          */
576 #define PCI_BUS_SETTLE_TIME_MSEC 1800
577         msleep(PCI_BUS_SETTLE_TIME_MSEC);
578 }
579
580 /**
581  * eeh_reset_pe - Reset the indicated PE
582  * @edev: PCI device associated EEH device
583  *
584  * This routine should be called to reset indicated device, including
585  * PE. A PE might include multiple PCI devices and sometimes PCI bridges
586  * might be involved as well.
587  */
588 int eeh_reset_pe(struct eeh_dev *edev)
589 {
590         int i, rc;
591         struct device_node *dn = eeh_dev_to_of_node(edev);
592
593         /* Take three shots at resetting the bus */
594         for (i=0; i<3; i++) {
595                 eeh_reset_pe_once(edev);
596
597                 rc = eeh_ops->wait_state(dn, PCI_BUS_RESET_WAIT_MSEC);
598                 if (rc == (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE))
599                         return 0;
600
601                 if (rc < 0) {
602                         printk(KERN_ERR "EEH: unrecoverable slot failure %s\n",
603                                dn->full_name);
604                         return -1;
605                 }
606                 printk(KERN_ERR "EEH: bus reset %d failed on slot %s, rc=%d\n",
607                        i+1, dn->full_name, rc);
608         }
609
610         return -1;
611 }
612
613 /** Save and restore of PCI BARs
614  *
615  * Although firmware will set up BARs during boot, it doesn't
616  * set up device BAR's after a device reset, although it will,
617  * if requested, set up bridge configuration. Thus, we need to
618  * configure the PCI devices ourselves.  
619  */
620
621 /**
622  * eeh_restore_one_device_bars - Restore the Base Address Registers for one device
623  * @edev: PCI device associated EEH device
624  *
625  * Loads the PCI configuration space base address registers,
626  * the expansion ROM base address, the latency timer, and etc.
627  * from the saved values in the device node.
628  */
629 static inline void eeh_restore_one_device_bars(struct eeh_dev *edev)
630 {
631         int i;
632         u32 cmd;
633         struct device_node *dn = eeh_dev_to_of_node(edev);
634
635         if (!edev->phb)
636                 return;
637
638         for (i=4; i<10; i++) {
639                 eeh_ops->write_config(dn, i*4, 4, edev->config_space[i]);
640         }
641
642         /* 12 == Expansion ROM Address */
643         eeh_ops->write_config(dn, 12*4, 4, edev->config_space[12]);
644
645 #define BYTE_SWAP(OFF) (8*((OFF)/4)+3-(OFF))
646 #define SAVED_BYTE(OFF) (((u8 *)(edev->config_space))[BYTE_SWAP(OFF)])
647
648         eeh_ops->write_config(dn, PCI_CACHE_LINE_SIZE, 1,
649                     SAVED_BYTE(PCI_CACHE_LINE_SIZE));
650
651         eeh_ops->write_config(dn, PCI_LATENCY_TIMER, 1,
652                     SAVED_BYTE(PCI_LATENCY_TIMER));
653
654         /* max latency, min grant, interrupt pin and line */
655         eeh_ops->write_config(dn, 15*4, 4, edev->config_space[15]);
656
657         /* Restore PERR & SERR bits, some devices require it,
658          * don't touch the other command bits
659          */
660         eeh_ops->read_config(dn, PCI_COMMAND, 4, &cmd);
661         if (edev->config_space[1] & PCI_COMMAND_PARITY)
662                 cmd |= PCI_COMMAND_PARITY;
663         else
664                 cmd &= ~PCI_COMMAND_PARITY;
665         if (edev->config_space[1] & PCI_COMMAND_SERR)
666                 cmd |= PCI_COMMAND_SERR;
667         else
668                 cmd &= ~PCI_COMMAND_SERR;
669         eeh_ops->write_config(dn, PCI_COMMAND, 4, cmd);
670 }
671
672 /**
673  * eeh_restore_bars - Restore the PCI config space info
674  * @edev: EEH device
675  *
676  * This routine performs a recursive walk to the children
677  * of this device as well.
678  */
679 void eeh_restore_bars(struct eeh_dev *edev)
680 {
681         struct device_node *dn;
682         if (!edev)
683                 return;
684         
685         if ((edev->mode & EEH_MODE_SUPPORTED) && !IS_BRIDGE(edev->class_code))
686                 eeh_restore_one_device_bars(edev);
687
688         for_each_child_of_node(eeh_dev_to_of_node(edev), dn)
689                 eeh_restore_bars(of_node_to_eeh_dev(dn));
690 }
691
692 /**
693  * eeh_save_bars - Save device bars
694  * @edev: PCI device associated EEH device
695  *
696  * Save the values of the device bars. Unlike the restore
697  * routine, this routine is *not* recursive. This is because
698  * PCI devices are added individually; but, for the restore,
699  * an entire slot is reset at a time.
700  */
701 static void eeh_save_bars(struct eeh_dev *edev)
702 {
703         int i;
704         struct device_node *dn;
705
706         if (!edev)
707                 return;
708         dn = eeh_dev_to_of_node(edev);
709         
710         for (i = 0; i < 16; i++)
711                 eeh_ops->read_config(dn, i * 4, 4, &edev->config_space[i]);
712 }
713
714 /**
715  * eeh_early_enable - Early enable EEH on the indicated device
716  * @dn: device node
717  * @data: BUID
718  *
719  * Enable EEH functionality on the specified PCI device. The function
720  * is expected to be called before real PCI probing is done. However,
721  * the PHBs have been initialized at this point.
722  */
723 static void *eeh_early_enable(struct device_node *dn, void *data)
724 {
725         int ret;
726         const u32 *class_code = of_get_property(dn, "class-code", NULL);
727         const u32 *vendor_id = of_get_property(dn, "vendor-id", NULL);
728         const u32 *device_id = of_get_property(dn, "device-id", NULL);
729         const u32 *regs;
730         int enable;
731         struct eeh_dev *edev = of_node_to_eeh_dev(dn);
732         struct eeh_pe pe;
733
734         edev->class_code = 0;
735         edev->mode = 0;
736         edev->check_count = 0;
737         edev->freeze_count = 0;
738         edev->false_positives = 0;
739
740         if (!of_device_is_available(dn))
741                 return NULL;
742
743         /* Ignore bad nodes. */
744         if (!class_code || !vendor_id || !device_id)
745                 return NULL;
746
747         /* There is nothing to check on PCI to ISA bridges */
748         if (dn->type && !strcmp(dn->type, "isa")) {
749                 edev->mode |= EEH_MODE_NOCHECK;
750                 return NULL;
751         }
752         edev->class_code = *class_code;
753
754         /* Ok... see if this device supports EEH.  Some do, some don't,
755          * and the only way to find out is to check each and every one.
756          */
757         regs = of_get_property(dn, "reg", NULL);
758         if (regs) {
759                 /* Initialize the fake PE */
760                 memset(&pe, 0, sizeof(struct eeh_pe));
761                 pe.phb = edev->phb;
762                 pe.config_addr = regs[0];
763
764                 /* First register entry is addr (00BBSS00)  */
765                 /* Try to enable eeh */
766                 ret = eeh_ops->set_option(&pe, EEH_OPT_ENABLE);
767
768                 enable = 0;
769                 if (ret == 0) {
770                         edev->config_addr = regs[0];
771
772                         /* If the newer, better, ibm,get-config-addr-info is supported, 
773                          * then use that instead.
774                          */
775                         edev->pe_config_addr = eeh_ops->get_pe_addr(&pe);
776                         pe.addr = edev->pe_config_addr;
777
778                         /* Some older systems (Power4) allow the
779                          * ibm,set-eeh-option call to succeed even on nodes
780                          * where EEH is not supported. Verify support
781                          * explicitly.
782                          */
783                         ret = eeh_ops->get_state(&pe, NULL);
784                         if (ret > 0 && ret != EEH_STATE_NOT_SUPPORT)
785                                 enable = 1;
786                 }
787
788                 if (enable) {
789                         eeh_subsystem_enabled = 1;
790                         edev->mode |= EEH_MODE_SUPPORTED;
791
792                         eeh_add_to_parent_pe(edev);
793
794                         pr_debug("EEH: %s: eeh enabled, config=%x pe_config=%x\n",
795                                  dn->full_name, edev->config_addr,
796                                  edev->pe_config_addr);
797                 } else {
798
799                         /* This device doesn't support EEH, but it may have an
800                          * EEH parent, in which case we mark it as supported.
801                          */
802                         if (dn->parent && of_node_to_eeh_dev(dn->parent) &&
803                             (of_node_to_eeh_dev(dn->parent)->mode & EEH_MODE_SUPPORTED)) {
804                                 /* Parent supports EEH. */
805                                 edev->mode |= EEH_MODE_SUPPORTED;
806                                 edev->config_addr = of_node_to_eeh_dev(dn->parent)->config_addr;
807                                 edev->pe_config_addr = of_node_to_eeh_dev(dn->parent)->pe_config_addr;
808
809                                 eeh_add_to_parent_pe(edev);
810
811                                 return NULL;
812                         }
813                 }
814         } else {
815                 printk(KERN_WARNING "EEH: %s: unable to get reg property.\n",
816                        dn->full_name);
817         }
818
819         eeh_save_bars(edev);
820         return NULL;
821 }
822
823 /**
824  * eeh_ops_register - Register platform dependent EEH operations
825  * @ops: platform dependent EEH operations
826  *
827  * Register the platform dependent EEH operation callback
828  * functions. The platform should call this function before
829  * any other EEH operations.
830  */
831 int __init eeh_ops_register(struct eeh_ops *ops)
832 {
833         if (!ops->name) {
834                 pr_warning("%s: Invalid EEH ops name for %p\n",
835                         __func__, ops);
836                 return -EINVAL;
837         }
838
839         if (eeh_ops && eeh_ops != ops) {
840                 pr_warning("%s: EEH ops of platform %s already existing (%s)\n",
841                         __func__, eeh_ops->name, ops->name);
842                 return -EEXIST;
843         }
844
845         eeh_ops = ops;
846
847         return 0;
848 }
849
850 /**
851  * eeh_ops_unregister - Unreigster platform dependent EEH operations
852  * @name: name of EEH platform operations
853  *
854  * Unregister the platform dependent EEH operation callback
855  * functions.
856  */
857 int __exit eeh_ops_unregister(const char *name)
858 {
859         if (!name || !strlen(name)) {
860                 pr_warning("%s: Invalid EEH ops name\n",
861                         __func__);
862                 return -EINVAL;
863         }
864
865         if (eeh_ops && !strcmp(eeh_ops->name, name)) {
866                 eeh_ops = NULL;
867                 return 0;
868         }
869
870         return -EEXIST;
871 }
872
873 /**
874  * eeh_init - EEH initialization
875  *
876  * Initialize EEH by trying to enable it for all of the adapters in the system.
877  * As a side effect we can determine here if eeh is supported at all.
878  * Note that we leave EEH on so failed config cycles won't cause a machine
879  * check.  If a user turns off EEH for a particular adapter they are really
880  * telling Linux to ignore errors.  Some hardware (e.g. POWER5) won't
881  * grant access to a slot if EEH isn't enabled, and so we always enable
882  * EEH for all slots/all devices.
883  *
884  * The eeh-force-off option disables EEH checking globally, for all slots.
885  * Even if force-off is set, the EEH hardware is still enabled, so that
886  * newer systems can boot.
887  */
888 static int __init eeh_init(void)
889 {
890         struct pci_controller *hose, *tmp;
891         struct device_node *phb;
892         int ret;
893
894         /* call platform initialization function */
895         if (!eeh_ops) {
896                 pr_warning("%s: Platform EEH operation not found\n",
897                         __func__);
898                 return -EEXIST;
899         } else if ((ret = eeh_ops->init())) {
900                 pr_warning("%s: Failed to call platform init function (%d)\n",
901                         __func__, ret);
902                 return ret;
903         }
904
905         raw_spin_lock_init(&confirm_error_lock);
906
907         /* Enable EEH for all adapters */
908         list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
909                 phb = hose->dn;
910                 traverse_pci_devices(phb, eeh_early_enable, NULL);
911         }
912
913         if (eeh_subsystem_enabled)
914                 printk(KERN_INFO "EEH: PCI Enhanced I/O Error Handling Enabled\n");
915         else
916                 printk(KERN_WARNING "EEH: No capable adapters found\n");
917
918         return ret;
919 }
920
921 core_initcall_sync(eeh_init);
922
923 /**
924  * eeh_add_device_early - Enable EEH for the indicated device_node
925  * @dn: device node for which to set up EEH
926  *
927  * This routine must be used to perform EEH initialization for PCI
928  * devices that were added after system boot (e.g. hotplug, dlpar).
929  * This routine must be called before any i/o is performed to the
930  * adapter (inluding any config-space i/o).
931  * Whether this actually enables EEH or not for this device depends
932  * on the CEC architecture, type of the device, on earlier boot
933  * command-line arguments & etc.
934  */
935 static void eeh_add_device_early(struct device_node *dn)
936 {
937         struct pci_controller *phb;
938
939         if (!dn || !of_node_to_eeh_dev(dn))
940                 return;
941         phb = of_node_to_eeh_dev(dn)->phb;
942
943         /* USB Bus children of PCI devices will not have BUID's */
944         if (NULL == phb || 0 == phb->buid)
945                 return;
946
947         eeh_early_enable(dn, NULL);
948 }
949
950 /**
951  * eeh_add_device_tree_early - Enable EEH for the indicated device
952  * @dn: device node
953  *
954  * This routine must be used to perform EEH initialization for the
955  * indicated PCI device that was added after system boot (e.g.
956  * hotplug, dlpar).
957  */
958 void eeh_add_device_tree_early(struct device_node *dn)
959 {
960         struct device_node *sib;
961
962         for_each_child_of_node(dn, sib)
963                 eeh_add_device_tree_early(sib);
964         eeh_add_device_early(dn);
965 }
966 EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
967
968 /**
969  * eeh_add_device_late - Perform EEH initialization for the indicated pci device
970  * @dev: pci device for which to set up EEH
971  *
972  * This routine must be used to complete EEH initialization for PCI
973  * devices that were added after system boot (e.g. hotplug, dlpar).
974  */
975 static void eeh_add_device_late(struct pci_dev *dev)
976 {
977         struct device_node *dn;
978         struct eeh_dev *edev;
979
980         if (!dev || !eeh_subsystem_enabled)
981                 return;
982
983         pr_debug("EEH: Adding device %s\n", pci_name(dev));
984
985         dn = pci_device_to_OF_node(dev);
986         edev = of_node_to_eeh_dev(dn);
987         if (edev->pdev == dev) {
988                 pr_debug("EEH: Already referenced !\n");
989                 return;
990         }
991         WARN_ON(edev->pdev);
992
993         pci_dev_get(dev);
994         edev->pdev = dev;
995         dev->dev.archdata.edev = edev;
996
997         pci_addr_cache_insert_device(dev);
998         eeh_sysfs_add_device(dev);
999 }
1000
1001 /**
1002  * eeh_add_device_tree_late - Perform EEH initialization for the indicated PCI bus
1003  * @bus: PCI bus
1004  *
1005  * This routine must be used to perform EEH initialization for PCI
1006  * devices which are attached to the indicated PCI bus. The PCI bus
1007  * is added after system boot through hotplug or dlpar.
1008  */
1009 void eeh_add_device_tree_late(struct pci_bus *bus)
1010 {
1011         struct pci_dev *dev;
1012
1013         list_for_each_entry(dev, &bus->devices, bus_list) {
1014                 eeh_add_device_late(dev);
1015                 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
1016                         struct pci_bus *subbus = dev->subordinate;
1017                         if (subbus)
1018                                 eeh_add_device_tree_late(subbus);
1019                 }
1020         }
1021 }
1022 EXPORT_SYMBOL_GPL(eeh_add_device_tree_late);
1023
1024 /**
1025  * eeh_remove_device - Undo EEH setup for the indicated pci device
1026  * @dev: pci device to be removed
1027  *
1028  * This routine should be called when a device is removed from
1029  * a running system (e.g. by hotplug or dlpar).  It unregisters
1030  * the PCI device from the EEH subsystem.  I/O errors affecting
1031  * this device will no longer be detected after this call; thus,
1032  * i/o errors affecting this slot may leave this device unusable.
1033  */
1034 static void eeh_remove_device(struct pci_dev *dev)
1035 {
1036         struct eeh_dev *edev;
1037
1038         if (!dev || !eeh_subsystem_enabled)
1039                 return;
1040         edev = pci_dev_to_eeh_dev(dev);
1041
1042         /* Unregister the device with the EEH/PCI address search system */
1043         pr_debug("EEH: Removing device %s\n", pci_name(dev));
1044
1045         if (!edev || !edev->pdev) {
1046                 pr_debug("EEH: Not referenced !\n");
1047                 return;
1048         }
1049         edev->pdev = NULL;
1050         dev->dev.archdata.edev = NULL;
1051         pci_dev_put(dev);
1052
1053         eeh_rmv_from_parent_pe(edev);
1054         pci_addr_cache_remove_device(dev);
1055         eeh_sysfs_remove_device(dev);
1056 }
1057
1058 /**
1059  * eeh_remove_bus_device - Undo EEH setup for the indicated PCI device
1060  * @dev: PCI device
1061  *
1062  * This routine must be called when a device is removed from the
1063  * running system through hotplug or dlpar. The corresponding
1064  * PCI address cache will be removed.
1065  */
1066 void eeh_remove_bus_device(struct pci_dev *dev)
1067 {
1068         struct pci_bus *bus = dev->subordinate;
1069         struct pci_dev *child, *tmp;
1070
1071         eeh_remove_device(dev);
1072
1073         if (bus && dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
1074                 list_for_each_entry_safe(child, tmp, &bus->devices, bus_list)
1075                          eeh_remove_bus_device(child);
1076         }
1077 }
1078 EXPORT_SYMBOL_GPL(eeh_remove_bus_device);
1079
1080 static int proc_eeh_show(struct seq_file *m, void *v)
1081 {
1082         if (0 == eeh_subsystem_enabled) {
1083                 seq_printf(m, "EEH Subsystem is globally disabled\n");
1084                 seq_printf(m, "eeh_total_mmio_ffs=%llu\n", eeh_stats.total_mmio_ffs);
1085         } else {
1086                 seq_printf(m, "EEH Subsystem is enabled\n");
1087                 seq_printf(m,
1088                                 "no device=%llu\n"
1089                                 "no device node=%llu\n"
1090                                 "no config address=%llu\n"
1091                                 "check not wanted=%llu\n"
1092                                 "eeh_total_mmio_ffs=%llu\n"
1093                                 "eeh_false_positives=%llu\n"
1094                                 "eeh_slot_resets=%llu\n",
1095                                 eeh_stats.no_device,
1096                                 eeh_stats.no_dn,
1097                                 eeh_stats.no_cfg_addr,
1098                                 eeh_stats.ignored_check,
1099                                 eeh_stats.total_mmio_ffs,
1100                                 eeh_stats.false_positives,
1101                                 eeh_stats.slot_resets);
1102         }
1103
1104         return 0;
1105 }
1106
1107 static int proc_eeh_open(struct inode *inode, struct file *file)
1108 {
1109         return single_open(file, proc_eeh_show, NULL);
1110 }
1111
1112 static const struct file_operations proc_eeh_operations = {
1113         .open      = proc_eeh_open,
1114         .read      = seq_read,
1115         .llseek    = seq_lseek,
1116         .release   = single_release,
1117 };
1118
1119 static int __init eeh_init_proc(void)
1120 {
1121         if (machine_is(pseries))
1122                 proc_create("powerpc/eeh", 0, NULL, &proc_eeh_operations);
1123         return 0;
1124 }
1125 __initcall(eeh_init_proc);